View Javadoc
1   package org.apache.maven.surefire.its;
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.its.fixture.SurefireJUnit4IntegrationTestCase;
23  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
24  
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.util.Properties;
30  import java.util.StringTokenizer;
31  
32  import static org.apache.maven.surefire.its.fixture.SurefireLauncher.EXT_JDK_HOME;
33  import static org.apache.maven.surefire.its.fixture.SurefireLauncher.EXT_JDK_HOME_KEY;
34  import static org.junit.Assert.fail;
35  import static org.junit.Assume.assumeTrue;
36  
37  /**
38   * Abstract test class for Jigsaw tests.
39   *
40   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
41   * @since 2.20.1
42   */
43  public abstract class AbstractJigsawIT
44          extends SurefireJUnit4IntegrationTestCase
45  {
46      private static final double JIGSAW_JAVA_VERSION = 9.0d;
47  
48      protected abstract String getProjectDirectoryName();
49  
50      protected SurefireLauncher assumeJigsaw() throws IOException
51      {
52          assumeTrue( "There's no JDK 9 provided.",
53                            isJavaVersion9AtLeast() || EXT_JDK_HOME != null && isExtJavaVerion9AtLeast() );
54          // fail( EXT_JDK_HOME_KEY + " was provided with value " + EXT_JDK_HOME + " but it is not Jigsaw Java 9." );
55  
56          SurefireLauncher launcher = unpack();
57  
58          return EXT_JDK_HOME == null ? launcher : launcher.setLauncherJavaHome( EXT_JDK_HOME );
59      }
60  
61      protected SurefireLauncher assumeJava9Property() throws IOException
62      {
63          assumeTrue( "There's no JDK 9 provided.", EXT_JDK_HOME != null && isExtJavaVerion9AtLeast() );
64          return unpack();
65      }
66  
67      protected String getSuffix()
68      {
69          return null;
70      }
71  
72      private SurefireLauncher unpack()
73      {
74          return unpack( getProjectDirectoryName(), getSuffix() );
75      }
76  
77      private static boolean isJavaVersion9AtLeast()
78      {
79          return Double.valueOf( System.getProperty( "java.specification.version" ) ) >= JIGSAW_JAVA_VERSION;
80      }
81  
82      private static boolean isExtJavaVerion9AtLeast() throws IOException
83      {
84          File release = new File( EXT_JDK_HOME, "release" );
85          assumeTrue( EXT_JDK_HOME_KEY + " was provided with value " + EXT_JDK_HOME + " but file does not exist "
86                  + EXT_JDK_HOME + File.separator + "release", release.exists() );
87  
88          Properties properties = new Properties();
89          try ( InputStream is = new FileInputStream( release ) )
90          {
91              properties.load( is );
92          }
93          String javaVersion = properties.getProperty( "JAVA_VERSION" ).replace( "\"", "" );
94          StringTokenizer versions = new StringTokenizer( javaVersion, "._" );
95  
96          if ( versions.countTokens() == 1 )
97          {
98              javaVersion = versions.nextToken();
99          }
100         else if ( versions.countTokens() >= 2 )
101         {
102             javaVersion = versions.nextToken() + "." + versions.nextToken();
103         }
104         else
105         {
106             fail( "unexpected java version format" );
107         }
108 
109         return Double.valueOf( javaVersion ) >= JIGSAW_JAVA_VERSION;
110     }
111 }