View Javadoc

1   package org.apache.maven.surefire.battery;
2   
3   /*
4    * Copyright 2001-2005 The Codehaus.
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  import org.apache.maven.surefire.report.ReportEntry;
20  import org.apache.maven.surefire.report.ReporterManager;
21  
22  import java.lang.reflect.InvocationHandler;
23  import java.lang.reflect.Method;
24  import java.util.HashSet;
25  
26  public class TestListenerInvocationHandler
27      implements InvocationHandler
28  {
29      // The String names of the four methods in interface junit.framework.TestListener
30      private static final String START_TEST = "startTest";
31      private static final String ADD_FAILURE = "addFailure";
32      private static final String ADD_ERROR = "addError";
33      private static final String END_TEST = "endTest";
34  
35      private HashSet failedTestsSet = new HashSet();
36  
37      private ReporterManager reportManager;
38  
39      private class FailedTest
40      {
41          private Object testThatFailed;
42  
43          private Thread threadOnWhichTestFailed;
44  
45          public FailedTest( Object testThatFailed, Thread threadOnWhichTestFailed )
46          {
47              if ( testThatFailed == null )
48              {
49                  throw new NullPointerException( "testThatFailed is null" );
50              }
51  
52              if ( threadOnWhichTestFailed == null )
53              {
54                  throw new NullPointerException( "threadOnWhichTestFailed is null" );
55              }
56  
57              this.testThatFailed = testThatFailed;
58  
59              this.threadOnWhichTestFailed = threadOnWhichTestFailed;
60          }
61  
62          public boolean equals( Object o )
63          {
64  
65              if ( ( o == null ) || ( getClass() != o.getClass() ) )
66              {
67                  return false;
68              }
69  
70              FailedTest ft = (FailedTest) o;
71  
72              if ( ft.testThatFailed != testThatFailed )
73              {
74                  return false;
75              }
76  
77              if ( !ft.threadOnWhichTestFailed.equals( threadOnWhichTestFailed ) )
78              {
79                  return false;
80              }
81  
82              return true;
83          }
84  
85          public int hashCode()
86          {
87              return threadOnWhichTestFailed.hashCode();
88          }
89      }
90  
91      public TestListenerInvocationHandler( ReporterManager reportManager,
92                                            Object instanceOfTestResult,
93                                            ClassLoader loader )
94      {
95          if ( reportManager == null )
96          {
97              throw new NullPointerException( "reportManager is null" );
98          }
99  
100         if ( instanceOfTestResult == null )
101         {
102             throw new NullPointerException( "instanceOfTestResult is null" );
103         }
104 
105         if ( loader == null )
106         {
107             throw new NullPointerException( "loader is null" );
108         }
109 
110         this.reportManager = reportManager;
111     }
112 
113     public Object invoke( Object proxy, Method method, Object[] args )
114         throws Throwable
115     {
116         String methodName = method.getName();
117 
118         if ( methodName.equals( START_TEST ) )
119         {
120             handleStartTest( args );
121         }
122         else if ( methodName.equals( ADD_ERROR ) )
123         {
124             handleAddError( args );
125         }
126         else if ( methodName.equals( ADD_FAILURE ) )
127         {
128             handleAddFailure( args );
129         }
130         else if ( methodName.equals( END_TEST ) )
131         {
132             handleEndTest( args );
133         }
134 
135         return null;
136     }
137 
138     // Handler for TestListener.startTest(Test)
139     public void handleStartTest( Object[] args )
140     {
141         ReportEntry report = new ReportEntry( args[0], args[0].toString(), args[0].getClass().getName() );
142 
143         reportManager.testStarting( report );
144     }
145 
146     // Handler for TestListener.addFailure(Test, Throwable)
147     private void handleAddError( Object[] args )
148     {
149         ReportEntry report = new ReportEntry( args[0], args[0].toString(), args[1].toString(), (Throwable) args[1] );
150 
151         reportManager.testError( report );
152 
153         failedTestsSet.add( new FailedTest( args[0], Thread.currentThread() ) );
154     }
155 
156     private void handleAddFailure( Object[] args )
157     {
158         ReportEntry report = new ReportEntry( args[0], args[0].toString(), args[1].toString(), (Throwable) args[1] );
159 
160         reportManager.testFailed( report );
161 
162         failedTestsSet.add( new FailedTest( args[0], Thread.currentThread() ) );
163     }
164 
165     private void handleEndTest( Object[] args )
166     {
167         boolean testHadFailed = failedTestsSet.remove( new FailedTest( args[0], Thread.currentThread() ) );
168 
169         if ( !testHadFailed )
170         {
171             ReportEntry report = new ReportEntry( args[0], args[0].toString(), args[0].getClass().getName() );
172 
173             reportManager.testSucceeded( report );
174         }
175     }
176 }