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