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