@Override public String format(Method testMethod, int invocationIndex, List<Object> arguments) { ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments); String result = format; for (BasePlaceholder placeHolder : placeholders) { result = placeHolder.process(data, result); } return result; } }
/** * Executes this placeholder for the given {@link String} by searching all occurrences of the regular expression * supplied in the constructor and replaces them with the retrieved replacement from * {@link #getReplacementFor(String, ReplacementData)}. If the regular expression does not match, an exact copy of * the given {@link String} is returned. * * @param data used to process given {@code formatPattern} * @param formatPattern to be processed * @return the given {@code formatPattern} containing the generated replacements instead of matching patterns */ public String process(ReplacementData data, String formatPattern) { StringBuffer sb = new StringBuffer(); Matcher matcher = pattern.matcher(formatPattern); while (matcher.find()) { matcher.appendReplacement(sb, Matcher.quoteReplacement(getReplacementFor(matcher.group(), data))); } matcher.appendTail(sb); return sb.toString(); }
/** * Executes this placeholder for the given {@link String} by searching all occurrences of the regular expression * supplied in the constructor and replaces them with the retrieved replacement from * {@link #getReplacementFor(String)}. If the regular expression does not match, an exact copy of the given * {@link String} is returned. * <p> * Note: For new and future-proof implementations, please inherit from * {@link com.tngtech.junit.dataprovider.placeholder.BasePlaceholder} and use * {@link #process(ReplacementData, String)} instead. * * @param formatPattern to be processed * @return the given {@code formatPattern} containing the generated replacements instead of matching patterns */ public String process(String formatPattern) { ReplacementData data = ReplacementData.of(method, idx, Arrays.asList(parameters)); return super.process(data, formatPattern); }
/** * Executes this placeholder for the given {@link String} by searching all occurrences of the regular expression * supplied in the constructor and replaces them with the retrieved replacement from * {@link #getReplacementFor(String)}. If the regular expression does not match, an exact copy of the given * {@link String} is returned. * <p> * Note: For new and future-proof implementations, please inherit from * {@link com.tngtech.junit.dataprovider.placeholder.BasePlaceholder} and use * {@link #process(ReplacementData, String)} instead. * * @param formatPattern to be processed * @return the given {@code formatPattern} containing the generated replacements instead of matching patterns */ public String process(String formatPattern) { ReplacementData data = ReplacementData.of(method, idx, Arrays.asList(parameters)); return super.process(data, formatPattern); }
@Override public String format(Method testMethod, int invocationIndex, List<Object> arguments) { String result = nameFormat; for (BasePlaceholder placeholder : placeholders) { if (placeholder instanceof com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) { com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder placeHolder = (com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) placeholder; synchronized (placeHolder) { placeHolder.setContext(testMethod, invocationIndex, arguments.toArray()); result = placeHolder.process(result); } } else { ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments); result = placeholder.process(data, result); } } return result; } }
@Override public String format(Method testMethod, int invocationIndex, List<Object> arguments) { String result = nameFormat; for (BasePlaceholder placeholder : placeholders) { if (placeholder instanceof com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) { com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder placeHolder = (com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) placeholder; synchronized (placeHolder) { placeHolder.setContext(testMethod, invocationIndex, arguments.toArray()); result = placeHolder.process(result); } } else { ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments); result = placeholder.process(data, result); } } return result; } }
@Test public void testProcessShouldReplaceSinglePlaceholderSurroundedByText() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "##bar##"); // When: String result = underTest.process(null, "Test %s()"); // Then: assertThat(result).isEqualTo("Test ##bar##()"); }
@Test public void testProcessShouldReplaceSinglePlaceholderSurroundedByOtherPlaceholders() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "##baz##"); // When: String result = underTest.process(null, "%a%s%b"); // Then: assertThat(result).isEqualTo("%a##baz##%b"); }
@Test public void testProcessShouldReplaceNothingForNotMatchingPlaceholder() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "##foo##"); // When: String result = underTest.process(null, "%d"); // Then: assertThat(result).isEqualTo("%d"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForSimpleFormatPatternContainingPlaceholderTwice() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "1234%s"); // When: String result = underTest.process(null, "%s%s"); // Then: assertThat(result).isEqualTo("1234%s1234%s"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForComplexFormatPattern() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "abc%s%"); // When: String result = underTest.process(null, "%ss%s"); // Then: assertThat(result).isEqualTo("abc%s%sabc%s%"); }
@Test public void testProcessShouldHandleBackslashesCorrectly() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "\\"); // When: String result = underTest.process(null, "%s"); // Then: assertThat(result).isEqualTo("\\"); } }
@Test public void testProcessShouldReplaceSinglePlaceholder() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "##foo##"); // When: String result = underTest.process(null, "%s"); // Then: assertThat(result).isEqualTo("##foo##"); }
@Test public void testProcessShouldReplaceMultiplePlaceholders() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "##bla##"); // When: String result = underTest.process(null, "ss%ss%ss%ss%"); // Then: assertThat(result).isEqualTo("ss##bla##s##bla##s##bla##s%"); }
@Test public void testProcessShouldReplacePlaceholderNotCauseAnStackOverflowExceptionIfPlaceholderIsReplacedbyItself() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "%s"); // When: String result = underTest.process(null, "%s"); // Then: assertThat(result).isEqualTo("%s"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForSimpleFormatPattern() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "1234%s"); // When: String result = underTest.process(null, "%s"); // Then: assertThat(result).isEqualTo("1234%s"); }
@Test public void testProcessShouldHandleDollarSignsCorrectly() { // Given: BasePlaceholder underTest = new TestPlaceholder("%s", "$"); // When: String result = underTest.process(null, "%s"); // Then: assertThat(result).isEqualTo("$"); }