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

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