KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > PropertyUtilsTest


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.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36 import junit.framework.TestResult;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.junit.NbTestCase;
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileSystem;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.Mutex;
44 import org.openide.util.NbCollections;
45 import org.openide.util.Utilities;
46
47 /**
48  * Test functionality of PropertyUtils.
49  * @author Jesse Glick
50  */

51 public class PropertyUtilsTest extends NbTestCase {
52     
53     public PropertyUtilsTest(String JavaDoc name) {
54         super(name);
55     }
56     
57     public void run(final TestResult result) {
58         ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
59             public Void JavaDoc run() {
60                 PropertyUtilsTest.super.run(result);
61                 return null;
62             }
63         });
64     }
65     
66     private static PropertyEvaluator evaluator(Map JavaDoc<String JavaDoc,String JavaDoc> predefs, List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> defs) {
67         PropertyProvider[] mainProviders = new PropertyProvider[defs.size()];
68         int i = 0;
69         for (Map JavaDoc<String JavaDoc,String JavaDoc> def : defs) {
70             mainProviders[i++] = PropertyUtils.fixedPropertyProvider(def);
71         }
72         return PropertyUtils.sequentialPropertyEvaluator(PropertyUtils.fixedPropertyProvider(predefs), mainProviders);
73     }
74     
75     private static String JavaDoc evaluate(String JavaDoc prop, Map JavaDoc<String JavaDoc,String JavaDoc> predefs, List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> defs) {
76         return evaluator(predefs, defs).getProperty(prop);
77     }
78     
79     private static Map JavaDoc<String JavaDoc,String JavaDoc> evaluateAll(Map JavaDoc<String JavaDoc,String JavaDoc> predefs, List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> defs) {
80         return evaluator(predefs, defs).getProperties();
81     }
82     
83     private static String JavaDoc evaluateString(String JavaDoc text, Map JavaDoc<String JavaDoc,String JavaDoc> predefs, List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> defs) {
84         return evaluator(predefs, defs).evaluate(text);
85     }
86     
87     public void testEvaluate() throws Exception JavaDoc {
88         // XXX check override order, property name evaluation, $$ escaping, bare or final $,
89
// cyclic errors, undef'd property substitution, no substs in predefs, etc.
90
Map JavaDoc<String JavaDoc,String JavaDoc> m1 = Collections.singletonMap("y", "val");
91         Map JavaDoc<String JavaDoc,String JavaDoc> m2 = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
92         m2.put("x", "${y}");
93         m2.put("y", "y-${x}");
94         List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> m1m2 = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>>();
95         m1m2.add(m1);
96         m1m2.add(m2);
97         assertEquals("x evaluates to former y", "val", evaluate("x", Collections.<String JavaDoc,String JavaDoc>emptyMap(), m1m2));
98         assertEquals("first y defines it", "val", evaluate("y", Collections.<String JavaDoc,String JavaDoc>emptyMap(), m1m2));
99         assertEquals("circularity error", null, evaluate("x", Collections.<String JavaDoc,String JavaDoc>emptyMap(), Collections.singletonList(m2)));
100         assertEquals("circularity error", null, evaluate("y", Collections.<String JavaDoc,String JavaDoc>emptyMap(), Collections.singletonList(m2)));
101         m2.clear();
102         m2.put("y", "yval_${z}");
103         m2.put("x", "xval_${y}");
104         m2.put("z", "zval");
105         Map JavaDoc<String JavaDoc,String JavaDoc> all = evaluateAll(Collections.<String JavaDoc,String JavaDoc>emptyMap(), Collections.singletonList(m2));
106         assertNotNull("no circularity error", all);
107         assertEquals("have three properties", 3, all.size());
108         assertEquals("double substitution", "xval_yval_zval", all.get("x"));
109         assertEquals("single substitution", "yval_zval", all.get("y"));
110         assertEquals("no substitution", "zval", all.get("z"));
111         // Yuck. But it failed once, so check it now.
112
Properties JavaDoc p = new Properties JavaDoc();
113         p.load(new ByteArrayInputStream JavaDoc("project.mylib=../mylib\njavac.classpath=${project.mylib}/build/mylib.jar\nrun.classpath=${javac.classpath}:build/classes".getBytes("US-ASCII")));
114         all = evaluateAll(Collections.<String JavaDoc,String JavaDoc>emptyMap(), Collections.singletonList(NbCollections.checkedMapByFilter(p, String JavaDoc.class, String JavaDoc.class, true)));
115         assertNotNull("no circularity error", all);
116         assertEquals("javac.classpath correctly substituted", "../mylib/build/mylib.jar", all.get("javac.classpath"));
117         assertEquals("run.classpath correctly substituted", "../mylib/build/mylib.jar:build/classes", all.get("run.classpath"));
118     }
119     
120     public void testTokenizePath() throws Exception JavaDoc {
121         assertEquals("basic tokenization works on ':'",
122                 Arrays.asList(new String JavaDoc[] {"foo", "bar"}),
123                 Arrays.asList(PropertyUtils.tokenizePath("foo:bar")));
124             assertEquals("basic tokenization works on ';'",
125                 Arrays.asList(new String JavaDoc[] {"foo", "bar"}),
126                 Arrays.asList(PropertyUtils.tokenizePath("foo;bar")));
127             assertEquals("Unix paths work",
128                 Arrays.asList(new String JavaDoc[] {"/foo/bar", "baz/quux"}),
129                 Arrays.asList(PropertyUtils.tokenizePath("/foo/bar:baz/quux")));
130             assertEquals("empty components are stripped with ':'",
131                 Arrays.asList(new String JavaDoc[] {"foo", "bar"}),
132                 Arrays.asList(PropertyUtils.tokenizePath(":foo::bar:")));
133             assertEquals("empty components are stripped with ';'",
134                 Arrays.asList(new String JavaDoc[] {"foo", "bar"}),
135                 Arrays.asList(PropertyUtils.tokenizePath(";foo;;bar;")));
136             assertEquals("DOS paths are recognized with ';'",
137                 Arrays.asList(new String JavaDoc[] {"c:\\foo", "D:\\\\bar"}),
138                 Arrays.asList(PropertyUtils.tokenizePath("c:\\foo;D:\\\\bar")));
139             assertEquals("DOS paths are recognized with ':'",
140                 Arrays.asList(new String JavaDoc[] {"c:\\foo", "D:\\\\bar"}),
141                 Arrays.asList(PropertyUtils.tokenizePath("c:\\foo:D:\\\\bar")));
142             assertEquals("a..z can be drive letters",
143                 Arrays.asList(new String JavaDoc[] {"a:\\foo", "z:\\\\bar"}),
144                 Arrays.asList(PropertyUtils.tokenizePath("a:\\foo:z:\\\\bar")));
145             assertEquals("A..Z can be drive letters",
146                 Arrays.asList(new String JavaDoc[] {"A:\\foo", "Z:\\\\bar"}),
147                 Arrays.asList(PropertyUtils.tokenizePath("A:\\foo:Z:\\\\bar")));
148             assertEquals("non-letters are not drives with ';'",
149                 Arrays.asList(new String JavaDoc[] {"1", "\\foo", "D:\\\\bar"}),
150                 Arrays.asList(PropertyUtils.tokenizePath("1;\\foo;D:\\\\bar")));
151             assertEquals("non-letters are not drives with ':'",
152                 Arrays.asList(new String JavaDoc[] {"1", "\\foo", "D:\\\\bar"}),
153                 Arrays.asList(PropertyUtils.tokenizePath("1:\\foo:D:\\\\bar")));
154             assertEquals(">1 letters are not drives with ';'",
155                 Arrays.asList(new String JavaDoc[] {"ab", "\\foo", "D:\\\\bar"}),
156                 Arrays.asList(PropertyUtils.tokenizePath("ab;\\foo;D:\\\\bar")));
157             assertEquals(">1 letters are not drives with ':'",
158                 Arrays.asList(new String JavaDoc[] {"ab", "\\foo", "D:\\\\bar"}),
159                 Arrays.asList(PropertyUtils.tokenizePath("ab:\\foo:D:\\\\bar")));
160             assertEquals("drives use ':'",
161                 Arrays.asList(new String JavaDoc[] {"c", "\\foo", "D:\\\\bar"}),
162                 Arrays.asList(PropertyUtils.tokenizePath("c;\\foo;D:\\\\bar")));
163             assertEquals("drives use only one ':'",
164                 Arrays.asList(new String JavaDoc[] {"c", "\\foo", "D:\\\\bar"}),
165                 Arrays.asList(PropertyUtils.tokenizePath("c::\\foo;D:\\\\bar")));
166             assertEquals("drives use only one drive letter",
167                 Arrays.asList(new String JavaDoc[] {"c", "c:\\foo", "D:\\\\bar"}),
168                 Arrays.asList(PropertyUtils.tokenizePath("c:c:\\foo;D:\\\\bar")));
169             assertEquals("DOS paths start with '\\'",
170                 Arrays.asList(new String JavaDoc[] {"c", "foo", "D:\\\\bar"}),
171                 Arrays.asList(PropertyUtils.tokenizePath("c:foo;D:\\\\bar")));
172             assertEquals("DOS paths start with '/'",
173                 Arrays.asList(new String JavaDoc[] {"c", "/foo", "D:/bar", "/path"}),
174                 Arrays.asList(PropertyUtils.tokenizePath("c;/foo;D:/bar:/path")));
175             assertEquals("empty path handled",
176                 Collections.EMPTY_LIST,
177                 Arrays.asList(PropertyUtils.tokenizePath("")));
178             assertEquals("effectively empty path handled",
179                 Collections.EMPTY_LIST,
180                 Arrays.asList(PropertyUtils.tokenizePath(":;:;")));
181             assertEquals("one letter directories handled",
182                 Arrays.asList(new String JavaDoc[] {"c:/foo/c", "/foo/c/bar", "c", "/foo/c", "/bar"}),
183                 Arrays.asList(PropertyUtils.tokenizePath("c:/foo/c;/foo/c/bar;c;/foo/c:/bar")));
184             assertEquals("one letter directories handled2",
185                 Arrays.asList(new String JavaDoc[] {"c"}),
186                 Arrays.asList(PropertyUtils.tokenizePath("c")));
187     }
188     
189     public void testRelativizeFile() throws Exception JavaDoc {
190         clearWorkDir();
191         File JavaDoc tmp = getWorkDir();
192         File JavaDoc d1 = new File JavaDoc(tmp, "d1");
193         File JavaDoc d1f = new File JavaDoc(d1, "f");
194         File JavaDoc d1s = new File JavaDoc(d1, "s p a c e");
195         File JavaDoc d1sf = new File JavaDoc(d1s, "f");
196         File JavaDoc d2 = new File JavaDoc(tmp, "d2");
197         File JavaDoc d2f = new File JavaDoc(d2, "f");
198         // Note that "/tmp/d11".startsWith("/tmp/d1"), hence this being interesting:
199
File JavaDoc d11 = new File JavaDoc(tmp, "d11");
200         // Note: none of these dirs/files exist yet.
201
assertEquals("d1f from d1", "f", PropertyUtils.relativizeFile(d1, d1f));
202         assertEquals("d1 from d1f", "..", PropertyUtils.relativizeFile(d1f, d1)); // #61687
203
assertEquals("d2f from d1", "../d2/f", PropertyUtils.relativizeFile(d1, d2f));
204         assertEquals("d1 from d1", ".", PropertyUtils.relativizeFile(d1, d1));
205         assertEquals("d2 from d1", "../d2", PropertyUtils.relativizeFile(d1, d2));
206         assertEquals("d1s from d1", "s p a c e", PropertyUtils.relativizeFile(d1, d1s));
207         assertEquals("d1sf from d1", "s p a c e/f", PropertyUtils.relativizeFile(d1, d1sf));
208         assertEquals("d11 from d1", "../d11", PropertyUtils.relativizeFile(d1, d11));
209         // Now make them and check that the results are the same.
210
assertTrue("made d1s", d1s.mkdirs());
211         assertTrue("made d1f", d1f.createNewFile());
212         assertTrue("made d1sf", d1sf.createNewFile());
213         assertTrue("made d2", d2.mkdirs());
214         assertTrue("made d2f", d2f.createNewFile());
215         assertEquals("existing d1f from d1", "f", PropertyUtils.relativizeFile(d1, d1f));
216         assertEquals("existing d2f from d1", "../d2/f", PropertyUtils.relativizeFile(d1, d2f));
217         assertEquals("existing d1 from d1", ".", PropertyUtils.relativizeFile(d1, d1));
218         assertEquals("existing d2 from d1", "../d2", PropertyUtils.relativizeFile(d1, d2));
219         assertEquals("existing d1s from d1", "s p a c e", PropertyUtils.relativizeFile(d1, d1s));
220         assertEquals("existing d1sf from d1", "s p a c e/f", PropertyUtils.relativizeFile(d1, d1sf));
221         assertEquals("existing d11 from d1", "../d11", PropertyUtils.relativizeFile(d1, d11));
222         // XXX: the below code should pass on Unix too I guess.
223
if (Utilities.isWindows()) {
224             // test Windows drives:
225
File JavaDoc f1 = new File JavaDoc("C:\\folder\\one");
226             File JavaDoc f2 = new File JavaDoc("D:\\t e m p\\two");
227             assertNull("different drives cannot be relative", PropertyUtils.relativizeFile(f1, f2));
228             f1 = new File JavaDoc("D:\\folder\\one");
229             f2 = new File JavaDoc("D:\\t e m p\\two");
230             assertEquals("relativization failed for Windows absolute paths", "../../t e m p/two", PropertyUtils.relativizeFile(f1, f2));
231         }
232     }
233     
234     public void testGlobalProperties() throws Exception JavaDoc {
235         clearWorkDir();
236         System.setProperty("netbeans.user", getWorkDir().getAbsolutePath());
237         File JavaDoc ubp = new File JavaDoc(getWorkDir(), "build.properties");
238         assertFalse("no build.properties yet", ubp.exists());
239         assertEquals("no properties to start", Collections.EMPTY_MAP, PropertyUtils.getGlobalProperties());
240         EditableProperties p = new EditableProperties();
241         p.setProperty("key1", "val1");
242         p.setProperty("key2", "val2");
243         PropertyUtils.putGlobalProperties(p);
244         assertTrue("now have build.properties", ubp.isFile());
245         p = PropertyUtils.getGlobalProperties();
246         assertEquals("two definitions now", 2, p.size());
247         assertEquals("key1 correct", "val1", p.getProperty("key1"));
248         assertEquals("key2 correct", "val2", p.getProperty("key2"));
249         Properties JavaDoc p2 = new Properties JavaDoc();
250         InputStream JavaDoc is = new FileInputStream JavaDoc(ubp);
251         try {
252             p2.load(is);
253         } finally {
254             is.close();
255         }
256         assertEquals("two definitions now from disk", 2, p2.size());
257         assertEquals("key1 correct from disk", "val1", p2.getProperty("key1"));
258         assertEquals("key2 correct from disk", "val2", p2.getProperty("key2"));
259         // Test the property provider too.
260
PropertyProvider gpp = PropertyUtils.globalPropertyProvider();
261         RakeBasedTestUtil.TestCL l = new RakeBasedTestUtil.TestCL();
262         gpp.addChangeListener(l);
263         p = PropertyUtils.getGlobalProperties();
264         assertEquals("correct initial definitions", p, gpp.getProperties());
265         p.setProperty("key3", "val3");
266         assertEquals("still have 2 defs", 2, gpp.getProperties().size());
267         assertFalse("no changes yet", l.expect());
268         PropertyUtils.putGlobalProperties(p);
269         assertTrue("got a change", l.expect());
270         assertEquals("now have 3 defs", 3, gpp.getProperties().size());
271         assertEquals("right val", "val3", gpp.getProperties().get("key3"));
272         assertFalse("no spurious changes", l.expect());
273         // Test changes made using Filesystems API.
274
p.setProperty("key1", "val1a");
275         FileObject fo = FileUtil.toFileObject(ubp);
276         assertNotNull("there is USER_BUILD_PROPERTIES on disk", fo);
277         FileLock lock = fo.lock();
278         OutputStream JavaDoc os = fo.getOutputStream(lock);
279         p.store(os);
280         os.close();
281         lock.releaseLock();
282         assertTrue("got a change from the Filesystems API", l.expect());
283         assertEquals("still have 3 defs", 3, gpp.getProperties().size());
284         assertEquals("right val for key1", "val1a", gpp.getProperties().get("key1"));
285         // XXX changes made on disk are not picked up... bad test, or something else?
286
/*
287         Thread.sleep(1000);
288         p.setProperty("key2", "val2a");
289         OutputStream os = new FileOutputStream(ubp);
290         p.store(os);
291         os.close();
292         FileUtil.toFileObject(ubp).getFileSystem().refresh(false);
293         Thread.sleep(1000);
294         assertTrue("got a change from disk", l.expect());
295         assertEquals("still have 3 defs", 3, gpp.getProperties().size());
296         assertEquals("right val for key2", "val2a", gpp.getProperties().get("key2"));
297          */

298     }
299     
300     public void testEvaluateString() throws Exception JavaDoc {
301         Map JavaDoc<String JavaDoc,String JavaDoc> predefs = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
302         predefs.put("homedir", "/home/me");
303         Map JavaDoc<String JavaDoc,String JavaDoc> defs1 = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
304         defs1.put("outdirname", "foo");
305         defs1.put("outdir", "${homedir}/${outdirname}");
306         Map JavaDoc<String JavaDoc,String JavaDoc> defs2 = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
307         defs2.put("outdir2", "${outdir}/subdir");
308         List JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>> defs12 = new ArrayList JavaDoc<Map JavaDoc<String JavaDoc,String JavaDoc>>();
309         defs12.add(defs1);
310         defs12.add(defs2);
311         assertEquals("correct evaluated string",
312             "/home/me/foo/subdir is in /home/me",
313             evaluateString("${outdir2} is in ${homedir}", predefs, defs12));
314     }
315     
316     public void testFixedPropertyProvider() throws Exception JavaDoc {
317         Map JavaDoc<String JavaDoc,String JavaDoc> defs = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
318         defs.put("key1", "val1");
319         defs.put("key2", "val2");
320         PropertyProvider pp = PropertyUtils.fixedPropertyProvider(defs);
321         assertEquals(defs, pp.getProperties());
322     }
323     
324     public void testPropertiesFilePropertyProvider() throws Exception JavaDoc {
325         clearWorkDir();
326         final FileObject scratch = FileUtil.toFileObject(getWorkDir());
327         PropertyProvider pp = PropertyUtils.propertiesFilePropertyProvider(new File JavaDoc(FileUtil.toFile(scratch), "test.properties"));
328         RakeBasedTestUtil.TestCL l = new RakeBasedTestUtil.TestCL();
329         pp.addChangeListener(l);
330         assertEquals("no defs yet (no file)", Collections.EMPTY_MAP, pp.getProperties());
331         assertFalse("no changes yet", l.expect());
332         final FileObject[] testProperties = new FileObject[1];
333         scratch.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
334             public void run() throws IOException JavaDoc {
335                 testProperties[0] = FileUtil.createData(scratch, "test.properties");
336                 FileLock lock = testProperties[0].lock();
337                 try {
338                     OutputStream JavaDoc os = testProperties[0].getOutputStream(lock);
339                     try {
340                         
341                         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
342                         pw.println("a=aval");
343                         pw.flush();
344                     } finally {
345                         os.close();
346                     }
347                 } finally {
348                     lock.releaseLock();
349                 }
350             }
351         });
352         assertTrue("got a change when file was created", l.expect());
353         assertEquals("one key", Collections.singletonMap("a", "aval"), pp.getProperties());
354         FileLock lock = testProperties[0].lock();
355         try {
356             OutputStream JavaDoc os = testProperties[0].getOutputStream(lock);
357             try {
358                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
359                 pw.println("a=aval");
360                 pw.println("b=bval");
361                 pw.flush();
362             } finally {
363                 os.close();
364             }
365         } finally {
366             lock.releaseLock();
367         }
368         Map JavaDoc<String JavaDoc,String JavaDoc> m = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
369         m.put("a", "aval");
370         m.put("b", "bval");
371         assertTrue("got a change when file was changed", l.expect());
372         assertEquals("right properties", m, pp.getProperties());
373         testProperties[0].delete();
374         assertTrue("got a change when file was deleted", l.expect());
375         assertEquals("no defs again (file deleted)", Collections.emptyMap(), pp.getProperties());
376     }
377     
378     public void testSequentialEvaluatorBasic() throws Exception JavaDoc {
379         Map JavaDoc<String JavaDoc,String JavaDoc> defs1 = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
380         defs1.put("key1", "val1");
381         defs1.put("key2", "val2");
382         defs1.put("key5", "5=${key1}");
383         defs1.put("key6", "6=${key3}");
384         Map JavaDoc<String JavaDoc,String JavaDoc> defs2 = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
385         defs2.put("key3", "val3");
386         defs2.put("key4", "4=${key1}:${key3}");
387         defs2.put("key7", "7=${undef}");
388         PropertyEvaluator eval = PropertyUtils.sequentialPropertyEvaluator(null,
389             PropertyUtils.fixedPropertyProvider(defs1),
390             PropertyUtils.fixedPropertyProvider(defs2));
391         String JavaDoc[] vals = {
392             "val1",
393             "val2",
394             "val3",
395             "4=val1:val3",
396             "5=val1",
397             "6=${key3}",
398             "7=${undef}",
399         };
400         Map JavaDoc<String JavaDoc,String JavaDoc> all = eval.getProperties();
401         assertEquals("right # of props", vals.length, all.size());
402         for (int i = 1; i <= vals.length; i++) {
403             assertEquals("key" + i + " is correct", vals[i - 1], eval.getProperty("key" + i));
404             assertEquals("key" + i + " is correct in all properties", vals[i - 1], all.get("key" + i));
405         }
406         assertEquals("evaluate works", "5=val1 x ${undef}", eval.evaluate("${key5} x ${undef}"));
407         // And test the preprovider...
408
Map JavaDoc<String JavaDoc,String JavaDoc> predefs = Collections.singletonMap("key3", "preval3");
409         eval = PropertyUtils.sequentialPropertyEvaluator(PropertyUtils.fixedPropertyProvider(predefs),
410             PropertyUtils.fixedPropertyProvider(defs1),
411             PropertyUtils.fixedPropertyProvider(defs2));
412         vals = new String JavaDoc[] {
413             "val1",
414             "val2",
415             "preval3",
416             "4=val1:preval3",
417             "5=val1",
418             "6=preval3",
419             "7=${undef}",
420         };
421         all = eval.getProperties();
422         assertEquals("right # of props", vals.length, all.size());
423         for (int i = 1; i <= vals.length; i++) {
424             assertEquals("key" + i + " is correct", vals[i - 1], eval.getProperty("key" + i));
425             assertEquals("key" + i + " is correct in all properties", vals[i - 1], all.get("key" + i));
426         }
427         assertEquals("evaluate works", "4=val1:preval3 x ${undef} x preval3", eval.evaluate("${key4} x ${undef} x ${key3}"));
428     }
429     
430     public void testSequentialEvaluatorChanges() throws Exception JavaDoc {
431         RakeBasedTestUtil.TestMutablePropertyProvider predefs = new RakeBasedTestUtil.TestMutablePropertyProvider(new HashMap JavaDoc<String JavaDoc,String JavaDoc>());
432         RakeBasedTestUtil.TestMutablePropertyProvider defs1 = new RakeBasedTestUtil.TestMutablePropertyProvider(new HashMap JavaDoc<String JavaDoc,String JavaDoc>());
433         RakeBasedTestUtil.TestMutablePropertyProvider defs2 = new RakeBasedTestUtil.TestMutablePropertyProvider(new HashMap JavaDoc<String JavaDoc,String JavaDoc>());
434         predefs.defs.put("x", "xval1");
435         predefs.defs.put("y", "yval1");
436         defs1.defs.put("a", "aval1");
437         defs1.defs.put("b", "bval1=${x}");
438         defs1.defs.put("c", "cval1=${z}");
439         defs2.defs.put("m", "mval1");
440         defs2.defs.put("n", "nval1=${x}:${b}");
441         defs2.defs.put("o", "oval1=${z}");
442         PropertyEvaluator eval = PropertyUtils.sequentialPropertyEvaluator(predefs, defs1, defs2);
443         RakeBasedTestUtil.TestPCL l = new RakeBasedTestUtil.TestPCL();
444         eval.addPropertyChangeListener(l);
445         Map JavaDoc<String JavaDoc,String JavaDoc> result = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
446         result.put("x", "xval1");
447         result.put("y", "yval1");
448         result.put("a", "aval1");
449         result.put("b", "bval1=xval1");
450         result.put("c", "cval1=${z}");
451         result.put("m", "mval1");
452         result.put("n", "nval1=xval1:bval1=xval1");
453         result.put("o", "oval1=${z}");
454         assertEquals("correct initial vals", result, eval.getProperties());
455         assertEquals("no changes yet", Collections.emptySet(), l.changed);
456         // Change predefs.
457
predefs.defs.put("x", "xval2");
458         predefs.mutated();
459         Map JavaDoc<String JavaDoc,String JavaDoc> oldvals = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
460         oldvals.put("x", result.get("x"));
461         oldvals.put("b", result.get("b"));
462         oldvals.put("n", result.get("n"));
463         Map JavaDoc<String JavaDoc,String JavaDoc> newvals = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
464         newvals.put("x", "xval2");
465         newvals.put("b", "bval1=xval2");
466         newvals.put("n", "nval1=xval2:bval1=xval2");
467         result.putAll(newvals);
468         assertEquals("some changes", newvals.keySet(), l.changed);
469         assertEquals("right old values", oldvals, l.oldvals);
470         assertEquals("right new values", newvals, l.newvals);
471         assertEquals("right total values now", result, eval.getProperties());
472         l.reset();
473         // Change some other defs.
474
defs1.defs.put("z", "zval1");
475         defs1.defs.remove("b");
476         defs1.mutated();
477         defs2.defs.put("m", "mval2");
478         defs2.mutated();
479         oldvals.clear();
480         oldvals.put("b", result.get("b"));
481         oldvals.put("c", result.get("c"));
482         oldvals.put("m", result.get("m"));
483         oldvals.put("n", result.get("n"));
484         oldvals.put("o", result.get("o"));
485         oldvals.put("z", result.get("z"));
486         newvals.clear();
487         newvals.put("b", null);
488         newvals.put("c", "cval1=zval1");
489         newvals.put("m", "mval2");
490         newvals.put("n", "nval1=xval2:${b}");
491         newvals.put("o", "oval1=zval1");
492         newvals.put("z", "zval1");
493         result.putAll(newvals);
494         result.remove("b");
495         assertEquals("some changes", newvals.keySet(), l.changed);
496         assertEquals("right old values", oldvals, l.oldvals);
497         assertEquals("right new values", newvals, l.newvals);
498         assertEquals("right total values now", result, eval.getProperties());
499         l.reset();
500     }
501     
502     private static final String JavaDoc ILLEGAL_CHARS = " !\"#$%&'()*+,/:;<=>?@[\\]^`{|}~";
503     
504     public void testIsUsablePropertyName() throws Exception JavaDoc {
505         for (int i=0; i<ILLEGAL_CHARS.length(); i++) {
506             String JavaDoc s = ILLEGAL_CHARS.substring(i, i+1);
507             assertFalse("Not a valid property name: "+s, PropertyUtils.isUsablePropertyName(s));
508         }
509         for (int i=127; i<256; i++) {
510             String JavaDoc s = ""+(char)i;
511             assertFalse("Not a valid property name: "+s+" - "+i, PropertyUtils.isUsablePropertyName(s));
512         }
513         assertFalse("Not a valid property name", PropertyUtils.isUsablePropertyName(ILLEGAL_CHARS));
514         for (int i=32; i<127; i++) {
515             String JavaDoc s = ""+(char)i;
516             if (ILLEGAL_CHARS.indexOf((char)i) == -1) {
517                 assertTrue("Valid property name: "+s, PropertyUtils.isUsablePropertyName(s));
518             }
519         }
520     }
521     
522     public void testGetUsablePropertyName() throws Exception JavaDoc {
523         StringBuffer JavaDoc bad = new StringBuffer JavaDoc();
524         StringBuffer JavaDoc good = new StringBuffer JavaDoc();
525         for (int i=0; i<ILLEGAL_CHARS.length(); i++) {
526             bad.append(ILLEGAL_CHARS.substring(i, i+1));
527             bad.append("x");
528             good.append("_");
529             good.append("x");
530         }
531         assertEquals("Corrected property name does match", good.toString(), PropertyUtils.getUsablePropertyName(bad.toString()));
532     }
533     
534     public void testSequentialPropertyEvaluatorStringAllocation() throws Exception JavaDoc {
535         // #48449: too many String instances.
536
// String constants used in the test are interned; make sure the results are the same.
537
// Not necessary for the provider to intern strings, just to not copy them.
538
Map JavaDoc<String JavaDoc,String JavaDoc> defs = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
539         defs.put("pre-a", "pre-a-val");
540         defs.put("pre-b", "pre-b-val");
541         PropertyProvider preprovider = PropertyUtils.fixedPropertyProvider(defs);
542         defs = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
543         defs.put("main-1-a", "main-1-a-val");
544         defs.put("main-1-b", "main-1-b-val+${pre-b}");
545         PropertyProvider provider1 = PropertyUtils.fixedPropertyProvider(defs);
546         defs = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
547         defs.put("main-2-a", "main-2-a-val");
548         defs.put("main-2-b", "main-2-b-val+${main-1-b}");
549         PropertyProvider provider2 = PropertyUtils.fixedPropertyProvider(defs);
550         PropertyEvaluator pp = PropertyUtils.sequentialPropertyEvaluator(preprovider, provider1, provider2);
551         defs = pp.getProperties();
552         assertSame("uncopied pre-a", "pre-a-val", defs.get("pre-a"));
553         assertSame("uncopied pre-b", "pre-b-val", defs.get("pre-b"));
554         assertSame("uncopied main-1-a", "main-1-a-val", defs.get("main-1-a"));
555         assertEquals("right main-1-b", "main-1-b-val+pre-b-val", defs.get("main-1-b"));
556         assertSame("uncopied main-2-a", "main-2-a-val", defs.get("main-2-a"));
557         assertEquals("right main-2-b", "main-2-b-val+main-1-b-val+pre-b-val", defs.get("main-2-b"));
558     }
559     
560 }
561
Popular Tags