View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.commons.text.lookup;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.commons.text.StringSubstitutor;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link PropertiesStringLookup}.
36   */
37  public class PropertiesStringLookupTest {
38  
39      private static final Path CURRENT_PATH = Paths.get(StringUtils.EMPTY); // NOT "."!
40      private static final String DOC_RELATIVE = "src/test/resources/org/apache/commons/text/document.properties";
41      private static final String DOC_ROOT = "/foo.txt";
42      private static final String KEY = "mykey";
43      private static final String KEY_RELATIVE = PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, KEY);
44      private static final String KEY_ROOT = PropertiesStringLookup.toPropertyKey(DOC_ROOT, KEY);
45      private static final Path[] NULL_PATH_ARRAY = null;
46  
47      public static void testFence(final StringSubstitutor stringSubstitutor) {
48          assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
49          assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:/foo.txt}"));
50          assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:../foo.txt}"));
51      }
52  
53      @Test
54      void testFenceOne() {
55          assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(KEY_ROOT));
56          assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(Paths.get("not a dir at all"), CURRENT_PATH).apply(KEY_ROOT));
57      }
58  
59      @Test
60      void testInterpolator() {
61          final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
62          assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
63      }
64  
65      @Test
66      void testInterpolatorNestedColon() {
67          final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
68          // Need to handle "C:" in the sys prop user.dir.
69          final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_RELATIVE + "}");
70          assertEquals("${properties:" + System.getProperty("user.dir") + "/src/test/resources/org/apache/commons/text/document.properties::mykey}", replaced);
71          assertEquals("Hello World!", stringSubstitutor.replace(replaced));
72      }
73  
74      @Test
75      void testInterpolatorReplace() {
76          final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
77          assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
78          final InterpolatorStringLookup stringLookup = (InterpolatorStringLookup) stringSubstitutor.getStringLookup();
79          stringLookup.getStringLookupMap().replace(StringLookupFactory.KEY_FILE, StringLookupFactory.INSTANCE.fileStringLookup(CURRENT_PATH));
80          testFence(stringSubstitutor);
81      }
82  
83      @Test
84      void testInterpolatorReplaceProperties() {
85          final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
86          assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
87          final InterpolatorStringLookup stringLookup = (InterpolatorStringLookup) stringSubstitutor.getStringLookup();
88          stringLookup.getStringLookupMap().replace(StringLookupFactory.KEY_PROPERTIES, StringLookupFactory.INSTANCE.propertiesStringLookup(CURRENT_PATH));
89          assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
90          assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${properties:UTF-8:/foo.txt}"));
91      }
92  
93      @Test
94      void testInterpolatorWithParameterizedKey() {
95          final Map<String, String> map = new HashMap<>();
96          map.put("KeyIsHere", KEY);
97          final StringSubstitutor stringSubstitutor = new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
98          final String replaced = stringSubstitutor.replace("$${properties:" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "${KeyIsHere}}"));
99          assertEquals("${properties:" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "mykey}"), replaced);
100         assertEquals("Hello World!", stringSubstitutor.replace(replaced));
101     }
102 
103     @Test
104     void testInterpolatorWithParameterizedKey2() {
105         final Map<String, String> map = new HashMap<>();
106         map.put("KeyIsHere", KEY);
107         final StringSubstitutor stringSubstitutor = new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
108         final String replaced = stringSubstitutor
109                 .replace("$${properties:${sys:user.dir}/" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "${KeyIsHere}}"));
110         assertEquals("${properties:" + System.getProperty("user.dir") + "/" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "mykey}"), replaced);
111         assertEquals("Hello World!", stringSubstitutor.replace(replaced));
112     }
113 
114     @Test
115     void testMissingFile() {
116         assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.apply("MissingFile"));
117     }
118 
119     @Test
120     void testMissingFileWithKey() {
121         assertThrows(IllegalArgumentException.class,
122                 () -> PropertiesStringLookup.INSTANCE.apply(PropertiesStringLookup.toPropertyKey("MissingFile", "AnyKey")));
123     }
124 
125     @Test
126     void testMissingKey() {
127         assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.apply(DOC_RELATIVE));
128         assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup().apply(DOC_RELATIVE));
129         assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(NULL_PATH_ARRAY).apply(DOC_RELATIVE));
130         assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(DOC_RELATIVE));
131     }
132 
133     @Test
134     void testNull() {
135         assertNull(PropertiesStringLookup.INSTANCE.apply(null));
136         assertNull(new PropertiesStringLookup().apply(null));
137         assertNull(new PropertiesStringLookup(NULL_PATH_ARRAY).apply(null));
138         assertNull(new PropertiesStringLookup(CURRENT_PATH).apply(null));
139     }
140 
141     @Test
142     void testOne() {
143         assertEquals("Hello World!", PropertiesStringLookup.INSTANCE.apply(KEY_RELATIVE));
144         assertEquals("Hello World!", new PropertiesStringLookup().apply(KEY_RELATIVE));
145         assertEquals("Hello World!", new PropertiesStringLookup(NULL_PATH_ARRAY).apply(KEY_RELATIVE));
146         assertEquals("Hello World!", new PropertiesStringLookup(CURRENT_PATH).apply(KEY_RELATIVE));
147         assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(KEY_ROOT));
148     }
149 
150     @Test
151     void testToString() {
152         // does not blow up and gives some kind of string.
153         assertFalse(PropertiesStringLookup.INSTANCE.toString().isEmpty());
154         assertFalse(new PropertiesStringLookup().toString().isEmpty());
155         assertFalse(new PropertiesStringLookup(NULL_PATH_ARRAY).toString().isEmpty());
156         assertFalse(new PropertiesStringLookup(CURRENT_PATH).toString().isEmpty());
157     }
158 
159 }