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