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.booter;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.lang.management.ManagementFactory;
24  import java.math.BigDecimal;
25  
26  import org.junit.Test;
27  import org.junit.experimental.runners.Enclosed;
28  import org.junit.runner.RunWith;
29  import org.powermock.core.classloader.annotations.PowerMockIgnore;
30  import org.powermock.core.classloader.annotations.PrepareForTest;
31  import org.powermock.modules.junit4.PowerMockRunner;
32  
33  import static java.io.File.separator;
34  import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_9;
35  import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT;
36  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_FREE_BSD;
37  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_LINUX;
38  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_NET_BSD;
39  import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_OPEN_BSD;
40  import static org.assertj.core.api.Assertions.assertThat;
41  import static org.junit.Assume.assumeTrue;
42  import static org.mockito.Matchers.any;
43  import static org.mockito.Matchers.anyString;
44  import static org.mockito.Mockito.times;
45  import static org.mockito.Mockito.when;
46  import static org.powermock.api.mockito.PowerMockito.mockStatic;
47  import static org.powermock.api.mockito.PowerMockito.verifyStatic;
48  
49  /**
50   * Test of {@link SystemUtils}.
51   *
52   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
53   * @since 2.20.1
54   */
55  @SuppressWarnings("checkstyle:magicnumber")
56  @RunWith(Enclosed.class)
57  public class SystemUtilsTest {
58      /**
59       *
60       */
61      public static class PlainUnitTests {
62          @Test
63          public void shouldParseProprietaryReleaseFile() throws IOException {
64              String classes = new File(".").getCanonicalPath() + separator + "target" + separator + "test-classes";
65  
66              File path = new File(classes, "jdk8-IBM" + separator + "bin" + separator + "java");
67              assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isFalse();
68  
69              path = new File(classes, "jdk8-oracle" + separator + "bin" + separator + "java");
70              assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isFalse();
71  
72              path = new File(classes, "jdk9-oracle" + separator + "bin" + separator + "java");
73              assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isTrue();
74          }
75  
76          @Test
77          public void incorrectJdkPath() {
78              File jre = new File(System.getProperty("java.home"));
79              File jdk = jre.getParentFile();
80              File incorrect = jdk.getParentFile();
81              assertThat(SystemUtils.isJava9AtLeast(incorrect.getAbsolutePath())).isFalse();
82          }
83  
84          @Test
85          public void shouldHaveJavaPath() {
86              String javaPath = System.getProperty("java.home") + separator + "bin" + separator + "java";
87              assertThat(SystemUtils.endsWithJavaPath(javaPath)).isTrue();
88          }
89  
90          @Test
91          public void shouldNotHaveJavaPath() {
92              assertThat(SystemUtils.endsWithJavaPath("/jdk")).isFalse();
93          }
94  
95          @Test
96          public void shouldNotExtractJdkHomeFromJavaExec() {
97              File pathToJdk = SystemUtils.toJdkHomeFromJvmExec("/jdk/binx/java");
98              assertThat(pathToJdk).isNull();
99          }
100 
101         @Test
102         public void shouldExtractJdkHomeFromJavaExec() {
103             File pathToJdk = SystemUtils.toJdkHomeFromJvmExec("/jdk/bin/java");
104             assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
105         }
106 
107         @Test
108         public void shouldNotExtractJdkHomeFromJreExec() throws IOException {
109             String classes = new File(".").getCanonicalPath() + separator + "target" + separator + "test-classes";
110             File jdk = new File(classes, "jdk");
111             String pathToJreExec = jdk.getAbsolutePath() + separator + "jre" + separator + "binx" + separator + "java";
112             File pathToJdk = SystemUtils.toJdkHomeFromJvmExec(pathToJreExec);
113             assertThat(pathToJdk).isNull();
114         }
115 
116         @Test
117         public void shouldExtractJdkHomeFromJreExec() throws IOException {
118             String classes = new File(".").getCanonicalPath() + separator + "target" + separator + "test-classes";
119             File jdk = new File(classes, "jdk");
120             String pathToJreExec = jdk.getAbsolutePath() + separator + "jre" + separator + "bin" + separator + "java";
121             File pathToJdk = SystemUtils.toJdkHomeFromJvmExec(pathToJreExec);
122             assertThat(pathToJdk).isEqualTo(jdk);
123         }
124 
125         @Test
126         public void shouldExtractJdkHomeFromJre() {
127             File pathToJdk = SystemUtils.toJdkHomeFromJre("/jdk/jre");
128             assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
129         }
130 
131         @Test
132         public void shouldExtractJdkHomeFromJdk() {
133             File pathToJdk = SystemUtils.toJdkHomeFromJre("/jdk/");
134             assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
135         }
136 
137         @Test
138         public void shouldExtractJdkHomeFromRealPath() {
139             File pathToJdk = SystemUtils.toJdkHomeFromJre();
140 
141             if (JAVA_RECENT.atLeast(JAVA_9)) {
142                 File realJdkHome = new File(System.getProperty("java.home")).getAbsoluteFile();
143                 assertThat(realJdkHome).isDirectory();
144                 assertThat(realJdkHome.getName()).isNotEqualTo("jre");
145                 assertThat(pathToJdk).isEqualTo(realJdkHome);
146             } else {
147                 File realJreHome = new File(System.getProperty("java.home")).getAbsoluteFile();
148                 assertThat(realJreHome).isDirectory();
149                 assertThat(realJreHome.getName()).isEqualTo("jre");
150                 File realJdkHome = realJreHome.getParentFile();
151                 assertThat(pathToJdk).isEqualTo(realJdkHome);
152             }
153         }
154 
155         @Test
156         public void shouldBeJavaVersion() {
157             assertThat(SystemUtils.isJava9AtLeast((BigDecimal) null)).isFalse();
158             assertThat(SystemUtils.isJava9AtLeast(new BigDecimal("1.8"))).isFalse();
159             assertThat(SystemUtils.isJava9AtLeast(new BigDecimal(9))).isTrue();
160         }
161 
162         @Test
163         public void shouldBePlatformClassLoader() {
164             ClassLoader cl = SystemUtils.platformClassLoader();
165             if (JAVA_RECENT.atLeast(JAVA_9)) {
166                 assertThat(cl).isNotNull();
167             } else {
168                 assertThat(cl).isNull();
169             }
170         }
171 
172         @Test
173         public void shouldNotFindClassLoader() {
174             ClassLoader cl = SystemUtils.reflectClassLoader(getClass(), "_getPlatformClassLoader_");
175             assertThat(cl).isNull();
176         }
177 
178         @Test
179         public void shouldFindClassLoader() {
180             ClassLoader cl = SystemUtils.reflectClassLoader(getClass(), "getPlatformClassLoader");
181             assertThat(cl).isSameAs(ClassLoader.getSystemClassLoader());
182         }
183 
184         @Test
185         public void shouldBePidOnJigsaw() {
186             assumeTrue(JAVA_RECENT.atLeast(JAVA_9));
187 
188             Long actualPid = SystemUtils.pidOnJava9();
189             String expectedPid =
190                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
191 
192             assertThat(actualPid + "").isEqualTo(expectedPid);
193         }
194 
195         @Test
196         public void shouldBePidStatusOnLinux() throws Exception {
197             assumeTrue(IS_OS_LINUX);
198 
199             Long actualPid = SystemUtils.pidStatusOnLinux();
200             String expectedPid =
201                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
202 
203             assertThat(actualPid + "").isEqualTo(expectedPid);
204         }
205 
206         @Test
207         public void shouldBeMockPidStatusOnLinux() throws Exception {
208             String root = new File(System.getProperty("user.dir"), "target/test-classes").getAbsolutePath();
209             Long actualPid = SystemUtils.pidStatusOnLinux(root);
210             assertThat(actualPid).isEqualTo(48982L);
211         }
212 
213         @Test
214         public void shouldBePidStatusOnBSD() throws Exception {
215             assumeTrue(IS_OS_FREE_BSD || IS_OS_NET_BSD || IS_OS_OPEN_BSD);
216 
217             Long actualPid = SystemUtils.pidStatusOnBSD();
218             String expectedPid =
219                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
220 
221             assertThat(actualPid + "").isEqualTo(expectedPid);
222         }
223 
224         @Test
225         public void shouldBeMockPidStatusOnBSD() throws Exception {
226             String root = new File(System.getProperty("user.dir"), "target/test-classes").getAbsolutePath();
227             Long actualPid = SystemUtils.pidStatusOnBSD(root);
228             assertThat(actualPid).isEqualTo(60424L);
229         }
230 
231         @Test
232         public void shouldBePidOnJMX() {
233             Long actualPid = SystemUtils.pidOnJMX();
234             String expectedPid =
235                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
236 
237             assertThat(actualPid + "").isEqualTo(expectedPid);
238         }
239 
240         @Test
241         public void shouldBePid() {
242             Long actualPid = SystemUtils.pid();
243             String expectedPid =
244                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
245 
246             assertThat(actualPid + "").isEqualTo(expectedPid);
247         }
248 
249         @SuppressWarnings("unused")
250         public static ClassLoader getPlatformClassLoader() {
251             return ClassLoader.getSystemClassLoader();
252         }
253     }
254 
255     /**
256      *
257      */
258     @RunWith(PowerMockRunner.class)
259     @PrepareForTest(SystemUtils.class)
260     @PowerMockIgnore({"org.jacoco.agent.rt.*", "com.vladium.emma.rt.*"})
261     public static class MockTest {
262 
263         @Test
264         public void shouldBeDifferentJdk9() {
265             testIsJava9AtLeast(new File(System.getProperty("java.home")));
266         }
267 
268         @Test
269         public void shouldBeSameJdk9() {
270             // PowerMockJUnit44RunnerDelegateImpl does not work with Assumptions: assumeFalse
271             if (!JAVA_RECENT.atLeast(JAVA_9)) {
272                 testIsJava9AtLeast(new File(System.getProperty("java.home")).getParentFile());
273             }
274         }
275 
276         private static void testIsJava9AtLeast(File pathInJdk) {
277             File path = new File(pathInJdk, "bin" + separator + "java");
278 
279             mockStatic(SystemUtils.class);
280 
281             when(SystemUtils.isJava9AtLeast(anyString())).thenCallRealMethod();
282 
283             when(SystemUtils.toJdkHomeFromJvmExec(anyString())).thenCallRealMethod();
284 
285             when(SystemUtils.toJdkHomeFromJre()).thenCallRealMethod();
286 
287             when(SystemUtils.toJdkHomeFromJre(anyString())).thenCallRealMethod();
288 
289             when(SystemUtils.isBuiltInJava9AtLeast()).thenCallRealMethod();
290 
291             when(SystemUtils.toJdkVersionFromReleaseFile(any(File.class))).thenCallRealMethod();
292 
293             when(SystemUtils.isJava9AtLeast(any(BigDecimal.class))).thenCallRealMethod();
294 
295             if (JAVA_RECENT.atLeast(JAVA_9)) {
296                 assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isTrue();
297             } else {
298                 assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isFalse();
299             }
300 
301             verifyStatic(SystemUtils.class, times(0));
302             SystemUtils.toJdkVersionFromReleaseFile(any(File.class));
303 
304             verifyStatic(SystemUtils.class, times(1));
305             SystemUtils.isBuiltInJava9AtLeast();
306         }
307     }
308 }