Skip to content

Commit

Permalink
Merge pull request #101 from jamezp/LOGMGR-144
Browse files Browse the repository at this point in the history
[LOGMGR-144] Allow a # to be used as an alternate for expressions in …
  • Loading branch information
jamezp committed Feb 16, 2017
2 parents b74c12c + eaef367 commit 68be924
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static FormatStep[] getSteps(final String formatString, ColorMap colors)
timeZone = TimeZone.getTimeZone(argument);
break;
}
case '#':
case '$': {
stepList.add(Formatters.systemPropertyFormatStep(argument, leftJustify, minimumWidth, truncateBeginning, maximumWidth));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,8 @@ public void threads() throws Exception {

@Test
public void systemProperties() throws Exception {
final ExtLogRecord record = createLogRecord("test");
PatternFormatter formatter = new PatternFormatter("%${org.jboss.logmanager.testProp}");
Assert.assertEquals("testValue", formatter.format(record));

formatter = new PatternFormatter("%${invalid:defaultValue}");
Assert.assertEquals("defaultValue", formatter.format(record));

formatter = new PatternFormatter("%${invalid}");
Assert.assertEquals("null", formatter.format(record));

// Test null arguments
try {
formatter = new PatternFormatter("%$");
formatter.format(record);
Assert.fail("Should not allow null arguments");
} catch (IllegalArgumentException ignore) {

}

try {
formatter = new PatternFormatter("%${}");
formatter.format(record);
Assert.fail("Should not allow null arguments");
} catch (IllegalArgumentException ignore) {

}
systemProperties("$");
systemProperties("#");
}

@Test
Expand Down Expand Up @@ -249,6 +225,36 @@ public void extendedThrowable() throws Exception {
Assert.assertTrue(formatted.contains("CIRCULAR REFERENCE:java.lang.IllegalStateException: suppressedLevel1"));
}


private void systemProperties(final String propertyPrefix) throws Exception {
final ExtLogRecord record = createLogRecord("test");
PatternFormatter formatter = new PatternFormatter("%" + propertyPrefix + "{org.jboss.logmanager.testProp}");
Assert.assertEquals("testValue", formatter.format(record));

formatter = new PatternFormatter("%" + propertyPrefix + "{invalid:defaultValue}");
Assert.assertEquals("defaultValue", formatter.format(record));

formatter = new PatternFormatter("%" + propertyPrefix + "{invalid}");
Assert.assertEquals("null", formatter.format(record));

// Test null arguments
try {
formatter = new PatternFormatter("%" + propertyPrefix);
formatter.format(record);
Assert.fail("Should not allow null arguments");
} catch (IllegalArgumentException ignore) {

}

try {
formatter = new PatternFormatter("%" + propertyPrefix + "{}");
formatter.format(record);
Assert.fail("Should not allow null arguments");
} catch (IllegalArgumentException ignore) {

}
}

protected static ExtLogRecord createLogRecord(final String msg) {
final ExtLogRecord result = new ExtLogRecord(org.jboss.logmanager.Level.INFO, msg, PatternFormatterTests.class.getName());
result.setSourceClassName(PatternFormatterTests.class.getName());
Expand Down

0 comments on commit 68be924

Please sign in to comment.