Search in sources :

Example 1 with StringLookup

use of org.apache.commons.text.lookup.StringLookup in project commons-text by apache.

the class StringSubstitutorTest method testReplaceInTakingTwoAndThreeIntsReturningFalse.

@Test
public void testReplaceInTakingTwoAndThreeIntsReturningFalse() {
    final Map<String, Object> hashMap = new HashMap<>();
    final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.mapStringLookup(hashMap);
    final StringMatcher strMatcher = StringMatcherFactory.INSTANCE.tabMatcher();
    final StringSubstitutor strSubstitutor = new StringSubstitutor(mapStringLookup, strMatcher, strMatcher, 'b', strMatcher);
    assertFalse(strSubstitutor.replaceIn((StringBuilder) null, 1315, (-1369)));
    assertEquals('b', strSubstitutor.getEscapeChar());
    assertFalse(strSubstitutor.isPreserveEscapes());
}
Also used : HashMap(java.util.HashMap) StringLookup(org.apache.commons.text.lookup.StringLookup) StringMatcher(org.apache.commons.text.matcher.StringMatcher) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 2 with StringLookup

use of org.apache.commons.text.lookup.StringLookup in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteWithDefaultValue.

@Test
void shouldSubstituteWithDefaultValue() throws IOException {
    StringLookup dummyLookup = (x) -> null;
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
    assertThat(provider.open("foo: ${bar:-default}")).hasSameContentAs(new ByteArrayInputStream("foo: default".getBytes(StandardCharsets.UTF_8)));
}
Also used : Test(org.junit.jupiter.api.Test) StringLookup(org.apache.commons.text.lookup.StringLookup) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) StandardCharsets(java.nio.charset.StandardCharsets) InputStream(java.io.InputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ByteArrayInputStream(java.io.ByteArrayInputStream) StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.jupiter.api.Test)

Example 3 with StringLookup

use of org.apache.commons.text.lookup.StringLookup in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteOnlyExistingVariables.

@Test
void shouldSubstituteOnlyExistingVariables() throws IOException {
    StringLookup dummyLookup = (x) -> null;
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
    assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: ${bar}".getBytes(StandardCharsets.UTF_8)));
}
Also used : Test(org.junit.jupiter.api.Test) StringLookup(org.apache.commons.text.lookup.StringLookup) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) StandardCharsets(java.nio.charset.StandardCharsets) InputStream(java.io.InputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ByteArrayInputStream(java.io.ByteArrayInputStream) StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.jupiter.api.Test)

Example 4 with StringLookup

use of org.apache.commons.text.lookup.StringLookup in project commons-text by apache.

the class StringSubstitutorTest method testReplaceInTakingStringBuilderWithNonNull.

@Test
public void testReplaceInTakingStringBuilderWithNonNull() {
    final StringLookup strLookup = StringLookupFactory.INSTANCE.systemPropertyStringLookup();
    final StringSubstitutor strSubstitutor = new StringSubstitutor(strLookup, "b<H", "b<H", '\'');
    final StringBuilder stringBuilder = new StringBuilder((CharSequence) "b<H");
    assertEquals('\'', strSubstitutor.getEscapeChar());
    assertFalse(strSubstitutor.replaceIn(stringBuilder));
}
Also used : StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.Test)

Example 5 with StringLookup

use of org.apache.commons.text.lookup.StringLookup in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteCorrectly.

@Test
void shouldSubstituteCorrectly() throws IOException {
    StringLookup dummyLookup = (x) -> "baz";
    DummySourceProvider dummyProvider = new DummySourceProvider();
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(dummyProvider, new StringSubstitutor(dummyLookup));
    assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: baz".getBytes(StandardCharsets.UTF_8)));
    // ensure that opened streams are closed
    assertThatIOException().isThrownBy(() -> dummyProvider.lastStream.read()).withMessage("Stream closed");
}
Also used : Test(org.junit.jupiter.api.Test) StringLookup(org.apache.commons.text.lookup.StringLookup) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) StandardCharsets(java.nio.charset.StandardCharsets) InputStream(java.io.InputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ByteArrayInputStream(java.io.ByteArrayInputStream) StringLookup(org.apache.commons.text.lookup.StringLookup) Test(org.junit.jupiter.api.Test)

Aggregations

StringLookup (org.apache.commons.text.lookup.StringLookup)5 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 StandardCharsets (java.nio.charset.StandardCharsets)3 StringSubstitutor (org.apache.commons.text.StringSubstitutor)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatIOException (org.assertj.core.api.Assertions.assertThatIOException)3 Test (org.junit.jupiter.api.Test)3 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 StringMatcher (org.apache.commons.text.matcher.StringMatcher)1