View Javadoc
1   package org.apache.maven.surefire.junitplatform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.api.util.ReflectionUtils;
23  import org.junit.platform.launcher.Launcher;
24  import org.junit.platform.launcher.LauncherDiscoveryRequest;
25  import org.junit.platform.launcher.TestExecutionListener;
26  import org.junit.platform.launcher.TestPlan;
27  import org.junit.platform.launcher.core.LauncherFactory;
28  
29  /**
30   * Launcher proxy which delays the most possible the initialization of the real JUnit
31   * Launcher in order to avoid stream/stdout corruption due to early logging.
32   */
33  class LazyLauncher implements Launcher, AutoCloseable
34  {
35      private AutoCloseable launcherSession;
36  
37      private Launcher launcher;
38  
39      @Override
40      public void registerTestExecutionListeners( TestExecutionListener... testExecutionListeners )
41      {
42          launcher().registerTestExecutionListeners( testExecutionListeners );
43      }
44  
45      @Override
46      public TestPlan discover( LauncherDiscoveryRequest launcherDiscoveryRequest )
47      {
48          return launcher().discover( launcherDiscoveryRequest );
49      }
50  
51      @Override
52      public void execute( LauncherDiscoveryRequest launcherDiscoveryRequest,
53                           TestExecutionListener... testExecutionListeners )
54      {
55          launcher().execute( launcherDiscoveryRequest, testExecutionListeners );
56      }
57  
58      private Launcher launcher()
59      {
60          if ( launcher == null )
61          {
62              try
63              {
64                  Class<?> sessionClass = Class.forName( "org.junit.platform.launcher.LauncherSession" );
65                  launcherSession = ReflectionUtils.invokeGetter( LauncherFactory.class, null, "openSession" ); 
66                  launcher = ReflectionUtils.invokeGetter( sessionClass, launcherSession, "getLauncher" );
67              }
68              catch ( ClassNotFoundException e )
69              {
70                  launcher = LauncherFactory.create();
71              }
72          }
73          return launcher;
74      }
75  
76      @Override
77      public void close() throws Exception
78      {
79          if ( launcherSession != null )
80          {
81              launcherSession.close();
82              launcherSession = null;
83          }
84          launcher = null;
85      }
86  }