1 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 ; 29 import java.io.File ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.PrintWriter ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 42 public class SystemTestGenerator { 43 44 private JClass[] classes; 45 private final File destDir; 46 private final DirectoryScanner clientsScanner; 47 private final DirectoryScanner brokersScanner; 48 private final File baseDir; 49 private final File scenariosFile; 50 private String licenseHeader; 51 52 public SystemTestGenerator(JClass[] classes, File destDir, DirectoryScanner clientsScanner, DirectoryScanner brokersScanner, File baseDir, 53 File 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 { 63 List list = new ArrayList (); 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 if (scenariosFile != null) { 74 generatePropertiesFile(list); 75 } 76 } 77 78 protected void generatePropertiesFile(List list) throws IOException { 79 PrintWriter writer = new PrintWriter (new FileWriter (scenariosFile)); 80 try { 81 for (Iterator 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 writer, JClass type) { 95 List interfaces = new ArrayList (); 96 addAllInterfaces(interfaces, type); 97 boolean first = true; 98 for (Iterator 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 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 { 123 String [] files = clientsScanner.getIncludedFiles(); 124 for (int i = 0; i < files.length; i++) { 125 String name = files[i]; 126 File file = new File (clientsScanner.getBasedir(), name); 127 generateTestsFor(type, name, file); 128 } 129 } 130 131 protected void generateTestsFor(JClass type, String clientName, File clientFile) throws IOException { 132 String [] files = brokersScanner.getIncludedFiles(); 133 for (int i = 0; i < files.length; i++) { 134 String name = files[i]; 135 File basedir = brokersScanner.getBasedir(); 136 File file = new File (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 clientName, File clientFile, String brokerName, File brokerFile, int destinationType) 148 throws IOException { 149 String clientPrefix = trimPostFix(clientName); 150 String brokerPrefix = trimPostFix(brokerName); 151 152 String destinationName = ScenarioTestSuite.destinationDescription(destinationType); 153 String [] paths = { "org", "activemq", "systest", brokerPrefix, destinationName, clientPrefix }; 154 String packageName = asPackageName(paths); 155 156 File dir = makeDirectories(paths); 157 File file = new File (dir, type.getSimpleName() + "Test.java"); 158 PrintWriter writer = new PrintWriter (new FileWriter (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 writer, JClass type, File clientFile, File brokerFile, String packageName, int destinationType) throws IOException { 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 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 relativePath(File file) { 212 String name = file.toString(); 213 String prefix = baseDir.toString(); 214 if (name.startsWith(prefix)) { 215 return name.substring(prefix.length() + 1); 216 } 217 return name; 218 } 219 220 protected File makeDirectories(String [] paths) { 221 File dir = destDir; 222 for (int i = 0; i < paths.length; i++) { 223 dir = new File (dir, paths[i]); 224 } 225 dir.mkdirs(); 226 return dir; 227 } 228 229 protected String asPackageName(String [] paths) { 230 StringBuffer buffer = new StringBuffer (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 trimPostFix(String 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 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 getLicenseHeader() throws IOException { 264 if( licenseHeader == null ) { 265 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 267 InputStream 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 (baos.toByteArray(),"UTF-8"); 278 } 279 return licenseHeader; 280 } 281 282 public void setLicenseHeader(String licenseHeader) { 283 this.licenseHeader = licenseHeader; 284 } 285 286 } 287 | Popular Tags |