Repository: logging-log4j2 Updated Branches: refs/heads/master db769237d -> 8b0e21408
LOG4J2-1296 added initial unit tests for ParameterFormatter Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8b0e2140 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8b0e2140 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8b0e2140 Branch: refs/heads/master Commit: 8b0e21408c07585b9618140a3683d1903607f81f Parents: db76923 Author: rpopma <[email protected]> Authored: Tue Mar 8 23:08:15 2016 +1100 Committer: rpopma <[email protected]> Committed: Tue Mar 8 23:08:15 2016 +1100 ---------------------------------------------------------------------- .../log4j/message/ParameterFormatterTest.java | 129 +++++++++++++++++++ 1 file changed, 129 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8b0e2140/log4j-api/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java new file mode 100644 index 0000000..4158b17 --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/message/ParameterFormatterTest.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.message; + +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Objects; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests ParameterFormatter. + */ +public class ParameterFormatterTest { + + @Test + public void testCountArgumentPlaceholders() throws Exception { + assertEquals(0, ParameterFormatter.countArgumentPlaceholders("")); + assertEquals(0, ParameterFormatter.countArgumentPlaceholders("aaa")); + assertEquals(0, ParameterFormatter.countArgumentPlaceholders("\\{}")); + assertEquals(1, ParameterFormatter.countArgumentPlaceholders("{}")); + assertEquals(1, ParameterFormatter.countArgumentPlaceholders("{}\\{}")); + assertEquals(2, ParameterFormatter.countArgumentPlaceholders("{}{}")); + assertEquals(3, ParameterFormatter.countArgumentPlaceholders("{}{}{}")); + assertEquals(4, ParameterFormatter.countArgumentPlaceholders("{}{}{}aa{}")); + assertEquals(4, ParameterFormatter.countArgumentPlaceholders("{}{}{}a{]b{}")); + assertEquals(5, ParameterFormatter.countArgumentPlaceholders("{}{}{}a{}b{}")); + } + + @Test + public void testFormat3StringArgs() { + final String testMsg = "Test message {}{} {}"; + final String[] args = { "a", "b", "c" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message ab c", result); + } + + @Test + public void testFormatNullArgs() { + final String testMsg = "Test message {} {} {} {} {} {}"; + final String[] args = { "a", null, "c", null, null, null }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message a null c null null null", result); + } + + @Test + public void testFormatStringArgsIgnoresSuperfluousArgs() { + final String testMsg = "Test message {}{} {}"; + final String[] args = { "a", "b", "c", "unnecessary", "superfluous" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message ab c", result); + } + + @Test + public void testFormatStringArgsWithEscape() { + final String testMsg = "Test message \\{}{} {}"; + final String[] args = { "a", "b", "c" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message {}a b", result); + } + + @Test + public void testFormatStringArgsWithTrailingEscape() { + final String testMsg = "Test message {}{} {}\\"; + final String[] args = { "a", "b", "c" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message ab c\\", result); + } + + @Test + public void testFormatStringArgsWithTrailingEscapedEscape() { + final String testMsg = "Test message {}{} {}\\\\"; + final String[] args = { "a", "b", "c" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message ab c\\\\", result); + } + + @Test + public void testFormatStringArgsWithEscapedEscape() { + final String testMsg = "Test message \\\\{}{} {}"; + final String[] args = { "a", "b", "c" }; + final String result = ParameterFormatter.format(testMsg, args); + assertEquals("Test message \\ab c", result); + } + + @Test + public void testFormatMessage() throws Exception { + + } + + @Test + public void testDeepToString() throws Exception { + List<Object> list = new ArrayList<>(); + list.add(1); + list.add(list); + list.add(2); + String actual = ParameterFormatter.deepToString(list); + String expected = "[1, [..." + ParameterFormatter.identityToString(list) + "...], 2]"; + assertEquals(expected, actual); + } + + @Test + public void testIdentityToString() throws Exception { + List<Object> list = new ArrayList<>(); + list.add(1); + list.add(list); + list.add(2); + String actual = ParameterFormatter.identityToString(list); + String expected = list.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(list)); + assertEquals(expected, actual); + } +} \ No newline at end of file
