View Javadoc

1   package org.apache.maven.abbot;
2   
3   /* ====================================================================
4    *   Copyright 2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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   */
19  
20  import java.io.File;
21  
22  import org.apache.tools.ant.Project;
23  import org.apache.tools.ant.taskdefs.optional.junit.FormatterElement;
24  import org.apache.tools.ant.taskdefs.optional.junit.JUnitTask;
25  import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
26  
27  /**
28   * Proxy for testing webstart application with Abbot. It replaces the main
29   * class of the application to test and uses the JUnit Ant task test runner
30   * to execute the Abbot tests. In order to use this class, it must be 
31   * referenced as the main class in the JNLP file. 
32   * 
33   * @version $Id: AbbotWebstartTestRunner.java 170200 2005-05-15 06:24:19Z brett $
34   */
35  public class AbbotWebstartTestRunner
36  {
37      /**
38       * Name of the generic TestCase to execute. That test will execute all the
39       * XML Abbot scripts.
40       */
41      private static final String GENERIC_TEST = AbbotTestAll.class.getName();
42  
43      /**
44       * Directory where junit reports are to be generated.
45       */
46      private static final String REPORT_DIR = 
47          System.getProperty("maven.abbot.reports.dir", "");
48  
49      /**
50       * Name given by the user to the suite of tests being executed. The Abbot
51       * test report will be name after the content of this property. This is
52       * useful when you're calling the Abbot plugin several times in a row as
53       * otherwise the reports will overwrite each other.
54       */
55      private static final String TESTSUITE_NAME = 
56          System.getProperty("maven.abbot.suite.name");
57      
58      /**
59       * Entry point for starting the Ant JUnit test runner which will start
60       * all our Abbot script tests. 
61       * 
62       * @param args the parameters that we need to use to start the Ant test
63       *        runner
64       * @throws Exception in case of unanticipated error
65       */
66      public static void main(String[] args) throws Exception
67      {
68          AbbotWebstartTestRunner main = new AbbotWebstartTestRunner();
69          main.execute(GENERIC_TEST, REPORT_DIR);
70      }
71      
72      /**
73       * Execute the JUnit test runner using the Ant JUnitTask task. 
74       * 
75       * Note: We absolutely need to call System.exit() as all webstart 
76       * applications are required to do so. This is required for the
77       * application to close at the end of the tests
78       * (see http://www.vamphq.com/jwsfaq.html#linger).
79       * 
80       * @param testName the name of the generic test that will in turn run all
81       *        the Abbot XML scripts
82       * @param reportdir directory where junit reports are to be generated
83       * @throws Exception in case of unanticipated error
84       */
85      public void execute(String testName, String reportDir)
86      {
87          try
88          {
89              executeAntJunitTestRunner(testName, reportDir);
90              System.exit(0);
91          }
92          catch (Throwable t)
93          {
94              System.exit(1);
95          }
96      }
97      
98      /**
99       * Execute the JUnit test runner using the Ant JUnitTask task.
100      * 
101      * @param testName the name of the generic test that will in turn run all
102      *        the Abbot XML scripts
103      * @param reportdir directory where junit reports are to be generated
104      * @throws Exception in case of unanticipated error
105      */
106     private void executeAntJunitTestRunner(String testName, String reportDir)
107     	throws Exception
108     {
109         Project project = new Project();
110 	    project.init();
111 	    
112 	    JUnitTask junit = new JUnitTask();
113 	    junit.setProject(project);
114 
115 	    // Do not fork so that we use the same classpath that was used to
116 	    // start this class.
117 	    junit.setFork(false);
118 	    
119 	    // Set properties of the JUnit Ant task
120 
121 	    // Set formatter
122 	    junit.addFormatter(createFormatterElement("xml"));
123 	    junit.addFormatter(createFormatterElement("plain"));
124 	    
125 	    // Add tests to run
126 	    JUnitTest test = new JUnitTest(testName);
127 	    test.setTodir(new File(reportDir));
128 
129         // Sets the name of the report file is the user has specified a name.
130         // Otherwise the name of the AbbotTestAll class will be used.
131         if (TESTSUITE_NAME != null)
132         {
133 	        test.setOutfile(TESTSUITE_NAME);
134         }
135         junit.addTest(test);
136 
137 	    // Other settings
138 	    junit.setHaltonerror(true);
139 	    junit.setHaltonfailure(true);
140 	    
141 	    // Execute the task...
142 	    junit.execute();        
143     }
144     
145     /**
146      * @param type the formatter type ("xml", "plain" or "brief")
147      * @return a properly set {@link FormatterElement} object 
148      */
149     private FormatterElement createFormatterElement(String type)
150     {
151 	    FormatterElement formatterElement = new FormatterElement();
152 	    FormatterElement.TypeAttribute typeAttribute =
153 	        new FormatterElement.TypeAttribute();
154 	    typeAttribute.setValue(type);
155 	    formatterElement.setType(typeAttribute);
156 	    return formatterElement;
157     }
158 }