KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > EditablePropertiesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.support.ant;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39 import org.netbeans.junit.NbTestCase;
40
41 public class EditablePropertiesTest extends NbTestCase {
42
43     public EditablePropertiesTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     public void testLoad() throws Exception JavaDoc {
48         Map JavaDoc<String JavaDoc,String JavaDoc> content = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
49         for (int i=1; i<=26; i++) {
50             content.put("key"+i, "value"+i);
51         }
52         content.put("@!#$%^keyA", "valueA!@#$%^&*(){}");
53         content.put(" =:keyB", "valueB =:");
54         content.put(""+(char)0x1234+"keyC", "valueC"+(char)0x9876);
55         content.put("keyD", "");
56         content.put("keyE", "");
57         content.put("keyF", "");
58         content.put("keyG", "");
59         content.put("keyH", "value#this is not comment");
60         content.put("keyI", "incorrect end: \\u123");
61         // #46234: does not handle bad Unicode escapes well
62
content.put("keyJ", "malformed Unicode escape: \\uabyz");
63         
64         EditableProperties ep = loadTestProperties();
65         
66         for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry : content.entrySet()) {
67             String JavaDoc key = entry.getKey();
68             String JavaDoc value = entry.getValue();
69             String JavaDoc epValue = ep.getProperty(key);
70             assertEquals("Expected value for key "+key+" is different", value, epValue);
71         }
72         int count = 0;
73         for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry : ep.entrySet()) {
74             if (entry.getKey() != null) {
75                 count++;
76             }
77         }
78         assertEquals("Number of items in property file", content.keySet().size(), count);
79     }
80     
81     /* Doesn't work; java.util.Properties throws IAE for malformed Unicode escapes:
82     public void testJavaUtilPropertiesEquivalence() throws Exception {
83         Properties p = loadTestJavaUtilProperties();
84         EditableProperties ep = loadTestProperties();
85         Iterator it = p.entrySet().iterator();
86         while (it.hasNext()) {
87             Map.Entry entry = (Map.Entry) it.next();
88             String key = (String) entry.getKey();
89             String val = (String) entry.getValue();
90             assertEquals("right value for " + key, val, ep.getProperty(key));
91         }
92         assertEquals("right number of items", p.size(), ep.size());
93     }
94      */

95
96     public void testSave() throws Exception JavaDoc {
97         clearWorkDir();
98         EditableProperties ep = loadTestProperties();
99         String JavaDoc dest = getWorkDirPath()+File.separatorChar+"new.properties";
100         saveProperties(ep, dest);
101         assertFile("Saved properties must be the same as original one", filenameOfTestProperties(), dest, (String JavaDoc)null);
102     }
103     
104     public void testClonability() throws Exception JavaDoc {
105         clearWorkDir();
106         EditableProperties ep = loadTestProperties();
107         
108         EditableProperties ep2 = ep.cloneProperties();
109         String JavaDoc dest = getWorkDirPath()+File.separatorChar+"new2.properties";
110         saveProperties(ep2, dest);
111         assertFile("Saved cloned properties must be the same as original one", filenameOfTestProperties(), dest, (String JavaDoc)null);
112         
113         EditableProperties ep3 = (EditableProperties)ep.clone();
114         dest = getWorkDirPath()+File.separatorChar+"new3.properties";
115         saveProperties(ep3, dest);
116         assertFile("Saved cloned properties must be the same as original one", filenameOfTestProperties(), dest, (String JavaDoc)null);
117     }
118
119     // test that modifications changes only necessary parts
120
public void testVersionability() throws Exception JavaDoc {
121         clearWorkDir();
122         
123         EditableProperties ep = loadTestProperties();
124         
125         EditableProperties ep2 = ep.cloneProperties();
126         ep2.setProperty("key24", "new value of key 24");
127         String JavaDoc dest = getWorkDirPath()+File.separatorChar+"mod1.properties";
128         saveProperties(ep2, dest);
129         int res[] = compare(filenameOfTestProperties(), dest);
130         assertEquals("One line modified", 1, res[0]);
131         assertEquals("No lines added", 0, res[1]);
132         assertEquals("No lines removed", 0, res[2]);
133         
134         ep2 = ep.cloneProperties();
135         ep2.setProperty("key23", "new value of key23");
136         dest = getWorkDirPath()+File.separatorChar+"mod2.properties";
137         saveProperties(ep2, dest);
138         res = compare(filenameOfTestProperties(), dest);
139         assertEquals("Four lines modified", 4, res[0]);
140         assertEquals("No lines added", 0, res[1]);
141         assertEquals("No lines removed", 0, res[2]);
142         
143         ep2 = ep.cloneProperties();
144         ep2.put("newkey", "new value");
145         dest = getWorkDirPath()+File.separatorChar+"mod3.properties";
146         saveProperties(ep2, dest);
147         res = compare(filenameOfTestProperties(), dest);
148         assertEquals("No lines modified", 0, res[0]);
149         assertEquals("One line added", 1, res[1]);
150         assertEquals("No lines removed", 0, res[2]);
151         
152         ep2 = ep.cloneProperties();
153         assertNotNull(ep2.get("key14"));
154         ep2.remove("key14");
155         assertNull(ep2.get("key14"));
156         dest = getWorkDirPath()+File.separatorChar+"mod4.properties";
157         saveProperties(ep2, dest);
158         res = compare(filenameOfTestProperties(), dest);
159         assertEquals("No lines modified", 0, res[0]);
160         assertEquals("No lines added", 0, res[1]);
161         assertEquals("Two lines removed", 2, res[2]);
162         
163         ep2 = ep.cloneProperties();
164         ep2.setProperty("key21", new String JavaDoc[]{"first line;", "second line;", "third line"});
165         dest = getWorkDirPath()+File.separatorChar+"mod5.properties";
166         saveProperties(ep2, dest);
167         res = compare(filenameOfTestProperties(), dest);
168         assertEquals("Four lines modified", 4, res[0]);
169         assertEquals("No lines added", 0, res[1]);
170         assertEquals("No lines removed", 0, res[2]);
171         ep2.setProperty("key21", "first line;second line;third line");
172         String JavaDoc dest2 = getWorkDirPath()+File.separatorChar+"mod6.properties";
173         saveProperties(ep2, dest2);
174         res = compare(dest, dest2);
175         assertEquals("Four lines modified", 4, res[0]);
176         assertEquals("No lines added", 0, res[1]);
177         assertEquals("No lines removed", 0, res[2]);
178     }
179
180     // test that array values are stored correctly
181
public void testArrayValues() throws Exception JavaDoc {
182         EditableProperties ep = new EditableProperties(false);
183         ep.setProperty("key1", new String JavaDoc[]{"1. line;", "2. line;", "3. line"});
184         ep.setProperty("key2", "1. line;2. line;3. line");
185         String JavaDoc output = getAsString(ep);
186         String JavaDoc expected =
187             "key1=\\"+System.getProperty("line.separator")+
188             " 1. line;\\"+System.getProperty("line.separator")+
189             " 2. line;\\"+System.getProperty("line.separator")+
190             " 3. line"+System.getProperty("line.separator")+
191             "key2=1. line;2. line;3. line"+System.getProperty("line.separator");
192         assertEquals(expected, output);
193         assertEquals(ep.getProperty("key1"), "1. line;2. line;3. line");
194         assertEquals(ep.getProperty("key2"), "1. line;2. line;3. line");
195         ep.setProperty("key1", "one; two; three");
196         output = getAsString(ep);
197         expected =
198             "key1=one; two; three"+System.getProperty("line.separator")+
199             "key2=1. line;2. line;3. line"+System.getProperty("line.separator");
200         assertEquals(expected, output);
201         assertEquals(ep.getProperty("key1"), "one; two; three");
202         assertEquals(ep.getProperty("key2"), "1. line;2. line;3. line");
203         ep.setProperty("key2", new String JavaDoc[]{"1. line;", "2. line;", "3. line", "one;", "more;", "line;"});
204         ep.setProperty("key", new String JavaDoc[0]);
205         output = getAsString(ep);
206         expected =
207             "key1=one; two; three"+System.getProperty("line.separator")+
208             "key2=\\"+System.getProperty("line.separator")+
209             " 1. line;\\"+System.getProperty("line.separator")+
210             " 2. line;\\"+System.getProperty("line.separator")+
211             " 3. line\\"+System.getProperty("line.separator")+
212             " one;\\"+System.getProperty("line.separator")+
213             " more;\\"+System.getProperty("line.separator")+
214             " line;"+System.getProperty("line.separator")+
215             "key="+System.getProperty("line.separator"); // #45061
216
assertEquals(expected, output);
217         assertEquals(ep.getProperty("key1"), "one; two; three");
218         assertEquals(ep.getProperty("key2"), "1. line;2. line;3. lineone;more;line;");
219         assertEquals(ep.getProperty("key"), "");
220     }
221         
222     public void testSorting() throws Exception JavaDoc {
223         EditableProperties ep = new EditableProperties(false);
224         ep.setProperty("a", "val-a");
225         ep.setProperty("c", "val-c");
226         ep.put("b", "val-b");
227         String JavaDoc output = getAsString(ep);
228         String JavaDoc expected = "a=val-a"+System.getProperty("line.separator")+"c=val-c"+
229                 System.getProperty("line.separator")+"b=val-b"+
230                 System.getProperty("line.separator");
231         assertEquals(expected, output);
232         
233         ep = new EditableProperties();
234         ep.setProperty("a", "val-a");
235         ep.setProperty("c", "val-c");
236         ep.put("b", "val-b");
237         output = getAsString(ep);
238         expected = "a=val-a"+System.getProperty("line.separator")+"c=val-c"+
239                 System.getProperty("line.separator")+"b=val-b"+
240                 System.getProperty("line.separator");
241         assertEquals(expected, output);
242         
243         ep = new EditableProperties(true);
244         ep.setProperty("a", "val-a");
245         ep.setProperty("c", "val-c");
246         ep.put("b", "val-b");
247         output = getAsString(ep);
248         expected = "a=val-a"+System.getProperty("line.separator")+"b=val-b"+
249                 System.getProperty("line.separator")+"c=val-c"+
250                 System.getProperty("line.separator");
251         assertEquals(expected, output);
252     }
253
254     // test that changing comments work and modify only comments
255
public void testComment() throws Exception JavaDoc {
256         clearWorkDir();
257         
258         EditableProperties ep = loadTestProperties();
259         
260         EditableProperties ep2 = ep.cloneProperties();
261         ep2.setComment("key10", new String JavaDoc[]{"# this is new comment for property key 10"}, false);
262         String JavaDoc dest = getWorkDirPath()+File.separatorChar+"comment1.properties";
263         saveProperties(ep2, dest);
264         int res[] = compare(filenameOfTestProperties(), dest);
265         assertEquals("No lines modified", 0, res[0]);
266         assertEquals("One line added", 1, res[1]);
267         assertEquals("No lines removed", 0, res[2]);
268         
269         ep2 = ep.cloneProperties();
270         ep2.setComment("key1", new String JavaDoc[]{"# new comment", "# new comment second line"}, true);
271         dest = getWorkDirPath()+File.separatorChar+"comment2.properties";
272         saveProperties(ep2, dest);
273         res = compare(filenameOfTestProperties(), dest);
274         assertEquals("No lines modified", 0, res[0]);
275         assertEquals("Two lines added", 2, res[1]);
276         assertEquals("No lines removed", 0, res[2]);
277         
278         ep2 = ep.cloneProperties();
279         ep2.setComment("key26", new String JavaDoc[]{"# changed comment"}, false);
280         dest = getWorkDirPath()+File.separatorChar+"comment3.properties";
281         saveProperties(ep2, dest);
282         res = compare(filenameOfTestProperties(), dest);
283         assertEquals("One line modified", 1, res[0]);
284         assertEquals("No lines added", 0, res[1]);
285         assertEquals("No lines removed", 0, res[2]);
286         
287         ep2 = ep.cloneProperties();
288         ep2.setComment("key25", new String JavaDoc[]{"# one line comment"}, false);
289         dest = getWorkDirPath()+File.separatorChar+"comment4.properties";
290         saveProperties(ep2, dest);
291         res = compare(filenameOfTestProperties(), dest);
292         assertEquals("Two lines modified", 2, res[0]);
293         assertEquals("No lines added", 0, res[1]);
294         assertEquals("No lines removed", 0, res[2]);
295         
296         ep2 = ep.cloneProperties();
297         ep2.setComment("key26", ep2.getComment("key26"), true);
298         dest = getWorkDirPath()+File.separatorChar+"comment5.properties";
299         saveProperties(ep2, dest);
300         res = compare(filenameOfTestProperties(), dest);
301         assertEquals("No line modified", 0, res[0]);
302         assertEquals("One line added", 1, res[1]);
303         assertEquals("No lines removed", 0, res[2]);
304         
305     }
306
307     // test that misc chars are correctly escaped, unicode encoded, etc.
308
public void testEscaping() throws Exception JavaDoc {
309         String JavaDoc umlaut = "" + (char)252;
310         EditableProperties ep = new EditableProperties(false);
311         ep.setProperty("a a", "a space a");
312         ep.setProperty("b"+(char)0x4567, "val"+(char)0x1234);
313         ep.setProperty("@!#$%^\\", "!@#$%^&*(){}\\");
314         ep.setProperty("d\nd", "d\nnewline\nd");
315         ep.setProperty("umlaut", umlaut);
316         ep.setProperty("_a a", new String JavaDoc[]{"a space a"});
317         ep.setProperty("_b"+(char)0x4567, new String JavaDoc[]{"val"+(char)0x1234});
318         ep.setProperty("_@!#$%^\\", new String JavaDoc[]{"!@#$%^&*\\", "(){}\\"});
319         ep.setProperty("_d\nd", new String JavaDoc[]{"d\nnew","line\nd", "\n", "end"});
320         ep.setProperty("_umlaut", new String JavaDoc[]{umlaut, umlaut});
321         String JavaDoc output = getAsString(ep);
322         String JavaDoc expected = "a\\ a=a space a"+System.getProperty("line.separator")+
323                 "b\\u4567=val\\u1234"+System.getProperty("line.separator")+
324                 "@!#$%^\\\\=!@#$%^&*(){}\\\\"+System.getProperty("line.separator")+
325                 "d\\nd=d\\nnewline\\nd"+System.getProperty("line.separator")+
326                 "umlaut=\\u00fc"+System.getProperty("line.separator")+
327                 "_a\\ a=\\"+System.getProperty("line.separator")+" a space a"+System.getProperty("line.separator")+
328                 "_b\\u4567=\\"+System.getProperty("line.separator")+" val\\u1234"+System.getProperty("line.separator")+
329                 "_@!#$%^\\\\=\\"+System.getProperty("line.separator")+" !@#$%^&*\\\\\\"+System.getProperty("line.separator")+
330                     " (){}\\\\"+System.getProperty("line.separator")+
331                 "_d\\nd=\\"+System.getProperty("line.separator")+" d\\nnew\\"+System.getProperty("line.separator")+
332                     " line\\nd\\"+System.getProperty("line.separator")+
333                     " \\n\\"+System.getProperty("line.separator")+
334                     " end"+System.getProperty("line.separator")+
335                 "_umlaut=\\" +System.getProperty("line.separator")+" \\u00fc\\"+System.getProperty("line.separator")+
336                     " \\u00fc"+System.getProperty("line.separator");
337         assertEquals(expected, output);
338         assertEquals("a space a", ep.getProperty("a a"));
339         assertEquals("val"+(char)0x1234, ep.getProperty("b"+(char)0x4567));
340         assertEquals("!@#$%^&*(){}\\", ep.getProperty("@!#$%^\\"));
341         assertEquals("d\nnewline\nd", ep.getProperty("d\nd"));
342         assertEquals(umlaut, ep.getProperty("umlaut"));
343         assertEquals("a space a", ep.getProperty("_a a"));
344         assertEquals("val"+(char)0x1234, ep.getProperty("_b"+(char)0x4567));
345         assertEquals("!@#$%^&*\\(){}\\", ep.getProperty("_@!#$%^\\"));
346         assertEquals("d\nnewline\nd\nend", ep.getProperty("_d\nd"));
347         assertEquals(umlaut+umlaut, ep.getProperty("_umlaut"));
348     }
349     
350     // test that iterator implementation is OK
351
public void testIterator() throws Exception JavaDoc {
352         EditableProperties ep = loadTestProperties();
353         Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc,String JavaDoc>> it1 = ep.entrySet().iterator();
354         while (it1.hasNext()) {
355             it1.next();
356         }
357         Iterator JavaDoc<String JavaDoc> it2 = ep.keySet().iterator();
358         while (it2.hasNext()) {
359             it2.next();
360         }
361         it2 = ep.keySet().iterator();
362         while (it2.hasNext()) {
363             it2.next();
364             it2.remove();
365         }
366         ep.put("a", "aval");
367         ep.remove("a");
368         ep = loadTestProperties();
369         it1 = ep.entrySet().iterator();
370         while (it1.hasNext()) {
371             Map.Entry JavaDoc<String JavaDoc,String JavaDoc> entry = it1.next();
372             assertNotNull("Property key cannot be null", entry.getKey());
373             assertNotNull("Property value cannot be null", entry.getValue());
374             entry.setValue(entry.getValue()+"-something-new");
375         }
376         it1 = ep.entrySet().iterator();
377         while (it1.hasNext()) {
378             it1.next();
379             it1.remove();
380         }
381     }
382     
383     // test that syntax errors are survived
384
public void testInvalidPropertiesFile() throws Exception JavaDoc {
385         String JavaDoc invalidProperty = "key=value without correct end\\";
386         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(invalidProperty.getBytes());
387         EditableProperties ep = new EditableProperties();
388         ep.load(is);
389         assertEquals("Syntax error should be resolved", 1, ep.keySet().size());
390         assertEquals("value without correct end", ep.getProperty("key"));
391     }
392     
393     public void testNonLatinComments() throws Exception JavaDoc {
394         // #60249.
395
String JavaDoc lsep = System.getProperty("line.separator");
396         EditableProperties p = new EditableProperties();
397         p.setProperty("k", "v");
398         p.setComment("k", new String JavaDoc[] {"# \u0158ekni koment teda!"}, false);
399         String JavaDoc expected = "# \\u0158ekni koment teda!" + lsep + "k=v" + lsep;
400         assertEquals("Storing non-Latin chars in comments works", expected, getAsString(p));
401         p = new EditableProperties();
402         p.load(new ByteArrayInputStream JavaDoc(expected.getBytes("ISO-8859-1")));
403         assertEquals("Reading non-Latin chars in comments works", Collections.singletonList("# \u0158ekni koment teda!"), Arrays.asList(p.getComment("k")));
404         p.setProperty("k", "v2");
405         expected = "# \\u0158ekni koment teda!" + lsep + "k=v2" + lsep;
406         assertEquals("Reading and re-writing non-Latin chars in comments works", expected, getAsString(p));
407     }
408
409     
410     // helper methods:
411

412     
413     private String JavaDoc filenameOfTestProperties() {
414         // #50987: never use URL.path for this purpose...
415
return new File JavaDoc(URI.create(EditablePropertiesTest.class.getResource("data/test.properties").toExternalForm())).getAbsolutePath();
416     }
417     
418     private EditableProperties loadTestProperties() throws IOException JavaDoc {
419         URL JavaDoc u = EditablePropertiesTest.class.getResource("data/test.properties");
420         EditableProperties ep = new EditableProperties();
421         InputStream JavaDoc is = u.openStream();
422         try {
423             ep.load(is);
424         } finally {
425             is.close();
426         }
427         return ep;
428     }
429     
430     /*
431     private Properties loadTestJavaUtilProperties() throws IOException {
432         URL u = EditablePropertiesTest.class.getResource("data/test.properties");
433         Properties p = new Properties();
434         InputStream is = u.openStream();
435         try {
436             p.load(is);
437         } finally {
438             is.close();
439         }
440         return p;
441     }
442      */

443     
444     private void saveProperties(EditableProperties ep, String JavaDoc path) throws Exception JavaDoc {
445         OutputStream JavaDoc os = new FileOutputStream JavaDoc(path);
446         try {
447             ep.store(os);
448         } finally {
449             os.close();
450         }
451     }
452
453     private int[] compare(String JavaDoc f1, String JavaDoc f2) throws Exception JavaDoc {
454         Reader JavaDoc r1 = null;
455         Reader JavaDoc r2 = null;
456         try {
457             r1 = new InputStreamReader JavaDoc(new FileInputStream JavaDoc(f1), "ISO-8859-1");
458             r2 = new InputStreamReader JavaDoc(new FileInputStream JavaDoc(f2), "ISO-8859-1");
459             return AntBasedTestUtil.countTextDiffs(r1, r2);
460         } finally {
461             if (r1 != null) {
462                 r1.close();
463             }
464             if (r2 != null) {
465                 r2.close();
466             }
467         }
468     }
469     
470     private String JavaDoc getAsString(EditableProperties ep) throws Exception JavaDoc {
471         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
472         ep.store(os);
473         os.close();
474         return os.toString("ISO-8859-1");
475     }
476     
477 }
478
Popular Tags