1 package org.apache.maven.plugin.surefire.booterclient;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.Assert;
23 import junit.framework.TestCase;
24 import org.apache.maven.surefire.shared.io.FileUtils;
25 import org.apache.maven.surefire.booter.BooterDeserializer;
26 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
27 import org.apache.maven.surefire.booter.ClasspathConfiguration;
28 import org.apache.maven.surefire.booter.PropertiesWrapper;
29 import org.apache.maven.surefire.booter.ProviderConfiguration;
30 import org.apache.maven.surefire.api.booter.Shutdown;
31 import org.apache.maven.surefire.booter.StartupConfiguration;
32 import org.apache.maven.surefire.booter.TypeEncodedValue;
33 import org.apache.maven.surefire.api.cli.CommandLineOption;
34 import org.apache.maven.surefire.api.report.ReporterConfiguration;
35 import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
36 import org.apache.maven.surefire.api.testset.ResolvedTest;
37 import org.apache.maven.surefire.api.testset.RunOrderParameters;
38 import org.apache.maven.surefire.api.testset.TestArtifactInfo;
39 import org.apache.maven.surefire.api.testset.TestListResolver;
40 import org.apache.maven.surefire.api.testset.TestRequest;
41 import org.apache.maven.surefire.api.util.RunOrder;
42 import org.junit.After;
43 import org.junit.Before;
44
45 import java.io.File;
46 import java.io.FileInputStream;
47 import java.io.IOException;
48 import java.util.Arrays;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.HashMap;
52 import java.util.List;
53
54 import static org.apache.maven.surefire.booter.ProcessCheckerType.ALL;
55 import static org.apache.maven.surefire.api.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
56 import static org.apache.maven.surefire.api.cli.CommandLineOption.REACTOR_FAIL_FAST;
57 import static org.apache.maven.surefire.api.cli.CommandLineOption.SHOW_ERRORS;
58
59
60
61
62
63
64 public class BooterDeserializerProviderConfigurationTest
65 extends TestCase
66 {
67
68 static final TypeEncodedValue TEST_TYPED = new TypeEncodedValue( String.class.getName(), "aTest" );
69
70 private static final String USER_REQUESTED_TEST = "aUserRequestedTest";
71
72 private static final String USER_REQUESTED_TEST_METHOD = "aUserRequestedTestMethod";
73
74 private static final int RERUN_FAILING_TEST_COUNT = 3;
75
76 private static int idx = 0;
77
78 private File basedir;
79
80 private final List<CommandLineOption> cli =
81 Arrays.asList( LOGGING_LEVEL_DEBUG, SHOW_ERRORS, REACTOR_FAIL_FAST );
82
83 @Before
84 public void setupDirectories() throws IOException
85 {
86 File target = new File( System.getProperty( "user.dir" ), "target" );
87 basedir = new File( target, "BooterDeserializerProviderConfigurationTest-" + ++idx );
88 FileUtils.deleteDirectory( basedir );
89 assertTrue( basedir.mkdirs() );
90 }
91
92 @After
93 public void deleteDirectories() throws IOException
94 {
95 FileUtils.deleteDirectory( basedir );
96 }
97
98 private static ClassLoaderConfiguration getForkConfiguration()
99 {
100 return new ClassLoaderConfiguration( true, false );
101 }
102
103
104 public void testDirectoryScannerParams()
105 throws IOException
106 {
107
108 File aDir = new File( "." );
109 List<String> includes = new ArrayList<>();
110 List<String> excludes = new ArrayList<>();
111 includes.add( "abc" );
112 includes.add( "cde" );
113 excludes.add( "xx1" );
114 excludes.add( "xx2" );
115
116 ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
117 final StartupConfiguration testStartupConfiguration = getTestStartupConfiguration( forkConfiguration );
118 ProviderConfiguration providerConfiguration = getReloadedProviderConfiguration();
119 ProviderConfiguration read = saveAndReload( providerConfiguration, testStartupConfiguration, false );
120
121 Assert.assertEquals( aDir, read.getBaseDir() );
122 Assert.assertEquals( includes.get( 0 ), read.getIncludes().get( 0 ) );
123 Assert.assertEquals( includes.get( 1 ), read.getIncludes().get( 1 ) );
124 Assert.assertEquals( excludes.get( 0 ), read.getExcludes().get( 0 ) );
125 Assert.assertEquals( excludes.get( 1 ), read.getExcludes().get( 1 ) );
126 assertEquals( cli, providerConfiguration.getMainCliOptions() );
127 }
128
129 public void testReporterConfiguration()
130 throws IOException
131 {
132 DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
133 ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
134
135 ProviderConfiguration providerConfiguration = getTestProviderConfiguration( directoryScannerParameters, false );
136
137 final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
138 ProviderConfiguration reloaded = saveAndReload( providerConfiguration, testProviderConfiguration, false );
139
140 assertTrue( reloaded.getReporterConfiguration().isTrimStackTrace() );
141 assertNotNull( reloaded.getReporterConfiguration().getReportsDirectory() );
142 assertEquals( cli, providerConfiguration.getMainCliOptions() );
143 }
144
145 public void testTestArtifact()
146 throws IOException
147 {
148 ProviderConfiguration reloaded = getReloadedProviderConfiguration();
149
150 Assert.assertEquals( "5.0", reloaded.getTestArtifact().getVersion() );
151 Assert.assertEquals( "ABC", reloaded.getTestArtifact().getClassifier() );
152 assertEquals( cli, reloaded.getMainCliOptions() );
153 }
154
155 public void testTestRequest()
156 throws IOException
157 {
158 ProviderConfiguration reloaded = getReloadedProviderConfiguration();
159
160 TestRequest testSuiteDefinition = reloaded.getTestSuiteDefinition();
161 List<?> suiteXmlFiles = testSuiteDefinition.getSuiteXmlFiles();
162 File[] expected = getSuiteXmlFiles();
163 Assert.assertEquals( expected[0], suiteXmlFiles.get( 0 ) );
164 Assert.assertEquals( expected[1], suiteXmlFiles.get( 1 ) );
165 Assert.assertEquals( getTestSourceDirectory(), testSuiteDefinition.getTestSourceDirectory() );
166 TestListResolver resolver = testSuiteDefinition.getTestListResolver();
167 Assert.assertNotNull( resolver );
168 Assert.assertFalse( resolver.isEmpty() );
169 Assert.assertEquals( USER_REQUESTED_TEST + "#" + USER_REQUESTED_TEST_METHOD,
170 resolver.getPluginParameterTest() );
171 Assert.assertFalse( resolver.getIncludedPatterns().isEmpty() );
172 Assert.assertTrue( resolver.getExcludedPatterns().isEmpty() );
173 Assert.assertEquals( 1, resolver.getIncludedPatterns().size() );
174 ResolvedTest filter = resolver.getIncludedPatterns().iterator().next();
175 Assert.assertNotNull( filter );
176 Assert.assertEquals( "**/" + USER_REQUESTED_TEST, filter.getTestClassPattern() );
177 Assert.assertEquals( USER_REQUESTED_TEST_METHOD, filter.getTestMethodPattern() );
178 Assert.assertEquals( RERUN_FAILING_TEST_COUNT, testSuiteDefinition.getRerunFailingTestsCount() );
179 assertEquals( cli, reloaded.getMainCliOptions() );
180 }
181
182 public void testTestForFork()
183 throws IOException
184 {
185 final ProviderConfiguration reloaded = getReloadedProviderConfiguration();
186 Assert.assertEquals( TEST_TYPED, reloaded.getTestForFork() );
187 assertEquals( cli, reloaded.getMainCliOptions() );
188 }
189
190 public void testTestForForkWithMultipleFiles()
191 throws IOException
192 {
193 final ProviderConfiguration reloaded = getReloadedProviderConfigurationForReadFromInStream();
194 Assert.assertNull( reloaded.getTestForFork() );
195 Assert.assertTrue( reloaded.isReadTestsFromInStream() );
196 assertEquals( cli, reloaded.getMainCliOptions() );
197 }
198
199 public void testFailIfNoTests()
200 throws IOException
201 {
202 ProviderConfiguration reloaded = getReloadedProviderConfiguration();
203 assertTrue( reloaded.isFailIfNoTests() );
204 assertEquals( cli, reloaded.getMainCliOptions() );
205 }
206
207 private ProviderConfiguration getReloadedProviderConfigurationForReadFromInStream()
208 throws IOException
209 {
210 return getReloadedProviderConfiguration( true );
211 }
212
213 private ProviderConfiguration getReloadedProviderConfiguration()
214 throws IOException
215 {
216 return getReloadedProviderConfiguration( false );
217 }
218
219 private ProviderConfiguration getReloadedProviderConfiguration( boolean readTestsFromInStream )
220 throws IOException
221 {
222 DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
223 ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
224 ProviderConfiguration booterConfiguration =
225 getTestProviderConfiguration( directoryScannerParameters, readTestsFromInStream );
226 assertEquals( cli, booterConfiguration.getMainCliOptions() );
227 final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
228 return saveAndReload( booterConfiguration, testProviderConfiguration, readTestsFromInStream );
229 }
230
231 private DirectoryScannerParameters getDirectoryScannerParametersWithoutSpecificTests()
232 {
233 File aDir = new File( "." );
234 List<String> includes = new ArrayList<>();
235 List<String> excludes = new ArrayList<>();
236 includes.add( "abc" );
237 includes.add( "cde" );
238 excludes.add( "xx1" );
239 excludes.add( "xx2" );
240
241 return new DirectoryScannerParameters( aDir, includes, excludes, Collections.<String>emptyList(), true,
242 RunOrder.asString( RunOrder.DEFAULT ) );
243 }
244
245 private ProviderConfiguration saveAndReload( ProviderConfiguration booterConfiguration,
246 StartupConfiguration testProviderConfiguration,
247 boolean readTestsFromInStream )
248 throws IOException
249 {
250 final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration( basedir, null );
251 PropertiesWrapper props = new PropertiesWrapper( new HashMap<String, String>() );
252 BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
253 Object test;
254 if ( readTestsFromInStream )
255 {
256 test = null;
257 }
258 else
259 {
260 test = "aTest";
261 }
262 final File propsTest = booterSerializer.serialize( props, booterConfiguration, testProviderConfiguration, test,
263 readTestsFromInStream, 51L, 1, "pipe://1" );
264 BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
265 assertEquals( "51", (Object) booterDeserializer.getPluginPid() );
266 assertEquals( "pipe://1", booterDeserializer.getConnectionString() );
267 return booterDeserializer.deserialize();
268 }
269
270 private ProviderConfiguration getTestProviderConfiguration( DirectoryScannerParameters directoryScannerParameters,
271 boolean readTestsFromInStream )
272 {
273 File cwd = new File( "." );
274 ReporterConfiguration reporterConfiguration = new ReporterConfiguration( cwd, true );
275 TestRequest testSuiteDefinition =
276 new TestRequest( getSuiteXmlFileStrings(), getTestSourceDirectory(),
277 new TestListResolver( USER_REQUESTED_TEST + "#aUserRequestedTestMethod" ),
278 RERUN_FAILING_TEST_COUNT );
279 RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
280 return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, true, reporterConfiguration,
281 new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new HashMap<String, String>(), TEST_TYPED,
282 readTestsFromInStream, cli, 0, Shutdown.DEFAULT, 0 );
283 }
284
285 private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
286 {
287 ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( true, true );
288
289 return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, ALL,
290 Collections.<String[]>emptyList() );
291 }
292
293 private File getTestSourceDirectory()
294 {
295 return new File( "TestSrc" );
296 }
297
298 private File[] getSuiteXmlFiles()
299 {
300 return new File[]{ new File( "A1" ), new File( "A2" ) };
301 }
302
303 private List<String> getSuiteXmlFileStrings()
304 {
305 return Arrays.asList( "A1", "A2" );
306 }
307 }