Search in sources :

Example 51 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project jdbi by jdbi.

the class StringSubstitutorTemplateEngine method render.

@Override
public String render(String template, StatementContext ctx) {
    StringSubstitutor substitutor = new StringSubstitutor(ctx.getAttributes());
    customizer.accept(substitutor);
    return substitutor.replace(template);
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 52 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class StringSubstitutorGetSetTest method testGetSetValueDelimiter.

/**
 * Tests get set.
 */
@Test
public void testGetSetValueDelimiter() {
    final StringSubstitutor sub = new StringSubstitutor();
    assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    sub.setValueDelimiter(':');
    assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharMatcher);
    sub.setValueDelimiter("||");
    assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    sub.setValueDelimiter((String) null);
    assertNull(sub.getValueDelimiterMatcher());
    final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
    sub.setValueDelimiterMatcher(matcher);
    assertSame(matcher, sub.getValueDelimiterMatcher());
    sub.setValueDelimiterMatcher((StringMatcher) null);
    assertNull(sub.getValueDelimiterMatcher());
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 53 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class StringSubstitutorGetSetTest method testGetSetSuffix.

/**
 * Tests get set.
 */
@Test
public void testGetSetSuffix() {
    final StringSubstitutor sub = new StringSubstitutor();
    assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharMatcher);
    sub.setVariableSuffix('<');
    assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharMatcher);
    sub.setVariableSuffix("<<");
    assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffix((String) null));
    assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
    sub.setVariableSuffixMatcher(matcher);
    assertSame(matcher, sub.getVariableSuffixMatcher());
    assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffixMatcher((StringMatcher) null));
    assertSame(matcher, sub.getVariableSuffixMatcher());
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 54 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class StringSubstitutorGetSetTest method testGetSetPrefix.

/**
 * Tests get set.
 */
@Test
public void testGetSetPrefix() {
    final StringSubstitutor sub = new StringSubstitutor();
    assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    sub.setVariablePrefix('<');
    assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharMatcher);
    sub.setVariablePrefix("<<");
    assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefix((String) null));
    assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
    final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
    sub.setVariablePrefixMatcher(matcher);
    assertSame(matcher, sub.getVariablePrefixMatcher());
    assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefixMatcher((StringMatcher) null));
    assertSame(matcher, sub.getVariablePrefixMatcher());
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 55 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class StringSubstitutorFilterReaderTest method testReadMixedBufferLengthsVarLenPlusToNoReplace.

@Test
public void testReadMixedBufferLengthsVarLenPlusToNoReplace() throws IOException {
    final StringSubstitutor substitutor = new StringSubstitutor(values);
    final String template = "123456";
    assertTrue(template.length() > getMinExpressionLength(substitutor) + 1);
    try (Reader reader = createReader(substitutor, template)) {
        final int endIndex = template.length() - 1;
        final char[] cbuf = new char[endIndex];
        reader.read(cbuf);
        final String result = String.valueOf(cbuf);
        assertEquals(template.substring(0, endIndex), result);
        assertEquals('6', reader.read());
    }
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Reader(java.io.Reader) StringReader(java.io.StringReader) NullReader(org.apache.commons.io.input.NullReader) Test(org.junit.jupiter.api.Test) StringSubstitutorTest(org.apache.commons.text.StringSubstitutorTest)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4