View Javadoc

1   package org.apache.maven.surefire.its;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.maven.it.VerificationException;
22  import org.apache.maven.it.Verifier;
23  import org.apache.maven.it.util.ResourceExtractor;
24  import org.apache.maven.surefire.its.misc.HelperAssertions;
25  
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.FileReader;
29  import java.io.IOException;
30  import java.util.List;
31  
32  /**
33   * Test forkMode
34   *
35   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
36   */
37  public class ForkModeIT
38      extends AbstractSurefireIntegrationTestClass
39  {
40      public void testForkModeAlways()
41          throws Exception
42      {
43          String[] pids = doTest( "always" );
44  
45          assertDifferentPids( pids );
46      }
47  
48      public void testForkModePerTest()
49          throws Exception
50      {
51          String[] pids = doTest( "pertest" );
52  
53          assertDifferentPids( pids );
54      }
55  
56      public void testForkModeNever()
57          throws Exception
58      {
59          String[] pids = doTest( "never" );
60  
61          assertSamePids( pids );
62      }
63  
64      public void testForkModeNone()
65          throws Exception
66      {
67          String[] pids = doTest( "none" );
68  
69          assertSamePids( pids );
70      }
71  
72      public void testForkModeOnce()
73          throws Exception
74      {
75          String[] pids = doTest( "once" );
76          // DGF It would be nice to assert that "once" was different
77          // from "never" ... but there's no way to check the PID of
78          // Maven itself.  No matter, "once" is tested by setting
79          // argLine, which can't be done except by forking.
80          assertSamePids( pids );
81      }
82  
83      private void assertSamePids( String[] pids )
84      {
85          assertEquals( "pid 1 didn't match pid 2", pids[0], pids[1] );
86          assertEquals( "pid 1 didn't match pid 3", pids[0], pids[2] );
87      }
88  
89      private void assertDifferentPids( String[] pids )
90      {
91          if ( pids[0].equals( pids[1] ) )
92          {
93              fail( "pid 1 matched pid 2: " + pids[0] );
94          }
95  
96          if ( pids[0].equals( pids[2] ) )
97          {
98              fail( "pid 1 matched pid 3: " + pids[0] );
99          }
100 
101         if ( pids[1].equals( pids[2] ) )
102         {
103             fail( "pid 2 matched pid 3: " + pids[0] );
104         }
105     }
106 
107     private String[] doTest( String forkMode )
108         throws IOException, VerificationException
109     {
110         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/fork-mode" );
111 
112         Verifier verifier = new Verifier( testDir.getAbsolutePath() );
113         List<String> goals = this.getInitialGoals();
114         goals.add( "test" );
115         goals.add( "-DforkMode=" + forkMode );
116         executeGoals( verifier, goals );
117         verifier.verifyErrorFreeLog();
118         verifier.resetStreams();
119 
120         HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, testDir );
121 
122         File targetDir = new File( testDir, "target" );
123         String[] pids = new String[3];
124         for ( int i = 1; i <= pids.length; i++ )
125         {
126             File pidFile = new File( targetDir, "test" + i + "-pid" );
127             String pid = slurpFile( pidFile );
128             pids[i - 1] = pid;
129         }
130         return pids;
131     }
132 
133     private String slurpFile( File textFile )
134         throws IOException
135     {
136         StringBuffer sb = new StringBuffer();
137         BufferedReader reader = new BufferedReader( new FileReader( textFile ) );
138         for ( String line = reader.readLine(); line != null; line = reader.readLine() )
139         {
140             sb.append( line );
141         }
142         reader.close();
143         return sb.toString();
144     }
145 }