1   package org.apache.maven.surefire.junit;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  public final class JUnitTestSet
30      implements SurefireTestSet
31  {
32      private final Class testClass;
33  
34      private final JUnit3Reflector reflector;
35  
36      public JUnitTestSet( Class testClass, JUnit3Reflector reflector )
37          throws TestSetFailedException
38      {
39          if ( testClass == null )
40          {
41              throw new NullPointerException( "testClass is null" );
42          }
43  
44          this.testClass = testClass;
45          this.reflector = reflector;
46  
47          
48          
49          
50          
51          
52          
53          
54          
55          
56          
57  
58          
59          
60      }
61  
62  
63      public void execute( RunListener reporter, ClassLoader loader )
64          throws TestSetFailedException
65      {
66          Class testClass = getTestClass();
67  
68          try
69          {
70              Object testObject = reflector.constructTestObject( testClass );
71              final Method runMethod;
72  
73              if ( this.reflector.getTestInterface().isAssignableFrom( testObject.getClass() ) )
74              {
75                  runMethod = this.reflector.getTestInterfaceRunMethod();
76              }
77              else
78              {
79                  runMethod = reflector.getRunMethod( this.testClass );
80              }
81  
82              Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
83  
84              TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler( reporter );
85  
86              Object testListener =
87                  Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler );
88  
89              Object[] addTestListenerParams = { testListener };
90  
91              reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams );
92  
93              Object[] runParams = { instanceOfTestResult };
94  
95              runMethod.invoke( testObject, runParams );
96          }
97          catch ( IllegalArgumentException e )
98          {
99              throw new TestSetFailedException( testClass.getName(), e );
100         }
101         catch ( InstantiationException e )
102         {
103             throw new TestSetFailedException( testClass.getName(), e );
104         }
105         catch ( IllegalAccessException e )
106         {
107             throw new TestSetFailedException( testClass.getName(), e );
108         }
109         catch ( InvocationTargetException e )
110         {
111             throw new TestSetFailedException( testClass.getName(), e.getTargetException() );
112         }
113         catch ( NoSuchMethodException e )
114         {
115             throw new TestSetFailedException( "Class is not a JUnit TestCase", e );
116         }
117     }
118 
119     public String getName()
120     {
121         return testClass.getName();
122     }
123 
124     Class getTestClass()
125     {
126         return testClass;
127     }
128 }