View Javadoc
1   package org.apache.maven.surefire.junit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.lang.reflect.Proxy;
25  import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
26  import org.apache.maven.surefire.report.RunListener;
27  import org.apache.maven.surefire.testset.TestSetFailedException;
28  
29  /**
30   * JUnit3 test set
31   *
32   */
33  public final class JUnitTestSet
34      implements SurefireTestSet
35  {
36      private final Class testClass;
37  
38      private final JUnit3Reflector reflector;
39  
40      public JUnitTestSet( Class testClass, JUnit3Reflector reflector )
41          throws TestSetFailedException
42      {
43          if ( testClass == null )
44          {
45              throw new NullPointerException( "testClass is null" );
46          }
47  
48          this.testClass = testClass;
49          this.reflector = reflector;
50  
51          // ----------------------------------------------------------------------
52          // Strategy for executing JUnit tests
53          //
54          // o look for the suite method and if that is present execute that method
55          //   to get the test object.
56          //
57          // o look for test classes that are assignable from TestCase
58          //
59          // o look for test classes that only implement the Test interface
60          // ----------------------------------------------------------------------
61  
62          // The interface implemented by the dynamic proxy (TestListener), happens to be
63          // the same as the param types of TestResult.addTestListener
64      }
65  
66  
67      @Override
68      public void execute( RunListener reporter, ClassLoader loader )
69          throws TestSetFailedException
70      {
71          Class testClass = getTestClass();
72  
73          try
74          {
75              Object testObject = reflector.constructTestObject( testClass );
76              final Method runMethod;
77  
78              if ( reflector.getTestInterface().isAssignableFrom( testObject.getClass() ) )
79              {
80                  runMethod = reflector.getTestInterfaceRunMethod();
81              }
82              else
83              {
84                  runMethod = reflector.getRunMethod( testClass );
85              }
86  
87              Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
88  
89              TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler( reporter );
90  
91              Object testListener =
92                  Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler );
93  
94              Object[] addTestListenerParams = { testListener };
95  
96              reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams );
97  
98              Object[] runParams = { instanceOfTestResult };
99  
100             runMethod.invoke( testObject, runParams );
101         }
102         catch ( IllegalArgumentException e )
103         {
104             throw new TestSetFailedException( testClass.getName(), e );
105         }
106         catch ( InstantiationException e )
107         {
108             throw new TestSetFailedException( testClass.getName(), e );
109         }
110         catch ( IllegalAccessException e )
111         {
112             throw new TestSetFailedException( testClass.getName(), e );
113         }
114         catch ( InvocationTargetException e )
115         {
116             throw new TestSetFailedException( testClass.getName(), e.getTargetException() );
117         }
118         catch ( NoSuchMethodException e )
119         {
120             throw new TestSetFailedException( "Class is not a JUnit TestCase", e );
121         }
122     }
123 
124     @Override
125     public String getName()
126     {
127         return testClass.getName();
128     }
129 
130     Class getTestClass()
131     {
132         return testClass;
133     }
134 }