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