Repository: maven-surefire
Updated Branches:
  refs/heads/SUREFIRE-1302_4 0103691a4 -> a39c79bf6 (forced update)


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a39c79bf/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
----------------------------------------------------------------------
diff --git 
a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
new file mode 100644
index 0000000..665e869
--- /dev/null
+++ 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java
@@ -0,0 +1,200 @@
+package org.apache.maven.surefire.booter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.surefire.util.ReflectionUtils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.lang.management.ManagementFactory;
+import java.lang.reflect.Method;
+
+import static java.lang.Thread.currentThread;
+import static org.apache.commons.lang3.JavaVersion.JAVA_9;
+import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_FREE_BSD;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_NET_BSD;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_OPEN_BSD;
+import static org.apache.maven.surefire.util.ReflectionUtils.invokeMethodChain;
+import static org.apache.maven.surefire.util.ReflectionUtils.tryLoadClass;
+
+/**
+ * JDK 9 support.
+ *
+ * @author <a href="mailto:[email protected]";>Tibor Digana (tibor17)</a>
+ * @since 2.20.1
+ */
+public final class SystemUtils
+{
+    private static final int PROC_STATUS_PID_FIRST_CHARS = 20;
+
+    public SystemUtils()
+    {
+        throw new IllegalStateException( "no instantiable constructor" );
+    }
+
+    public static ClassLoader platformClassLoader()
+    {
+        if ( JAVA_RECENT.atLeast( JAVA_9 ) )
+        {
+            return reflectClassLoader( ClassLoader.class, 
"getPlatformClassLoader" );
+        }
+        return null;
+    }
+
+    public static Long pid()
+    {
+        if ( JAVA_RECENT.atLeast( JAVA_9 ) )
+        {
+            Long pid = pidOnJava9();
+            if ( pid != null )
+            {
+                return pid;
+            }
+        }
+
+        if ( IS_OS_LINUX )
+        {
+            try
+            {
+                return pidOnLinux();
+            }
+            catch ( Exception e )
+            {
+                // examine PID via JMX
+            }
+        }
+
+        if ( IS_OS_FREE_BSD || IS_OS_NET_BSD || IS_OS_OPEN_BSD )
+        {
+            try
+            {
+                return pidStatusOnBSD();
+            }
+            catch ( Exception e )
+            {
+                // examine PID via JMX
+            }
+        }
+
+        return pidOnJMX();
+    }
+
+    static Long pidOnJMX()
+    {
+        String processName = ManagementFactory.getRuntimeMXBean().getName();
+        if ( processName.contains( "@" ) )
+        {
+            String pid = processName.substring( 0, processName.indexOf( '@' ) 
).trim();
+            try
+            {
+                return Long.parseLong( pid );
+            }
+            catch ( NumberFormatException e )
+            {
+                return null;
+            }
+        }
+
+        return null;
+    }
+
+    static Long pidOnLinux() throws Exception
+    {
+        try
+        {
+            return pidLinkOnLinux();
+        }
+        catch ( Exception e )
+        {
+            return pidStatusOnLinux();
+        }
+    }
+
+    static Long pidLinkOnLinux() throws Exception
+    {
+        String pid = new File( "/proc/self" ).getCanonicalFile().getName();
+        return Long.parseLong( pid );
+    }
+
+    static Long pidStatusOnLinux() throws Exception
+    {
+        FileReader input = new FileReader( "/proc/self/stat" );
+        try
+        {
+            // Reading and encoding 20 characters is bit faster than whole 
line.
+            // size of (long) = 19, + 1 space
+            char[] buffer = new char[PROC_STATUS_PID_FIRST_CHARS];
+            String startLine = new String( buffer, 0, input.read( buffer ) );
+            return Long.parseLong( startLine.substring( 0, startLine.indexOf( 
' ' ) ) );
+        }
+        finally
+        {
+            input.close();
+        }
+    }
+
+    /**
+     * The process status.  This file is read-only and returns a single
+     * line containing multiple space-separated fields.
+     * See <a 
href="https://www.freebsd.org/cgi/man.cgi?query=procfs&sektion=5";>procfs 
status</a>
+     *
+     * @return current PID
+     * @throws Exception if could not read /proc/curproc/status
+     */
+    static Long pidStatusOnBSD() throws Exception
+    {
+        BufferedReader input = new BufferedReader( new FileReader( 
"/proc/curproc/status" ) );
+        try
+        {
+            String line = input.readLine();
+            int i1 = 1 + line.indexOf( ' ' );
+            int i2 = line.indexOf( ' ', i1 );
+            return Long.parseLong( line.substring( i1, i2 ) );
+        }
+        finally
+        {
+            input.close();
+        }
+    }
+
+    static Long pidOnJava9()
+    {
+        ClassLoader classLoader = currentThread().getContextClassLoader();
+        Class<?> processHandle = tryLoadClass( classLoader, 
"java.lang.ProcessHandle" );
+        String[] methodChain = { "current", "pid" };
+        return (Long) invokeMethodChain( processHandle, methodChain, null );
+    }
+
+    static ClassLoader reflectClassLoader( Class<?> target, String 
getterMethodName )
+    {
+        try
+        {
+            Method getter = ReflectionUtils.getMethod( target, 
getterMethodName );
+            return (ClassLoader) ReflectionUtils.invokeMethodWithArray( null, 
getter );
+        }
+        catch ( RuntimeException e )
+        {
+            return null;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a39c79bf/surefire-booter/src/test/java/org/apache/maven/surefire/booter/JUnit4SuiteTest.java
----------------------------------------------------------------------
diff --git 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/JUnit4SuiteTest.java
 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/JUnit4SuiteTest.java
index 2bdcf21..f073a8b 100644
--- 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/JUnit4SuiteTest.java
+++ 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/JUnit4SuiteTest.java
@@ -34,7 +34,9 @@ import org.junit.runners.Suite;
     ClasspathTest.class,
     CommandReaderTest.class,
     PropertiesWrapperTest.class,
-    SurefireReflectorTest.class
+    SurefireReflectorTest.class,
+    PpidCheckerTest.class,
+    SystemUtilsTest.class
 } )
 @RunWith( Suite.class )
 public class JUnit4SuiteTest

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a39c79bf/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
----------------------------------------------------------------------
diff --git 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
new file mode 100644
index 0000000..b0153ac
--- /dev/null
+++ 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
@@ -0,0 +1,144 @@
+package org.apache.maven.surefire.booter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.Test;
+
+import java.lang.management.ManagementFactory;
+import java.util.regex.Matcher;
+
+import static org.apache.commons.lang3.SystemUtils.IS_OS_UNIX;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assume.assumeTrue;
+
+/**
+ * Testing {@link PpidChecker} on a platform.
+ *
+ * @author <a href="mailto:[email protected]";>Tibor Digana (tibor17)</a>
+ * @since 2.20.1
+ */
+public class PpidCheckerTest
+{
+    @Test
+    public void shouldHavePpidAsWindows()
+    {
+        assumeTrue( IS_OS_WINDOWS );
+
+        long expectedPid = Long.parseLong( 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim() );
+
+        PpidChecker checker = new PpidChecker( expectedPid );
+        ProcessInfo processInfo = checker.windows();
+
+        assertThat( processInfo )
+                .isNotNull();
+
+        assertThat( checker.canUse() )
+                .isTrue();
+
+        assertThat( checker.isProcessAlive() )
+                .isTrue();
+
+        assertThat( processInfo.getPID() )
+                .isEqualTo( expectedPid );
+
+        assertThat( processInfo.getTime() )
+                .isNotNull();
+    }
+
+    @Test
+    public void shouldHavePpidAsUnix()
+    {
+        assumeTrue( IS_OS_UNIX );
+
+        long expectedPid = Long.parseLong( 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim() );
+
+        PpidChecker checker = new PpidChecker( expectedPid );
+        ProcessInfo processInfo = checker.unix();
+
+        assertThat( processInfo )
+                .isNotNull();
+
+        assertThat( checker.canUse() )
+                .isTrue();
+
+        assertThat( checker.isProcessAlive() )
+                .isTrue();
+
+        assertThat( processInfo.getPID() )
+                .isEqualTo( expectedPid );
+
+        assertThat( processInfo.getTime() )
+                .isNotNull();
+    }
+
+    @Test
+    public void shouldNotFindSuchPID()
+    {
+        PpidChecker checker = new PpidChecker( 1000000L );
+        assertThat( checker.canUse() )
+                .isTrue();
+
+        boolean isAlive = checker.isProcessAlive();
+
+        assertThat( isAlive )
+                .isFalse();
+    }
+
+    @Test
+    public void shouldParseEtime()
+    {
+        Matcher m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "38" );
+        assertThat( m.matches() )
+                .isFalse();
+
+        m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "05:38" );
+        assertThat( m.matches() )
+                .isTrue();
+        assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
+        assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 0L );
+        assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
+        assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
+
+        m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "00:05:38" );
+        assertThat( m.matches() )
+                .isTrue();
+        assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
+        assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 0L );
+        assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
+        assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
+
+        m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "01:05:38" );
+        assertThat( m.matches() )
+                .isTrue();
+        assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 0L );
+        assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 3600L );
+        assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
+        assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
+
+        m = PpidChecker.UNIX_CMD_OUT_PATTERN.matcher( "02-01:05:38" );
+        assertThat( m.matches() )
+                .isTrue();
+        assertThat( PpidChecker.fromDays( m ) ).isEqualTo( 2 * 24 * 3600L );
+        assertThat( PpidChecker.fromHours( m ) ).isEqualTo( 3600L );
+        assertThat( PpidChecker.fromMinutes( m ) ).isEqualTo( 300L );
+        assertThat( PpidChecker.fromSeconds( m ) ).isEqualTo( 38L );
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a39c79bf/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java
 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java
new file mode 100644
index 0000000..fc2cbd7
--- /dev/null
+++ 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java
@@ -0,0 +1,143 @@
+package org.apache.maven.surefire.booter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.Test;
+
+import java.lang.management.ManagementFactory;
+
+import static org.apache.commons.lang3.JavaVersion.JAVA_9;
+import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_FREE_BSD;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_NET_BSD;
+import static org.apache.commons.lang3.SystemUtils.IS_OS_OPEN_BSD;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assume.assumeTrue;
+
+/**
+ * Test of {@link SystemUtils}.
+ *
+ * @author <a href="mailto:[email protected]";>Tibor Digana (tibor17)</a>
+ * @since 2.20.1
+ */
+public class SystemUtilsTest
+{
+    @Test
+    public void shouldBePlatformClassLoader()
+    {
+        ClassLoader cl = SystemUtils.platformClassLoader();
+        if ( JAVA_RECENT.atLeast( JAVA_9 ) )
+        {
+            assertThat( cl ).isNotNull();
+        }
+        else
+        {
+            assertThat( cl ).isNull();
+        }
+    }
+
+    @Test
+    public void shouldNotFindClassLoader()
+    {
+        ClassLoader cl = SystemUtils.reflectClassLoader( getClass(), 
"_getPlatformClassLoader_" );
+        assertThat( cl ).isNull();
+    }
+
+    @Test
+    public void shouldFindClassLoader()
+    {
+        ClassLoader cl = SystemUtils.reflectClassLoader( getClass(), 
"getPlatformClassLoader" );
+        assertThat( cl ).isSameAs( ClassLoader.getSystemClassLoader() );
+    }
+
+    @Test
+    public void shouldBePidOnJigsaw()
+    {
+        assumeTrue( JAVA_RECENT.atLeast( JAVA_9 ) );
+
+        Long actualPid = SystemUtils.pidOnJava9();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    @Test
+    public void shouldBePidOnLinux() throws Exception
+    {
+        assumeTrue( IS_OS_LINUX );
+
+        Long actualPid = SystemUtils.pidLinkOnLinux();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    @Test
+    public void shouldBePidStatOnLinux() throws Exception
+    {
+        assumeTrue( IS_OS_LINUX );
+
+        Long actualPid = SystemUtils.pidStatusOnLinux();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    @Test
+    public void shouldBePidStatOnBSD() throws Exception
+    {
+        assumeTrue( IS_OS_FREE_BSD || IS_OS_NET_BSD || IS_OS_OPEN_BSD );
+
+        Long actualPid = SystemUtils.pidStatusOnBSD();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    @Test
+    public void shouldBePidOnJMX()
+    {
+        Long actualPid = SystemUtils.pidOnJMX();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    @Test
+    public void shouldBePid()
+    {
+        Long actualPid = SystemUtils.pid();
+        String expectedPid = 
ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+
+        assertThat( actualPid + "" )
+                .isEqualTo( expectedPid );
+    }
+
+    public static ClassLoader getPlatformClassLoader()
+    {
+        return ClassLoader.getSystemClassLoader();
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/a39c79bf/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1295AttributeJvmCrashesToTestsIT.java
----------------------------------------------------------------------
diff --git 
a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1295AttributeJvmCrashesToTestsIT.java
 
b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1295AttributeJvmCrashesToTestsIT.java
index 1fa88f6..f051c1c 100644
--- 
a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1295AttributeJvmCrashesToTestsIT.java
+++ 
b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1295AttributeJvmCrashesToTestsIT.java
@@ -27,8 +27,9 @@ import org.junit.Before;
 import org.junit.Test;
 
 import java.util.Iterator;
-import java.util.Locale;
 
+import static org.apache.commons.lang.SystemUtils.IS_OS_LINUX;
+import static org.apache.commons.lang.SystemUtils.IS_OS_MAC_OSX;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
@@ -46,8 +47,7 @@ public class Surefire1295AttributeJvmCrashesToTestsIT
     @Before
     public void skipWindows()
     {
-        String os = System.getProperty( "os.name" ).toLowerCase( Locale.ROOT );
-        assumeTrue( os.equals( "mac os x" ) || os.equals( "linux" ) /*||  
os.contains( "windows" )*/ );
+        assumeTrue( IS_OS_LINUX || IS_OS_MAC_OSX );
     }
 
     @Test

Reply via email to