@Test public void testGetFormattedMessage_ReturnsLatestSetString() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); msg.set("abc"); assertEquals("abc", msg.getFormattedMessage()); msg.set("def"); assertEquals("def", msg.getFormattedMessage()); msg.set("xyz"); assertEquals("xyz", msg.getFormattedMessage()); }
@Test public void testGetFormat_ReturnsLatestSetString() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); msg.set("abc"); assertEquals("abc", msg.getFormat()); msg.set("def"); assertEquals("def", msg.getFormat()); msg.set("xyz"); assertEquals("xyz", msg.getFormat()); }
@Test public void testGetParameters_ReturnsSetObjectInParameterArrayAfterMessageSet() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); msg.set("abc"); assertArrayEquals(new Object[]{"abc"}, msg.getParameters()); msg.set("def"); assertArrayEquals(new Object[]{"def"}, msg.getParameters()); }
@Test public void testGetThrowable_ReturnsNullAfterMessageSet() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); msg.set("abc"); assertNull(msg.getThrowable()); msg.set("def"); assertNull(msg.getThrowable()); }
@Test public void testFormatTo_WritesLatestSetString() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); final StringBuilder sb = new StringBuilder(); msg.formatTo(sb); assertEquals("null", sb.toString()); sb.setLength(0); msg.set("abc"); msg.formatTo(sb); assertEquals("abc", sb.toString()); sb.setLength(0); msg.set("def"); msg.formatTo(sb); assertEquals("def", sb.toString()); sb.setLength(0); msg.set("xyz"); msg.formatTo(sb); assertEquals("xyz", sb.toString()); } }
@Test public void testGetFormattedMessage_InitiallyNullString() throws Exception { assertEquals("null", new ReusableObjectMessage().getFormattedMessage()); }
private static ReusableObjectMessage getObject() { ReusableObjectMessage result = threadLocalObjectMessage.get(); if (result == null) { result = new ReusableObjectMessage(); threadLocalObjectMessage.set(result); } return result; }
@Test public void testGetParameters_InitiallyReturnsNullObjectInLength1Array() throws Exception { assertArrayEquals(new Object[]{null}, new ReusableObjectMessage().getParameters()); }
@Test public void testGetFormat_InitiallyNull() throws Exception { assertNull(new ReusableObjectMessage().getFormat()); }
@Test public void testFormatTo_InitiallyWritesNull() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); final StringBuilder sb = new StringBuilder(); msg.formatTo(sb); assertEquals("null", sb.toString()); }
@Test public void testGetThrowable_InitiallyReturnsNull() throws Exception { assertNull(new ReusableObjectMessage().getThrowable()); }
@Override public String toString() { return getFormattedMessage(); }
/** * Creates {@link ReusableObjectMessage} instances. * * @param message The message Object. * @return The Message. * * @see MessageFactory#newMessage(Object) */ @Override public Message newMessage(final Object message) { final ReusableObjectMessage result = getObject(); result.set(message); return result; } }
@Test public void testToImmutableNotSame() { final LogEvent logEvent = new Log4jLogEvent.Builder().setMessage(new ReusableObjectMessage()).build(); final LogEvent immutable = logEvent.toImmutable(); Assert.assertSame(logEvent, immutable); Assert.assertFalse(immutable.getMessage() instanceof ReusableMessage); }
@Override public String toString() { return getFormattedMessage(); }
/** * Creates {@link ReusableObjectMessage} instances. * * @param message The message Object. * @return The Message. * * @see MessageFactory#newMessage(Object) */ @Override public Message newMessage(final Object message) { final ReusableObjectMessage result = getObject(); result.set(message); return result; } }
@Test public void testSet_InitializesFormattedMessage() throws Exception { final ReusableObjectMessage msg = new ReusableObjectMessage(); msg.set("abc"); assertEquals("abc", msg.getFormattedMessage()); }
private static ReusableObjectMessage getObject() { ReusableObjectMessage result = threadLocalObjectMessage.get(); if (result == null) { result = new ReusableObjectMessage(); threadLocalObjectMessage.set(result); } return result; }
/** * Returns the object formatted using its toString method. * * @return the String representation of the object. */ @Override public String getFormat() { return getFormattedMessage(); }