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 org.apache.maven.surefire.common.junit3.JUnit3Reflector;
23  import org.apache.maven.surefire.report.RunListener;
24  import org.apache.maven.surefire.testset.TestSetFailedException;
25  
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  import java.lang.reflect.Proxy;
29  
30  public final class JUnitTestSet
31      implements SurefireTestSet
32  {
33      private final Class testClass;
34  
35      private final JUnit3Reflector reflector;
36  
37      public JUnitTestSet( Class testClass, JUnit3Reflector reflector )
38          throws TestSetFailedException
39      {
40          if ( testClass == null )
41          {
42              throw new NullPointerException( "testClass is null" );
43          }
44  
45          this.testClass = testClass;
46          this.reflector = reflector;
47  
48          // ----------------------------------------------------------------------
49          // Strategy for executing JUnit tests
50          //
51          // o look for the suite method and if that is present execute that method
52          //   to get the test object.
53          //
54          // o look for test classes that are assignable from TestCase
55          //
56          // o look for test classes that only implement the Test interface
57          // ----------------------------------------------------------------------
58  
59          // The interface implemented by the dynamic proxy (TestListener), happens to be
60          // the same as the param types of TestResult.addTestListener
61      }
62  
63  
64      public void execute( RunListener reporter, ClassLoader loader )
65          throws TestSetFailedException
66      {
67          Class testClass = getTestClass();
68  
69          try
70          {
71              Object testObject = reflector.constructTestObject( testClass );
72              final Method runMethod;
73  
74              if ( this.reflector.getTestInterface().isAssignableFrom( testObject.getClass() ) )
75              {
76                  runMethod = this.reflector.getTestInterfaceRunMethod();
77              }
78              else
79              {
80                  runMethod = reflector.getRunMethod( this.testClass );
81              }
82  
83              Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
84  
85              TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler( reporter );
86  
87              Object testListener =
88                  Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler );
89  
90              Object[] addTestListenerParams = { testListener };
91  
92              reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams );
93  
94              Object[] runParams = { instanceOfTestResult };
95  
96              runMethod.invoke( testObject, runParams );
97          }
98          catch ( IllegalArgumentException e )
99          {
100             throw new TestSetFailedException( testClass.getName(), e );
101         }
102         catch ( InstantiationException e )
103         {
104             throw new TestSetFailedException( testClass.getName(), e );
105         }
106         catch ( IllegalAccessException e )
107         {
108             throw new TestSetFailedException( testClass.getName(), e );
109         }
110         catch ( InvocationTargetException e )
111         {
112             throw new TestSetFailedException( testClass.getName(), e.getTargetException() );
113         }
114         catch ( NoSuchMethodException e )
115         {
116             throw new TestSetFailedException( "Class is not a JUnit TestCase", e );
117         }
118     }
119 
120     public String getName()
121     {
122         return testClass.getName();
123     }
124 
125     Class getTestClass()
126     {
127         return testClass;
128     }
129 }