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 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
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 }