1 package org.apache.maven.surefire.its;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
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
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
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
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 }