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