KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > 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.modules.ruby.spi.project.support.rake;
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         Iterator JavaDoc it = content.keySet().iterator();
67         while (it.hasNext()) {
68             String JavaDoc key = (String JavaDoc)it.next();
69             String JavaDoc value = (String JavaDoc)content.get(key);
70             String JavaDoc epValue = ep.getProperty(key);
71             assertEquals("Expected value for key "+key+" is different", value, epValue);
72         }
73         int count = 0;
74         it = ep.entrySet().iterator();
75         while (it.hasNext()) {
76             if (((Map.Entry JavaDoc)it.next()).getKey() != null) {
77                 count++;
78             }
79         }
80         assertEquals("Number of items in property file", content.keySet().size(), count);
81     }
82     
83     /* Doesn't work; java.util.Properties throws IAE for malformed Unicode escapes:
84     public void testJavaUtilPropertiesEquivalence() throws Exception {
85         Properties p = loadTestJavaUtilProperties();
86         EditableProperties ep = loadTestProperties();
87         Iterator it = p.entrySet().iterator();
88         while (it.hasNext()) {
89             Map.Entry entry = (Map.Entry) it.next();
90             String key = (String) entry.getKey();
91             String val = (String) entry.getValue();
92             assertEquals("right value for " + key, val, ep.getProperty(key));
93         }
94         assertEquals("right number of items", p.size(), ep.size());
95     }
96      */

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

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

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