Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » task » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.systest.task;
   18   
   19   import org.apache.tools.ant.BuildException;
   20   import org.apache.tools.ant.DirectoryScanner;
   21   import org.apache.tools.ant.taskdefs.MatchingTask;
   22   import org.apache.tools.ant.types.FileSet;
   23   import org.apache.tools.ant.types.Path;
   24   import org.apache.tools.ant.types.Reference;
   25   import org.codehaus.jam.JClass;
   26   import org.codehaus.jam.JamService;
   27   import org.codehaus.jam.JamServiceFactory;
   28   import org.codehaus.jam.JamServiceParams;
   29   
   30   import java.io.File;
   31   import java.io.IOException;
   32   import java.io.InputStream;
   33   import java.util.ArrayList;
   34   import java.util.Iterator;
   35   import java.util.List;
   36   import java.util.Map;
   37   import java.util.Properties;
   38   import java.util.StringTokenizer;
   39   
   40   /**
   41    * An Ant task for executing Gram scripts, which are Groovy scripts executed on
   42    * the JAM context.
   43    * 
   44    * @version $Revision: 1.2 $
   45    */
   46   public class SystemTestTask extends MatchingTask {
   47   
   48       private static final String SCENARIOS_PROPERTIES_FILE = "activemq-scenarios.properties";
   49   
   50       private Path srcDir = null;
   51       private Path mToolpath = null;
   52       private Path mClasspath = null;
   53       private String mIncludes = "**/*.java";
   54       private File destDir;
   55       private FileSet clientFiles;
   56       private FileSet brokerFiles;
   57       private File scenariosFile;
   58   
   59       public File getScenariosFile() {
   60           return scenariosFile;
   61       }
   62   
   63       public void setScenariosFile(File scenariosFile) {
   64           this.scenariosFile = scenariosFile;
   65       }
   66   
   67       /**
   68        * Sets the directory into which source files should be generated.
   69        */
   70       public void setDestDir(File destDir) {
   71           this.destDir = destDir;
   72       }
   73   
   74       public void setSrcDir(Path srcDir) {
   75           this.srcDir = srcDir;
   76       }
   77   
   78       public void setToolpath(Path path) {
   79           if (mToolpath == null) {
   80               mToolpath = path;
   81           }
   82           else {
   83               mToolpath.append(path);
   84           }
   85       }
   86   
   87       public void setToolpathRef(Reference r) {
   88           createToolpath().setRefid(r);
   89       }
   90   
   91       public FileSet createBrokerFiles() {
   92           return new FileSet();
   93       }
   94   
   95       public FileSet getBrokerFiles() {
   96           return brokerFiles;
   97       }
   98   
   99       public void setBrokerFiles(FileSet brokerFiles) {
  100           this.brokerFiles = brokerFiles;
  101       }
  102   
  103       public FileSet createClientFiles() {
  104           return new FileSet();
  105       }
  106   
  107       public FileSet getClientFiles() {
  108           return clientFiles;
  109       }
  110   
  111       public void setClientFiles(FileSet clientFiles) {
  112           this.clientFiles = clientFiles;
  113       }
  114   
  115       public Path createToolpath() {
  116           if (mToolpath == null) {
  117               mToolpath = new Path(getProject());
  118           }
  119           return mToolpath.createPath();
  120       }
  121   
  122       public void setClasspath(Path path) {
  123           if (mClasspath == null) {
  124               mClasspath = path;
  125           }
  126           else {
  127               mClasspath.append(path);
  128           }
  129       }
  130   
  131       public void setClasspathRef(Reference r) {
  132           createClasspath().setRefid(r);
  133       }
  134   
  135       public Path createClasspath() {
  136           if (mClasspath == null) {
  137               mClasspath = new Path(getProject());
  138           }
  139           return mClasspath.createPath();
  140       }
  141   
  142       public void execute() throws BuildException {
  143           /*
  144            * if (srcDir == null) { throw new BuildException("'srcDir' must be
  145            * specified"); }
  146            */
  147           if (scenariosFile == null) {
  148               throw new BuildException("'scenariosFile' must be specified");
  149           }
  150           if (destDir == null) {
  151               throw new BuildException("'destDir' must be specified");
  152           }
  153           if (clientFiles == null) {
  154               throw new BuildException("'clientFiles' must be specified");
  155           }
  156           if (brokerFiles == null) {
  157               throw new BuildException("'clientFiles' must be specified");
  158           }
  159           JamServiceFactory jamServiceFactory = JamServiceFactory.getInstance();
  160           JamServiceParams serviceParams = jamServiceFactory.createServiceParams();
  161           if (mToolpath != null) {
  162               File[] tcp = path2files(mToolpath);
  163               for (int i = 0; i < tcp.length; i++) {
  164                   serviceParams.addToolClasspath(tcp[i]);
  165               }
  166           }
  167           if (mClasspath != null) {
  168               File[] cp = path2files(mClasspath);
  169               for (int i = 0; i < cp.length; i++) {
  170                   serviceParams.addClasspath(cp[i]);
  171               }
  172           }
  173   
  174           JClass[] classes = null;
  175           File propertiesFile = scenariosFile;
  176           try {
  177               if (srcDir != null) {
  178                   serviceParams.includeSourcePattern(path2files(srcDir), mIncludes);
  179                   JamService jam = jamServiceFactory.createService(serviceParams);
  180                   classes = jam.getAllClasses();
  181               }
  182               else {
  183                   // lets try load the properties file
  184                   classes = loadScenarioClasses();
  185                   propertiesFile = null;
  186               }
  187               DirectoryScanner clientsScanner = clientFiles.getDirectoryScanner(getProject());
  188               DirectoryScanner brokersScanner = brokerFiles.getDirectoryScanner(getProject());
  189               SystemTestGenerator generator = new SystemTestGenerator(classes, destDir, clientsScanner, brokersScanner, getProject().getBaseDir(), propertiesFile);
  190               generator.generate();
  191   
  192               log("...done.");
  193           }
  194           catch (Exception e) {
  195               throw new BuildException(e);
  196           }
  197       }
  198   
  199       protected JClass[] loadScenarioClasses() throws IOException {
  200           InputStream in = getClass().getClassLoader().getResourceAsStream(SCENARIOS_PROPERTIES_FILE);
  201           if (in == null) {
  202               ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
  203               if (contextClassLoader != null) {
  204                   in = contextClassLoader.getResourceAsStream(SCENARIOS_PROPERTIES_FILE);
  205               }
  206               if (in == null) {
  207                   throw new IOException("Could not find ActiveMQ scenarios properties file on the classpath: " + SCENARIOS_PROPERTIES_FILE);
  208               }
  209           }
  210           Properties properties = new Properties();
  211           properties.load(in);
  212           List list = new ArrayList();
  213           for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
  214               Map.Entry entry = (Map.Entry) iter.next();
  215               String className = (String) entry.getKey();
  216               String names = (String) entry.getValue();
  217               String[] interfaceNameArray = parseNames(names);
  218               list.add(new ScenarioJClassStub(className, interfaceNameArray));
  219           }
  220           JClass[] answer = new JClass[list.size()];
  221           list.toArray(answer);
  222           return answer;
  223       }
  224   
  225       protected String[] parseNames(String names) {
  226           StringTokenizer iter = new StringTokenizer(names);
  227           List list = new ArrayList();
  228           while (iter.hasMoreTokens()) {
  229               String text = iter.nextToken().trim();
  230               if (text.length() > 0) {
  231                   list.add(text);
  232               }
  233           }
  234           String[] answer = new String[list.size()];
  235           list.toArray(answer);
  236           return answer;
  237       }
  238   
  239       protected File[] path2files(Path path) {
  240           String[] list = path.list();
  241           File[] out = new File[list.length];
  242           for (int i = 0; i < out.length; i++) {
  243               out[i] = new File(list[i]).getAbsoluteFile();
  244           }
  245           return out;
  246       }
  247   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » task » [javadoc | source]