NamedArgumentPlaceholder extends AbstractArgumentPlaceholder { protected String formatAll(Parameter[] parameters, List<Object> arguments) { StringBuilder stringBuilder = new StringBuilder(); for (int idx = 0; idx < arguments.size(); idx++) { String parameterName = (parameters.length > idx) ? parameters[idx].getName() : "?"; Object argument = arguments.get(idx); stringBuilder.append(parameterName).append("=").append(format(argument)); if (idx < arguments.size() - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); } NamedArgumentPlaceholder();   }
@Test void testFormatAllReplacesCarriageReturnWithTheirPrintableCounterpart() { final List<Object> arguments = list("\r"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=\\r"); }
@Test void testFormatAllReplacesCarriageReturnsWithTheirPrintableCounterpartEvenIfWithText() { final List<Object> arguments = list("test\rtest\r"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=test\\rtest\\r"); }
@Test void testFormatAllReplacesLineFeedWithTheirPrintableCounterpart() { final List<Object> arguments = list("\n"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=\\n"); }
@Test void testFormatAllReplacesLineFeedsWithTheirPrintableCounterpartEvenIfWithText() { final List<Object> arguments = list("1\n2\n3"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=1\\n2\\n3"); }
@Test void testFormatAllReplacesNonPrintableCharactersWithPredefinedPrintableCounterpart() { final List<Object> arguments = list("\u001F"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=" + STRING_NON_PRINTABLE); }
@Test void testFormatAllReplacesNonPrintableCharactersWithPredefinedPrintableCounterpartEvenIfWithText() { final List<Object> arguments = list("test\btest\uFFFF"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=test" + STRING_NON_PRINTABLE + "test" + STRING_NON_PRINTABLE); }
@Test void testFormatAllReplacesCarriageReturnsAndLineFeedsWithTheirPrintableCounterpart() { final List<Object> arguments = list("A very\r\nlong text\nwith multiple\rdifferent newline\n\rvariations."); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=A very\\r\\nlong text\\nwith multiple\\rdifferent newline\\n\\rvariations."); }
@Test void testFormatAllShouldPrintQuestionMarkForUnknownParametersToBeFailureTolerantInsteadOfThrowAnException() { final List<Object> arguments = list(0, '1', 2.0); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=0, ?=1, ?=2.0"); }
@Test void testFormatAllShouldOnlyPrintArgumentsEvenIfTooManyParametersAreGiven() { final List<Object> arguments = list(0, '1', 2.0, 3l); String result = underTest.formatAll(paramsWith(10), arguments); assertThat(result).isEqualTo("c0=0, i1=1, l2=2.0, d3=3"); }
@Test void testFormatAllHandleObjectArrayCorrectly() { final List<Object> arguments = list(new Object[] { 7.5, "test" }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[7.5, test]"); }
@Test void testFormatAllHandlePrimitiveBooleanTypeArrayCorrectly() { final List<Object> arguments = list(new boolean[] { true, false }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[true, false]"); }
@Test void testFormatAllHandlePrimitiveByteTypeArrayCorrectly() { final List<Object> arguments = list(new byte[] { 12, 24 }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[12, 24]"); }
@Test void testFormatAllHandlePrimitiveCharTypeArrayCorrectly() { final List<Object> arguments = list(new char[] { 'a', '0' }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[a, 0]"); }
@Test void testFormatAllHandlePrimitiveShortTypeArrayCorrectly() { final List<Object> arguments = list(new short[] { 1024 }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[1024]"); }
@Test void testFormatAllHandlePrimitiveIntTypeArrayCorrectly() { final List<Object> arguments = list(new int[] { 11, 2 }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[11, 2]"); }
@Test void testFormatAllHandlePrimitiveLongTypeArrayCorrectly() { final List<Object> arguments = list(new long[] { 111L, 222L, 333L }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[111, 222, 333]"); }
@Test void testFormatAllHandlePrimitiveFloatTypeArrayCorrectly() { final List<Object> arguments = list(new float[] { 0.3f, 0.9f, 0.81f, 0.6561f }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[0.3, 0.9, 0.81, 0.6561]"); }
@Test void testFormatAllHandlePrimitiveDoubleTypeArrayCorrectly() { final List<Object> arguments = list(new double[] { .78, 3.15E2 }); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=[0.78, 315.0]"); }
@Test void testFormatAllHandleFurtherNestedArraysCorrectly() { final List<Object> arguments = list(new Object[] { 2, new char[] { 'a', 'b' }, new String[] { "a", "b" } }); String result = underTest.formatAll(paramsWith(2), arguments); assertThat(result).isEqualTo("c0=[2, [a, b], [a, b]]"); }
@Test void testFormatAllHandleObjectCorrectly() { final List<Object> arguments = list(new Object()); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).matches("c0=java.lang.Object@[0-9a-f]+"); }
@Test void testFormatAllShouldHandleSingleValueCorrectly() { final List<Object> arguments = list(12.45); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=12.45"); }
@Test void testFormatAllHandleListsCorrectly() { final List<Object> arguments = list(list("test", 1, 1723940567289346512L), 3); String result = underTest.formatAll(paramsWith(2), arguments); assertThat(result).isEqualTo("c0=[test, 1, 1723940567289346512], i1=3"); }
@Test void testFormatAllHandleEnumsCorrectly() { final List<Object> arguments = list(Thread.State.RUNNABLE); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=RUNNABLE"); }
@Test void testFormatAllHandleComplexExampleCorrectly() { Date now = new Date(); final List<Object> arguments = list( now, Double.valueOf(3.5), new StringBuilder("1").append("|2").append("|3"), new File("src/main/java/com/tngtech") ); String result = underTest.formatAll(paramsWith(4), arguments); assertThat(result).isEqualTo("c0=" + now.toString() + ", i1=3.5, l2=1|2|3, d3=src/main/java/com/tngtech"); }
@Test void testFormatAllShouldReturnAllThreeValuesCorrectly() { final List<Object> arguments = list("test", 1, 2L); String result = underTest.formatAll(paramsWith(3), arguments); assertThat(result).isEqualTo("c0=test, i1=1, l2=2"); }
@Test void testFormatAllHandleNullSpecially() { final List<Object> arguments = list(null); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=" + STRING_NULL); }
@Test void testFormatAllHandleNullNullCorrectly() { final List<Object> arguments = list(null, (Object) null); String result = underTest.formatAll(paramsWith(2), arguments); assertThat(result).isEqualTo("c0=<null>, i1=<null>"); }
@Test void testFormatAllDoesNotThrowNullPointerExceptionIfParamsToStringReturningNull() { final List<Object> arguments = list(new TestToString(null)); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=" + STRING_NULL); }
@Test void testFormatAllHandleEmtpyStringSpecially() { final List<Object> arguments = list(""); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=" + STRING_EMPTY); }
@Test void testFormatAllReplacesNullTerminatorWithTheirPrintableCounterpart() { final List<Object> arguments = list("\0"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=\\0"); }
@Test void testFormatAllReplacesNullTerminatorWithTheirPrintableCounterpartEvenIfWithText() { final List<Object> arguments = list("test\0test\0"); String result = underTest.formatAll(paramsWith(1), arguments); assertThat(result).isEqualTo("c0=test\\0test\\0"); }