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          child.getTargetFile( "out.txt" ).delete();
51          unpack.executeTest().verifyErrorFreeLog();
52          child.assertTestSuiteResults( 1, 0, 0, 0 );
53          verifyOutputDirectory( child );
54      }
55  
56      @Test
57      public void testWorkingDirectoryNoFork()
58          throws Exception
59      {
60          final SurefireLauncher unpack = getUnpacked();
61          final OutputValidator child = getPreparedChild( unpack );
62          child.getTargetFile( "out.txt" ).delete();
63          unpack.forkNever().executeTest().verifyErrorFreeLog();
64          child.assertTestSuiteResults( 1, 0, 0, 0 );
65          verifyOutputDirectory( child );
66      }
67  
68      @Test
69      public void testWorkingDirectoryChildOnly()
70          throws Exception
71      {
72          final SurefireLauncher unpack = getUnpacked();
73          final SurefireLauncher child = unpack.getSubProjectLauncher( "child" );
74          child.getSubProjectValidator( "child" ).getTargetFile( "out.txt" ).delete();
75          final OutputValidator outputValidator = child.executeTest().assertTestSuiteResults( 1, 0, 0, 0 );
76          verifyOutputDirectory( outputValidator );
77      }
78  
79      @Test
80      public void testWorkingDirectoryChildOnlyNoFork()
81          throws Exception
82      {
83          final SurefireLauncher unpack = getUnpacked();
84          final SurefireLauncher child = unpack.getSubProjectLauncher( "child" );
85          child.getSubProjectValidator( "child" ).getTargetFile( "out.txt" ).delete();
86          final OutputValidator outputValidator = child.forkNever().executeTest().assertTestSuiteResults( 1, 0, 0, 0 );
87          verifyOutputDirectory( outputValidator );
88      }
89  
90      private SurefireLauncher getUnpacked()
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     private 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         try ( FileInputStream is = outFile.getFileInputStream() )
116         {
117             p.load( is );
118         }
119         String userDirPath = p.getProperty( "user.dir" );
120         assertNotNull( "user.dir was null in property file", userDirPath );
121         File userDir = new File( userDirPath );
122         // test if not a symlink
123         if ( childTestDir.getBaseDir().getCanonicalFile().equals( childTestDir.getBaseDir().getAbsoluteFile() ) )
124         {
125             assertTrue( "wrong user.dir ! symlink ",
126                         childTestDir.getBaseDir().getAbsolutePath().equalsIgnoreCase( userDir.getAbsolutePath() ) );
127         }
128         else
129         {
130             assertEquals( "wrong user.dir symlink ", childTestDir.getBaseDir().getCanonicalPath(),
131                           userDir.getCanonicalPath() );
132         }
133     }
134 }