1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli.props;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.StringReader;
26 import java.io.StringWriter;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.impl.model.DefaultInterpolator;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertNotNull;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37
38
39
40
41 @Deprecated
42 public class MavenPropertiesTest {
43
44 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
45 private static final String COMMENT = "# comment";
46 private static final String KEY1 = "mvn:foo/bar";
47 private static final String KEY1A = "mvn\\:foo/bar";
48 private static final String KEY2 = "foo:bar:version:type:classifier";
49 private static final String KEY2A = "foo\\:bar\\:version\\:type\\:classifier";
50 private static final String VALUE1 = "value";
51
52 private MavenProperties properties;
53
54 static final String TEST_PROPERTIES = """
55 #
56 # test.properties
57 # Used in the PropertiesTest
58 #
59 test=test
60 """;
61
62
63
64
65
66 @BeforeEach
67 public void setUp() throws Exception {
68 properties = new MavenProperties();
69 properties.load(new StringReader(TEST_PROPERTIES));
70 }
71
72 @Test
73 public void testSpaces() throws Exception {
74 String config = "\n" + "\n"
75 + " \n"
76 + " \n"
77 + " \\ \\r \\n \\t \\f\n"
78 + " \n"
79 + " \n"
80 + "! dshfjklahfjkldashgjl;as\n"
81 + " #jdfagdfjagkdjfghksdajfd\n"
82 + " \n"
83 + "!!properties\n"
84 + "\n"
85 + "a=a\n"
86 + "b bb as,dn \n"
87 + "c\\r\\ \\t\\nu =:: cu\n"
88 + "bu= b\\\n"
89 + " u\n"
90 + "d=d\\r\\ne=e\n"
91 + "f :f\\\n"
92 + "f\\\n"
93 + " f\n"
94 + "g g\n"
95 + "h\\u0020h\n"
96 + "\\ i=i\n"
97 + "j=\\ j\n"
98 + "space=\\ c\n"
99 + "\n"
100 + "dblbackslash=\\\\\n"
101 + " \n";
102
103 java.util.Properties props1 = new java.util.Properties();
104 props1.load(new StringReader(config));
105
106 MavenProperties props2 = new MavenProperties();
107 props2.load(new StringReader(config));
108
109 String s325 = props1.getProperty(" \r");
110 assertEquals("\n \t \f", s325, "1");
111 String s324 = props1.getProperty("a");
112 assertEquals("a", s324, "2");
113 String s323 = props1.getProperty("b");
114 assertEquals("bb as,dn ", s323, "3");
115 String s322 = props1.getProperty("c\r \t\nu");
116 assertEquals(":: cu", s322, "4");
117 String s321 = props1.getProperty("bu");
118 assertEquals("bu", s321, "5");
119 String s320 = props1.getProperty("d");
120 assertEquals("d\r\ne=e", s320, "6");
121 String s319 = props1.getProperty("f");
122 assertEquals("fff", s319, "7");
123 String s318 = props1.getProperty("g");
124 assertEquals("g", s318, "8");
125 String s317 = props1.getProperty("h h");
126 assertEquals("", s317, "9");
127 String s316 = props1.getProperty(" ");
128 assertEquals("i=i", s316, "10");
129 String s315 = props1.getProperty("j");
130 assertEquals(" j", s315, "11");
131 String s314 = props1.getProperty("space");
132 assertEquals(" c", s314, "12");
133 String s313 = props1.getProperty("dblbackslash");
134 assertEquals("\\", s313, "13");
135
136 String s312 = props2.getProperty(" \r");
137 assertEquals("\n \t \f", s312, "1");
138 String s311 = props2.getProperty("a");
139 assertEquals("a", s311, "2");
140 String s310 = props2.getProperty("b");
141 assertEquals("bb as,dn ", s310, "3");
142 String s39 = props2.getProperty("c\r \t\nu");
143 assertEquals(":: cu", s39, "4");
144 String s38 = props2.getProperty("bu");
145 assertEquals("bu", s38, "5");
146 String s37 = props2.getProperty("d");
147 assertEquals("d\r\ne=e", s37, "6");
148 String s36 = props2.getProperty("f");
149 assertEquals("fff", s36, "7");
150 String s35 = props2.getProperty("g");
151 assertEquals("g", s35, "8");
152 String s34 = props2.getProperty("h h");
153 assertEquals("", s34, "9");
154 String s33 = props2.getProperty(" ");
155 assertEquals("i=i", s33, "10");
156 String s32 = props2.getProperty("j");
157 assertEquals(" j", s32, "11");
158 String s31 = props2.getProperty("space");
159 assertEquals(" c", s31, "12");
160 String s3 = props2.getProperty("dblbackslash");
161 assertEquals("\\", s3, "13");
162 assertEquals(props1, props2);
163 }
164
165 @Test
166 public void testConfigInterpolation() throws IOException {
167 String config = "a=$\\\\\\\\{var}\n" + "ab=${a}b\n" + "abc=${ab}c";
168 Map<String, String> expected = Map.of("a", "$\\{var}", "ab", "$\\{var}b", "abc", "$\\{var}bc");
169
170 java.util.Properties props1 = new java.util.Properties();
171 props1.load(new StringReader(config));
172 new DefaultInterpolator().performSubstitution((Map) props1, null, true);
173 assertEquals(expected, props1);
174
175 MavenProperties props2 = new MavenProperties();
176 props2.load(new StringReader(config));
177 assertEquals(expected, props2);
178 }
179
180
181
182
183
184
185
186
187 @Test
188 public void testGettingProperty() throws Exception {
189 Object o2 = properties.get("test");
190 assertEquals("test", o2);
191 }
192
193 @Test
194 public void testLoadSave() throws IOException {
195 StringWriter sw = new StringWriter();
196 PrintWriter pw = new PrintWriter(sw);
197 pw.println("# ");
198 pw.println("# The Main ");
199 pw.println("# ");
200 pw.println("# Comment ");
201 pw.println("# ");
202 pw.println("");
203 pw.println("# Another comment");
204 pw.println("");
205 pw.println("# A value comment");
206 pw.println("key1 = val1");
207 pw.println("");
208 pw.println("# Another value comment");
209 pw.println("key2 = ${key1}/foo");
210 pw.println("");
211 pw.println("# A third comment");
212 pw.println("key3 = val3");
213 pw.println("");
214
215 MavenProperties props = new MavenProperties();
216 props.load(new StringReader(sw.toString()));
217 props.save(System.err);
218 System.err.println("=====");
219
220 props.put("key2", props.get("key2"));
221 props.put("key3", "foo");
222 props.save(System.err);
223 System.err.println("=====");
224 }
225
226 @Test
227 public void testJavaUtilPropertiesCompatibility() throws Exception {
228 MavenProperties properties = new MavenProperties();
229 properties.load(new StringReader(TEST_PROPERTIES));
230
231 String test = properties.getProperty("test");
232 assertEquals("test", test);
233
234 String defaultValue = properties.getProperty("notfound", "default");
235 assertEquals("default", defaultValue);
236
237 properties.setProperty("another", "another");
238 Object o1 = properties.getProperty("another");
239 assertEquals("another", o1);
240
241 properties.store(System.err, null);
242 System.err.println("====");
243 }
244
245 private static final String RESULT1 = COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1 + LINE_SEPARATOR;
246
247 @Test
248 public void testSaveComment1() throws Exception {
249 properties.put(KEY1, COMMENT, VALUE1);
250 StringWriter sw = new StringWriter();
251 properties.save(sw);
252 String msg = sw.toString();
253 assertTrue(sw.toString().endsWith(RESULT1), msg);
254 }
255
256 private static final String RESULT1A = COMMENT + LINE_SEPARATOR + KEY2A + " = " + VALUE1 + LINE_SEPARATOR;
257
258 @Test
259 public void testSaveComment1a() throws Exception {
260 properties.put(KEY2, COMMENT, VALUE1);
261 StringWriter sw = new StringWriter();
262 properties.save(sw);
263 String msg = sw.toString();
264 assertTrue(sw.toString().endsWith(RESULT1A), msg);
265 }
266
267 private static final String RESULT2 =
268 COMMENT + LINE_SEPARATOR + COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1 + LINE_SEPARATOR;
269
270 @Test
271 public void testSaveComment2() throws Exception {
272 properties.put(KEY1, List.of(new String[] {COMMENT, COMMENT}), VALUE1);
273 StringWriter sw = new StringWriter();
274 properties.save(sw);
275 String msg = sw.toString();
276 assertTrue(sw.toString().endsWith(RESULT2), msg);
277 }
278
279 private static final String RESULT3 = COMMENT + LINE_SEPARATOR + COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1
280 + "\\" + LINE_SEPARATOR + VALUE1 + LINE_SEPARATOR;
281
282 @Test
283 public void testSaveComment3() throws Exception {
284 properties.put(KEY1, List.of(new String[] {COMMENT, COMMENT}), List.of(new String[] {VALUE1, VALUE1}));
285 StringWriter sw = new StringWriter();
286 properties.save(sw);
287 String msg = sw.toString();
288 assertTrue(sw.toString().endsWith(RESULT3), msg);
289 List<String> rawValue = properties.getRaw(KEY1);
290 assertEquals(2, (Object) rawValue.size());
291 assertEquals(KEY1A + " = " + VALUE1, rawValue.get(0));
292 assertEquals(VALUE1, rawValue.get(1));
293 }
294
295 @Test
296 public void testEntrySetValue() throws Exception {
297 properties.put(KEY1, VALUE1);
298
299 ByteArrayOutputStream baos = new ByteArrayOutputStream();
300 properties.save(baos);
301
302 properties = new MavenProperties();
303 properties.load(new ByteArrayInputStream(baos.toByteArray()));
304 Object o22 = properties.get(KEY1);
305 assertEquals(VALUE1, o22);
306 for (Map.Entry<String, String> entry : properties.entrySet()) {
307 entry.setValue(entry.getValue() + "x");
308 }
309 Object o21 = properties.get(KEY1);
310 assertEquals(VALUE1 + "x", o21);
311
312 baos = new ByteArrayOutputStream();
313 properties.save(baos);
314
315 properties = new MavenProperties();
316 properties.load(new ByteArrayInputStream(baos.toByteArray()));
317 Object o2 = properties.get(KEY1);
318 assertEquals(VALUE1 + "x", o2);
319 }
320
321 @Test
322 public void testMultiValueEscaping() throws IOException {
323 StringWriter sw = new StringWriter();
324 PrintWriter pw = new PrintWriter(sw);
325 pw.println("fruits apple, banana, pear, \\");
326 pw.println(" cantaloupe, watermelon, \\");
327 pw.println(" kiwi, mango");
328
329 java.util.Properties p = new java.util.Properties();
330 p.load(new StringReader(sw.toString()));
331 Object o24 = p.getProperty("fruits");
332 assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o24);
333
334 MavenProperties props = new MavenProperties();
335 props.load(new StringReader(sw.toString()));
336 Object o23 = props.getProperty("fruits");
337 assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o23);
338 List<String> raw = props.getRaw("fruits");
339 assertNotNull(raw);
340 assertEquals(3, (Object) raw.size());
341 assertEquals("fruits apple, banana, pear, ", raw.get(0));
342
343 props = new MavenProperties();
344 props.put(
345 "fruits",
346 props.getComments("fruits"),
347 List.of(
348 "fruits apple, banana, pear, ",
349 " cantaloupe, watermelon, ",
350 " kiwi, mango"));
351 Object o22 = props.getProperty("fruits");
352 assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o22);
353 raw = props.getRaw("fruits");
354 assertNotNull(raw);
355 assertEquals(3, (Object) raw.size());
356 assertEquals("fruits apple, banana, pear, ", raw.get(0));
357
358 sw = new StringWriter();
359 props.save(sw);
360 props = new MavenProperties();
361 props.load(new StringReader(sw.toString()));
362 Object o21 = props.getProperty("fruits");
363 assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o21);
364 raw = props.getRaw("fruits");
365 assertNotNull(raw);
366 assertEquals(3, (Object) raw.size());
367 assertEquals("fruits apple, banana, pear, ", raw.get(0));
368
369 props = new MavenProperties();
370 props.put(
371 "fruits",
372 props.getComments("fruits"),
373 List.of(
374 " apple, banana, pear, ",
375 " cantaloupe, watermelon, ",
376 " kiwi, mango"));
377 Object o2 = props.getProperty("fruits");
378 assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o2);
379 raw = props.getRaw("fruits");
380 assertNotNull(raw);
381 assertEquals(3, (Object) raw.size());
382 assertEquals("fruits = apple, banana, pear, ", raw.get(0));
383 }
384
385 @Test
386 public void testUpdate() throws Exception {
387 MavenProperties p1 = new MavenProperties();
388 p1.put(
389 "fruits",
390 List.of("#", "# List of fruits", "#"),
391 List.of(
392 " apple, banana, pear, ",
393 " cantaloupe, watermelon, ",
394 " kiwi, mango"));
395 p1.put("trees", List.of("#", "# List of trees", "#"), List.of(" fir, oak, maple"));
396 p1.put("vegetables", List.of("#", "# List of vegetables", "#"), List.of(" potatoes"));
397
398 MavenProperties p2 = new MavenProperties();
399 p2.put(
400 "fruits",
401 List.of("#", "# List of good fruits", "#"),
402 List.of(" apple, banana, pear"));
403 p2.put("trees", "fir, oak, maple");
404 p1.update(p2);
405
406 assertEquals(2, (Object) p1.size());
407 Object o23 = p1.getComments("trees");
408 assertEquals(List.of("#", "# List of trees", "#"), o23);
409 Object o22 = p1.getProperty("trees");
410 assertEquals("fir, oak, maple", o22);
411 Object o21 = p1.getComments("fruits");
412 assertEquals(List.of("#", "# List of good fruits", "#"), o21);
413 Object o2 = p1.getProperty("fruits");
414 assertEquals("apple, banana, pear", o2);
415 }
416
417 @Test
418 public void testSubstitution() throws IOException {
419 String str = "port = 4141" + LINE_SEPARATOR + "host = localhost"
420 + LINE_SEPARATOR + "url = https://${host}:${port}/service"
421 + LINE_SEPARATOR;
422 MavenProperties properties = new MavenProperties();
423 properties.load(new StringReader(str));
424 properties.put("url", "https://localhost:4141/service");
425 StringWriter sw = new StringWriter();
426 properties.save(sw);
427 Object o2 = sw.toString();
428 assertEquals(str, o2);
429 }
430 }