1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.assertThrows;
22
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.commons.text.StringSubstitutor;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class PropertiesStringLookupTest {
37
38 private static final Path CURRENT_PATH = Paths.get(StringUtils.EMPTY);
39 private static final String DOC_RELATIVE = "src/test/resources/org/apache/commons/text/document.properties";
40 private static final String DOC_ROOT = "/foo.txt";
41 private static final String KEY = "mykey";
42 private static final String KEY_RELATIVE = PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, KEY);
43 private static final String KEY_ROOT = PropertiesStringLookup.toPropertyKey(DOC_ROOT, KEY);
44 private static final Path[] NULL_PATH_ARRAY = null;
45
46 public static void testFence(final StringSubstitutor stringSubstitutor) {
47 assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
48 assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:/foo.txt}"));
49 assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${file:UTF-8:../foo.txt}"));
50 }
51
52 @Test
53 void testFenceOne() {
54 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(KEY_ROOT));
55 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(Paths.get("not a dir at all"), CURRENT_PATH).apply(KEY_ROOT));
56 }
57
58 @Test
59 void testInterpolator() {
60 final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
61 assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
62 }
63
64 @Test
65 void testInterpolatorNestedColon() {
66 final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
67
68 final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_RELATIVE + "}");
69 assertEquals("${properties:" + System.getProperty("user.dir") + "/src/test/resources/org/apache/commons/text/document.properties::mykey}", replaced);
70 assertEquals("Hello World!", stringSubstitutor.replace(replaced));
71 }
72
73 @Test
74 void testInterpolatorReplace() {
75 final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
76 assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
77 final InterpolatorStringLookup stringLookup = (InterpolatorStringLookup) stringSubstitutor.getStringLookup();
78 stringLookup.getStringLookupMap().replace(StringLookupFactory.KEY_FILE, StringLookupFactory.INSTANCE.fileStringLookup(CURRENT_PATH));
79 testFence(stringSubstitutor);
80 }
81
82 @Test
83 void testInterpolatorReplaceProperties() {
84 final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
85 assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
86 final InterpolatorStringLookup stringLookup = (InterpolatorStringLookup) stringSubstitutor.getStringLookup();
87 stringLookup.getStringLookupMap().replace(StringLookupFactory.KEY_PROPERTIES, StringLookupFactory.INSTANCE.propertiesStringLookup(CURRENT_PATH));
88 assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_RELATIVE + "}"));
89 assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${properties:UTF-8:/foo.txt}"));
90 }
91
92 @Test
93 void testInterpolatorWithParameterizedKey() {
94 final Map<String, String> map = new HashMap<>();
95 map.put("KeyIsHere", KEY);
96 final StringSubstitutor stringSubstitutor = new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
97 final String replaced = stringSubstitutor.replace("$${properties:" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "${KeyIsHere}}"));
98 assertEquals("${properties:" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "mykey}"), replaced);
99 assertEquals("Hello World!", stringSubstitutor.replace(replaced));
100 }
101
102 @Test
103 void testInterpolatorWithParameterizedKey2() {
104 final Map<String, String> map = new HashMap<>();
105 map.put("KeyIsHere", KEY);
106 final StringSubstitutor stringSubstitutor = new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
107 final String replaced = stringSubstitutor
108 .replace("$${properties:${sys:user.dir}/" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "${KeyIsHere}}"));
109 assertEquals("${properties:" + System.getProperty("user.dir") + "/" + PropertiesStringLookup.toPropertyKey(DOC_RELATIVE, "mykey}"), replaced);
110 assertEquals("Hello World!", stringSubstitutor.replace(replaced));
111 }
112
113 @Test
114 void testMissingFile() {
115 assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.apply("MissingFile"));
116 }
117
118 @Test
119 void testMissingFileWithKey() {
120 assertThrows(IllegalArgumentException.class,
121 () -> PropertiesStringLookup.INSTANCE.apply(PropertiesStringLookup.toPropertyKey("MissingFile", "AnyKey")));
122 }
123
124 @Test
125 void testMissingKey() {
126 assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.apply(DOC_RELATIVE));
127 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup().apply(DOC_RELATIVE));
128 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(NULL_PATH_ARRAY).apply(DOC_RELATIVE));
129 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(DOC_RELATIVE));
130 }
131
132 @Test
133 void testNull() {
134 Assertions.assertNull(PropertiesStringLookup.INSTANCE.apply(null));
135 Assertions.assertNull(new PropertiesStringLookup().apply(null));
136 Assertions.assertNull(new PropertiesStringLookup(NULL_PATH_ARRAY).apply(null));
137 Assertions.assertNull(new PropertiesStringLookup(CURRENT_PATH).apply(null));
138 }
139
140 @Test
141 void testOne() {
142 assertEquals("Hello World!", PropertiesStringLookup.INSTANCE.apply(KEY_RELATIVE));
143 assertEquals("Hello World!", new PropertiesStringLookup().apply(KEY_RELATIVE));
144 assertEquals("Hello World!", new PropertiesStringLookup(NULL_PATH_ARRAY).apply(KEY_RELATIVE));
145 assertEquals("Hello World!", new PropertiesStringLookup(CURRENT_PATH).apply(KEY_RELATIVE));
146 assertThrows(IllegalArgumentException.class, () -> new PropertiesStringLookup(CURRENT_PATH).apply(KEY_ROOT));
147 }
148
149 @Test
150 void testToString() {
151
152 Assertions.assertFalse(PropertiesStringLookup.INSTANCE.toString().isEmpty());
153 Assertions.assertFalse(new PropertiesStringLookup().toString().isEmpty());
154 Assertions.assertFalse(new PropertiesStringLookup(NULL_PATH_ARRAY).toString().isEmpty());
155 Assertions.assertFalse(new PropertiesStringLookup(CURRENT_PATH).toString().isEmpty());
156 }
157
158 }