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.jiras;
20  
21  import javax.xml.parsers.DocumentBuilderFactory;
22  import javax.xml.xpath.XPath;
23  import javax.xml.xpath.XPathFactory;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  
28  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
29  import org.junit.Before;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  import org.junit.runners.Parameterized;
34  import org.junit.runners.Parameterized.Parameter;
35  import org.junit.runners.Parameterized.Parameters;
36  import org.w3c.dom.Document;
37  
38  import static org.assertj.core.api.Assertions.assertThat;
39  
40  /**
41   *
42   */
43  @RunWith(Parameterized.class)
44  @SuppressWarnings("checkstyle:magicnumber")
45  public class Surefire946KillMainProcessInReusableForkIT extends SurefireJUnit4IntegrationTestCase {
46      // there are 10 test classes that each would wait 3.5 seconds.
47      private static final int TEST_SLEEP_TIME = 3_500;
48  
49      private String classifierOfDummyDependency;
50  
51      @Parameter
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      public String shutdownMavenMethod;
54  
55      @Parameter(1)
56      @SuppressWarnings("checkstyle:visibilitymodifier")
57      public String shutdownSurefireMethod;
58  
59      @Parameters(name = "{0}-{1}")
60      public static Iterable<Object[]> data() {
61          ArrayList<Object[]> args = new ArrayList<>();
62          args.add(new Object[] {"halt", "exit"});
63          args.add(new Object[] {"halt", "kill"});
64          args.add(new Object[] {"exit", "exit"});
65          args.add(new Object[] {"exit", "kill"});
66          args.add(new Object[] {"interrupt", "exit"});
67          args.add(new Object[] {"interrupt", "kill"});
68          return args;
69      }
70  
71      @BeforeClass
72      public static void installSelfdestructPlugin() {
73          unpack(Surefire946KillMainProcessInReusableForkIT.class, "surefire-946-self-destruct-plugin", "plugin")
74                  .executeInstall();
75      }
76  
77      @Before
78      public void dummyDep() {
79          classifierOfDummyDependency = shutdownMavenMethod + shutdownSurefireMethod;
80          unpack("surefire-946-dummy-dependency", classifierOfDummyDependency)
81                  .sysProp("distinct.classifier", classifierOfDummyDependency)
82                  .executeInstall();
83      }
84  
85      @Test(timeout = 60_000)
86      public void test() throws Exception {
87          unpack("surefire-946-killMainProcessInReusableFork", "-" + shutdownMavenMethod + "-" + shutdownSurefireMethod)
88                  .sysProp("distinct.classifier", classifierOfDummyDependency)
89                  .sysProp("surefire.shutdown", shutdownSurefireMethod)
90                  .sysProp("selfdestruct.timeoutInMillis", "20000")
91                  .sysProp("selfdestruct.method", shutdownMavenMethod)
92                  .sysProp("testSleepTime", String.valueOf(TEST_SLEEP_TIME))
93                  .addGoal("org.apache.maven.plugins.surefire:maven-selfdestruct-plugin:selfdestruct")
94                  .setForkJvm()
95                  .forkPerThread(1)
96                  .maven()
97                  .withFailure()
98                  .executeTest();
99  
100         XPathFactory xpathFactory = XPathFactory.newInstance();
101         XPath xpath = xpathFactory.newXPath();
102         File settings = new File(System.getProperty("maven.settings.file")).getCanonicalFile();
103         Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(settings);
104         String localRepository = xpath.evaluate("/settings/localRepository", doc);
105         assertThat(localRepository).isNotNull().isNotEmpty();
106 
107         File dep = new File(
108                 localRepository,
109                 "org/apache/maven/plugins/surefire/surefire-946-dummy-dependency/0.1/"
110                         + "surefire-946-dummy-dependency-0.1-" + classifierOfDummyDependency + ".jar");
111 
112         assertThat(dep).exists();
113 
114         boolean deleted;
115         int iterations = 0;
116         do {
117             Thread.sleep(1_000L);
118             deleted = dep.delete();
119         } while (!deleted && ++iterations < 10);
120         assertThat(deleted).isTrue();
121     }
122 }