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.its;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import org.apache.maven.surefire.its.fixture.OutputValidator;
28  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
29  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.junit.runners.Parameterized;
33  import org.junit.runners.Parameterized.Parameter;
34  import org.junit.runners.Parameterized.Parameters;
35  
36  import static org.hamcrest.Matchers.containsString;
37  import static org.hamcrest.Matchers.equalTo;
38  
39  /**
40   * Test project using -Dtest=mtClass#myMethod
41   *
42   * @author Olivier Lamy
43   */
44  @RunWith(Parameterized.class)
45  public class TestMethodPatternIT extends SurefireJUnit4IntegrationTestCase {
46      private static final String RUNNING_WITH_PROVIDER47 = "parallel='none', perCoreThreadCount=true, threadCount=0";
47  
48      private static final String LEGACY_FORK_NODE = "org.apache.maven.plugin.surefire.extensions.LegacyForkNodeFactory";
49  
50      private static final String SUREFIRE_FORK_NODE =
51              "org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory";
52  
53      @Parameters
54      public static Iterable<Object[]> data() {
55          ArrayList<Object[]> args = new ArrayList<>();
56          args.add(new Object[] {"tcp"});
57          args.add(new Object[] {null});
58          return args;
59      }
60  
61      @Parameter
62      @SuppressWarnings("checkstyle:visibilitymodifier")
63      public String profileId;
64  
65      private OutputValidator runMethodPattern(String projectName, Map<String, String> props, String... goals)
66              throws Exception {
67          SurefireLauncher launcher = unpack(projectName, profileId == null ? "" : "-" + profileId);
68  
69          if (profileId != null) {
70              launcher.activateProfile(profileId);
71          }
72  
73          for (Entry<String, String> entry : props.entrySet()) {
74              launcher.sysProp(entry.getKey(), entry.getValue());
75          }
76          for (String goal : goals) {
77              launcher.addGoal(goal);
78          }
79          String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
80          return launcher.showErrorStackTraces()
81                  .debugLogging()
82                  .executeTest()
83                  .assertTestSuiteResults(2, 0, 0, 0)
84                  .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
85      }
86  
87      @Test
88      public void testJUnit44() throws Exception {
89          runMethodPattern("junit44-method-pattern", Collections.emptyMap());
90      }
91  
92      @Test
93      public void testJUnit48Provider4() throws Exception {
94          runMethodPattern("junit48-method-pattern", Collections.emptyMap(), "-P surefire-junit4");
95      }
96  
97      @Test
98      public void testJUnit48Provider47() throws Exception {
99          runMethodPattern("junit48-method-pattern", Collections.emptyMap(), "-P surefire-junit47")
100                 .verifyTextInLog(RUNNING_WITH_PROVIDER47);
101     }
102 
103     @Test
104     public void testJUnit48WithCategoryFilter() throws Exception {
105         String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
106         SurefireLauncher launcher = unpack("junit48-method-pattern", profileId == null ? "" : "-" + profileId);
107 
108         if (profileId != null) {
109             launcher.activateProfile(profileId);
110         }
111 
112         launcher.debugLogging()
113                 .addGoal("-Dgroups=junit4.SampleCategory")
114                 .executeTest()
115                 .assertTestSuiteResults(1, 0, 0, 0)
116                 .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
117     }
118 
119     @Test
120     public void testTestNgMethodBefore() throws Exception {
121         Map<String, String> props = new HashMap<>();
122         props.put("testNgVersion", "5.7");
123         props.put("testNgClassifier", "jdk15");
124         runMethodPattern("testng-method-pattern-before", props);
125     }
126 
127     @Test
128     public void testTestNGMethodPattern() throws Exception {
129         Map<String, String> props = new HashMap<>();
130         props.put("testNgVersion", "5.7");
131         props.put("testNgClassifier", "jdk15");
132         runMethodPattern("/testng-method-pattern", props);
133     }
134 
135     @Test
136     public void testMethodPatternAfter() throws Exception {
137         String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
138         SurefireLauncher launcher = unpack("testng-method-pattern-after", profileId == null ? "" : "-" + profileId);
139 
140         if (profileId != null) {
141             launcher.activateProfile(profileId);
142         }
143 
144         launcher.debugLogging()
145                 .sysProp("testNgVersion", "5.7")
146                 .sysProp("testNgClassifier", "jdk15")
147                 .executeTest()
148                 .verifyErrorFree(2)
149                 .verifyTextInLog("Called tearDown")
150                 .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
151     }
152 }