public class formatMessage
implements Testlet
{
public void test(TestHarness h)
{
XMLFormatter formatter = new XMLFormatter();
LogRecord rec;
// Check #1.
try
{
formatter.formatMessage(null);
h.check(false);
}
catch (NullPointerException _)
{
h.check(true);
}
catch (Exception _)
{
h.check(false);
}
// Check #2.
rec = new LogRecord(Level.INFO, "foobar");
//Need to force the default time zone to UTC or else
//the comparison uses system time zone and makes the tests
//break.
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
rec.setMillis(1234567);
rec.setSequenceNumber(42);
rec.setThreadID(21);
h.check(formatter.format(rec),
EXPECTED_PREFIX
+ " <thread>21</thread>\n"
+ " <message>foobar</message>\n"
+ "</record>\n");
// Check #3.
rec.setSourceClassName(
"FakeClass");
rec.setSourceMethodName("test(fake)");
h.check(formatter.format(rec),
EXPECTED_PREFIX
+ " <class>FakeClass</class>\n"
+ " <method>test(fake)</method>\n"
+ " <thread>21</thread>\n"
+ " <message>foobar</message>\n"
+ "</record>\n");
// Check #4.
rec.setMessage("foobar {1}-{0}");
rec.setParameters(new String[] { "peace", "love" });
h.check(formatter.format(rec),
EXPECTED_PREFIX
+ " <class>FakeClass</class>\n"
+ " <method>test(fake)</method>\n"
+ " <thread>21</thread>\n"
+ " <message>foobar love-peace</message>\n"
+ "</record>\n");
// Check #5.
rec.setThrown(new TestException("non-localized message"));
rec.setMessage("mauve is a beautiful color");
h.check(deleteFrames(formatter.format(rec)),
EXPECTED_PREFIX
+ " <class>FakeClass</class>\n"
+ " <method>test(fake)</method>\n"
+ " <thread>21</thread>\n"
+ " <message>mauve is a beautiful color</message>\n"
+ " <exception>\n"
+ " <message>gnu.testlet.java.util.logging"
+ ".XMLFormatter.formatMessage$TestException: localized "
+ "message</message>\n"
+ " </exception>\n"
+ "</record>\n");
// Check #6.
rec.setMessage("ENTRY {0}");
rec.setParameters(new String[] { "foo.bar" });
rec.setResourceBundleName(TestResourceBundle.class.getName());
rec.setThrown(null);
h.check(formatter.format(rec),
EXPECTED_PREFIX
+ " <class>FakeClass</class>\n"
+ " <method>test(fake)</method>\n"
+ " <thread>21</thread>\n"
+ " <message>ENTRY foo.bar</message>\n"