-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #123: Refactored Label into LabelBase + Label to use in the future…
… for other tags with same behavior. Added tests.
- Loading branch information
Showing
10 changed files
with
173 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/labels/LabelBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pl.tkowalcz.tjahzi.log4j2.labels; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.status.StatusLogger; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public abstract class LabelBase { | ||
|
||
private static final Logger LOGGER = StatusLogger.getLogger(); | ||
|
||
private static final Pattern LABEL_NAME_PATTER = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]*"); | ||
|
||
private final String name; | ||
private final String value; | ||
private final String pattern; | ||
|
||
LabelBase(String name, String value, String pattern) { | ||
this.name = name; | ||
this.value = value; | ||
this.pattern = pattern; | ||
|
||
if (name == null) { | ||
LOGGER.error("Property name cannot be null"); | ||
} | ||
|
||
if (pattern == null && value == null) { | ||
LOGGER.error("Property must have pattern or value specified"); | ||
} | ||
} | ||
|
||
public boolean hasValidName() { | ||
return hasValidName(getName()); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPattern() { | ||
return pattern; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public static boolean hasValidName(String label) { | ||
return LABEL_NAME_PATTER.matcher(label).matches(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/labels/LabelBaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pl.tkowalcz.tjahzi.log4j2.labels; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
abstract class LabelBaseTest { | ||
|
||
public abstract LabelBase createCUT(String name, String value, String pattern); | ||
|
||
@Test | ||
void shouldValidateNamePattern() { | ||
// Given | ||
LabelBase correctLabel = createCUT("aaa_bbb_ccc", "fobar", null); | ||
LabelBase tooShortLabel = createCUT("", "foobar", null); | ||
LabelBase labelWithInvalidChars = createCUT("log-level", "foobar", null); | ||
|
||
// When & Then | ||
assertThat(correctLabel.hasValidName()).isTrue(); | ||
assertThat(tooShortLabel.hasValidName()).isFalse(); | ||
assertThat(labelWithInvalidChars.hasValidName()).isFalse(); | ||
} | ||
} |
20 changes: 4 additions & 16 deletions
20
log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/labels/LabelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,9 @@ | ||
package pl.tkowalcz.tjahzi.log4j2.labels; | ||
|
||
import org.junit.jupiter.api.Test; | ||
class LabelTest extends LabelBaseTest { | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class LabelTest { | ||
|
||
@Test | ||
void shouldValidateNamePattern() { | ||
// Given | ||
Label correctLabel = Label.createLabel("aaa_bbb_ccc", "fobar", null); | ||
Label tooShortLabel = Label.createLabel("", "foobar", null); | ||
Label labelWithInvalidChars = Label.createLabel("log-level", "foobar", null); | ||
|
||
// When & Then | ||
assertThat(correctLabel.hasValidName()).isTrue(); | ||
assertThat(tooShortLabel.hasValidName()).isFalse(); | ||
assertThat(labelWithInvalidChars.hasValidName()).isFalse(); | ||
@Override | ||
public LabelBase createCUT(String name, String value, String pattern) { | ||
return Label.createLabel(name, value, pattern); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/labels/LabelsDescriptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pl.tkowalcz.tjahzi.log4j2.labels; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class LabelsDescriptorTest { | ||
|
||
@Test | ||
void shouldCombineDynamicAndStaticLabels() { | ||
// Given | ||
Map<String, String> actualStaticLabels = Map.of("foo", "bar", "123", "456"); | ||
Map<String, LabelPrinter> actualDynamicLabels = Map.of( | ||
"stuff", MDCLookup.of("zzz", "yyy") | ||
); | ||
|
||
LabelsDescriptor descriptor = new LabelsDescriptor( | ||
"level", | ||
actualStaticLabels, | ||
actualDynamicLabels | ||
); | ||
|
||
// When | ||
Map<String, String> staticLabels = descriptor.getStaticLabels(); | ||
Map<String, LabelPrinter> dynamicLabels = descriptor.getDynamicLabels(); | ||
Map<String, LabelPrinter> allLabels = descriptor.getAllLabels(); | ||
|
||
// Then | ||
assertThat(staticLabels).isEqualTo(actualStaticLabels); | ||
assertThat(dynamicLabels).isEqualTo(actualDynamicLabels); | ||
assertThat(allLabels) | ||
.containsExactlyInAnyOrderEntriesOf(Map.<String, LabelPrinter>of( | ||
"foo", Literal.of("bar"), | ||
"123", Literal.of("456"), | ||
"stuff", MDCLookup.of("zzz", "yyy") | ||
)); | ||
} | ||
} |