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 junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
26  import org.apache.maven.surefire.report.ReportEntry;
27  import org.apache.maven.surefire.report.RunListener;
28  import org.apache.maven.surefire.report.TestSetReportEntry;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  public class JUnitTestSetTest
34      extends TestCase
35  {
36  
37      public void testExecuteSuiteClass()
38          throws Exception
39      {
40          ClassLoader testClassLoader = this.getClass().getClassLoader();
41          JUnit3Reflector reflector = new JUnit3Reflector( testClassLoader );
42          JUnitTestSet testSet = new JUnitTestSet( Suite.class, reflector );
43          SuccessListener listener = new SuccessListener();
44          testSet.execute( listener, testClassLoader );
45          List succeededTests = listener.getSucceededTests();
46          assertEquals( 1, succeededTests.size() );
47          assertEquals( "testSuccess(org.apache.maven.surefire.junit.JUnitTestSetTest$AlwaysSucceeds)",
48                        ( (ReportEntry) succeededTests.get( 0 ) ).getName() );
49      }
50  
51      public static final class AlwaysSucceeds
52          extends TestCase
53      {
54          public void testSuccess()
55          {
56              assertTrue( true );
57          }
58      }
59  
60      public static class SuccessListener
61          implements RunListener
62      {
63  
64          private List<ReportEntry> succeededTests = new ArrayList<>();
65  
66          @Override
67          public void testSetStarting( TestSetReportEntry report )
68          {
69          }
70  
71          @Override
72          public void testSetCompleted( TestSetReportEntry report )
73          {
74          }
75  
76          @Override
77          public void testStarting( ReportEntry report )
78          {
79          }
80  
81          @Override
82          public void testSucceeded( ReportEntry report )
83          {
84              this.succeededTests.add( report );
85          }
86  
87          @Override
88          public void testAssumptionFailure( ReportEntry report )
89          {
90              throw new IllegalStateException();
91          }
92  
93          @Override
94          public void testError( ReportEntry report )
95          {
96              throw new IllegalStateException();
97          }
98  
99          @Override
100         public void testFailed( ReportEntry report )
101         {
102             throw new IllegalStateException();
103         }
104 
105         @Override
106         public void testSkipped( ReportEntry report )
107         {
108             throw new IllegalStateException();
109         }
110 
111         @Override
112         public void testExecutionSkippedByUser()
113         {
114         }
115 
116         public void testSkippedByUser( ReportEntry report )
117         {
118             testSkipped( report );
119         }
120 
121         public List getSucceededTests()
122         {
123             return succeededTests;
124         }
125 
126     }
127 
128     public static class Suite
129     {
130 
131         public static Test suite()
132         {
133             TestSuite suite = new TestSuite();
134             suite.addTestSuite( AlwaysSucceeds.class );
135             return suite;
136         }
137     }
138 }