View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.common.junit4;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Set;
24  
25  import junit.framework.TestCase;
26  import org.apache.maven.surefire.api.util.internal.ClassMethod;
27  import org.junit.runner.Description;
28  import org.junit.runner.notification.Failure;
29  
30  import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.generateFailingTestDescriptions;
31  
32  /**
33   * @author Qingzhou Luo
34   */
35  public class JUnit4ProviderUtilTest extends TestCase {
36      public void testGenerateFailingTestDescriptions() {
37          List<Failure> failures = new ArrayList<>();
38  
39          Description test1Description = Description.createTestDescription(T1.class, "testOne");
40          Description test2Description = Description.createTestDescription(T1.class, "testTwo");
41          Description test3Description = Description.createTestDescription(T2.class, "testThree");
42          Description test4Description = Description.createTestDescription(T2.class, "testFour");
43          Description test5Description = Description.createSuiteDescription("Test mechanism");
44  
45          failures.add(new Failure(test1Description, new AssertionError()));
46          failures.add(new Failure(test2Description, new AssertionError()));
47          failures.add(new Failure(test3Description, new RuntimeException()));
48          failures.add(new Failure(test4Description, new AssertionError()));
49          failures.add(new Failure(test5Description, new RuntimeException()));
50  
51          Set<Description> result = generateFailingTestDescriptions(failures);
52  
53          assertEquals(4, result.size());
54  
55          assertTrue(result.contains(test1Description));
56          assertTrue(result.contains(test2Description));
57          assertTrue(result.contains(test3Description));
58          assertTrue(result.contains(test4Description));
59      }
60  
61      public void testIllegalTestDescriptionNegativeTest() {
62          Description test = Description.createSuiteDescription("someTestMethod");
63          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod(test);
64          assertFalse(classMethod.isValidTest());
65      }
66  
67      public void testOldJUnitParameterizedDescriptionParser() {
68          Description test = Description.createTestDescription(T1.class, " \n testMethod[5] ");
69          assertEquals(" \n testMethod[5] (" + T1.class.getName() + ")", test.getDisplayName());
70          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod(test);
71          assertTrue(classMethod.isValidTest());
72          assertEquals(" \n testMethod[5] ", classMethod.getMethod());
73          assertEquals(T1.class.getName(), classMethod.getClazz());
74      }
75  
76      public void testNewJUnitParameterizedDescriptionParser() {
77          Description test = Description.createTestDescription(T1.class, "flakyTest[3: (Test11); Test12; Test13;]");
78          ClassMethod classMethod = JUnit4ProviderUtil.toClassMethod(test);
79          assertTrue(classMethod.isValidTest());
80          assertEquals("flakyTest[3: (Test11); Test12; Test13;]", classMethod.getMethod());
81          assertEquals(T1.class.getName(), classMethod.getClazz());
82      }
83  
84      private static class T1 {}
85  
86      private static class T2 {}
87  }