View Javadoc
1   package org.apache.maven.surefire.its;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.util.Properties;
26  import org.apache.maven.it.VerificationException;
27  import org.apache.maven.surefire.its.fixture.*;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Test working directory configuration, SUREFIRE-416
36   *
37   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
38   * @author <a href="mailto:krosenvold@apache.org">Kristian Rosenvold</a>
39   */
40  public class WorkingDirectoryIT
41      extends SurefireJUnit4IntegrationTestCase
42  {
43  
44      @Test
45      public void testWorkingDirectory()
46          throws Exception
47      {
48          final SurefireLauncher unpack = getUnpacked();
49          final OutputValidator child = getPreparedChild( unpack );
50          unpack.executeTest().verifyErrorFreeLog();
51          child.assertTestSuiteResults( 1, 0, 0, 0 );
52          verifyOutputDirectory( child );
53      }
54  
55      @Test
56      public void testWorkingDirectoryNoFork()
57          throws Exception
58      {
59          final SurefireLauncher unpack = getUnpacked();
60          final OutputValidator child = getPreparedChild( unpack );
61          unpack.forkNever().executeTest().verifyErrorFreeLog();
62          child.assertTestSuiteResults( 1, 0, 0, 0 );
63          verifyOutputDirectory( child );
64      }
65  
66      @Test
67      public void testWorkingDirectoryChildOnly()
68          throws Exception
69      {
70          final SurefireLauncher unpack = getUnpacked();
71          final SurefireLauncher child = unpack.getSubProjectLauncher( "child" );
72          //child.getTargetFile( "out.txt" ).delete();
73          final OutputValidator outputValidator = child.executeTest().assertTestSuiteResults( 1, 0, 0, 0 );
74          verifyOutputDirectory( outputValidator );
75      }
76  
77      @Test
78      public void testWorkingDirectoryChildOnlyNoFork()
79          throws Exception
80      {
81  
82          final SurefireLauncher unpack = getUnpacked();
83          final SurefireLauncher child = unpack.getSubProjectLauncher( "child" );
84          //child.getTargetFile( "out.txt" ).delete();
85          final OutputValidator outputValidator = child.forkNever().executeTest().assertTestSuiteResults( 1, 0, 0, 0 );
86          verifyOutputDirectory( outputValidator );
87      }
88  
89      private SurefireLauncher getUnpacked()
90          throws VerificationException, IOException
91      {
92          return unpack( "working-directory" );
93      }
94  
95      private OutputValidator getPreparedChild( SurefireLauncher unpack )
96          throws VerificationException
97      {
98          final OutputValidator child = unpack.getSubProjectValidator( "child" );
99          getOutFile( child ).delete();
100         return child;
101     }
102 
103 
104     private TestFile getOutFile( OutputValidator child )
105     {
106         return child.getTargetFile( "out.txt" );
107     }
108 
109     public void verifyOutputDirectory( OutputValidator childTestDir )
110         throws IOException
111     {
112         final TestFile outFile = getOutFile( childTestDir );
113         assertTrue( "out.txt doesn't exist: " + outFile.getAbsolutePath(), outFile.exists() );
114         Properties p = new Properties();
115         FileInputStream is = outFile.getFileInputStream();
116         p.load( is );
117         is.close();
118         String userDirPath = p.getProperty( "user.dir" );
119         assertNotNull( "user.dir was null in property file", userDirPath );
120         File userDir = new File( userDirPath );
121         // test if not a symlink
122         if ( childTestDir.getBaseDir().getCanonicalFile().equals( childTestDir.getBaseDir().getAbsoluteFile() ) )
123         {
124             assertTrue( "wrong user.dir ! symlink ",
125                         childTestDir.getBaseDir().getAbsolutePath().equalsIgnoreCase( userDir.getAbsolutePath() ) );
126         }
127         else
128         {
129             assertEquals( "wrong user.dir symlink ", childTestDir.getBaseDir().getCanonicalPath(),
130                           userDir.getCanonicalPath() );
131         }
132     }
133 
134 }