Repository: commons-text Updated Branches: refs/heads/master 4f28f3795 -> a8824266f
[TEXT-113] Add an interpolator string lookup. Deprecate some ctors. Access package private lookup classes through a factory. Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/a8824266 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/a8824266 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/a8824266 Branch: refs/heads/master Commit: a8824266fa5f37d9f0f9da0780af32be67d12910 Parents: 4f28f37 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Sat Feb 10 23:20:01 2018 -0700 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Sat Feb 10 23:20:01 2018 -0700 ---------------------------------------------------------------------- .../org/apache/commons/text/StrSubstitutor.java | 108 ++++++++++++++++++- .../commons/text/lookup/DateStringLookup.java | 4 +- .../lookup/EnvironmentVariableStringLookup.java | 4 +- .../text/lookup/InterpolatorStringLookup.java | 6 +- .../text/lookup/JavaPlatformStringLookup.java | 4 +- .../commons/text/lookup/MapStringLookup.java | 17 ++- .../commons/text/lookup/NullStringLookup.java | 4 +- .../text/lookup/ResourceBundleStringLookup.java | 4 +- .../text/lookup/StringLookupFactory.java | 80 ++++++++++++++ .../text/lookup/SystemPropertyStringLookup.java | 4 +- .../apache/commons/text/StrSubstitutorTest.java | 45 ++++++++ ...titutorWithInterpolatorStringLookupTest.java | 47 ++++++++ .../lookup/InterpolatorStringLookupTest.java | 2 +- .../text/lookup/MapStringLookupTest.java | 2 +- .../StrSubstitutorWithInterpolatorTest.java | 47 -------- 15 files changed, 308 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/StrSubstitutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/StrSubstitutor.java b/src/main/java/org/apache/commons/text/StrSubstitutor.java index 453c06e..08d132a 100644 --- a/src/main/java/org/apache/commons/text/StrSubstitutor.java +++ b/src/main/java/org/apache/commons/text/StrSubstitutor.java @@ -24,6 +24,8 @@ import java.util.Map; import java.util.Properties; import org.apache.commons.lang3.Validate; +import org.apache.commons.text.lookup.StringLookup; +import org.apache.commons.text.lookup.StringLookupFactory; /** * Substitutes variables within a string by values. @@ -167,7 +169,7 @@ public class StrSubstitutor { /** * Variable resolution is delegated to an implementor of VariableResolver. */ - private StrLookup<?> variableResolver; + private StringLookup variableResolver; /** * The flag whether substitution in variable names is enabled. @@ -248,7 +250,7 @@ public class StrSubstitutor { * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { - return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); + return new StrSubstitutor(StringLookupFactory.INSTANCE.systemPropertyStringLookup()).replace(source); } //----------------------------------------------------------------------- @@ -319,7 +321,9 @@ public class StrSubstitutor { * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, may be null + * @deprecated Use the StringLookup version of this constructor. */ + @Deprecated public StrSubstitutor(final StrLookup<?> variableResolver) { this(variableResolver, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE); } @@ -332,7 +336,9 @@ public class StrSubstitutor { * @param suffix the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null + * @deprecated Use the StringLookup version of this constructor. */ + @Deprecated public StrSubstitutor(final StrLookup<?> variableResolver, final String prefix, final String suffix, final char escape) { this.setVariableResolver(variableResolver); @@ -351,7 +357,9 @@ public class StrSubstitutor { * @param escape the escape character * @param valueDelimiter the variable default value delimiter string, may be null * @throws IllegalArgumentException if the prefix or suffix is null + * @deprecated Use the StringLookup version of this constructor. */ + @Deprecated public StrSubstitutor(final StrLookup<?> variableResolver, final String prefix, final String suffix, final char escape, final String valueDelimiter) { this.setVariableResolver(variableResolver); @@ -369,7 +377,9 @@ public class StrSubstitutor { * @param suffixMatcher the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null + * @deprecated Use the StringLookup version of this constructor. */ + @Deprecated public StrSubstitutor( final StrLookup<?> variableResolver, final StrMatcher prefixMatcher, final StrMatcher suffixMatcher, final char escape) { @@ -385,7 +395,9 @@ public class StrSubstitutor { * @param escape the escape character * @param valueDelimiterMatcher the variable default value delimiter matcher, may be null * @throws IllegalArgumentException if the prefix or suffix is null + * @deprecated Use the StringLookup version of this constructor. */ + @Deprecated public StrSubstitutor( final StrLookup<?> variableResolver, final StrMatcher prefixMatcher, final StrMatcher suffixMatcher, final char escape, final StrMatcher valueDelimiterMatcher) { @@ -396,6 +408,87 @@ public class StrSubstitutor { this.setValueDelimiterMatcher(valueDelimiterMatcher); } + /** + * Creates a new instance and initializes it. + * + * @param variableResolver the variable resolver, may be null + */ + public StrSubstitutor(final StringLookup variableResolver) { + this(variableResolver, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE); + } + + /** + * Creates a new instance and initializes it. + * + * @param variableResolver the variable resolver, may be null + * @param prefix the prefix for variables, not null + * @param suffix the suffix for variables, not null + * @param escape the escape character + * @throws IllegalArgumentException if the prefix or suffix is null + */ + public StrSubstitutor(final StringLookup variableResolver, final String prefix, final String suffix, + final char escape) { + this.setVariableResolver(variableResolver); + this.setVariablePrefix(prefix); + this.setVariableSuffix(suffix); + this.setEscapeChar(escape); + this.setValueDelimiterMatcher(DEFAULT_VALUE_DELIMITER); + } + + /** + * Creates a new instance and initializes it. + * + * @param variableResolver the variable resolver, may be null + * @param prefix the prefix for variables, not null + * @param suffix the suffix for variables, not null + * @param escape the escape character + * @param valueDelimiter the variable default value delimiter string, may be null + * @throws IllegalArgumentException if the prefix or suffix is null + */ + public StrSubstitutor(final StringLookup variableResolver, final String prefix, final String suffix, + final char escape, final String valueDelimiter) { + this.setVariableResolver(variableResolver); + this.setVariablePrefix(prefix); + this.setVariableSuffix(suffix); + this.setEscapeChar(escape); + this.setValueDelimiter(valueDelimiter); + } + + /** + * Creates a new instance and initializes it. + * + * @param variableResolver the variable resolver, may be null + * @param prefixMatcher the prefix for variables, not null + * @param suffixMatcher the suffix for variables, not null + * @param escape the escape character + * @throws IllegalArgumentException if the prefix or suffix is null + */ + public StrSubstitutor( + final StringLookup variableResolver, final StrMatcher prefixMatcher, final StrMatcher suffixMatcher, + final char escape) { + this(variableResolver, prefixMatcher, suffixMatcher, escape, DEFAULT_VALUE_DELIMITER); + } + + /** + * Creates a new instance and initializes it. + * + * @param variableResolver the variable resolver, may be null + * @param prefixMatcher the prefix for variables, not null + * @param suffixMatcher the suffix for variables, not null + * @param escape the escape character + * @param valueDelimiterMatcher the variable default value delimiter matcher, may be null + * @throws IllegalArgumentException if the prefix or suffix is null + */ + public StrSubstitutor( + final StringLookup variableResolver, final StrMatcher prefixMatcher, final StrMatcher suffixMatcher, + final char escape, final StrMatcher valueDelimiterMatcher) { + this.setVariableResolver(variableResolver); + this.setVariablePrefixMatcher(prefixMatcher); + this.setVariableSuffixMatcher(suffixMatcher); + this.setEscapeChar(escape); + this.setValueDelimiterMatcher(valueDelimiterMatcher); + } + //----------------------------------------------------------------------- /** * Replaces all the occurrences of variables with their matching values @@ -1174,7 +1267,7 @@ public class StrSubstitutor { * @return the VariableResolver */ public StrLookup<?> getVariableResolver() { - return this.variableResolver; + return (StrLookup<?>) this.variableResolver; } /** @@ -1186,6 +1279,15 @@ public class StrSubstitutor { this.variableResolver = variableResolver; } + /** + * Sets the VariableResolver that is used to lookup variables. + * + * @param variableResolver the VariableResolver + */ + public void setVariableResolver(final StringLookup variableResolver) { + this.variableResolver = variableResolver; + } + // Substitution support in variable names //----------------------------------------------------------------------- /** http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java index ee2a89d..d5cbf7d 100644 --- a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java @@ -23,12 +23,12 @@ import java.util.Date; /** * Formats the current date or the date in the LogEvent. The "key" is used as the format String. */ -public final class DateStringLookup extends AbstractStringLookup { +final class DateStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final DateStringLookup INSTANCE = new DateStringLookup(); + public static final StringLookup INSTANCE = new DateStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java b/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java index 9f7a927..def095a 100644 --- a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java @@ -21,12 +21,12 @@ package org.apache.commons.text.lookup; * * @since 1.3 */ -public final class EnvironmentVariableStringLookup extends AbstractStringLookup { +final class EnvironmentVariableStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final EnvironmentVariableStringLookup INSTANCE = new EnvironmentVariableStringLookup(); + public static final StringLookup INSTANCE = new EnvironmentVariableStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java index 2ded621..b696dc3 100644 --- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java @@ -23,7 +23,7 @@ import java.util.Map; /** * Proxies other {@link StringLookup}s using a keys within ${} markers using the format "${StringLookup:Key}". */ -public class InterpolatorStringLookup extends AbstractStringLookup { +class InterpolatorStringLookup extends AbstractStringLookup { /** Constant for the prefix separator. */ private static final char PREFIX_SEPARATOR = ':'; @@ -63,8 +63,8 @@ public class InterpolatorStringLookup extends AbstractStringLookup { * @param defaultMap * the default map for string lookups. */ - public InterpolatorStringLookup(final Map<String, String> defaultMap) { - this(new MapStringLookup<>(defaultMap == null ? new HashMap<String, String>() : defaultMap)); + public <V> InterpolatorStringLookup(final Map<String, V> defaultMap) { + this(MapStringLookup.on(defaultMap == null ? new HashMap<String, V>() : defaultMap)); // TODO: Use a service loader stringLookupMap.put("sys", SystemPropertyStringLookup.INSTANCE); stringLookupMap.put("env", EnvironmentVariableStringLookup.INSTANCE); http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java index f7ecee1..5dc5121 100644 --- a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java @@ -23,12 +23,12 @@ import java.util.Locale; * * @since 1.3 */ -public final class JavaPlatformStringLookup extends AbstractStringLookup { +final class JavaPlatformStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup(); + public static final StringLookup INSTANCE = new JavaPlatformStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java index 51a9c5b..2b0e9d9 100644 --- a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java @@ -29,6 +29,17 @@ import java.util.Map; public final class MapStringLookup<V> implements StringLookup { /** + * Creates a new instance backed by a Map. Used by the default lookup. + * + * @param map + * the map of keys to values, may be null. + * @return a new instance backed by the given map. + */ + public static <T> MapStringLookup<T> on(final Map<String, T> map) { + return new MapStringLookup<>(map); + } + + /** * Map keys are variable names and value. */ private final Map<String, V> map; @@ -37,9 +48,9 @@ public final class MapStringLookup<V> implements StringLookup { * Creates a new instance backed by a Map. Used by the default lookup. * * @param map - * the map of keys to values, may be null + * the map of keys to values, may be null. */ - public MapStringLookup(final Map<String, V> map) { + private MapStringLookup(final Map<String, V> map) { this.map = map; } @@ -54,7 +65,7 @@ public final class MapStringLookup<V> implements StringLookup { * </p> * * @param key - * the key to be looked up, may be null + * the key to be looked up, may be null. * @return the matching value, null if no match */ @Override http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java b/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java index 53e2fa7..3aee8a8 100644 --- a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java @@ -22,12 +22,12 @@ package org.apache.commons.text.lookup; * * @since 1.3 */ -public final class NullStringLookup extends AbstractStringLookup { +final class NullStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final NullStringLookup INSTANCE = new NullStringLookup(); + public static final StringLookup INSTANCE = new NullStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java index 7f31cd1..1662745 100644 --- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java @@ -25,12 +25,12 @@ import java.util.ResourceBundle; * @see ResourceBundle * @since 1.3 */ -public final class ResourceBundleStringLookup extends AbstractStringLookup { +final class ResourceBundleStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup(); + public static final StringLookup INSTANCE = new ResourceBundleStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java new file mode 100644 index 0000000..593822b --- /dev/null +++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java @@ -0,0 +1,80 @@ +/* + * 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.commons.text.lookup; + +import java.util.Map; + +/** + * Provides access to lookups defined in this package. + * + * @since 1.3 + */ +public class StringLookupFactory { + + /** + * Defines the singleton for this class. + */ + public static final StringLookupFactory INSTANCE = new StringLookupFactory(); + + /** + * No need to build instances for now. + */ + private StringLookupFactory() { + // empty + } + + public StringLookup dateStringLookup() { + return DateStringLookup.INSTANCE; + } + + public StringLookup environmentVariableStringLookup() { + return EnvironmentVariableStringLookup.INSTANCE; + } + + public StringLookup interpolatorStringLookup() { + return new InterpolatorStringLookup(); + } + + public <V> StringLookup interpolatorStringLookup(final Map<String, V> map) { + return new InterpolatorStringLookup(map); + } + + public StringLookup interpolatorStringLookup(final StringLookup defaultStringLookup) { + return new InterpolatorStringLookup(defaultStringLookup); + } + + public StringLookup javaPlatformStringLookup() { + return JavaPlatformStringLookup.INSTANCE; + } + + public <V> StringLookup mapStringLookup(final Map<String, V> map) { + return MapStringLookup.on(map); + } + + public StringLookup nullStringLookup() { + return NullStringLookup.INSTANCE; + } + + public StringLookup resourceBundleStringLookup() { + return ResourceBundleStringLookup.INSTANCE; + } + + public StringLookup systemPropertyStringLookup() { + return SystemPropertyStringLookup.INSTANCE; + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java b/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java index 1856b09..8281008 100644 --- a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java @@ -21,12 +21,12 @@ package org.apache.commons.text.lookup; * * @since 1.3 */ -public final class SystemPropertyStringLookup extends AbstractStringLookup { +final class SystemPropertyStringLookup extends AbstractStringLookup { /** * Defines the singleton for this class. */ - public static final SystemPropertyStringLookup INSTANCE = new SystemPropertyStringLookup(); + public static final StringLookup INSTANCE = new SystemPropertyStringLookup(); /** * No need to build instances for now. http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/test/java/org/apache/commons/text/StrSubstitutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java index 24c24a6..8cd435a 100644 --- a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java +++ b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java @@ -29,6 +29,9 @@ import java.util.Map; import java.util.Properties; import org.apache.commons.lang3.mutable.MutableObject; +import org.apache.commons.text.lookup.MapStringLookup; +import org.apache.commons.text.lookup.StringLookup; +import org.apache.commons.text.lookup.StringLookupFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -767,6 +770,19 @@ public class StrSubstitutorTest { @Test public void testReplaceInTakingTwoAndThreeIntsReturningFalse() { final Map<String, Object> hashMap = new HashMap<>(); + final MapStringLookup<Object> strLookupMapStrLookup = MapStringLookup.on(hashMap); + final StrMatcher strMatcher = StrMatcher.tabMatcher(); + final StrSubstitutor strSubstitutor = + new StrSubstitutor(strLookupMapStrLookup, strMatcher, strMatcher, 'b', strMatcher); + + assertFalse(strSubstitutor.replaceIn((StringBuilder) null, 1315, (-1369))); + assertEquals('b', strSubstitutor.getEscapeChar()); + assertFalse(strSubstitutor.isPreserveEscapes()); + } + + @Test + public void testReplaceInTakingTwoAndThreeIntsReturningFalse_deprecated() { + final Map<String, Object> hashMap = new HashMap<>(); final StrLookup.MapStrLookup<Object> strLookupMapStrLookup = new StrLookup.MapStrLookup<>(hashMap); final StrMatcher strMatcher = StrMatcher.tabMatcher(); final StrSubstitutor strSubstitutor = @@ -779,6 +795,16 @@ public class StrSubstitutorTest { @Test public void testReplaceInTakingStringBuilderWithNonNull() { + final StringLookup strLookup = StringLookupFactory.INSTANCE.systemPropertyStringLookup(); + final StrSubstitutor strSubstitutor = new StrSubstitutor(strLookup, "b<H", "b<H", '\''); + final StringBuilder stringBuilder = new StringBuilder((CharSequence) "b<H"); + + assertEquals('\'', strSubstitutor.getEscapeChar()); + assertFalse(strSubstitutor.replaceIn(stringBuilder)); + } + + @Test + public void testReplaceInTakingStringBuilderWithNonNull_deprecated() { final StrLookup<String> strLookup = StrLookup.systemPropertiesLookup(); final StrSubstitutor strSubstitutor = new StrSubstitutor(strLookup, "b<H", "b<H", '\''); final StringBuilder stringBuilder = new StringBuilder((CharSequence) "b<H"); @@ -800,6 +826,16 @@ public class StrSubstitutorTest { @Test public void testCreatesStrSubstitutorTakingStrLookupAndCallsReplaceTakingTwoAndThreeInts() { final Map<String, CharacterPredicates> map = new HashMap<>(); + final MapStringLookup<CharacterPredicates> strLookupMapStrLookup = MapStringLookup.on(map); + final StrSubstitutor strSubstitutor = new StrSubstitutor(strLookupMapStrLookup); + + assertNull(strSubstitutor.replace((CharSequence) null, 0, 0)); + assertEquals('$', strSubstitutor.getEscapeChar()); + } + + @Test + public void testCreatesStrSubstitutorTakingStrLookupAndCallsReplaceTakingTwoAndThreeInts_deprecated() { + final Map<String, CharacterPredicates> map = new HashMap<>(); final StrLookup.MapStrLookup<CharacterPredicates> strLookupMapStrLookup = new StrLookup.MapStrLookup<>(map); final StrSubstitutor strSubstitutor = new StrSubstitutor(strLookupMapStrLookup); @@ -809,6 +845,15 @@ public class StrSubstitutorTest { @Test public void testReplaceTakingCharSequenceReturningNull() { + final StrSubstitutor strSubstitutor = new StrSubstitutor((StringLookup) null); + + assertNull(strSubstitutor.replace((CharSequence) null)); + assertFalse(strSubstitutor.isPreserveEscapes()); + assertEquals('$', strSubstitutor.getEscapeChar()); + } + + @Test + public void testReplaceTakingCharSequenceReturningNull_deprecated() { final StrSubstitutor strSubstitutor = new StrSubstitutor((StrLookup<?>) null); assertNull(strSubstitutor.replace((CharSequence) null)); http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/test/java/org/apache/commons/text/StrSubstitutorWithInterpolatorStringLookupTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StrSubstitutorWithInterpolatorStringLookupTest.java new file mode 100644 index 0000000..d50d3b7 --- /dev/null +++ b/src/test/java/org/apache/commons/text/StrSubstitutorWithInterpolatorStringLookupTest.java @@ -0,0 +1,47 @@ +/* + * 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.commons.text; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.text.lookup.StringLookupFactory; +import org.junit.Assert; +import org.junit.Test; + +public class StrSubstitutorWithInterpolatorStringLookupTest { + + @Test + public void testMapAndSystemProperty() { + final String key = "key"; + final String value = "value"; + final Map<String, String> map = new HashMap<>(); + map.put(key, value); + final StrSubstitutor strSubst = new StrSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); + final String spKey = "user.name"; + Assert.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}")); + Assert.assertEquals(value, strSubst.replace("${" + key + "}")); + } + + @Test + public void testSystemProperty() { + final StrSubstitutor strSubst = new StrSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup()); + final String spKey = "user.name"; + Assert.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}")); + } +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java index f88bc9c..0a86d9c 100644 --- a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java @@ -66,7 +66,7 @@ public class InterpolatorStringLookupTest { public void testLookup() { final Map<String, String> map = new HashMap<>(); map.put(TESTKEY, TESTVAL); - final StringLookup lookup = new InterpolatorStringLookup(new MapStringLookup<>(map)); + final StringLookup lookup = new InterpolatorStringLookup(MapStringLookup.on(map)); String value = lookup.lookup(TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("ctx:" + TESTKEY); http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java index 664078b..5e7d1b7 100644 --- a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java @@ -31,7 +31,7 @@ public class MapStringLookupTest { final String value = "value"; final Map<String, String> map = new HashMap<>(); map.put(key, value); - Assert.assertEquals(value, new MapStringLookup<>(map).lookup(key)); + Assert.assertEquals(value, MapStringLookup.on(map).lookup(key)); } } http://git-wip-us.apache.org/repos/asf/commons-text/blob/a8824266/src/test/java/org/apache/commons/text/lookup/StrSubstitutorWithInterpolatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/lookup/StrSubstitutorWithInterpolatorTest.java b/src/test/java/org/apache/commons/text/lookup/StrSubstitutorWithInterpolatorTest.java deleted file mode 100644 index 893eb5f..0000000 --- a/src/test/java/org/apache/commons/text/lookup/StrSubstitutorWithInterpolatorTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.commons.text.lookup; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.text.StrSubstitutor; -import org.junit.Assert; -import org.junit.Test; - -public class StrSubstitutorWithInterpolatorTest { - - @Test - public void testMapAndSystemProperty() { - final String key = "key"; - final String value = "value"; - final Map<String, String> map = new HashMap<>(); - map.put(key, value); - final StrSubstitutor strSubst = new StrSubstitutor(new InterpolatorStringLookup(map)); - final String spKey = "user.name"; - Assert.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}")); - Assert.assertEquals(value, strSubst.replace("${" + key + "}")); - } - - @Test - public void testSystemProperty() { - final StrSubstitutor strSubst = new StrSubstitutor(new InterpolatorStringLookup()); - final String spKey = "user.name"; - Assert.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}")); - } -}