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