You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ag...@apache.org on 2013/03/22 22:11:56 UTC

[2/2] git commit: Fixed ForkMode*IT on mvn 2.2.1 and in other non-embedded modes

Fixed ForkMode*IT on mvn 2.2.1 and in other non-embedded modes


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/4221437c
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/4221437c
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/4221437c

Branch: refs/heads/master
Commit: 4221437cb9a632c7a532bd613dc05e712c1d150d
Parents: 2410d01
Author: Andreas Gudian <ag...@apache.org>
Authored: Fri Mar 22 22:11:30 2013 +0100
Committer: Andreas Gudian <ag...@apache.org>
Committed: Fri Mar 22 22:11:30 2013 +0100

----------------------------------------------------------------------
 .../org/apache/maven/surefire/its/ForkModeIT.java  |   91 +++++++++------
 .../resources/test-helper-dump-pid-plugin/pom.xml  |   62 ++++++++++
 .../plugins/surefire/dumppid/DumpPidMojo.java      |   70 +++++++++++
 3 files changed, 188 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4221437c/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ForkModeIT.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ForkModeIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ForkModeIT.java
index 233a993..061c5a6 100644
--- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ForkModeIT.java
+++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ForkModeIT.java
@@ -19,18 +19,21 @@ package org.apache.maven.surefire.its;
  * under the License.
  */
 
-import java.lang.management.ManagementFactory;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
 
-import org.apache.maven.surefire.its.fixture.*;
+import org.apache.maven.surefire.its.fixture.OutputValidator;
+import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
+import org.apache.maven.surefire.its.fixture.SurefireLauncher;
+import org.apache.maven.surefire.its.fixture.TestFile;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-
 /**
  * Test forkMode
  * 
@@ -39,13 +42,23 @@ import static org.junit.Assert.fail;
 public class ForkModeIT
     extends SurefireJUnit4IntegrationTestCase
 {
+
+    private OutputValidator outputValidator;
+
+    @BeforeClass
+    public static void installDumpPidPlugin()
+        throws Exception
+    {
+        unpack( ForkModeIT.class, "test-helper-dump-pid-plugin", "plugin" ).executeInstall();
+    }
+
     @Test
     public void testForkModeAlways()
     {
         String[] pids = doTest( unpack( getProject() ).setForkJvm().forkAlways() );
         assertDifferentPids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertEndWith( pids, "_1_1", 3 );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
@@ -53,8 +66,8 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).setForkJvm().forkPerTest() );
         assertDifferentPids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertEndWith( pids, "_1_1", 3 );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
@@ -62,8 +75,8 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).forkNever() );
         assertSamePids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertEquals( "my pid is equal to pid 1 of the test", getMyPID(), pids[0] );
+        assertEndWith( pids, "_1_1", 3 );
+        assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
     }
 
     @Test
@@ -71,8 +84,8 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).forkMode( "none" ) );
         assertSamePids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertEquals( "my pid is equal to pid 1 of the test", getMyPID(), pids[0] );
+        assertEndWith( pids, "_1_1", 3 );
+        assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
     }
 
     @Test
@@ -80,16 +93,17 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).setForkJvm().forkOncePerThread().threadCount( 1 ) );
         assertSamePids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertEndWith( pids, "_1_1", 3 );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
     public void testForkModeOncePerThreadTwoThreads()
     {
-        String[] pids = doTest( unpack( getProject() ).forkOncePerThread().threadCount( 2 ).addGoal( "-DsleepLength=1200" ) );
+        String[] pids =
+            doTest( unpack( getProject() ).forkOncePerThread().threadCount( 2 ).addGoal( "-DsleepLength=1200" ) );
         assertDifferentPids( pids, 2 );
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
@@ -97,8 +111,8 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).forkCount( 0 ) );
         assertSamePids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertEquals( "my pid is equal to pid 1 of the test", getMyPID(), pids[0] );
+        assertEndWith( pids, "_1_1", 3 );
+        assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
     }
 
     @Test
@@ -106,8 +120,8 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( false ) );
         assertDifferentPids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertEndWith( pids, "_1_1", 3 );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
@@ -115,35 +129,39 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( true ) );
         assertSamePids( pids );
-        assertEndWith( pids, "_1_1", 3);
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertEndWith( pids, "_1_1", 3 );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
     public void testForkCountTwoNoReuse()
     {
-        String[] pids = doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( false ).addGoal( "-DsleepLength=1200" ) );
+        String[] pids =
+            doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( false ).addGoal( "-DsleepLength=1200" ) );
         assertDifferentPids( pids );
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     @Test
     public void testForkCountTwoReuse()
     {
-        String[] pids = doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ).addGoal( "-DsleepLength=1200" ) );
+        String[] pids =
+            doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ).addGoal( "-DsleepLength=1200" ) );
         assertDifferentPids( pids, 2 );
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
     private void assertEndWith( String[] pids, String suffix, int expectedMatches )
     {
         int matches = 0;
-        for (String pid : pids) {
-            if ( pid.endsWith( suffix )) {
+        for ( String pid : pids )
+        {
+            if ( pid.endsWith( suffix ) )
+            {
                 matches++;
             }
         }
-        
+
         assertEquals( "suffix " + suffix + " matched the correct number of pids", expectedMatches, matches );
     }
 
@@ -158,12 +176,14 @@ public class ForkModeIT
     {
         String[] pids = doTest( unpack( getProject() ).forkOnce() );
         assertSamePids( pids );
-        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMyPID() ) );
+        assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
     }
 
-    private String getMyPID()
+    private String getMainPID()
     {
-        return ManagementFactory.getRuntimeMXBean().getName() + " testValue_1_1";
+        final TestFile targetFile = outputValidator.getTargetFile( "maven.pid" );
+        String pid = targetFile.slurpFile();
+        return pid + " testValue_1_1";
     }
 
     private void assertSamePids( String[] pids )
@@ -193,7 +213,8 @@ public class ForkModeIT
     private String[] doTest( SurefireLauncher forkMode )
     {
         forkMode.sysProp( "testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}" );
-        final OutputValidator outputValidator = forkMode.debugLogging().executeTest();
+        forkMode.addGoal( "org.apache.maven.plugins.surefire:maven-dump-pid-plugin:dump-pid" );
+        outputValidator = forkMode.executeTest();
         outputValidator.verifyErrorFreeLog().assertTestSuiteResults( 3, 0, 0, 0 );
         String[] pids = new String[3];
         for ( int i = 1; i <= pids.length; i++ )

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4221437c/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/pom.xml b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/pom.xml
new file mode 100644
index 0000000..26af45b
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/pom.xml
@@ -0,0 +1,62 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<groupId>org.apache.maven.plugins.surefire</groupId>
+	<artifactId>maven-dump-pid-plugin</artifactId>
+	<version>0.1</version>
+	<packaging>maven-plugin</packaging>
+
+	<name>maven-dump-pid-plugin Maven Plugin</name>
+	<url>http://maven.apache.org</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.maven</groupId>
+			<artifactId>maven-plugin-api</artifactId>
+			<version>2.0.9</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.maven.plugin-tools</groupId>
+			<artifactId>maven-plugin-annotations</artifactId>
+			<version>3.2</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>3.8.1</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-plugin-plugin</artifactId>
+				<version>3.2</version>
+				<configuration>
+					<!-- see http://jira.codehaus.org/browse/MNG-5346 -->
+					<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+				</configuration>
+				<executions>
+					<execution>
+						<id>mojo-descriptor</id>
+						<goals>
+							<goal>descriptor</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.5.1</version>
+			</plugin>
+		</plugins>
+	</build>
+</project>

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4221437c/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java
----------------------------------------------------------------------
diff --git a/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java
new file mode 100644
index 0000000..035976b
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/test-helper-dump-pid-plugin/src/main/java/org/apache/maven/plugins/surefire/dumppid/DumpPidMojo.java
@@ -0,0 +1,70 @@
+package org.apache.maven.plugins.surefire.dumppid;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed 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 java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+
+/**
+ * Goal dumps the PID of the maven process
+ */
+@Mojo( name = "dump-pid", defaultPhase = LifecyclePhase.GENERATE_TEST_RESOURCES )
+public class DumpPidMojo
+    extends AbstractMojo
+{
+
+    @Parameter( defaultValue = "${project.build.directory}", property = "dumpPid.targetDir" )
+    private File targetDir;
+
+    public void execute()
+        throws MojoExecutionException
+    {
+        File target;
+        try
+        {
+            getLog().info( "Dumping PID to " + targetDir );
+            
+            if ( !targetDir.exists() )
+            {
+                targetDir.mkdirs();
+            }
+            
+            target = new File( targetDir, "maven.pid" ).getCanonicalFile();
+
+            FileWriter fw = new FileWriter( target );
+            String pid = ManagementFactory.getRuntimeMXBean().getName();
+            fw.write( pid );
+            fw.flush();
+            fw.close();
+            
+            getLog().info( "Wrote " + pid + " to " + target );
+            
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Unable to create pid file", e );
+        }
+    }
+}