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.junitplatform;
20  
21  import org.apache.maven.surefire.api.util.ReflectionUtils;
22  import org.junit.platform.launcher.Launcher;
23  import org.junit.platform.launcher.LauncherDiscoveryRequest;
24  import org.junit.platform.launcher.TestExecutionListener;
25  import org.junit.platform.launcher.TestPlan;
26  import org.junit.platform.launcher.core.LauncherFactory;
27  
28  /**
29   * Launcher proxy which delays the most possible the initialization of the real JUnit
30   * Launcher in order to avoid stream/stdout corruption due to early logging.
31   */
32  class LazyLauncher implements Launcher, AutoCloseable {
33      private AutoCloseable launcherSession;
34  
35      private Launcher launcher;
36  
37      @Override
38      public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners) {
39          launcher().registerTestExecutionListeners(testExecutionListeners);
40      }
41  
42      @Override
43      public TestPlan discover(LauncherDiscoveryRequest launcherDiscoveryRequest) {
44          return launcher().discover(launcherDiscoveryRequest);
45      }
46  
47      @Override
48      public void execute(
49              LauncherDiscoveryRequest launcherDiscoveryRequest, TestExecutionListener... testExecutionListeners) {
50          launcher().execute(launcherDiscoveryRequest, testExecutionListeners);
51      }
52  
53      private Launcher launcher() {
54          if (launcher == null) {
55              try {
56                  Class<?> sessionClass = Class.forName("org.junit.platform.launcher.LauncherSession");
57                  launcherSession = ReflectionUtils.invokeGetter(LauncherFactory.class, null, "openSession");
58                  launcher = ReflectionUtils.invokeGetter(sessionClass, launcherSession, "getLauncher");
59              } catch (ClassNotFoundException e) {
60                  launcher = LauncherFactory.create();
61              }
62          }
63          return launcher;
64      }
65  
66      @Override
67      public void close() throws Exception {
68          if (launcherSession != null) {
69              launcherSession.close();
70              launcherSession = null;
71          }
72          launcher = null;
73      }
74  }