KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > task > SystemTestTask


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. 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 package org.apache.activemq.systest.task;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.DirectoryScanner;
22 import org.apache.tools.ant.taskdefs.MatchingTask;
23 import org.apache.tools.ant.types.FileSet;
24 import org.apache.tools.ant.types.Path;
25 import org.apache.tools.ant.types.Reference;
26 import org.codehaus.jam.JClass;
27 import org.codehaus.jam.JamService;
28 import org.codehaus.jam.JamServiceFactory;
29 import org.codehaus.jam.JamServiceParams;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 /**
42  * An Ant task for executing Gram scripts, which are Groovy scripts executed on
43  * the JAM context.
44  *
45  * @version $Revision: 1.2 $
46  */

47 public class SystemTestTask extends MatchingTask {
48
49     private static final String JavaDoc SCENARIOS_PROPERTIES_FILE = "activemq-scenarios.properties";
50
51     private Path srcDir = null;
52     private Path mToolpath = null;
53     private Path mClasspath = null;
54     private String JavaDoc mIncludes = "**/*.java";
55     private File JavaDoc destDir;
56     private FileSet clientFiles;
57     private FileSet brokerFiles;
58     private File JavaDoc scenariosFile;
59
60     public File JavaDoc getScenariosFile() {
61         return scenariosFile;
62     }
63
64     public void setScenariosFile(File JavaDoc scenariosFile) {
65         this.scenariosFile = scenariosFile;
66     }
67
68     /**
69      * Sets the directory into which source files should be generated.
70      */

71     public void setDestDir(File JavaDoc destDir) {
72         this.destDir = destDir;
73     }
74
75     public void setSrcDir(Path srcDir) {
76         this.srcDir = srcDir;
77     }
78
79     public void setToolpath(Path path) {
80         if (mToolpath == null) {
81             mToolpath = path;
82         }
83         else {
84             mToolpath.append(path);
85         }
86     }
87
88     public void setToolpathRef(Reference r) {
89         createToolpath().setRefid(r);
90     }
91
92     public FileSet createBrokerFiles() {
93         return new FileSet();
94     }
95
96     public FileSet getBrokerFiles() {
97         return brokerFiles;
98     }
99
100     public void setBrokerFiles(FileSet brokerFiles) {
101         this.brokerFiles = brokerFiles;
102     }
103
104     public FileSet createClientFiles() {
105         return new FileSet();
106     }
107
108     public FileSet getClientFiles() {
109         return clientFiles;
110     }
111
112     public void setClientFiles(FileSet clientFiles) {
113         this.clientFiles = clientFiles;
114     }
115
116     public Path createToolpath() {
117         if (mToolpath == null) {
118             mToolpath = new Path(getProject());
119         }
120         return mToolpath.createPath();
121     }
122
123     public void setClasspath(Path path) {
124         if (mClasspath == null) {
125             mClasspath = path;
126         }
127         else {
128             mClasspath.append(path);
129         }
130     }
131
132     public void setClasspathRef(Reference r) {
133         createClasspath().setRefid(r);
134     }
135
136     public Path createClasspath() {
137         if (mClasspath == null) {
138             mClasspath = new Path(getProject());
139         }
140         return mClasspath.createPath();
141     }
142
143     public void execute() throws BuildException {
144         /*
145          * if (srcDir == null) { throw new BuildException("'srcDir' must be
146          * specified"); }
147          */

148         if (scenariosFile == null) {
149             throw new BuildException("'scenariosFile' must be specified");
150         }
151         if (destDir == null) {
152             throw new BuildException("'destDir' must be specified");
153         }
154         if (clientFiles == null) {
155             throw new BuildException("'clientFiles' must be specified");
156         }
157         if (brokerFiles == null) {
158             throw new BuildException("'clientFiles' must be specified");
159         }
160         JamServiceFactory jamServiceFactory = JamServiceFactory.getInstance();
161         JamServiceParams serviceParams = jamServiceFactory.createServiceParams();
162         if (mToolpath != null) {
163             File JavaDoc[] tcp = path2files(mToolpath);
164             for (int i = 0; i < tcp.length; i++) {
165                 serviceParams.addToolClasspath(tcp[i]);
166             }
167         }
168         if (mClasspath != null) {
169             File JavaDoc[] cp = path2files(mClasspath);
170             for (int i = 0; i < cp.length; i++) {
171                 serviceParams.addClasspath(cp[i]);
172             }
173         }
174
175         JClass[] classes = null;
176         File JavaDoc propertiesFile = scenariosFile;
177         try {
178             if (srcDir != null) {
179                 serviceParams.includeSourcePattern(path2files(srcDir), mIncludes);
180                 JamService jam = jamServiceFactory.createService(serviceParams);
181                 classes = jam.getAllClasses();
182             }
183             else {
184                 // lets try load the properties file
185
classes = loadScenarioClasses();
186                 propertiesFile = null;
187             }
188             DirectoryScanner clientsScanner = clientFiles.getDirectoryScanner(getProject());
189             DirectoryScanner brokersScanner = brokerFiles.getDirectoryScanner(getProject());
190             SystemTestGenerator generator = new SystemTestGenerator(classes, destDir, clientsScanner, brokersScanner, getProject().getBaseDir(), propertiesFile);
191             generator.generate();
192
193             log("...done.");
194         }
195         catch (Exception JavaDoc e) {
196             throw new BuildException(e);
197         }
198     }
199
200     protected JClass[] loadScenarioClasses() throws IOException JavaDoc {
201         InputStream JavaDoc in = getClass().getClassLoader().getResourceAsStream(SCENARIOS_PROPERTIES_FILE);
202         if (in == null) {
203             ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
204             if (contextClassLoader != null) {
205                 in = contextClassLoader.getResourceAsStream(SCENARIOS_PROPERTIES_FILE);
206             }
207             if (in == null) {
208                 throw new IOException JavaDoc("Could not find ActiveMQ scenarios properties file on the classpath: " + SCENARIOS_PROPERTIES_FILE);
209             }
210         }
211         Properties JavaDoc properties = new Properties JavaDoc();
212         properties.load(in);
213         List JavaDoc list = new ArrayList JavaDoc();
214         for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
215             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
216             String JavaDoc className = (String JavaDoc) entry.getKey();
217             String JavaDoc names = (String JavaDoc) entry.getValue();
218             String JavaDoc[] interfaceNameArray = parseNames(names);
219             list.add(new ScenarioJClassStub(className, interfaceNameArray));
220         }
221         JClass[] answer = new JClass[list.size()];
222         list.toArray(answer);
223         return answer;
224     }
225
226     protected String JavaDoc[] parseNames(String JavaDoc names) {
227         StringTokenizer JavaDoc iter = new StringTokenizer JavaDoc(names);
228         List JavaDoc list = new ArrayList JavaDoc();
229         while (iter.hasMoreTokens()) {
230             String JavaDoc text = iter.nextToken().trim();
231             if (text.length() > 0) {
232                 list.add(text);
233             }
234         }
235         String JavaDoc[] answer = new String JavaDoc[list.size()];
236         list.toArray(answer);
237         return answer;
238     }
239
240     protected File JavaDoc[] path2files(Path path) {
241         String JavaDoc[] list = path.list();
242         File JavaDoc[] out = new File JavaDoc[list.length];
243         for (int i = 0; i < out.length; i++) {
244             out[i] = new File JavaDoc(list[i]).getAbsoluteFile();
245         }
246         return out;
247     }
248 }
249
Popular Tags