View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Unit tests on <code>MavenProperties</code>.
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      @BeforeEach
63      public void setUp() throws Exception {
64          properties = new MavenProperties();
65          properties.load(new StringReader(TEST_PROPERTIES));
66      }
67  
68      @Test
69      public void testSpaces() throws Exception {
70          String config = "\n" + "\n"
71                  + "    \n"
72                  + "                \n"
73                  + "   \\ \\r \\n \\t \\f\n"
74                  + "   \n"
75                  + "                                                \n"
76                  + "! dshfjklahfjkldashgjl;as\n"
77                  + "     #jdfagdfjagkdjfghksdajfd\n"
78                  + "     \n"
79                  + "!!properties\n"
80                  + "\n"
81                  + "a=a\n"
82                  + "b bb as,dn   \n"
83                  + "c\\r\\ \\t\\nu =:: cu\n"
84                  + "bu= b\\\n"
85                  + "                u\n"
86                  + "d=d\\r\\ne=e\n"
87                  + "f   :f\\\n"
88                  + "f\\\n"
89                  + "                        f\n"
90                  + "g               g\n"
91                  + "h\\u0020h\n"
92                  + "\\   i=i\n"
93                  + "j=\\   j\n"
94                  + "space=\\   c\n"
95                  + "\n"
96                  + "dblbackslash=\\\\\n"
97                  + "                        \n";
98  
99          java.util.Properties props1 = new java.util.Properties();
100         props1.load(new StringReader(config));
101 
102         MavenProperties props2 = new MavenProperties();
103         props2.load(new StringReader(config));
104 
105         String s325 = props1.getProperty(" \r");
106         assertEquals("\n \t \f", s325, "1");
107         String s324 = props1.getProperty("a");
108         assertEquals("a", s324, "2");
109         String s323 = props1.getProperty("b");
110         assertEquals("bb as,dn   ", s323, "3");
111         String s322 = props1.getProperty("c\r \t\nu");
112         assertEquals(":: cu", s322, "4");
113         String s321 = props1.getProperty("bu");
114         assertEquals("bu", s321, "5");
115         String s320 = props1.getProperty("d");
116         assertEquals("d\r\ne=e", s320, "6");
117         String s319 = props1.getProperty("f");
118         assertEquals("fff", s319, "7");
119         String s318 = props1.getProperty("g");
120         assertEquals("g", s318, "8");
121         String s317 = props1.getProperty("h h");
122         assertEquals("", s317, "9");
123         String s316 = props1.getProperty(" ");
124         assertEquals("i=i", s316, "10");
125         String s315 = props1.getProperty("j");
126         assertEquals("   j", s315, "11");
127         String s314 = props1.getProperty("space");
128         assertEquals("   c", s314, "12");
129         String s313 = props1.getProperty("dblbackslash");
130         assertEquals("\\", s313, "13");
131 
132         String s312 = props2.getProperty(" \r");
133         assertEquals("\n \t \f", s312, "1");
134         String s311 = props2.getProperty("a");
135         assertEquals("a", s311, "2");
136         String s310 = props2.getProperty("b");
137         assertEquals("bb as,dn   ", s310, "3");
138         String s39 = props2.getProperty("c\r \t\nu");
139         assertEquals(":: cu", s39, "4");
140         String s38 = props2.getProperty("bu");
141         assertEquals("bu", s38, "5");
142         String s37 = props2.getProperty("d");
143         assertEquals("d\r\ne=e", s37, "6");
144         String s36 = props2.getProperty("f");
145         assertEquals("fff", s36, "7");
146         String s35 = props2.getProperty("g");
147         assertEquals("g", s35, "8");
148         String s34 = props2.getProperty("h h");
149         assertEquals("", s34, "9");
150         String s33 = props2.getProperty(" ");
151         assertEquals("i=i", s33, "10");
152         String s32 = props2.getProperty("j");
153         assertEquals("   j", s32, "11");
154         String s31 = props2.getProperty("space");
155         assertEquals("   c", s31, "12");
156         String s3 = props2.getProperty("dblbackslash");
157         assertEquals("\\", s3, "13");
158         assertEquals(props1, props2);
159     }
160 
161     @Test
162     public void testConfigInterpolation() throws IOException {
163         String config = "a=$\\\\\\\\{var}\n" + "ab=${a}b\n" + "abc=${ab}c";
164         Map<String, String> expected = Map.of("a", "$\\{var}", "ab", "$\\{var}b", "abc", "$\\{var}bc");
165 
166         java.util.Properties props1 = new java.util.Properties();
167         props1.load(new StringReader(config));
168         new DefaultInterpolator().performSubstitution((Map) props1, null, true);
169         assertEquals(expected, props1);
170 
171         MavenProperties props2 = new MavenProperties();
172         props2.load(new StringReader(config));
173         assertEquals(expected, props2);
174     }
175 
176     /**
177      * <p>
178      * Test getting property.
179      * </p>
180      *
181      * @throws Exception
182      */
183     @Test
184     public void testGettingProperty() throws Exception {
185         Object o2 = properties.get("test");
186         assertEquals("test", o2);
187     }
188 
189     @Test
190     public void testLoadSave() throws IOException {
191         StringWriter sw = new StringWriter();
192         PrintWriter pw = new PrintWriter(sw);
193         pw.println("# ");
194         pw.println("# The Main  ");
195         pw.println("# ");
196         pw.println("# Comment ");
197         pw.println("# ");
198         pw.println("");
199         pw.println("# Another comment");
200         pw.println("");
201         pw.println("# A value comment");
202         pw.println("key1 = val1");
203         pw.println("");
204         pw.println("# Another value comment");
205         pw.println("key2 = ${key1}/foo");
206         pw.println("");
207         pw.println("# A third comment");
208         pw.println("key3 = val3");
209         pw.println("");
210 
211         MavenProperties props = new MavenProperties();
212         props.load(new StringReader(sw.toString()));
213         props.save(System.err);
214         System.err.println("=====");
215 
216         props.put("key2", props.get("key2"));
217         props.put("key3", "foo");
218         props.save(System.err);
219         System.err.println("=====");
220     }
221 
222     @Test
223     public void testJavaUtilPropertiesCompatibility() throws Exception {
224         MavenProperties properties = new MavenProperties();
225         properties.load(new StringReader(TEST_PROPERTIES));
226 
227         String test = properties.getProperty("test");
228         assertEquals("test", test);
229 
230         String defaultValue = properties.getProperty("notfound", "default");
231         assertEquals("default", defaultValue);
232 
233         properties.setProperty("another", "another");
234         Object o1 = properties.getProperty("another");
235         assertEquals("another", o1);
236 
237         properties.store(System.err, null);
238         System.err.println("====");
239     }
240 
241     private static final String RESULT1 = COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1 + LINE_SEPARATOR;
242 
243     @Test
244     public void testSaveComment1() throws Exception {
245         properties.put(KEY1, COMMENT, VALUE1);
246         StringWriter sw = new StringWriter();
247         properties.save(sw);
248         String msg = sw.toString();
249         assertTrue(sw.toString().endsWith(RESULT1), msg);
250     }
251 
252     private static final String RESULT1A = COMMENT + LINE_SEPARATOR + KEY2A + " = " + VALUE1 + LINE_SEPARATOR;
253 
254     @Test
255     public void testSaveComment1a() throws Exception {
256         properties.put(KEY2, COMMENT, VALUE1);
257         StringWriter sw = new StringWriter();
258         properties.save(sw);
259         String msg = sw.toString();
260         assertTrue(sw.toString().endsWith(RESULT1A), msg);
261     }
262 
263     private static final String RESULT2 =
264             COMMENT + LINE_SEPARATOR + COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1 + LINE_SEPARATOR;
265 
266     @Test
267     public void testSaveComment2() throws Exception {
268         properties.put(KEY1, List.of(new String[] {COMMENT, COMMENT}), VALUE1);
269         StringWriter sw = new StringWriter();
270         properties.save(sw);
271         String msg = sw.toString();
272         assertTrue(sw.toString().endsWith(RESULT2), msg);
273     }
274 
275     private static final String RESULT3 = COMMENT + LINE_SEPARATOR + COMMENT + LINE_SEPARATOR + KEY1A + " = " + VALUE1
276             + "\\" + LINE_SEPARATOR + VALUE1 + LINE_SEPARATOR;
277 
278     @Test
279     public void testSaveComment3() throws Exception {
280         properties.put(KEY1, List.of(new String[] {COMMENT, COMMENT}), List.of(new String[] {VALUE1, VALUE1}));
281         StringWriter sw = new StringWriter();
282         properties.save(sw);
283         String msg = sw.toString();
284         assertTrue(sw.toString().endsWith(RESULT3), msg);
285         List<String> rawValue = properties.getRaw(KEY1);
286         assertEquals(2, (Object) rawValue.size());
287         assertEquals(KEY1A + " = " + VALUE1, rawValue.get(0));
288         assertEquals(VALUE1, rawValue.get(1));
289     }
290 
291     @Test
292     public void testEntrySetValue() throws Exception {
293         properties.put(KEY1, VALUE1);
294 
295         ByteArrayOutputStream baos = new ByteArrayOutputStream();
296         properties.save(baos);
297 
298         properties = new MavenProperties();
299         properties.load(new ByteArrayInputStream(baos.toByteArray()));
300         Object o22 = properties.get(KEY1);
301         assertEquals(VALUE1, o22);
302         for (Map.Entry<String, String> entry : properties.entrySet()) {
303             entry.setValue(entry.getValue() + "x");
304         }
305         Object o21 = properties.get(KEY1);
306         assertEquals(VALUE1 + "x", o21);
307 
308         baos = new ByteArrayOutputStream();
309         properties.save(baos);
310 
311         properties = new MavenProperties();
312         properties.load(new ByteArrayInputStream(baos.toByteArray()));
313         Object o2 = properties.get(KEY1);
314         assertEquals(VALUE1 + "x", o2);
315     }
316 
317     @Test
318     public void testMultiValueEscaping() throws IOException {
319         StringWriter sw = new StringWriter();
320         PrintWriter pw = new PrintWriter(sw);
321         pw.println("fruits                           apple, banana, pear, \\");
322         pw.println("                                 cantaloupe, watermelon, \\");
323         pw.println("                                 kiwi, mango");
324 
325         java.util.Properties p = new java.util.Properties();
326         p.load(new StringReader(sw.toString()));
327         Object o24 = p.getProperty("fruits");
328         assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o24);
329 
330         MavenProperties props = new MavenProperties();
331         props.load(new StringReader(sw.toString()));
332         Object o23 = props.getProperty("fruits");
333         assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o23);
334         List<String> raw = props.getRaw("fruits");
335         assertNotNull(raw);
336         assertEquals(3, (Object) raw.size());
337         assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
338 
339         props = new MavenProperties();
340         props.put(
341                 "fruits",
342                 props.getComments("fruits"),
343                 List.of(
344                         "fruits                           apple, banana, pear, ",
345                         "                                 cantaloupe, watermelon, ",
346                         "                                 kiwi, mango"));
347         Object o22 = props.getProperty("fruits");
348         assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o22);
349         raw = props.getRaw("fruits");
350         assertNotNull(raw);
351         assertEquals(3, (Object) raw.size());
352         assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
353 
354         sw = new StringWriter();
355         props.save(sw);
356         props = new MavenProperties();
357         props.load(new StringReader(sw.toString()));
358         Object o21 = props.getProperty("fruits");
359         assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o21);
360         raw = props.getRaw("fruits");
361         assertNotNull(raw);
362         assertEquals(3, (Object) raw.size());
363         assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
364 
365         props = new MavenProperties();
366         props.put(
367                 "fruits",
368                 props.getComments("fruits"),
369                 List.of(
370                         "                           apple, banana, pear, ",
371                         "                                 cantaloupe, watermelon, ",
372                         "                                 kiwi, mango"));
373         Object o2 = props.getProperty("fruits");
374         assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", o2);
375         raw = props.getRaw("fruits");
376         assertNotNull(raw);
377         assertEquals(3, (Object) raw.size());
378         assertEquals("fruits =                            apple, banana, pear, ", raw.get(0));
379     }
380 
381     @Test
382     public void testUpdate() throws Exception {
383         MavenProperties p1 = new MavenProperties();
384         p1.put(
385                 "fruits",
386                 List.of("#", "# List of fruits", "#"),
387                 List.of(
388                         "                           apple, banana, pear, ",
389                         "                                 cantaloupe, watermelon, ",
390                         "                                 kiwi, mango"));
391         p1.put("trees", List.of("#", "# List of trees", "#"), List.of("                           fir, oak, maple"));
392         p1.put("vegetables", List.of("#", "# List of vegetables", "#"), List.of("                           potatoes"));
393 
394         MavenProperties p2 = new MavenProperties();
395         p2.put(
396                 "fruits",
397                 List.of("#", "# List of good fruits", "#"),
398                 List.of("                           apple, banana, pear"));
399         p2.put("trees", "fir, oak, maple");
400         p1.update(p2);
401 
402         assertEquals(2, (Object) p1.size());
403         Object o23 = p1.getComments("trees");
404         assertEquals(List.of("#", "# List of trees", "#"), o23);
405         Object o22 = p1.getProperty("trees");
406         assertEquals("fir, oak, maple", o22);
407         Object o21 = p1.getComments("fruits");
408         assertEquals(List.of("#", "# List of good fruits", "#"), o21);
409         Object o2 = p1.getProperty("fruits");
410         assertEquals("apple, banana, pear", o2);
411     }
412 
413     @Test
414     public void testSubstitution() throws IOException {
415         String str = "port = 4141" + LINE_SEPARATOR + "host = localhost"
416                 + LINE_SEPARATOR + "url = https://${host}:${port}/service"
417                 + LINE_SEPARATOR;
418         MavenProperties properties = new MavenProperties();
419         properties.load(new StringReader(str));
420         properties.put("url", "https://localhost:4141/service");
421         StringWriter sw = new StringWriter();
422         properties.save(sw);
423         Object o2 = sw.toString();
424         assertEquals(str, o2);
425     }
426 }