/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1) { return new StringFormattedMessage(message, p0, p1); }
@Override public String toString() { return getFormattedMessage(); }
/** * Returns the formatted message. * @return the formatted message. */ @Override public String getFormattedMessage() { if (formattedMessage == null) { formattedMessage = formatMessage(messagePattern, argArray); } return formattedMessage; }
@Test public void testNoArgs() { final String testMsg = "Test message %1s"; StringFormattedMessage msg = new StringFormattedMessage(testMsg, (Object[]) null); String result = msg.getFormattedMessage(); final String expected = "Test message null"; assertEquals(expected, result); final Object[] array = null; msg = new StringFormattedMessage(testMsg, array, null); result = msg.getFormattedMessage(); assertEquals(expected, result); }
@Test public void testSerialization() throws IOException, ClassNotFoundException { final StringFormattedMessage expected = new StringFormattedMessage("Msg", "a", "b", "c"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final ObjectOutputStream out = new ObjectOutputStream(baos)) { out.writeObject(expected); } final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ObjectInputStream in = new ObjectInputStream(bais); final StringFormattedMessage actual = (StringFormattedMessage) in.readObject(); Assert.assertEquals(expected, actual); Assert.assertEquals(expected.getFormat(), actual.getFormat()); Assert.assertEquals(expected.getFormattedMessage(), actual.getFormattedMessage()); Assert.assertArrayEquals(expected.getParameters(), actual.getParameters()); } }
@Test public void testOneStringArg() { final String testMsg = "Test message %1s"; final StringFormattedMessage msg = new StringFormattedMessage(testMsg, "Apache"); final String result = msg.getFormattedMessage(); final String expected = "Test message Apache"; assertEquals(expected, result); }
/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1, final Object p2, final Object p3, final Object p4, final Object p5, final Object p6, final Object p7, final Object p8, final Object p9) { return new StringFormattedMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); } }
@Test public void testOneIntArgLocaleUs() { final String testMsg = "Test e = %+10.4f"; final StringFormattedMessage msg = new StringFormattedMessage(Locale.US, testMsg, Math.E); final String result = msg.getFormattedMessage(); final String expected = "Test e = +2.7183"; assertEquals(expected, result); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); getFormattedMessage(); out.writeUTF(formattedMessage); out.writeUTF(messagePattern); out.writeInt(argArray.length); stringArgs = new String[argArray.length]; int i = 0; for (final Object obj : argArray) { final String string = String.valueOf(obj); stringArgs[i] = string; out.writeUTF(string); ++i; } }
/** * Returns the formatted message. * @return the formatted message. */ @Override public String getFormattedMessage() { if (formattedMessage == null) { formattedMessage = formatMessage(messagePattern, argArray); } return formattedMessage; }
/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1, final Object p2) { return new StringFormattedMessage(message, p0, p1, p2); }
@Test public void testOneArgLocaleFrance() { final String testMsg = "Test e = %+10.4f"; final StringFormattedMessage msg = new StringFormattedMessage(Locale.FRANCE, testMsg, Math.E); final String result = msg.getFormattedMessage(); final String expected = "Test e = +2,7183"; assertEquals(expected, result); }
@Override public String toString() { return getFormattedMessage(); }
/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1, final Object p2, final Object p3) { return new StringFormattedMessage(message, p0, p1, p2, p3); }
@Test public void testSafeAfterGetFormattedMessageIsCalled() { // LOG4J2-763 final String testMsg = "Test message %s"; final Mutable param = new Mutable().set("abc"); final StringFormattedMessage msg = new StringFormattedMessage(testMsg, param); // modify parameter after calling msg.getFormattedMessage msg.getFormattedMessage(); param.set("XYZ"); final String actual = msg.getFormattedMessage(); assertEquals("Should use initial param value", "Test message abc", actual); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); getFormattedMessage(); out.writeUTF(formattedMessage); out.writeUTF(messagePattern); out.writeInt(argArray.length); stringArgs = new String[argArray.length]; int i = 0; for (final Object obj : argArray) { final String string = String.valueOf(obj); stringArgs[i] = string; out.writeUTF(string); ++i; } }
/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1, final Object p2, final Object p3, final Object p4) { return new StringFormattedMessage(message, p0, p1, p2, p3, p4); }
@Test public void testUnsafeWithMutableParams() { // LOG4J2-763 final String testMsg = "Test message %s"; final Mutable param = new Mutable().set("abc"); final StringFormattedMessage msg = new StringFormattedMessage(testMsg, param); // modify parameter before calling msg.getFormattedMessage param.set("XYZ"); final String actual = msg.getFormattedMessage(); assertEquals("Should use initial param value", "Test message XYZ", actual); }
/** * @since 2.6.1 */ @Override public Message newMessage(final String message, final Object p0, final Object p1, final Object p2, final Object p3, final Object p4, final Object p5, final Object p6, final Object p7, final Object p8) { return new StringFormattedMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8); }
@Test public void testStringPerf() { final String testMsg = "Test message %1s %2s"; final Timer timer = new Timer("StringFormat", LOOP_CNT); timer.start(); for (int i = 0; i < LOOP_CNT; ++i) { final StringFormattedMessage msg = new StringFormattedMessage(testMsg, "Apache", "Log4j"); array[i] = msg.getFormattedMessage(); } timer.stop(); stringTime = timer.getElapsedNanoTime(); System.out.println(timer.toString()); }