1   package org.apache.maven.surefire.its;
2   
3   
4   import junit.framework.TestCase;
5   import org.apache.maven.it.VerificationException;
6   import org.apache.maven.it.Verifier;
7   import org.apache.maven.it.util.ResourceExtractor;
8   import org.apache.maven.reporting.MavenReportException;
9   
10  import java.io.BufferedReader;
11  import java.io.File;
12  import java.io.FileReader;
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * Test forkMode
19   * 
20   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
21   * 
22   */
23  public class ForkModeTest
24      extends TestCase
25  {
26      public void testForkModeAlways () throws Exception
27      {
28          String[] pids = doTest("always");
29          
30          assertDifferentPids( pids );
31      }
32      
33      public void testForkModePerTest() throws Exception
34      {
35          String[] pids = doTest( "pertest" );
36  
37          assertDifferentPids( pids );
38      }
39      
40      public void testForkModeNever() throws Exception 
41      {
42          String[] pids = doTest( "never" );
43  
44          assertSamePids( pids );
45      }
46      
47      public void testForkModeNone() throws Exception 
48      {
49          String[] pids = doTest( "none" );
50  
51          assertSamePids( pids );
52      }
53      
54      public void testForkModeOnce() throws Exception 
55      {
56          String[] pids = doTest( "once" );
57          // DGF It would be nice to assert that "once" was different
58          // from "never" ... but there's no way to check the PID of
59          // Maven itself.  No matter, "once" is tested by setting
60          // argLine, which can't be done except by forking.
61          assertSamePids( pids );
62      }
63  
64      private void assertSamePids ( String[] pids ) {
65          assertEquals ("pid 1 didn't match pid 2", pids[0], pids[1]);
66          assertEquals ("pid 1 didn't match pid 3", pids[0], pids[2]);
67      }
68      
69      private void assertDifferentPids( String[] pids )
70      {
71          if (pids[0].equals( pids[1] )) {
72              fail ("pid 1 matched pid 2: " + pids[0]);
73          }
74          
75          if (pids[0].equals( pids[2] )) {
76              fail ("pid 1 matched pid 3: " + pids[0]);
77          }
78          
79          if (pids[1].equals( pids[2] )) {
80              fail ("pid 2 matched pid 3: " + pids[0]);
81          }
82      }
83  
84      private String[] doTest(String forkMode)
85          throws IOException, VerificationException, MavenReportException
86      {
87          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/fork-mode" );
88  
89          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
90          List goals = new ArrayList();
91          goals.add( "test" );
92          goals.add( "-DforkMode=" + forkMode );
93          verifier.executeGoals( goals );
94          verifier.verifyErrorFreeLog();
95          verifier.resetStreams();
96          
97          HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, testDir );
98          
99          File targetDir = new File(testDir, "target" );
100         String[] pids = new String[3];
101         for (int i = 1; i <= pids.length; i++) {
102             File pidFile = new File(targetDir, "test" + i + "-pid");
103             String pid = slurpFile( pidFile );
104             pids[i-1] = pid;
105         }
106         return pids;
107     }
108     
109     private String slurpFile(File textFile) throws IOException {
110         StringBuffer sb = new StringBuffer();
111         BufferedReader reader = new BufferedReader(new FileReader(textFile));
112         for (String line = reader.readLine(); line != null; line = reader.readLine()) {
113             sb.append( line );
114         }
115         reader.close();
116         return sb.toString();
117     }
118 }