KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > solo > VerifyFixture


1 /**
2  * $Id: VerifyFixture.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-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 as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * 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 (GNU Lesser General Public License) 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 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.condition.solo;
30
31 import java.text.MessageFormat JavaDoc;
32 import java.util.Stack JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.taskdefs.condition.Condition;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.apis.BuildAssertionException;
40 import com.idaremedia.antx.apis.Requester;
41 import com.idaremedia.antx.helpers.Tk;
42 import com.idaremedia.antx.parameters.Handling;
43 import com.idaremedia.antx.parameters.IsA;
44
45 /**
46  * Helper that verifies the expected state of fixture information. Basically a simplified
47  * target-independent assertion that verifies the most basic build iteration constraints.
48  * Usually defined as &lt;fixturecheck&gt; or &lt;verifyfixture&gt;. This task
49  * is important when defining rules that expect certain properties to exist.
50  * <p>
51  * <b>Examples:</b><pre>
52  * &lt;fixturecheck isset="module_version"/&gt;
53  * &lt;fixturecheck isnotwhitespace="ANTPATTERN"/&gt;
54  * &lt;fixturecheck antIs="1.6+"/&gt;
55  *
56  * -OR-
57  *
58  * &lt;rule id="required.subbuild.fixture"&gt;
59  * &lt;fixturecheck isset="ANTPATTERN"&gt;
60  * &lt;require msgid="err.misin.filters"&gt;
61  * &lt;isreference name="copyfilters.sources" class="${ANTPATTERN}"/&gt;
62  * &lt;isreference name="copyfilters.manifests" class="${ANTPATTERN}"/&gt;
63  * &lt;/require&gt;
64  * &lt;/rule&gt;
65  * </pre>
66  *
67  * @since JWare/AntX 0.3
68  * @author ssmc, &copy;2003-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
69  * @version 0.5
70  * @.safety single
71  * @.group api,helper
72  * @see AssertTask
73  **/

74
75 public final class VerifyFixture extends BooleanRule
76 {
77     /**
78      * Initializes a new standalone VerifyFixture instance.
79      **/

80     public VerifyFixture()
81     {
82         super(AntX.rules+"fixturecheck",false);
83     }
84
85
86     /**
87      * Initializes a new standalone custom-labeled
88      * VerifyFixture instance.
89      * @param iam CV-label (non-null)
90      **/

91     public VerifyFixture(String JavaDoc iam)
92     {
93         super(iam,false);
94     }
95
96
97     /**
98      * Initializes a new embedded custom-labeled
99      * VerifyFixture instance.
100      * @param embed <i>true</i> if this is an embedded task
101      * @since JWare/AntX 0.4
102      **/

103     public VerifyFixture(boolean embed)
104     {
105         super(AntX.rules+"fixturecheck",embed);
106     }
107
108
109     /**
110      * Initializes the enclosing project for this task. Also
111      * updates internal project-component helpers.
112      **/

113     public void setProject(Project project)
114     {
115         super.setProject(project);
116         m_shh.setProject(project);
117     }
118
119
120     /**
121      * Any short-hand condition setup triggers an immediate
122      * evaluation.
123      **/

124     private void evalIfEmbedded()
125     {
126         if (isEmbedded()) {
127             eval();
128         }
129     }
130
131 // ---------------------------------------------------------------------------------------
132
// Parameters:
133
// ---------------------------------------------------------------------------------------
134

135     /**
136      * Sets an inlined message to be displayed if check fails.
137      * Diagnostics aid.
138      **/

139     public final void setMsg(String JavaDoc msg)
140     {
141         if (!Tk.isWhitespace(msg)) {
142             m_defaultMsg = uistrs().get("brul.assert.failed.lead",msg);
143         } else {
144             m_defaultMsg = msg;
145         }
146     }
147
148
149     /**
150      * Returns this task's inlined default message. Returns
151      * <i>null</i> if never set.
152      */

153     public final String JavaDoc getDefaultMsg()
154     {
155         return m_defaultMsg;
156     }
157
158
159     /**
160      * Will verify the named property is defined in fixture.
161      * @param property the property to check (non-null)
162      **/

163     public void setIsSet(String JavaDoc property)
164     {
165         m_shh.setIsSet(property);
166         m_shh.setMalformed(Handling.REJECT);
167         evalIfEmbedded();
168     }
169
170
171     /**
172      * Will verify the named property is defined to a positive
173      * boolean string in fixture. Synonyms like "on" are allowed.
174      * @param property the property to check (non-null)
175      **/

176     public void setIsSetTrue(String JavaDoc property)
177     {
178         m_shh.setIsSetTrue(property);
179         evalIfEmbedded();
180     }
181
182
183     /**
184      * Will verify the named property is not defined in fixture.
185      * @param property the property to check (non-null)
186      **/

187     public void setIsNotSet(String JavaDoc property)
188     {
189         m_shh.setIsNotSet(property);
190         evalIfEmbedded();
191     }
192
193     /**
194      * Will verify the named variable is defined in fixture.
195      * @param variable the variable to check (non-null)
196      **/

197     public void setVarSet(String JavaDoc variable)
198     {
199         m_shh.setVarSet(variable);
200         evalIfEmbedded();
201     }
202
203
204     /**
205      * Will verify the named variable is defined to a positive
206      * boolean string in fixture. Synonyms like "yes" are allowed.
207      * @param variable the variable to check (non-null)
208      **/

209     public void setVarSetTrue(String JavaDoc variable)
210     {
211         m_shh.setVarSetTrue(variable);
212         evalIfEmbedded();
213     }
214
215
216     /**
217      * Will verify the named variable is not defined in fixture.
218      * @param variable the variable to check (non-null)
219      **/

220     public void setVarNotSet(String JavaDoc variable)
221     {
222         m_shh.setVarNotSet(variable);
223         evalIfEmbedded();
224     }
225
226
227     /**
228      * Will verify the named reference is defined in fixture.
229      * @param refid the reference to check (non-null)
230      **/

231     public void setIsRef(String JavaDoc refid)
232     {
233         m_shh.setIsRef(refid);
234         evalIfEmbedded();
235     }
236
237
238     /**
239      * Will verify the named reference is not defined in fixture.
240      * @param refid the reference to check (non-null)
241      * @since JWare/AntX 0.5
242      **/

243     public void setIsNotRef(String JavaDoc refid)
244     {
245         m_shh.setIsNotRef(refid);
246         evalIfEmbedded();
247     }
248
249
250     /**
251      * Will verify that at least one of the named properties
252      * exists in the fixture.
253      * @param properties the comma-delimited list of property
254      * names (non-null)
255      **/

256     public void setAnySet(String JavaDoc properties)
257     {
258         m_shh.setAnySet(properties);
259         m_shh.setMalformed(Handling.REJECT);
260         evalIfEmbedded();
261     }
262
263
264     /**
265      * Will verify that all of the named properties exist in
266      * the fixture.
267      * @param properties the comma-delimited list of property
268      * names (non-null)
269      **/

270     public void setAllSet(String JavaDoc properties)
271     {
272         m_shh.setAllSet(properties);
273         m_shh.setMalformed(Handling.REJECT);
274         evalIfEmbedded();
275     }
276
277
278     /**
279      * Will verify that all of the matching properties exist
280      * and are resolvable in the fixture.
281      * @param pattern the filtering pattern for all script
282      * properties
283      * @since JWare/AntX 0.4
284      **/

285     public void setAllSetLike(String JavaDoc pattern)
286     {
287         m_shh.setAllSetLike(pattern);
288         evalIfEmbedded();
289     }
290
291
292     /**
293      * Will verify that none of the named properties exist in
294      * the fixture.
295      * @param properties the comma-delimited list of property
296      * names (non-null)
297      **/

298     public void setNoneSet(String JavaDoc properties)
299     {
300         m_shh.setNoneSet(properties);
301         evalIfEmbedded();
302     }
303
304
305     /**
306      * Will verify that the named property is defined as a
307      * boolean string in fixture. Both positive and negative boolean
308      * strings allowed (including synonyms like "on" and "no").
309      * @param property the property to check (non-null)
310      **/

311     public void setIsBoolean(String JavaDoc property)
312     {
313         m_shh.setIsBoolean(property);
314         m_shh.setIsA(IsA.PROPERTY);
315         evalIfEmbedded();
316     }
317
318
319     /**
320      * Will verify that the named property is defined as a
321      * parseable integral value (short, int, long) in fixture. Both
322      * positive and negative values allowed.
323      * @param property the property to check (non-null)
324      **/

325     public void setIsNumeric(String JavaDoc property)
326     {
327         m_shh.setIsNumeric(property);
328         m_shh.setIsA(IsA.PROPERTY);
329         evalIfEmbedded();
330     }
331
332
333     /**
334      * Will verify that the named property is defined to non-
335      * whitespace characters. Failed property substitution is
336      * automatically checked.
337      * @param property the property to check (non-null)
338      **/

339     public void setIsNotWhitespace(String JavaDoc property)
340     {
341         m_shh.setIsNotWhitespace(property);
342         m_shh.setIsA(IsA.PROPERTY);
343         m_shh.setMalformed(Handling.REJECT);
344         evalIfEmbedded();
345     }
346
347 // ---------------------------------------------------------------------------------------
348

349     /**
350      * Defines short hand &lt;antversion is="&#8230;"/&gt; condition.
351      * @since JWare/AntX 0.4
352      **/

353     public void setAntIs(String JavaDoc version)
354     {
355         m_shh.setAntIs(version);
356         evalIfEmbedded();
357     }
358
359
360     /**
361      * Defines short hand &lt;antversion pattern="&#8230;"/&gt;
362      * condition.
363      * @since JWare/AntX 0.4
364      **/

365     public void setAntLike(String JavaDoc pattern)
366     {
367         m_shh.setAntLike(pattern);
368         evalIfEmbedded();
369     }
370
371
372
373     /**
374      * Defines short hand check for specific os configuration.
375      * @since JWare/AntX 0.4
376      **/

377     public void setOS(String JavaDoc selector)
378     {
379         m_shh.setOS(selector);
380         evalIfEmbedded();
381     }
382
383
384 // ---------------------------------------------------------------------------------------
385
// Rule Evaluation:
386
// ---------------------------------------------------------------------------------------
387

388     /**
389      * Evaluates this fixture verification. If verification fails,
390      * this evaluation throws a build exception immediately so
391      * never returns <i>false</i>.
392      * @throws BuildAssertionException if assertion <i>false</i>
393      **/

394     public boolean eval() throws BuildException
395     {
396         verifyInProject_("eval");
397
398         Condition c = getRootCondition();
399
400         if (c==null) {
401             String JavaDoc msgid= isEmpty()
402                 ? "brul.err.atleast.one.condition"
403                 : "brul.err.only.one.condition";
404             String JavaDoc ermsg = uistrs().get(msgid);
405             log(ermsg,Project.MSG_ERR);
406             throw new BuildException(ermsg, getLocation());
407         }
408
409         boolean istrue = c.eval();
410         setEvalResult(istrue,getConditionNames(1));
411         return istrue;
412     }
413
414
415     /**
416      * Called to record this verification's evaluation results. For
417      * fixture verifications a <i>false</i> result is very bad.
418      *<i>true</i> results are basically ignored.
419      **/

420     protected void setEvalResult(boolean istrue, final String JavaDoc listing)
421         throws BuildException
422     {
423         if (!istrue) {
424             String JavaDoc error;
425
426             if (getMsgId()==null) {
427                 String JavaDoc inlined = getDefaultMsg();
428                 if (inlined==null) {
429                     error = uistrs().get("brul.assert.failed",listing);
430                 } else {
431                     error = MessageFormat.format(inlined,new Object JavaDoc[]{listing});
432                 }
433             } else {
434                 error = getMsg(newMsgGetter(listing));
435             }
436
437             log(error, Project.MSG_ERR);
438             throw new BuildAssertionException(error, getLocation());
439
440         }
441     }
442
443
444     /**
445      * No-op.
446      **/

447     public void verifyNoCircularDependency(Stack JavaDoc stk, Requester clnt)
448     {
449     }
450
451 // ---------------------------------------------------------------------------------------
452

453     private String JavaDoc m_defaultMsg;
454     private final ShortHandHelper m_shh = new ShortHandHelper(this);
455 }
456
457 /* end-of-VerifyFixture.java */
458
Popular Tags