View Javadoc
1   package org.apache.maven.surefire.common.junit4;
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.TestCase;
23  import org.apache.maven.surefire.util.internal.ClassMethod;
24  import org.junit.runner.Description;
25  import org.junit.runner.notification.Failure;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Set;
30  
31  import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.generateFailingTestDescriptions;
32  
33  /**
34   * @author Qingzhou Luo
35   */
36  public class JUnit4ProviderUtilTest
37      extends TestCase
38  {
39      public void testGenerateFailingTestDescriptions()
40      {
41          List<Failure> failures = new ArrayList<>();
42  
43          Description test1Description = Description.createTestDescription( T1.class, "testOne" );
44          Description test2Description = Description.createTestDescription( T1.class, "testTwo" );
45          Description test3Description = Description.createTestDescription( T2.class, "testThree" );
46          Description test4Description = Description.createTestDescription( T2.class, "testFour" );
47          Description test5Description = Description.createSuiteDescription( "Test mechanism" );
48  
49          failures.add( new Failure( test1Description, new AssertionError() ) );
50          failures.add( new Failure( test2Description, new AssertionError() ) );
51          failures.add( new Failure( test3Description, new RuntimeException() ) );
52          failures.add( new Failure( test4Description, new AssertionError() ) );
53          failures.add( new Failure( test5Description, new RuntimeException() ) );
54  
55          Set<Description> result = generateFailingTestDescriptions( failures );
56  
57          assertEquals( 4, result.size() );
58  
59          assertTrue( result.contains( test1Description ) );
60          assertTrue( result.contains( test2Description ) );
61          assertTrue( result.contains( test3Description ) );
62          assertTrue( result.contains( test4Description ) );
63      }
64  
65      public void testIllegalTestDescriptionNegativeTest()
66      {
67          Description test = Description.createSuiteDescription( "someTestMethod" );
68          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod( test );
69          assertFalse( classMethod.isValidTest() );
70      }
71  
72      public void testOldJUnitParameterizedDescriptionParser()
73      {
74          Description test = Description.createTestDescription( T1.class, " \n testMethod[5] " );
75          assertEquals( " \n testMethod[5] (" + T1.class.getName() + ")", test.getDisplayName() );
76          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod( test );
77          assertTrue( classMethod.isValidTest() );
78          assertEquals( " \n testMethod[5] ", classMethod.getMethod() );
79          assertEquals( T1.class.getName(), classMethod.getClazz() );
80      }
81  
82      public void testNewJUnitParameterizedDescriptionParser()
83      {
84          Description test = Description.createTestDescription( T1.class, "flakyTest[3: (Test11); Test12; Test13;]" );
85          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod( test );
86          assertTrue( classMethod.isValidTest() );
87          assertEquals( "flakyTest[3: (Test11); Test12; Test13;]", classMethod.getMethod() );
88          assertEquals( T1.class.getName(), classMethod.getClazz() );
89      }
90  
91      private static class T1
92      {
93  
94      }
95  
96      private static class T2
97      {
98  
99      }
100 }