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 java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.maven.it.Verifier;
28  import org.apache.maven.it.util.ResourceExtractor;
29  import org.apache.maven.surefire.its.misc.HelperAssertions;
30  
31  /**
32   * Test working directory configuration, SUREFIRE-416
33   *
34   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
35   */
36  public class WorkingDirectoryIT
37      extends AbstractSurefireIntegrationTestClass
38  {
39  
40      private File testDir;
41  
42      private File childTestDir;
43  
44      private File targetDir;
45  
46      private File outFile;
47  
48      public void setUp()
49          throws IOException
50      {
51          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/working-directory" );
52          childTestDir = new File( testDir, "child" );
53          targetDir = new File( childTestDir, "target" );
54          outFile = new File( targetDir, "out.txt" );
55          outFile.delete();
56      }
57  
58      public void testWorkingDirectory()
59          throws Exception
60      {
61          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
62          this.executeGoal( verifier, "test" );
63          verifier.verifyErrorFreeLog();
64          verifier.resetStreams();
65  
66          HelperAssertions.assertTestSuiteResults( 1, 0, 0, 0, childTestDir );
67          verifyOutputDirectory( childTestDir );
68      }
69  
70      public void verifyOutputDirectory( File childTestDir )
71          throws IOException
72      {
73          assertTrue( "out.txt doesn't exist: " + outFile.getAbsolutePath(), outFile.exists() );
74          Properties p = new Properties();
75          FileInputStream is = new FileInputStream( outFile );
76          p.load( is );
77          is.close();
78          String userDirPath = p.getProperty( "user.dir" );
79          assertNotNull( "user.dir was null in property file", userDirPath );
80          File userDir = new File( userDirPath );
81          // test if not a symlink
82          if ( childTestDir.getCanonicalFile().equals( childTestDir.getAbsoluteFile() ) )
83          {
84              assertEquals( "wrong user.dir ! symlink ", childTestDir.getAbsolutePath(), userDir.getAbsolutePath() );
85          }
86          else
87          {
88              assertEquals( "wrong user.dir symlink ", childTestDir.getCanonicalPath(), userDir.getCanonicalPath() );
89          }
90      }
91  
92      public void testWorkingDirectoryNoFork()
93          throws Exception
94      {
95          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
96          List<String> goals = this.getInitialGoals();
97          goals.add( "test" );
98          goals.add( "-DforkMode=never" );
99          executeGoals( verifier, goals );
100         verifier.verifyErrorFreeLog();
101         verifier.resetStreams();
102 
103         HelperAssertions.assertTestSuiteResults( 1, 0, 0, 0, childTestDir );
104         verifyOutputDirectory( childTestDir );
105     }
106 
107     public void testWorkingDirectoryChildOnly()
108         throws Exception
109     {
110         Verifier verifier = new Verifier( childTestDir.getAbsolutePath() );
111         this.executeGoal( verifier, "test" );
112         verifier.verifyErrorFreeLog();
113         verifier.resetStreams();
114 
115         HelperAssertions.assertTestSuiteResults( 1, 0, 0, 0, childTestDir );
116         verifyOutputDirectory( childTestDir );
117     }
118 
119     public void testWorkingDirectoryChildOnlyNoFork()
120         throws Exception
121     {
122 
123         Verifier verifier = new Verifier( childTestDir.getAbsolutePath() );
124         List<String> goals = this.getInitialGoals();
125         goals.add( "test" );
126         goals.add( "-DforkMode=never" );
127         executeGoals( verifier, goals );
128         verifier.verifyErrorFreeLog();
129         verifier.resetStreams();
130 
131         HelperAssertions.assertTestSuiteResults( 1, 0, 0, 0, childTestDir );
132         verifyOutputDirectory( childTestDir );
133     }
134 }