KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: BatchChecksTaskSet.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.condition.solo;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.Task;
34 import org.apache.tools.ant.taskdefs.condition.Condition;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.apis.AntLibFriendly;
38 import com.idaremedia.antx.apis.BuildAssertionException;
39 import com.idaremedia.antx.helpers.TaskHandle;
40 import com.idaremedia.antx.helpers.Tk;
41 import com.idaremedia.antx.ownhelpers.TaskExaminer;
42 import com.idaremedia.antx.starters.TaskSet;
43
44 /**
45  * Aggregating taskset that always executes each of its nested assertions and/or fixture
46  * checks tasks even if one or more of them fail. Once all checks have been performed,
47  * this taskset will propagate a wrapper assertion exception if at least one check failed.
48  * <p>
49  * <b>Example Usage:</b><pre>
50  * &lt;<b>batchchecks</b> failproperty="..."&gt;
51  * &lt;fixturecheck isset="module_basedir" msgid="..."/&gt;
52  * &lt;assert allset="module_id,module_name" whitespace="reject" msgid="..."&gt;
53  * &lt;/batchchecks&gt;
54  * </pre>
55  *
56  * @since JWare/AntX 0.5
57  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
58  * @version 0.5
59  * @.safety single
60  * @.group api,infra
61  * @see AssertTask
62  * @see VerifyFixture
63  **/

64
65 public final class BatchChecksTaskSet extends TaskSet implements Condition, AntLibFriendly
66 {
67     /**
68      * Initializes a new batch checks taskset.
69      **/

70     public BatchChecksTaskSet()
71     {
72         super(AntX.conditions+"BatchChecks:");
73     }
74
75
76
77     /**
78      * Sets name of the property set if this batchcheck fails.
79      * @param property the property to update (non-null, non-whitespace)
80      **/

81     public final void setFailProperty(String JavaDoc property)
82     {
83         require_(!Tk.isWhitespace(property),"setfailp- nonwspc");
84         m_failProperty = property;
85     }
86
87
88     /**
89      * Returns the property updated if this assertion fails. Returns
90      * default property-name if this attribute never set.
91      **/

92     public final String JavaDoc getFailProperty()
93     {
94         return m_failProperty;
95     }
96
97
98
99     /**
100      * Returns <i>true</i> if candidate task is a boolean rule.
101      * @param taskH the task to be nested (as handle)
102      **/

103     protected boolean includeTask(TaskHandle taskH)
104     {
105         Class JavaDoc c = TaskExaminer.trueClass(taskH.getTask());
106         return c!=null && BooleanRule.class.isAssignableFrom(c);
107     }
108
109
110
111     /**
112      * Runs each nested assertion or fixture check task in order
113      * capturing any thrown signals until all checks have been done
114      * @throws BuildException if any nested check does.
115      **/

116     protected void performNestedTasks() throws BuildException
117     {
118         Task[] tasks = getTasks();
119         int signals = 0;
120         for (int i=0;i<tasks.length;i++) {
121             try {
122                 tasks[i].perform();
123             } catch(RuntimeException JavaDoc rtX) {
124                 signals++;
125                 if (!(rtX instanceof BuildAssertionException)) {//make sure it's seen!
126
log(rtX.getMessage(), Project.MSG_ERR);
127                 }
128             }
129         }
130         if (signals>0) {
131             String JavaDoc what = String.valueOf(signals);
132             String JavaDoc msg = getAntXMsg("brul.batch.asserts.failed",what);
133             if (m_failProperty!=null) {
134                 checkIfProperty_(m_failProperty,true);
135                 getProject().setNewProperty(m_failProperty, what);
136                 log("BatchCheck false; setting failure property "+m_failProperty+
137                     " to "+what, Project.MSG_DEBUG);
138             } else {
139                 log(msg, Project.MSG_WARN);
140             }
141             throw new BuildAssertionException(msg,getLocation());
142         }
143     }
144     
145     
146     /**
147      * Executes this collection of checks. Will always return
148      * <i>true</i> if all assertions pass; otherwise an assertion
149      * exception is always signalled (nothing returned).
150      * @throws BuildException if any nested check does.
151      **/

152     public boolean eval()
153     {
154         execute();
155         return true;
156     }
157
158
159     private String JavaDoc m_failProperty;//NB:null => none!
160
}
161
162 /* end-of-BatchChecks.java */
Popular Tags