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 shouldHaveJavaPath() {
78              String javaPath = System.getProperty("java.home") + separator + "bin" + separator + "java";
79              assertThat(SystemUtils.endsWithJavaPath(javaPath)).isTrue();
80          }
81  
82          @Test
83          public void shouldNotHaveJavaPath() {
84              assertThat(SystemUtils.endsWithJavaPath("/jdk")).isFalse();
85          }
86  
87          @Test
88          public void shouldNotExtractJdkHomeFromJavaExec() {
89              File pathToJdk = SystemUtils.toJdkHomeFromJvmExec("/jdk/binx/java");
90              assertThat(pathToJdk).isNull();
91          }
92  
93          @Test
94          public void shouldExtractJdkHomeFromJavaExec() {
95              File pathToJdk = SystemUtils.toJdkHomeFromJvmExec("/jdk/bin/java");
96              assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
97          }
98  
99          @Test
100         public void shouldNotExtractJdkHomeFromJreExec() throws IOException {
101             String classes = new File(".").getCanonicalPath() + separator + "target" + separator + "test-classes";
102             File jdk = new File(classes, "jdk");
103             String pathToJreExec = jdk.getAbsolutePath() + separator + "jre" + separator + "binx" + separator + "java";
104             File pathToJdk = SystemUtils.toJdkHomeFromJvmExec(pathToJreExec);
105             assertThat(pathToJdk).isNull();
106         }
107 
108         @Test
109         public void shouldExtractJdkHomeFromJreExec() throws IOException {
110             String classes = new File(".").getCanonicalPath() + separator + "target" + separator + "test-classes";
111             File jdk = new File(classes, "jdk");
112             String pathToJreExec = jdk.getAbsolutePath() + separator + "jre" + separator + "bin" + separator + "java";
113             File pathToJdk = SystemUtils.toJdkHomeFromJvmExec(pathToJreExec);
114             assertThat(pathToJdk).isEqualTo(jdk);
115         }
116 
117         @Test
118         public void shouldExtractJdkHomeFromJre() {
119             File pathToJdk = SystemUtils.toJdkHomeFromJre("/jdk/jre");
120             assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
121         }
122 
123         @Test
124         public void shouldExtractJdkHomeFromJdk() {
125             File pathToJdk = SystemUtils.toJdkHomeFromJre("/jdk/");
126             assertThat(pathToJdk).isEqualTo(new File("/jdk").getAbsoluteFile());
127         }
128 
129         @Test
130         public void shouldExtractJdkHomeFromRealPath() {
131             File pathToJdk = SystemUtils.toJdkHomeFromJre();
132 
133             if (JAVA_RECENT.atLeast(JAVA_9)) {
134                 File realJdkHome = new File(System.getProperty("java.home")).getAbsoluteFile();
135                 assertThat(realJdkHome).isDirectory();
136                 assertThat(realJdkHome.getName()).isNotEqualTo("jre");
137                 assertThat(pathToJdk).isEqualTo(realJdkHome);
138             } else {
139                 File realJreHome = new File(System.getProperty("java.home")).getAbsoluteFile();
140                 assertThat(realJreHome).isDirectory();
141                 assertThat(realJreHome.getName()).isEqualTo("jre");
142                 File realJdkHome = realJreHome.getParentFile();
143                 assertThat(pathToJdk).isEqualTo(realJdkHome);
144             }
145         }
146 
147         @Test
148         public void shouldBeJavaVersion() {
149             assertThat(SystemUtils.isJava9AtLeast((BigDecimal) null)).isFalse();
150             assertThat(SystemUtils.isJava9AtLeast(new BigDecimal("1.8"))).isFalse();
151             assertThat(SystemUtils.isJava9AtLeast(new BigDecimal(9))).isTrue();
152         }
153 
154         @Test
155         public void shouldBePlatformClassLoader() {
156             ClassLoader cl = SystemUtils.platformClassLoader();
157             if (JAVA_RECENT.atLeast(JAVA_9)) {
158                 assertThat(cl).isNotNull();
159             } else {
160                 assertThat(cl).isNull();
161             }
162         }
163 
164         @Test
165         public void shouldNotFindClassLoader() {
166             ClassLoader cl = SystemUtils.reflectClassLoader(getClass(), "_getPlatformClassLoader_");
167             assertThat(cl).isNull();
168         }
169 
170         @Test
171         public void shouldFindClassLoader() {
172             ClassLoader cl = SystemUtils.reflectClassLoader(getClass(), "getPlatformClassLoader");
173             assertThat(cl).isSameAs(ClassLoader.getSystemClassLoader());
174         }
175 
176         @Test
177         public void shouldBePidOnJigsaw() {
178             assumeTrue(JAVA_RECENT.atLeast(JAVA_9));
179 
180             Long actualPid = SystemUtils.pidOnJava9();
181             String expectedPid =
182                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
183 
184             assertThat(actualPid + "").isEqualTo(expectedPid);
185         }
186 
187         @Test
188         public void shouldBePidStatusOnLinux() throws Exception {
189             assumeTrue(IS_OS_LINUX);
190 
191             Long actualPid = SystemUtils.pidStatusOnLinux();
192             String expectedPid =
193                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
194 
195             assertThat(actualPid + "").isEqualTo(expectedPid);
196         }
197 
198         @Test
199         public void shouldBeMockPidStatusOnLinux() throws Exception {
200             String root = new File(System.getProperty("user.dir"), "target/test-classes").getAbsolutePath();
201             Long actualPid = SystemUtils.pidStatusOnLinux(root);
202             assertThat(actualPid).isEqualTo(48982L);
203         }
204 
205         @Test
206         public void shouldBePidStatusOnBSD() throws Exception {
207             assumeTrue(IS_OS_FREE_BSD || IS_OS_NET_BSD || IS_OS_OPEN_BSD);
208 
209             Long actualPid = SystemUtils.pidStatusOnBSD();
210             String expectedPid =
211                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
212 
213             assertThat(actualPid + "").isEqualTo(expectedPid);
214         }
215 
216         @Test
217         public void shouldBeMockPidStatusOnBSD() throws Exception {
218             String root = new File(System.getProperty("user.dir"), "target/test-classes").getAbsolutePath();
219             Long actualPid = SystemUtils.pidStatusOnBSD(root);
220             assertThat(actualPid).isEqualTo(60424L);
221         }
222 
223         @Test
224         public void shouldBePidOnJMX() {
225             Long actualPid = SystemUtils.pidOnJMX();
226             String expectedPid =
227                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
228 
229             assertThat(actualPid + "").isEqualTo(expectedPid);
230         }
231 
232         @Test
233         public void shouldBePid() {
234             Long actualPid = SystemUtils.pid();
235             String expectedPid =
236                     ManagementFactory.getRuntimeMXBean().getName().split("@")[0].trim();
237 
238             assertThat(actualPid + "").isEqualTo(expectedPid);
239         }
240 
241         @SuppressWarnings("unused")
242         public static ClassLoader getPlatformClassLoader() {
243             return ClassLoader.getSystemClassLoader();
244         }
245     }
246 
247     /**
248      *
249      */
250     @RunWith(PowerMockRunner.class)
251     @PrepareForTest(SystemUtils.class)
252     @PowerMockIgnore({"org.jacoco.agent.rt.*", "com.vladium.emma.rt.*"})
253     public static class MockTest {
254 
255         @Test
256         public void shouldBeDifferentJdk9() {
257             testIsJava9AtLeast(new File(System.getProperty("java.home")));
258         }
259 
260         @Test
261         public void shouldBeSameJdk9() {
262             // PowerMockJUnit44RunnerDelegateImpl does not work with Assumptions: assumeFalse
263             if (!JAVA_RECENT.atLeast(JAVA_9)) {
264                 testIsJava9AtLeast(new File(System.getProperty("java.home")).getParentFile());
265             }
266         }
267 
268         private static void testIsJava9AtLeast(File pathInJdk) {
269             File path = new File(pathInJdk, "bin" + separator + "java");
270 
271             mockStatic(SystemUtils.class);
272 
273             when(SystemUtils.isJava9AtLeast(anyString())).thenCallRealMethod();
274 
275             when(SystemUtils.toJdkHomeFromJvmExec(anyString())).thenCallRealMethod();
276 
277             when(SystemUtils.toJdkHomeFromJre()).thenCallRealMethod();
278 
279             when(SystemUtils.toJdkHomeFromJre(anyString())).thenCallRealMethod();
280 
281             when(SystemUtils.isBuiltInJava9AtLeast()).thenCallRealMethod();
282 
283             when(SystemUtils.toJdkVersionFromReleaseFile(any(File.class))).thenCallRealMethod();
284 
285             when(SystemUtils.isJava9AtLeast(any(BigDecimal.class))).thenCallRealMethod();
286 
287             if (JAVA_RECENT.atLeast(JAVA_9)) {
288                 assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isTrue();
289             } else {
290                 assertThat(SystemUtils.isJava9AtLeast(path.getAbsolutePath())).isFalse();
291             }
292 
293             verifyStatic(SystemUtils.class, times(0));
294             SystemUtils.toJdkVersionFromReleaseFile(any(File.class));
295 
296             verifyStatic(SystemUtils.class, times(1));
297             SystemUtils.isBuiltInJava9AtLeast();
298         }
299     }
300 }