KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > tests > PropertiesIteratorTest


1 /**
2  * $Id: PropertiesIteratorTest.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.ownhelpers.tests;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import junit.framework.TestSuite;
36
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.taskdefs.Property;
39 import org.apache.tools.ant.types.RegularExpression;
40
41 import com.idaremedia.antx.ut.HTC;
42 import com.idaremedia.antx.ut.HTCUtils;
43
44 import com.idaremedia.antx.FixtureExaminer;
45 import com.idaremedia.antx.ownhelpers.InnerProperties;
46 import com.idaremedia.antx.ownhelpers.PropertiesIterator;
47 import com.idaremedia.antx.ownhelpers.UnresolvedProperty;
48 import com.idaremedia.antx.parameters.PropertySource;
49
50 /**
51  * Testsuite for {@linkplain PropertiesIterator}.
52  *
53  * @since JWare/AntX 0.4
54  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
55  * @version 0.5
56  * @.safety single
57  * @.group impl,test
58  **/

59
60 public final class PropertiesIteratorTest extends HTC
61 {
62     /** <i>PET</i> Test Category. **/
63     public static final String JavaDoc TEST_CATEGORY="CLASS";
64
65
66     /**
67      * Create new PropertiesIteratorTest testcase.
68      **/

69     public PropertiesIteratorTest(String JavaDoc methodName)
70     {
71         super("PropertiesIterator::",methodName);
72     }
73
74
75     /**
76      * Create full test suite for PropertiesIterator.
77      **/

78     public static TestSuite suite()
79     {
80         return new TestSuite(PropertiesIteratorTest.class);
81     }
82
83
84     /**
85      * Create baseline test suite for PropertiesIterator (same as full).
86      **/

87     public static TestSuite baseline()
88     {
89         return suite();
90     }
91
92
93     /**
94      * Make this test (standalone) self-running.
95      **/

96     public static void main(String JavaDoc[] argv)
97     {
98         HTCUtils.quickCheck(suite());
99     }
100
101
102 // ---------------------------------------------------------------------------------------------------------
103
// --------------------------------------- [ Misc Factory Methods ] ----------------------------------------
104
// ---------------------------------------------------------------------------------------------------------
105

106     protected void setUp() throws Exception JavaDoc
107     {
108         configureProjectFromResource("blank.xml");
109     }
110
111
112     private PropertiesIterator newOUT()
113     {
114         return new PropertiesIterator();
115     }
116
117
118     private PropertiesIterator newOUT(Project P)
119     {
120         PropertiesIterator out = newOUT();
121         out.setProject(P);
122         return out;
123     }
124
125     private PropertiesIterator newOUT(InnerProperties helper)
126     {
127         return new PropertiesIterator(helper);
128     }
129
130     private void disposeOUT(PropertiesIterator out)
131     {
132         out.dispose();
133     }
134
135
136 // ---------------------------------------------------------------------------------------------------------
137
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
138
// ---------------------------------------------------------------------------------------------------------
139

140     public void checkBaseline()
141     {
142         //--Ensures setUp() works and can find our xml file!
143

144         PropertiesIterator out = newOUT();
145
146         assertIdent(out.getDomain(),PropertySource.ALL);
147         assertFalse(out.isIgnoreCase());
148         assertFalse(out.willCheckRE());
149
150         disposeOUT(out);
151     }
152
153
154     public void testBaseline()
155     {
156         checkBaseline();
157     }
158
159
160     /**
161      * Ensure we need to associate a live project with
162      * iterator before it's usable.
163      **/

164     public void testFailNoProject_AntX04()
165     {
166         PropertiesIterator out = newOUT();
167         try {
168             out.hasNext();
169             fail("Able to iterator unassociated iterator?!");
170         } catch(Exception JavaDoc barfage) {
171             assertExpected(barfage,"ProjectDependent:");
172         }
173         disposeOUT(out);
174     }
175
176
177     /**
178      * Ensure you cannot remove properties via iterator.
179      **/

180     public void testFailRemoval_AntX04()
181     {
182         PropertiesIterator out = newOUT(getProject());
183         try {
184             assertTrue(out.hasNext());
185             assertNotNil(out.next());
186             out.remove();
187             fail("Able to remove property via Iterator!?");
188         } catch(UnsupportedOperationException JavaDoc Xpected) {
189             /*burp*/
190         }
191         disposeOUT(out);
192     }
193
194
195     /**
196      * Verify can iterate *ALL* properties in project.
197      **/

198     public void testIterateAllProperties_AntX04()
199     {
200         PropertiesIterator out = newOUT(getProject());
201         Map JavaDoc mp = getProject().getProperties();
202         final int N= mp.size();
203         int i=0;
204         while (out.hasNext()) {
205             Object JavaDoc item = out.next();
206             assertTrue((item instanceof Map.Entry JavaDoc),"Is Map.Entry @"+i);
207             i++;
208         }
209         assertEqual(i,N,"PropertyCount");
210         disposeOUT(out);
211     }
212
213
214     /**
215      * Verify can iterate lite version of *ALL*.
216      **/

217     public void testIterateAllLiteProperties_AntX04()
218     {
219         PropertiesIterator out = newOUT();
220         out.setProject(getProject());
221
222         out.setDomain(PropertySource.from("all--"));
223         assertIdent(out.getDomain(),PropertySource.ALLlite);
224
225         Map JavaDoc nonJre = getProject().getProperties();
226         Map JavaDoc jre = getProject().getProperties();
227
228         final int fullCount= jre.size();
229
230         FixtureExaminer.removeImmutableJavaProperties(nonJre);
231         jre.keySet().removeAll(nonJre.keySet());
232
233         final int nonJreCount= nonJre.size();
234         assertTrue(nonJreCount<fullCount,
235                    "JRE-bits-free map is smaller than original");
236
237         int I=0;
238         while (out.hasNext()) {
239             Map.Entry JavaDoc mE = (Map.Entry JavaDoc)out.next();
240             nonJre.remove(mE.getKey());
241             assertFalse(jre.containsKey(mE.getKey()),"JRE bit");
242             I++;
243         }
244
245         assertEqual(I,nonJreCount,"Iterated JRE-free bits");
246         assertTrue(nonJre.isEmpty(),"JRE-bits removed as expected");
247
248         jre= null;
249         nonJre= null;
250         disposeOUT(out);
251     }
252
253
254     /**
255      * Verify can filter selection by regular expression.
256      **/

257     public void testREFilterAllDomain_AntX04()
258     {
259         Map JavaDoc mp = getProject().getProperties();
260         Iterator JavaDoc itr= mp.keySet().iterator();
261         ArrayList JavaDoc hits = new ArrayList JavaDoc();
262
263         while (itr.hasNext()) {
264             String JavaDoc key = (String JavaDoc)itr.next();
265             if (key.startsWith("ant.")) {
266                 hits.add(key);
267             }
268         }
269         assertFalse(hits.isEmpty(),"Unable to find any 'ant.' properties");
270         assertTrue(hits.size()!=mp.size(),"Hits are subset of all");
271
272         RegularExpression re= new RegularExpression();
273         re.setPattern("^ant\\..*$");
274
275         PropertiesIterator out = newOUT(getProject());
276         out.setRE(re);
277         assertTrue(out.willCheckRE(),"RE attached");
278
279         while (out.hasNext()) {
280             Map.Entry JavaDoc hit = (Map.Entry JavaDoc)out.next();
281             assertTrue(hits.contains(hit.getKey()),"Result("+hit.getKey()+") expected");
282             hits.remove(hit.getKey());
283         }
284
285         assertEqual(hits.size(),0,"Leftover Matches");
286
287         disposeOUT(out);
288     }
289
290
291     /**
292      * Verifies that unresolved properties are also returned
293      * by default.
294      **/

295     public void testNoFilterUnresolvedByDefault_AntX04()
296     {
297         Project P = getProject();
298         Map JavaDoc mp = P.getProperties();
299         final int N = mp.size();
300
301         Property setter = new Property();
302         setter.setProject(P);
303
304         setter.setName("__unresolved_1__");
305         setter.setValue("${doinkdoinkdoinkdoinkdoink}");
306         setter.execute();
307         setter.setName("__unresolved_2__");
308         setter.setValue("${OINKOINKOINKOINKOINK}");
309         setter.execute();
310
311         assertEqual(P.getProperty("__unresolved_1__"),
312                     "${doinkdoinkdoinkdoinkdoink}","Unresolved_1");
313
314         int i=0;
315         PropertiesIterator out= newOUT();
316         out.setProject(P);
317         while (out.hasNext()) {
318             out.next();
319             i++;
320         }
321
322         assertEqual(i,N+2,"PropertyCount+Unresolved");
323
324         disposeOUT(out);
325     }
326
327
328     /**
329      * Verify can limit domain to CLI-based properties (and some
330      * special Ant properites).
331      **/

332     public void testIterateCLIProperties_AntX04()
333     {
334         getProject().setUserProperty("ant.blablabla","hi!");
335
336         Map JavaDoc mp = getProject().getProperties();
337         final int N = mp.size();
338
339         PropertiesIterator out = newOUT();
340         out.setProject(getProject());
341
342         out.setDomain(PropertySource.from("command"));
343         assertIdent(out.getDomain(),PropertySource.COMMAND);
344
345         int i=0,Nblablabla=0;
346         while (out.hasNext()) {
347             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
348             String JavaDoc property = mE.getKey().toString();
349             println(""+i+") "+property+"='"+mE.getValue()+"'");
350             assertTrue(property.startsWith("ant."),"Property '"+property+"' is Ant property");
351             if ("ant.blablabla".equals(property)) {
352                 Nblablabla++;
353             }
354             i++;
355         }
356
357         assertTrue(i<N,"CLI (much) lessthan ALL");
358         assertEqual(Nblablabla,1,"InternalCommandCount");
359
360         disposeOUT(out);
361     }
362
363
364     /**
365      * Verify can limit domain to non-command/control properties.
366      **/

367     public void testIterateScriptOnlyProperties_AntX04()
368     {
369         getProject().setUserProperty("p.maybe","error!");
370         getProject().setInheritedProperty("p.watup","errortoo!");
371
372         PropertiesIterator out= newOUT(getProject());
373         out.setDomain(PropertySource.SCRIPT);
374
375         int N=0,LIMIT=20;
376         while (out.hasNext()) {
377             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
378             String JavaDoc property = mE.getKey().toString();
379             if (--LIMIT>0) {
380                 println(property+"='"+mE.getValue()+"'");
381             }
382             assertNotEqual("ant.file",property,"is 'ant.file'");
383             assertNotEqual("p.maybe",property,"is 'p.maybe'");
384             assertNotEqual("p.watup",property,"is 'p.watup'");
385             N++;
386         }
387         println("Number SCRIPT properties: "+N);
388         disposeOUT(out);
389     }
390
391
392     /**
393      * Verify can limit domain to Java System properties.
394      **/

395     public void testIterateJavaRuntimeOnlyProperties_AntX04()
396     {
397         getProject().setNewProperty("_script.0", "broken");
398         getProject().setUserProperty("_command.0", "broken too");
399         getProject().setUserProperty("_control.0", "broken too too");
400
401         PropertiesIterator out= newOUT(getProject());
402         out.setDomain(PropertySource.JRE);
403
404         int N=0,LIMIT=30;
405         while (out.hasNext()) {
406             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
407             String JavaDoc property = mE.getKey().toString();
408             if (--LIMIT>0) {
409                 println(property+"='"+mE.getValue()+"'");
410             }
411             assertNotEqual("ant.file",property,"is 'ant.file'");
412             assertNotEqual("_script.0",property,"is 'script property'");
413             assertNotEqual("_command.0",property,"is 'command property'");
414             assertNotEqual("_control.0",property,"is 'control property'");
415             N++;
416         }
417         println("Number of SYSTEM properties: "+N);
418         disposeOUT(out);
419     }
420
421
422     /**
423      * Verify can limit domain and apply a filter.
424      **/

425     public void testREFilterScriptProperties_AntX04()
426     {
427         getProject().setUserProperty("p.maybe","error!");
428         getProject().setInheritedProperty("p.watup","errortoo!");
429
430         PropertiesIterator out= newOUT(getProject());
431         out.setDomain(PropertySource.SCRIPT);
432
433         RegularExpression re= new RegularExpression();
434         re.setPattern("^p\\.(true|false|maybe|watup)$");
435         out.setRE(re);
436
437         int i=0;
438         while (out.hasNext()) {
439             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
440             String JavaDoc property = mE.getKey().toString();
441             assertTrue("p.true".equals(property) || "p.false".equals(property),
442                        "p.true =or= p.false");
443             i++;
444         }
445         assertEqual(i,2,"MatchCount");
446
447         disposeOUT(out);
448     }
449
450
451     /**
452      * Verify can ignore properties with unresolved bits. Note that
453      * the unresolved 'p.unresolved' property is defined in our test
454      * build script.
455      **/

456     public void testIgnoreSimpleUnresolvedProperties_AntX04()
457     {
458         getProject().setUserProperty("p.doink","${doinkdoinkdoink}");
459
460         PropertiesIterator out= newOUT(getProject());
461         out.setCheckSimpleBrokenSubstitution();
462
463         while (out.hasNext()) {
464             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
465             String JavaDoc property = mE.getKey().toString();
466             if ("p.doink".equals(property) || "p.unresolved".equals(property)) {
467                 fail("Unresolved property("+property+","+mE.getValue()+
468                      ") should be filtered");
469             }
470         }
471         disposeOUT(out);
472     }
473
474
475
476     /**
477      * Verify can ignore properties with unresolved bits. Note that
478      * the unresolved 'p.unresolved' property is defined in our test
479      * build script.
480      **/

481     public void testIgnoreAllUnresolvedProperties_AntX04()
482     {
483         getProject().setProperty("p.doink","hi_${doinkdoinkdoink}_");
484         getProject().setProperty("p.superdoink"," ${bla} ${bla} ${doink}_");
485
486         PropertiesIterator out= newOUT(getProject());
487         out.setCheckBrokenSubstitution();
488
489         while (out.hasNext()) {
490             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
491             String JavaDoc property = mE.getKey().toString();
492             if ("p.doink".equals(property) || "p.unresolved".equals(property)
493                 || "p.superdoink".equals(property)) {
494                 fail("Unresolved property("+property+","+mE.getValue()+
495                      ") should be filtered");
496             }
497         }
498         disposeOUT(out);
499     }
500
501
502     /**
503      * Verify can determine which properties are unresolved by assigning
504      * our own proxy marker.
505      **/

506     public void testCanProxyUnresolvedProperties_AntX04()
507     {
508         getProject().setProperty("p.doink","hi_${doinkdoinkdoink}_");
509
510         PropertiesIterator out= newOUT(getProject());
511         out.setDomain(PropertySource.SCRIPT);
512         out.setCheckBrokenSubstitution();
513         out.setBrokenSubstitutionProxy(UnresolvedProperty.VALUE);
514
515         ArrayList JavaDoc broken = new ArrayList JavaDoc();
516
517         while (out.hasNext()) {
518             Map.Entry JavaDoc mE = (Map.Entry JavaDoc)out.next();
519             if (mE.getValue()==UnresolvedProperty.VALUE) {
520                 broken.add(mE);
521             }
522         }
523         Iterator JavaDoc itr= broken.iterator();
524         while (itr.hasNext()) {
525             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)itr.next();
526             println(""+mE.getKey()+"='"+mE.getValue()+"'");
527         }
528         assertEqual(broken.size(),2,"BrokenCount");
529         disposeOUT(out);
530     }
531
532
533     /**
534      * Verify the 'ignorecase' option works for both prefix and regular
535      * expression selection.
536      **/

537     public void testIgnoreCaseWorks_AntX04()
538     {
539         getProject().setProperty("BeGiN.one","ralph");
540         getProject().setProperty("bEgIn.ONE","norton");
541         getProject().setProperty("BEginError","doink!");
542
543         PropertiesIterator out= newOUT(getProject());
544         out.setDomain(PropertySource.SCRIPTlite);
545         out.setIgnoreCase(true);
546
547         out.setPrefix("BEgin.");
548         int N=0;
549         while (out.hasNext()) {
550             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
551             String JavaDoc name = (String JavaDoc)mE.getKey();
552             assertFalse(name.startsWith("BEgin"),name+" not 'BEgin' prefix");
553             N++;
554         }
555         assertEqual(N,2,"prefixMatchCount");
556
557         out.reset();
558
559         out.setPrefix(null);
560         RegularExpression re= new RegularExpression();
561         re.setPattern("^BEgin\\..*");
562         out.setRE(re);
563
564         N=0;
565         while (out.hasNext()) {
566             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
567             String JavaDoc name = (String JavaDoc)mE.getKey();
568             assertFalse(name.startsWith("BEgin"),name+" not 'BEgin' re");
569             N++;
570         }
571         assertEqual(N,2,"reMatchCount");
572
573         disposeOUT(out);
574     }
575
576
577     /**
578      * Verify that initialization using an InnerProperties bean works
579      * as expected. Also checks the "notLike" match variant.
580      **/

581     public void testInitByInnerProperties_AntX04()
582     {
583         Project P= getProject();
584         P.setProperty("_fu.first", "first");
585         P.setProperty("_fu.second", "second");
586         P.setProperty("f_u.third", "third");
587
588         InnerProperties bean = new InnerProperties();
589         bean.setProject(P);
590         bean.setDomain("script");
591         bean.setNotLike("^_fu\\..*");
592
593         PropertiesIterator out= newOUT(bean);
594         assertTrue(out.isExclusion());
595         assertFalse(out.willCheckPrefix());
596
597         while(out.hasNext()) {
598             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)out.next();
599             String JavaDoc name = (String JavaDoc)mE.getKey();
600             assertFalse(name.startsWith("_fu."),name+" not '_fu.' re match");
601         }
602
603         disposeOUT(out);
604     }
605 }
606
607 /* end-of-PropertiesIteratorTest.java */
608
Popular Tags