KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > tests > IterationTest


1 /**
2  * $Id: IterationTest.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2004,2007 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://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.tests;
30
31 import java.util.Map JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import junit.framework.TestSuite;
36
37 import com.idaremedia.antx.FixtureExaminer;
38 import com.idaremedia.antx.Iteration;
39 import com.idaremedia.antx.helpers.SIDs;
40 import com.idaremedia.antx.ut.HTC;
41 import com.idaremedia.antx.ut.HTCUtils;
42
43 /**
44  * Test suite for {@linkplain com.idaremedia.antx.Iteration Iteration} extension.
45  *
46  * @since JWare/AntX 0.5
47  * @author ssmc, &copy;2004,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
48  * @version 0.5.1
49  * @.safety single
50  * @.group impl,test
51  **/

52
53 public final class IterationTest extends HTC
54 {
55     /**
56      * Create new IterationTest testcase.
57      **/

58     public IterationTest(String JavaDoc methodName)
59     {
60         super("(Plain)Iteration::",methodName);
61     }
62
63
64     /**
65      * Create full test suite for Iteration.
66      **/

67     public static TestSuite suite()
68     {
69         return new TestSuite(IterationTest.class);
70     }
71
72
73     /**
74      * Make this test (standalone) self-running.
75      **/

76     public static void main(String JavaDoc[] argv)
77     {
78         HTCUtils.quickCheck(suite());
79     }
80
81
82 // ---------------------------------------------------------------------------------------------------------
83
// ---------------------------------------- [ Misc Setup Methods ] -----------------------------------------
84
// ---------------------------------------------------------------------------------------------------------
85

86     protected void setUp() throws Exception JavaDoc
87     {
88         if ("testBaseline".equals(getName())) {
89             Iteration.set(new FakeIteration());
90             configureProjectFromResource("fakeiteration.xml");
91         } else {
92             Iteration.reset();
93             /* Required for Ant 1.7's version of BuildFileTest (requires at least 1 target). */
94             configureProjectFromResource("standardblank.xml");
95         }
96     }
97
98
99 // ---------------------------------------------------------------------------------------------------------
100
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
101
// ---------------------------------------------------------------------------------------------------------
102

103     public void checkBaseline()
104     {
105         //--Ensures setUp() works and can find our xml file!
106
}
107
108     public void testBaseline()
109     {
110         checkBaseline();
111         runTarget("AllTests");
112     }
113
114
115     public void testSetTopLevelFlatProperty_AntX05()
116     {
117         String JavaDoc key0 = SIDs.next();
118         String JavaDoc key1 = key0+".b";
119         
120         assertNull(Iteration.getProperty(key0));
121         Iteration.setProperty(key0,"helloworld");
122         assertEquals(new String JavaDoc("h"+"elloworld"),Iteration.getProperty(key0));
123         String JavaDoc val0 = String.valueOf(Iteration.getProperty(key0));
124
125         Iteration.setProperty(key1,new Integer JavaDoc(111));
126         assertEquals(val0,Iteration.getProperty(key0));
127         assertTrue(Iteration.getProperty(key1) instanceof Integer JavaDoc);
128         assertEquals("111",String.valueOf(Iteration.getProperty(key1)));
129
130         Iteration.removeProperty(key0);
131         assertNil(Iteration.getProperty(key0));
132         assertNotNil(Iteration.getProperty(key1));
133         
134         Iteration.setProperty(key1, new RuntimeException JavaDoc("imbroken"));
135         assertTrue(Iteration.getProperty(key1) instanceof RuntimeException JavaDoc);
136         assertNil(Iteration.getProperty(key0));
137         
138         Iteration.removeProperty(key1);
139         assertNil(Iteration.getProperty(key1));
140     }
141
142
143     private void verifyFixture00(String JavaDoc category0, String JavaDoc category1, String JavaDoc key0)
144     {
145         assertNil(Iteration.getProperty(category0));
146         assertNil(Iteration.getProperty(category0,key0));
147         assertNil(Iteration.getProperty(key0));
148         assertNil(Iteration.getProperty(category1));
149     }
150
151
152     public void testSetTopLevelNestedProperty_AntX05()
153     {
154         String JavaDoc category0 = SIDs.next("ITCAT");
155         String JavaDoc key0 = SIDs.next();
156         String JavaDoc category1 = key0+".ITCAT";
157         
158         verifyFixture00(category0,category1,key0);
159         
160         Iteration.setProperty(category0,key0,"Hola Mundo");
161         assertTrue(Iteration.getProperty(category0) instanceof Map JavaDoc);
162         assertEquals(new String JavaDoc("Hola "+"Mundo"),Iteration.getProperty(category0,key0));
163         assertNil(Iteration.getProperty(key0));
164         
165         Iteration.setProperty(category1,"flat");
166         Iteration.setProperty(category1,key0,"ERROR");
167         assertEquals("flat",Iteration.getProperty(category1));
168         assertNil(Iteration.getProperty(key0));
169         assertNil(Iteration.getProperty(category0,category1));
170         
171         Iteration.removeProperty(category1);
172         Iteration.setProperty(category1,key0,"OK!");
173         assertTrue(Iteration.getProperty(category1) instanceof Map JavaDoc);
174         assertIdent("OK!",Iteration.getProperty(category1,key0));
175         
176         Iteration.removeProperty(category1,key0);
177         Object JavaDoc o = Iteration.getProperty(category1);
178         assertTrue(o instanceof Map JavaDoc);
179         assertEqual(0,((Map JavaDoc)o).size(),"map size");
180         
181         Iteration.setProperty(category1,category0);//bad idea generally
182
assertIdent(Iteration.getProperty(category1),category0);
183         Iteration.removeProperty(category0);
184         Iteration.removeProperty(category1);
185         
186         verifyFixture00(category0,category1,key0);
187     }
188     
189     
190     public void testTopLevelValueURIBuiltin_AntX05()
191     {
192         final Project P = new Project();
193         Object JavaDoc itid = Iteration.getProperty("Iteration.ITID");
194         assertNotNil(itid,"Iteration.ITID property");
195         assertIdent(itid,FixtureExaminer.findValue(P,"$itprop:Iteration.ITID",""));
196
197         String JavaDoc category0 = SIDs.next("ITCAT");
198         String JavaDoc key0 = SIDs.next();
199         String JavaDoc refkey0 = "$itproperty:"+category0+"->"+key0;
200         
201         assertNil(FixtureExaminer.findValue(P,refkey0,""),refkey0);
202         Iteration.setProperty(key0,"HelloWorld");
203         Iteration.setProperty(category0,key0,"ByeWorld");
204         assertEqual("HelloWorld",FixtureExaminer.findValue(P,"$itproperty:"+key0,""));
205         assertEqual("ByeWorld",FixtureExaminer.findValue(P,refkey0,""));
206         
207         Iteration.removeProperty(category0);
208         Iteration.removeProperty(key0);
209     }
210     
211     
212     public void testFailGetNullProperties_AntX05()
213     {
214         try {
215             Iteration.getProperty(null);
216             fail("Should not be able to get NULL property name");
217         } catch(IllegalArgumentException JavaDoc Xpected) {
218             assertExpected(Xpected,"property name");
219         }
220         try {
221             Iteration.getProperty(null,"item0");
222             fail("Should not be able to get NULL category name");
223         } catch(IllegalArgumentException JavaDoc Xpected) {
224             assertExpected(Xpected,"category");
225         }
226         try {
227             Iteration.getProperty("category0",null);
228             fail("Should not be able to get NULL category property");
229         } catch(IllegalArgumentException JavaDoc Xpected) {
230             assertExpected(Xpected,"property name");
231         }
232     }
233
234
235     public void testFailPutNullProperties_AntX05()
236     {
237         try {
238             Iteration.setProperty(null,"value0");
239             fail("Should not be able to set NULL property name");
240         } catch(IllegalArgumentException JavaDoc Xpected) {
241             assertExpected(Xpected,"property name");
242         }
243         try {
244             Iteration.setProperty(null,"item0","value0");
245             fail("Should not be able to set NULL category name");
246         } catch(IllegalArgumentException JavaDoc Xpected) {
247             assertExpected(Xpected,"category");
248         }
249         try {
250             Iteration.setProperty("category0",null,"value0");
251             fail("Should not be able to set NULL category property");
252         } catch(IllegalArgumentException JavaDoc Xpected) {
253             assertExpected(Xpected,"property name");
254         }
255     }
256
257
258     public void testFailRemoveNullProperties_AntX05()
259     {
260         try {
261             Iteration.removeProperty(null);
262             fail("Should not be able to remove NULL property name");
263         } catch(IllegalArgumentException JavaDoc Xpected) {
264             assertExpected(Xpected,"property name");
265         }
266         try {
267             Iteration.removeProperty(null,"item0");
268             fail("Should not be able to remove NULL category name");
269         } catch(IllegalArgumentException JavaDoc Xpected) {
270             assertExpected(Xpected,"category");
271         }
272         try {
273             Iteration.removeProperty("category0",null);
274             fail("Should not be able to remove NULL category property");
275         } catch(IllegalArgumentException JavaDoc Xpected) {
276             assertExpected(Xpected,"property name");
277         }
278     }
279     
280
281     public void testHaveAccessToInheritedIterationFields_AntX05()
282         throws Exception JavaDoc
283     {
284         FakeIteration out = new FakeIteration();
285         String JavaDoc value = out.getInheritedField("m_itid");
286         assertEqual("java.lang.String",value,"inherited 'm_itid' field");
287         value = out.getInheritedField("m_defaultsINSTANCE");
288         assertEqual("com.idaremedia.antx.Defaults",value,"inherited 'm_defaultsINSTANCE' field");
289     }
290 }
291
292 /* end-of-IterationTest.java */
Popular Tags