KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ConditionalParametersTest.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 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.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import junit.framework.TestSuite;
35
36 import com.idaremedia.antx.helpers.InnerNameValuePair;
37 import com.idaremedia.antx.helpers.Strings;
38 import com.idaremedia.antx.ownhelpers.ConditionalInnerNameValuePair;
39 import com.idaremedia.antx.ownhelpers.ConditionalParameters;
40 import com.idaremedia.antx.parameters.Conditional;
41 import com.idaremedia.antx.ut.HTC;
42 import com.idaremedia.antx.ut.HTCUtils;
43
44 /**
45  * Test suite for {@linkplain com.idaremedia.antx.ownhelpers.ConditionalParameters}.
46  *
47  * @since JWare/AntX 0.5
48  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety single
51  * @.group impl,test
52  **/

53
54 public final class ConditionalParametersTest extends HTC
55 {
56     /** <i>PET</i> Test Category. **/
57     public static final String JavaDoc TEST_CATEGORY="CLASS";
58
59
60     /**
61      * Create new ConditionalParametersTest testcase.
62      **/

63     public ConditionalParametersTest(String JavaDoc methodName)
64     {
65         super("ConditionalParameters::",methodName);
66     }
67
68
69     /**
70      * Create full test suite for ConditionalParameters.
71      **/

72     public static TestSuite suite()
73     {
74         return new TestSuite(ConditionalParametersTest.class);
75     }
76
77
78     /**
79      * Create baseline test suite for ConditionalParameters (same as full).
80      **/

81     public static TestSuite baseline()
82     {
83         return suite();
84     }
85
86
87     /**
88      * Make this test (standalone) self-running.
89      **/

90     public static void main(String JavaDoc[] argv)
91     {
92         HTCUtils.quickCheck(suite());
93     }
94
95
96 // ---------------------------------------------------------------------------------------------------------
97
// --------------------------------------- [ Misc Factory Methods ] ----------------------------------------
98
// ---------------------------------------------------------------------------------------------------------
99

100     protected void setUp() throws Exception JavaDoc
101     {
102         configureProjectFromResource("blank.xml");
103     }
104
105
106     private ConditionalParameters newOUT()
107     {
108         return new ConditionalParameters(getProject());
109     }
110
111     private ConditionalInnerNameValuePair newIf(String JavaDoc name, String JavaDoc value, String JavaDoc property)
112     {
113         ConditionalInnerNameValuePair arg = new ConditionalInnerNameValuePair(name,value);
114         arg.setIf(property);
115         if (value==null) {
116             arg.setValue(Strings.UNDEFINED);
117         }
118         return arg;
119     }
120
121     private ConditionalInnerNameValuePair newUn(String JavaDoc name, String JavaDoc value, String JavaDoc property)
122     {
123         ConditionalInnerNameValuePair arg = new ConditionalInnerNameValuePair(name,value);
124         arg.setUnless(property);
125         if (value==null) {
126             arg.setValue(Strings.UNDEFINED);
127         }
128         return arg;
129     }
130
131     private ConditionalInnerNameValuePair newAlways(String JavaDoc name, String JavaDoc value)
132     {
133         ConditionalInnerNameValuePair arg = new ConditionalInnerNameValuePair(name,value);
134         if (value==null) {
135             arg.setValue(Strings.UNDEFINED);
136         }
137         return arg;
138     }
139
140
141 // ---------------------------------------------------------------------------------------------------------
142
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
143
// ---------------------------------------------------------------------------------------------------------
144

145     public void checkBaseline()
146     {
147         //--Ensures setUp() works and can find our xml file!
148
ConditionalParameters out = newOUT();
149         assertTrue(out.isEmpty());
150     }
151
152
153     public void testBaseline()
154     {
155         checkBaseline();
156     }
157
158
159     /**
160      * Verifies that if-constraints paid attention to as expected.
161      * @since JWare/AntX 0.5
162      **/

163     public void testIfFiltersParameters_AntX05()
164     {
165         ConditionalParameters out = newOUT();
166         out.addConfiguredArg(newAlways("name","frank"));
167         out.addConfiguredArg(newIf("height","5'7","verbose"));
168         out.addConfiguredArg(newIf("weight","197lbs","verbose"));
169         out.addConfiguredArg(newIf("longname","Frank Burns","longnames"));
170         assertEqual(out.size(),4,"ParamCount");
171
172         out.getProject().setProperty("verbose","anyvalue");
173         Properties JavaDoc filtered = out.copyOfSimpleKeyValues(getProject(),true);
174         assertEqual(filtered.size(),3,"If-Filtered ParamCount");
175         assertTrue(filtered.containsKey("name"),"'name' found");
176         assertTrue(filtered.containsKey("height"),"'height' found");
177         assertTrue(filtered.containsKey("weight"),"'weight' found");
178     }
179
180
181
182     /**
183      * Verifies that if-constraints paid attention to as expected.
184      * @since JWare/AntX 0.5
185      **/

186     public void testUnlessFiltersParameters_AntX05()
187     {
188         ConditionalParameters out = newOUT();
189         out.addConfiguredArg(newAlways("name","billy"));
190         out.addConfiguredArg(newUn("height","5'5","brief"));
191         out.addConfiguredArg(newUn("weight","227lbs","brief"));
192         out.addConfiguredArg(newUn("longname","Billy Joel","shortnames"));
193         assertEqual(out.size(),4,"ParamCount");
194
195         out.getProject().setProperty("shortnames","anyvalue");
196         Properties JavaDoc filtered = out.copyOfSimpleKeyValues(getProject(),true);
197         assertEqual(filtered.size(),3,"Unless-Filtered ParamCount");
198         assertTrue(filtered.containsKey("name"),"'name' found");
199         assertTrue(filtered.containsKey("height"),"'height' found");
200         assertTrue(filtered.containsKey("weight"),"'weight' found");
201     }
202
203
204     /**
205      * Verifies that both if-unless ocnstraints applied as expected.
206      * @since JWare/AntX 0.5
207      **/

208     public void testBothFiltersAppliedAllTrue_AntX05()
209     {
210         ConditionalParameters out = newOUT();
211         out.addConfiguredArg(newAlways("name","billy"));
212         out.addConfiguredArg(newIf("height","5'5","verbose"));
213         out.addConfiguredArg(newIf("weight","227lbs","verbose"));
214         out.addConfiguredArg(newUn("longname","Billy Joel","shortnames"));
215         assertEqual(out.size(),4,"ParamCount");
216
217         out.getProject().setProperty("verbose","anyvalue");
218         Properties JavaDoc filtered = out.copyOfSimpleKeyValues(getProject(),true);
219         assertEqual(filtered.size(),4,"If/Unless-Filtered ParamCount");
220         assertTrue(filtered.containsKey("name"),"'name' found");
221         assertTrue(filtered.containsKey("height"),"'height' found");
222         assertTrue(filtered.containsKey("weight"),"'weight' found");
223         assertTrue(filtered.containsKey("longname"),"'longname' found");
224     }
225
226
227
228
229     /**
230      * Verifies that both if-unless ocnstraints applied as expected.
231      * @since JWare/AntX 0.5
232      **/

233     public void testBothFiltersAppliedAllFalse_AntX05()
234     {
235         ConditionalParameters out = newOUT();
236         out.addConfiguredArg(newAlways("name","billy"));
237         out.addConfiguredArg(newIf("height","5'5","verbose"));
238         out.addConfiguredArg(newIf("weight","227lbs","verbose"));
239         out.addConfiguredArg(newUn("longname","Billy Joel","shortnames"));
240         assertEqual(out.size(),4,"ParamCount");
241
242         out.getProject().setProperty("shortnames","anyvalue");
243         Properties JavaDoc filtered = out.copyOfSimpleKeyValues(getProject(),true);
244         assertEqual(filtered.size(),1,"If/Unless-Filtered ParamCount");
245         assertTrue(filtered.containsKey("name"),"'name' found");
246     }
247
248
249
250     /**
251      * Ensures the parameters object can handle different typed
252      * contents (because client passes in references) with general
253      * Conditional interface being key to inclusion/exclusion.
254      * @since JWare/AntX 0.5
255      **/

256     public void testHandlesHetergenousItems_AntX05()
257     {
258         ConditionalParameters out = newOUT();
259         out.addConfiguredArg(new InnerNameValuePair("name","WildThing"));
260         out.addConfiguredArg(newIf("streak","continue","winning"));
261         out.addConfiguredArg(newUn("hairstyle","mohawk","losing"));
262         out.addConfiguredArg(new SeekritNVItem("team","indians"));
263         out.addConfiguredArg(new SeekritConditionalNVItem("status","avail"));
264         assertEqual(out.size(),5,"ParamCount");
265
266         out.getProject().setProperty("losing","as-always");
267         out.getProject().setProperty("freeagent","");
268
269         Properties JavaDoc filtered = out.copyOfSimpleKeyValues(getProject());
270         assertEqual(filtered.size(),3,"Custom NVItems included");
271         assertTrue(filtered.containsKey("name"),"'name' found");
272         assertTrue(filtered.containsKey("team"),"'team' found");
273         assertEqual(filtered.getProperty("status"),"avail","status");
274     }
275
276
277     private static class SeekritNVItem extends InnerNameValuePair {
278         SeekritNVItem(String JavaDoc name, String JavaDoc value) {
279             super();
280             setNV(name,value);
281         }
282     };
283
284     private static class SeekritConditionalNVItem extends InnerNameValuePair
285         implements Conditional {
286         SeekritConditionalNVItem(String JavaDoc name, String JavaDoc value) {
287             super();
288             setNV(name,value);
289         }
290         public void setIf(String JavaDoc property) {
291         }
292         public String JavaDoc getIfProperty() {
293             return "freeagent";
294         }
295         public void setUnless(String JavaDoc property) {
296         }
297         public String JavaDoc getUnlessProperty() {
298             return null;
299         }
300     };
301
302
303     /**
304      * Ensures the filters are applied to copies of name-value objects
305      * that are returned by CUT.
306      * @since JWare/AntX 0.5
307      **/

308     public void testFiltersAppliedToRawItemsToo_AntX05()
309     {
310         ConditionalParameters out = newOUT();
311         out.addConfiguredParameter(newIf("slow","turtle","slow"));
312         out.addConfiguredProperty(newUn("fast","hare","slow"));
313         out.addConfiguredArg(new SeekritNVItem("medium","gerbil"));
314         ConditionalInnerNameValuePair item = new ConditionalInnerNameValuePair("superfast","cheetah");
315         item.setIf("superfast");
316         item.setUnless("superslow");
317         out.addConfiguredProperty(item);
318         assertEqual(out.size(),4,"ParamCount");
319
320         getProject().setProperty("slow","yes");
321         getProject().setProperty("superfast","yes");
322
323         Map JavaDoc copy = out.copyOfParameterObjects();
324         assertEqual(copy.size(),3,"Filtered ParamCount");
325         assertNotNil(copy.get("slow"),"'slow'");
326         assertNotNil(copy.get("medium"),"'medium'");
327         assertNotNil(copy.get("superfast"),"'superfast'");
328
329         assertIdent(copy.get("medium").getClass(),SeekritNVItem.class,"SeekritNVItem");
330     }
331 }
332
333 /* end-of-ConditionalParametersTest.java */
Popular Tags