KickJava   Java API By Example, From Geeks To Geeks.

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


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.activemq.systest.DestinationFactory;
21 import org.apache.activemq.systest.QueueOnlyScenario;
22 import org.apache.activemq.systest.Scenario;
23 import org.apache.activemq.systest.ScenarioTestSuite;
24 import org.apache.activemq.systest.TopicOnlyScenario;
25 import org.apache.tools.ant.DirectoryScanner;
26 import org.codehaus.jam.JClass;
27
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  *
40  * @version $Revision: 1.1 $
41  */

42 public class SystemTestGenerator {
43
44     private JClass[] classes;
45     private final File JavaDoc destDir;
46     private final DirectoryScanner clientsScanner;
47     private final DirectoryScanner brokersScanner;
48     private final File JavaDoc baseDir;
49     private final File JavaDoc scenariosFile;
50     private String JavaDoc licenseHeader;
51     
52     public SystemTestGenerator(JClass[] classes, File JavaDoc destDir, DirectoryScanner clientsScanner, DirectoryScanner brokersScanner, File JavaDoc baseDir,
53             File JavaDoc scenariosFile) {
54         this.classes = classes;
55         this.destDir = destDir;
56         this.clientsScanner = clientsScanner;
57         this.brokersScanner = brokersScanner;
58         this.baseDir = baseDir;
59         this.scenariosFile = scenariosFile;
60     }
61
62     public void generate() throws IOException JavaDoc {
63         List JavaDoc list = new ArrayList JavaDoc();
64         for (int i = 0; i < classes.length; i++) {
65             JClass type = classes[i];
66             if (implementsInterface(type, Scenario.class) && !type.isAbstract() && !type.isInterface()) {
67                 generateTestsFor(type);
68                 list.add(type);
69             }
70         }
71
72         // now lets generate a list of all the available
73
if (scenariosFile != null) {
74             generatePropertiesFile(list);
75         }
76     }
77
78     protected void generatePropertiesFile(List JavaDoc list) throws IOException JavaDoc {
79         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(scenariosFile));
80         try {
81             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
82                 JClass type = (JClass) iter.next();
83                 writer.print(type.getQualifiedName());
84                 writer.print(" = ");
85                 writeInterfaces(writer, type);
86                 writer.println();
87             }
88         }
89         finally {
90             writer.close();
91         }
92     }
93
94     protected void writeInterfaces(PrintWriter JavaDoc writer, JClass type) {
95         List JavaDoc interfaces = new ArrayList JavaDoc();
96         addAllInterfaces(interfaces, type);
97         boolean first = true;
98         for (Iterator JavaDoc iter = interfaces.iterator(); iter.hasNext();) {
99             JClass interfaceType = (JClass) iter.next();
100             if (first) {
101                 first = false;
102             }
103             else {
104                 writer.print(", ");
105             }
106             writer.print(interfaceType.getQualifiedName());
107         }
108     }
109
110     protected void addAllInterfaces(List JavaDoc list, JClass type) {
111         JClass[] interfaces = type.getInterfaces();
112         for (int i = 0; i < interfaces.length; i++) {
113             JClass interfaceType = interfaces[i];
114             list.add(interfaceType);
115         }
116         JClass superclass = type.getSuperclass();
117         if (superclass != null) {
118             addAllInterfaces(list, superclass);
119         }
120     }
121
122     protected void generateTestsFor(JClass type) throws IOException JavaDoc {
123         String JavaDoc[] files = clientsScanner.getIncludedFiles();
124         for (int i = 0; i < files.length; i++) {
125             String JavaDoc name = files[i];
126             File JavaDoc file = new File JavaDoc(clientsScanner.getBasedir(), name);
127             generateTestsFor(type, name, file);
128         }
129     }
130
131     protected void generateTestsFor(JClass type, String JavaDoc clientName, File JavaDoc clientFile) throws IOException JavaDoc {
132         String JavaDoc[] files = brokersScanner.getIncludedFiles();
133         for (int i = 0; i < files.length; i++) {
134             String JavaDoc name = files[i];
135             File JavaDoc basedir = brokersScanner.getBasedir();
136             File JavaDoc file = new File JavaDoc(basedir, name);
137
138             if (!implementsInterface(type, TopicOnlyScenario.class)) {
139                 generateTestsFor(type, clientName, clientFile, name, file, DestinationFactory.QUEUE);
140             }
141             if (!implementsInterface(type, QueueOnlyScenario.class)) {
142                 generateTestsFor(type, clientName, clientFile, name, file, DestinationFactory.TOPIC);
143             }
144         }
145     }
146
147     protected void generateTestsFor(JClass type, String JavaDoc clientName, File JavaDoc clientFile, String JavaDoc brokerName, File JavaDoc brokerFile, int destinationType)
148             throws IOException JavaDoc {
149         String JavaDoc clientPrefix = trimPostFix(clientName);
150         String JavaDoc brokerPrefix = trimPostFix(brokerName);
151
152         String JavaDoc destinationName = ScenarioTestSuite.destinationDescription(destinationType);
153         String JavaDoc[] paths = { "org", "activemq", "systest", brokerPrefix, destinationName, clientPrefix };
154         String JavaDoc packageName = asPackageName(paths);
155
156         File JavaDoc dir = makeDirectories(paths);
157         File JavaDoc file = new File JavaDoc(dir, type.getSimpleName() + "Test.java");
158         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
159         try {
160             System.out.println("Generating: " + file);
161             generateFile(writer, type, clientFile, brokerFile, packageName, destinationType);
162         }
163         finally {
164             writer.close();
165         }
166     }
167
168     protected void generateFile(PrintWriter JavaDoc writer, JClass type, File JavaDoc clientFile, File JavaDoc brokerFile, String JavaDoc packageName, int destinationType) throws IOException JavaDoc {
169         writer.println(getLicenseHeader());
170         writer.println("package " + packageName + ";");
171         writer.println();
172         writer.println("import org.apache.activemq.systest.DestinationFactory;");
173         writer.println("import org.apache.activemq.systest.ScenarioTestSuite;");
174         writer.println("import " + type.getQualifiedName() + ";");
175         writer.println("import org.springframework.context.ApplicationContext;");
176         writer.println("import org.springframework.context.support.FileSystemXmlApplicationContext;");
177         writer.println();
178         writer.println("import junit.framework.TestSuite;");
179         writer.println();
180         writer.println("/**");
181         writer.println(" * System test case for Scenario: " + type.getSimpleName());
182         writer.println(" *");
183         writer.println(" *");
184         writer.println(" * NOTE!: This file is auto generated - do not modify!");
185         writer.println(" * if you need to make a change, please see SystemTestGenerator code");
186         writer.println(" * in the activemq-systest module in ActiveMQ 4.x");
187         writer.println(" *");
188         writer.println(" * @version $Revision:$");
189         writer.println(" */");
190         writer.println("public class " + type.getSimpleName() + "Test extends ScenarioTestSuite {");
191         writer.println();
192         writer.println(" public static TestSuite suite() throws Exception {");
193         writer.println(" ApplicationContext clientContext = new FileSystemXmlApplicationContext(\"" + relativePath(clientFile) + "\");");
194         writer.println(" ApplicationContext brokerContext = new FileSystemXmlApplicationContext(\"" + relativePath(brokerFile) + "\");");
195         writer.println(" Class[] scenarios = { " + type.getSimpleName() + ".class };");
196         writer.println(" return createSuite(clientContext, brokerContext, scenarios, " + destinationExpression(destinationType) + ");");
197         writer.println(" }");
198         writer.println("}");
199     }
200
201     protected String JavaDoc destinationExpression(int destinationType) {
202         switch (destinationType) {
203         case DestinationFactory.QUEUE:
204             return "DestinationFactory.QUEUE";
205
206         default:
207             return "DestinationFactory.TOPIC";
208         }
209     }
210
211     protected String JavaDoc relativePath(File JavaDoc file) {
212         String JavaDoc name = file.toString();
213         String JavaDoc prefix = baseDir.toString();
214         if (name.startsWith(prefix)) {
215             return name.substring(prefix.length() + 1);
216         }
217         return name;
218     }
219
220     protected File JavaDoc makeDirectories(String JavaDoc[] paths) {
221         File JavaDoc dir = destDir;
222         for (int i = 0; i < paths.length; i++) {
223             dir = new File JavaDoc(dir, paths[i]);
224         }
225         dir.mkdirs();
226         return dir;
227     }
228
229     protected String JavaDoc asPackageName(String JavaDoc[] paths) {
230         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(paths[0]);
231         for (int i = 1; i < paths.length; i++) {
232             buffer.append(".");
233             buffer.append(paths[i]);
234         }
235         return buffer.toString();
236     }
237
238     protected String JavaDoc trimPostFix(String JavaDoc uri) {
239         int idx = uri.lastIndexOf('.');
240         if (idx > 0) {
241             return uri.substring(0, idx);
242         }
243         return uri;
244     }
245
246     protected boolean implementsInterface(JClass type, Class JavaDoc interfaceClass) {
247         JClass[] interfaces = type.getInterfaces();
248         for (int i = 0; i < interfaces.length; i++) {
249             JClass anInterface = interfaces[i];
250             if (anInterface.getQualifiedName().equals(interfaceClass.getName())) {
251                 return true;
252             }
253         }
254         JClass superclass = type.getSuperclass();
255         if (superclass == null || superclass == type) {
256             return false;
257         }
258         else {
259             return implementsInterface(superclass, interfaceClass);
260         }
261     }
262
263     public String JavaDoc getLicenseHeader() throws IOException JavaDoc {
264         if( licenseHeader == null ) {
265             // read the LICENSE_HEADER.txt into the licenseHeader variable.
266
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
267             InputStream JavaDoc is = SystemTestGenerator.class.getResourceAsStream("LICENSE_HEADER.txt");
268             try {
269                 int c;
270                 while( (c=is.read())>=0 ) {
271                     baos.write(c);
272                 }
273             } finally {
274                 is.close();
275             }
276             baos.close();
277             licenseHeader = new String JavaDoc(baos.toByteArray(),"UTF-8");
278         }
279         return licenseHeader;
280     }
281
282     public void setLicenseHeader(String JavaDoc licenseHeader) {
283         this.licenseHeader = licenseHeader;
284     }
285
286 }
287
Popular Tags