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