KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > maven > CactusScannerTag


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.maven;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 import org.apache.commons.jelly.JellyTagException;
25 import org.apache.commons.jelly.TagSupport;
26 import org.apache.commons.jelly.XMLOutput;
27 import org.apache.commons.jelly.MissingAttributeException;
28 import org.apache.commons.jelly.tags.ant.TaskSource;
29 import org.apache.commons.jelly.tags.ant.AntTagLibrary;
30 import org.apache.commons.beanutils.BeanUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.tools.ant.types.FileSet;
34 import org.apache.tools.ant.types.Path;
35 import org.apache.tools.ant.types.Reference;
36
37 /**
38  * Cactus Jelly Tag that scans Ant FileSets and return a list of
39  * qualified class name that are Cactus TestCases (i.e.
40  * ServletTestCase, JspTestCase or FilterTestCase) or subclasses
41  * of Cactus TestCases.
42  *
43  * Note: This is useful when used with the <junit> Ant
44  * task for example, in order to find out the list of tests to
45  * execute.
46  *
47  * @version $Id: CactusScannerTag.java,v 1.3 2004/02/29 16:34:44 vmassol Exp $
48  */

49 public class CactusScannerTag extends TagSupport implements TaskSource
50 {
51     /**
52      * Log instance.
53      */

54     private Log log = LogFactory.getLog(CactusScannerTag.class);
55
56     /**
57      * The {@link CactusScanner} object that is exposed by this tag
58      * to the Jelly script.
59      */

60     private CactusScanner cactusScanner;
61
62     /**
63      * We need to save the fileset as its XML attributes are set after the
64      * fileset object is created and thus we need to wait until it is
65      * completely initialized before being able to process it using
66      * {@link CactusScanner#processFileSet}.
67      */

68     private FileSet fileset;
69
70     /**
71      * Nested <classpath> tag values. This is the classpath that will
72      * be used to dynamically load the test classes to decide whether they
73      * are Cactus tests or not.
74      *
75      * Note: There is a bug in Jelly and it does not work yet. ATM you should
76      * use the classpathref attribute instead.
77      */

78     private Path classpath;
79
80     /**
81      * Reference to an Ant {@link Path} object containing the classpath.
82      * @see #classpath
83      */

84     private String JavaDoc classpathref;
85
86     /**
87      * The Jelly variable (exposed to the Jelly script) that will
88      * contain a reference to the {@link CactusScanner} object.
89      */

90     private String JavaDoc var;
91
92     /**
93      * Initializations.
94      */

95     public CactusScannerTag()
96     {
97         this.cactusScanner = new CactusScanner();
98     }
99
100     /**
101      * @see TagSupport#doTag(XMLOutput)
102      */

103     public void doTag(XMLOutput theXmlOutput) throws JellyTagException
104     {
105         this.cactusScanner.setProject(AntTagLibrary.getProject(context));
106         this.cactusScanner.clear();
107
108         // run the body first to configure the task via nested tags
109
invokeBody(theXmlOutput);
110
111         // Process the fileset to extract Cactus test cases. We need to pass
112
// the project dependency classpath as the CactusScanner will need
113
// to load the cactus test classes to decide whether they are Cactus
114
// test case or not and that needs the dependent jars to be in the
115
// classpath.
116
Path cp = this.classpath;
117         if (this.classpathref != null)
118         {
119             cp = (Path) AntTagLibrary.getProject(
120                 context).getReference(this.classpathref);
121         }
122
123         this.cactusScanner.processFileSet(this.fileset, cp);
124
125         // output the cactusScanner
126
if (var == null)
127         {
128             throw new MissingAttributeException("var");
129         }
130         context.setVariable(var, cactusScanner);
131     }
132
133     /**
134      * This method is called internally by Jelly to know on which object to
135      * call the {@link TaskSource#setTaskProperty} method.
136      *
137      * @see TaskSource#getTaskObject()
138      */

139     public Object JavaDoc getTaskObject()
140     {
141         return this;
142     }
143
144     /**
145      * @see TaskSource#setTaskProperty(String, Object)
146      */

147     public void setTaskProperty(String JavaDoc theName, Object JavaDoc theValue)
148         throws JellyTagException
149     {
150         try
151         {
152             BeanUtils.setProperty(this, theName, theValue);
153         }
154         catch (IllegalAccessException JavaDoc anException)
155         {
156             throw new JellyTagException(anException);
157         }
158         catch (InvocationTargetException JavaDoc anException)
159         {
160             throw new JellyTagException(anException);
161         }
162         
163     }
164
165     /**
166      * Adds a set of files (nested fileset attribute). This method is called
167      * dynamically by {@link #setTaskProperty}.
168      *
169      * @param theSet the Ant fileset to add
170      */

171     public void addFileset(FileSet theSet)
172     {
173         log.debug("Adding fileset [" + theSet + "]");
174         this.fileset = theSet;
175     }
176
177     /**
178      * @return a newly created and empty {@link Path} object
179      */

180     public Path createClasspath()
181     {
182         log.debug("Creating classpath");
183         if (this.classpath == null)
184         {
185             this.classpath = new Path(AntTagLibrary.getProject(context));
186         }
187         return this.classpath.createPath();
188     }
189
190     /**
191      * @return the classpath in which we will look for Cactus test cases.
192      */

193     public Path getClasspath()
194     {
195         return this.classpath;
196     }
197
198     /**
199      * Sets the classapth in which we will look for Cactus test cases.
200      * @param theClasspath the classapth to set
201      */

202     public void setClasspath(Path theClasspath)
203     {
204         log.debug("Setting classpath [" + theClasspath + "]");
205         if (this.classpath == null)
206         {
207             this.classpath = theClasspath;
208         }
209         else
210         {
211             this.classpath.append(theClasspath);
212         }
213     }
214
215     /**
216      * Sets the classpath in which we will look for Cactus test cases, using
217      * a reference.
218      *
219      * @param theReference the classpath reference
220      */

221     public void setClasspathRef(Reference theReference)
222     {
223         createClasspath().setRefid(theReference);
224     }
225
226     /**
227      * @return the Cactus scanner object
228      */

229     public CactusScanner getCactusScanner()
230     {
231         return this.cactusScanner;
232     }
233
234     /**
235      * Sets the name of the variable exported by this tag.
236      * @param theVar the variable that will be exported by this tag and which
237      * will contain the list of Cactus test cases
238      */

239     public void setVar(String JavaDoc theVar)
240     {
241         this.var = theVar;
242     }
243
244     /**
245      * Sets the classpath in which we will look for Cactus test cases, using
246      * a String reference.
247      * @param theClasspathref the classpath reference
248      */

249     public void setClasspathref(String JavaDoc theClasspathref)
250     {
251         this.classpathref = theClasspathref;
252     }
253 }
254
Popular Tags