1 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 ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 41 47 public class SystemTestTask extends MatchingTask { 48 49 private static final String 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 mIncludes = "**/*.java"; 55 private File destDir; 56 private FileSet clientFiles; 57 private FileSet brokerFiles; 58 private File scenariosFile; 59 60 public File getScenariosFile() { 61 return scenariosFile; 62 } 63 64 public void setScenariosFile(File scenariosFile) { 65 this.scenariosFile = scenariosFile; 66 } 67 68 71 public void setDestDir(File 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 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 [] 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 [] 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 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 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 e) { 196 throw new BuildException(e); 197 } 198 } 199 200 protected JClass[] loadScenarioClasses() throws IOException { 201 InputStream in = getClass().getClassLoader().getResourceAsStream(SCENARIOS_PROPERTIES_FILE); 202 if (in == null) { 203 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 204 if (contextClassLoader != null) { 205 in = contextClassLoader.getResourceAsStream(SCENARIOS_PROPERTIES_FILE); 206 } 207 if (in == null) { 208 throw new IOException ("Could not find ActiveMQ scenarios properties file on the classpath: " + SCENARIOS_PROPERTIES_FILE); 209 } 210 } 211 Properties properties = new Properties (); 212 properties.load(in); 213 List list = new ArrayList (); 214 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 215 Map.Entry entry = (Map.Entry ) iter.next(); 216 String className = (String ) entry.getKey(); 217 String names = (String ) entry.getValue(); 218 String [] 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 [] parseNames(String names) { 227 StringTokenizer iter = new StringTokenizer (names); 228 List list = new ArrayList (); 229 while (iter.hasMoreTokens()) { 230 String text = iter.nextToken().trim(); 231 if (text.length() > 0) { 232 list.add(text); 233 } 234 } 235 String [] answer = new String [list.size()]; 236 list.toArray(answer); 237 return answer; 238 } 239 240 protected File [] path2files(Path path) { 241 String [] list = path.list(); 242 File [] out = new File [list.length]; 243 for (int i = 0; i < out.length; i++) { 244 out[i] = new File (list[i]).getAbsoluteFile(); 245 } 246 return out; 247 } 248 } 249 | Popular Tags |