View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.booterclient;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.List;
29  
30  import junit.framework.Assert;
31  import junit.framework.TestCase;
32  import org.apache.maven.surefire.api.booter.Shutdown;
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.apache.maven.surefire.booter.BooterDeserializer;
43  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
44  import org.apache.maven.surefire.booter.ClasspathConfiguration;
45  import org.apache.maven.surefire.booter.PropertiesWrapper;
46  import org.apache.maven.surefire.booter.ProviderConfiguration;
47  import org.apache.maven.surefire.booter.StartupConfiguration;
48  import org.apache.maven.surefire.booter.TypeEncodedValue;
49  import org.apache.maven.surefire.shared.io.FileUtils;
50  import org.junit.After;
51  import org.junit.Before;
52  
53  import static org.apache.maven.surefire.api.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
54  import static org.apache.maven.surefire.api.cli.CommandLineOption.REACTOR_FAIL_FAST;
55  import static org.apache.maven.surefire.api.cli.CommandLineOption.SHOW_ERRORS;
56  import static org.apache.maven.surefire.booter.ProcessCheckerType.ALL;
57  
58  /**
59   * Performs roundtrip testing of serialization/deserialization of the ProviderConfiguration
60   *
61   * @author Kristian Rosenvold
62   */
63  public class BooterDeserializerProviderConfigurationTest extends TestCase {
64  
65      static final TypeEncodedValue TEST_TYPED = new TypeEncodedValue(String.class.getName(), "aTest");
66  
67      private static final String USER_REQUESTED_TEST = "aUserRequestedTest";
68  
69      private static final String USER_REQUESTED_TEST_METHOD = "aUserRequestedTestMethod";
70  
71      private static final int RERUN_FAILING_TEST_COUNT = 3;
72  
73      private static int idx = 0;
74  
75      private File basedir;
76  
77      private final List<CommandLineOption> cli = Arrays.asList(LOGGING_LEVEL_DEBUG, SHOW_ERRORS, REACTOR_FAIL_FAST);
78  
79      @Before
80      public void setupDirectories() throws IOException {
81          File target = new File(System.getProperty("user.dir"), "target");
82          basedir = new File(target, "BooterDeserializerProviderConfigurationTest-" + ++idx);
83          FileUtils.deleteDirectory(basedir);
84          assertTrue(basedir.mkdirs());
85      }
86  
87      @After
88      public void deleteDirectories() throws IOException {
89          FileUtils.deleteDirectory(basedir);
90      }
91  
92      private static ClassLoaderConfiguration getForkConfiguration() {
93          return new ClassLoaderConfiguration(true, false);
94      }
95  
96      // ProviderConfiguration methods
97      public void testDirectoryScannerParams() throws IOException {
98  
99          File aDir = new File(".");
100         List<String> includes = new ArrayList<>();
101         List<String> excludes = new ArrayList<>();
102         includes.add("abc");
103         includes.add("cde");
104         excludes.add("xx1");
105         excludes.add("xx2");
106 
107         ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
108         final StartupConfiguration testStartupConfiguration = getTestStartupConfiguration(forkConfiguration);
109         ProviderConfiguration providerConfiguration = getReloadedProviderConfiguration();
110         ProviderConfiguration read = saveAndReload(providerConfiguration, testStartupConfiguration, false);
111 
112         Assert.assertEquals(aDir, read.getBaseDir());
113         Assert.assertEquals(includes.get(0), read.getIncludes().get(0));
114         Assert.assertEquals(includes.get(1), read.getIncludes().get(1));
115         Assert.assertEquals(excludes.get(0), read.getExcludes().get(0));
116         Assert.assertEquals(excludes.get(1), read.getExcludes().get(1));
117         assertEquals(cli, providerConfiguration.getMainCliOptions());
118     }
119 
120     public void testReporterConfiguration() throws IOException {
121         DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
122         ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
123 
124         ProviderConfiguration providerConfiguration = getTestProviderConfiguration(directoryScannerParameters, false);
125 
126         final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration(forkConfiguration);
127         ProviderConfiguration reloaded = saveAndReload(providerConfiguration, testProviderConfiguration, false);
128 
129         assertTrue(reloaded.getReporterConfiguration().isTrimStackTrace());
130         assertNotNull(reloaded.getReporterConfiguration().getReportsDirectory());
131         assertEquals(cli, providerConfiguration.getMainCliOptions());
132     }
133 
134     public void testTestArtifact() throws IOException {
135         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
136 
137         Assert.assertEquals("5.0", reloaded.getTestArtifact().getVersion());
138         Assert.assertEquals("ABC", reloaded.getTestArtifact().getClassifier());
139         assertEquals(cli, reloaded.getMainCliOptions());
140     }
141 
142     public void testTestRequest() throws IOException {
143         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
144 
145         TestRequest testSuiteDefinition = reloaded.getTestSuiteDefinition();
146         List<?> suiteXmlFiles = testSuiteDefinition.getSuiteXmlFiles();
147         File[] expected = getSuiteXmlFiles();
148         Assert.assertEquals(expected[0], suiteXmlFiles.get(0));
149         Assert.assertEquals(expected[1], suiteXmlFiles.get(1));
150         Assert.assertEquals(getTestSourceDirectory(), testSuiteDefinition.getTestSourceDirectory());
151         TestListResolver resolver = testSuiteDefinition.getTestListResolver();
152         Assert.assertNotNull(resolver);
153         Assert.assertFalse(resolver.isEmpty());
154         Assert.assertEquals(USER_REQUESTED_TEST + "#" + USER_REQUESTED_TEST_METHOD, resolver.getPluginParameterTest());
155         Assert.assertFalse(resolver.getIncludedPatterns().isEmpty());
156         Assert.assertTrue(resolver.getExcludedPatterns().isEmpty());
157         Assert.assertEquals(1, resolver.getIncludedPatterns().size());
158         ResolvedTest filter = resolver.getIncludedPatterns().iterator().next();
159         Assert.assertNotNull(filter);
160         Assert.assertEquals("**/" + USER_REQUESTED_TEST, filter.getTestClassPattern());
161         Assert.assertEquals(USER_REQUESTED_TEST_METHOD, filter.getTestMethodPattern());
162         Assert.assertEquals(RERUN_FAILING_TEST_COUNT, testSuiteDefinition.getRerunFailingTestsCount());
163         assertEquals(cli, reloaded.getMainCliOptions());
164     }
165 
166     public void testTestForFork() throws IOException {
167         final ProviderConfiguration reloaded = getReloadedProviderConfiguration();
168         Assert.assertEquals(TEST_TYPED, reloaded.getTestForFork());
169         assertEquals(cli, reloaded.getMainCliOptions());
170     }
171 
172     public void testTestForForkWithMultipleFiles() throws IOException {
173         final ProviderConfiguration reloaded = getReloadedProviderConfigurationForReadFromInStream();
174         Assert.assertNull(reloaded.getTestForFork());
175         Assert.assertTrue(reloaded.isReadTestsFromInStream());
176         assertEquals(cli, reloaded.getMainCliOptions());
177     }
178 
179     public void testFailIfNoTests() throws IOException {
180         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
181         assertEquals(cli, reloaded.getMainCliOptions());
182     }
183 
184     private ProviderConfiguration getReloadedProviderConfigurationForReadFromInStream() throws IOException {
185         return getReloadedProviderConfiguration(true);
186     }
187 
188     private ProviderConfiguration getReloadedProviderConfiguration() throws IOException {
189         return getReloadedProviderConfiguration(false);
190     }
191 
192     private ProviderConfiguration getReloadedProviderConfiguration(boolean readTestsFromInStream) throws IOException {
193         DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
194         ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
195         ProviderConfiguration booterConfiguration =
196                 getTestProviderConfiguration(directoryScannerParameters, readTestsFromInStream);
197         assertEquals(cli, booterConfiguration.getMainCliOptions());
198         final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration(forkConfiguration);
199         return saveAndReload(booterConfiguration, testProviderConfiguration, readTestsFromInStream);
200     }
201 
202     private DirectoryScannerParameters getDirectoryScannerParametersWithoutSpecificTests() {
203         File aDir = new File(".");
204         List<String> includes = new ArrayList<>();
205         List<String> excludes = new ArrayList<>();
206         includes.add("abc");
207         includes.add("cde");
208         excludes.add("xx1");
209         excludes.add("xx2");
210 
211         return new DirectoryScannerParameters(
212                 aDir, includes, excludes, Collections.<String>emptyList(), RunOrder.asString(RunOrder.DEFAULT));
213     }
214 
215     private ProviderConfiguration saveAndReload(
216             ProviderConfiguration booterConfiguration,
217             StartupConfiguration testProviderConfiguration,
218             boolean readTestsFromInStream)
219             throws IOException {
220         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration(basedir, null);
221         PropertiesWrapper props = new PropertiesWrapper(new HashMap<String, String>());
222         BooterSerializer booterSerializer = new BooterSerializer(forkConfiguration);
223         Object test;
224         if (readTestsFromInStream) {
225             test = null;
226         } else {
227             test = "aTest";
228         }
229         final File propsTest = booterSerializer.serialize(
230                 props, booterConfiguration, testProviderConfiguration, test, readTestsFromInStream, 51L, 1, "pipe://1");
231         BooterDeserializer booterDeserializer = new BooterDeserializer(new FileInputStream(propsTest));
232         assertEquals("51", (Object) booterDeserializer.getPluginPid());
233         assertEquals("pipe://1", booterDeserializer.getConnectionString());
234         return booterDeserializer.deserialize();
235     }
236 
237     private ProviderConfiguration getTestProviderConfiguration(
238             DirectoryScannerParameters directoryScannerParameters, boolean readTestsFromInStream) {
239         File cwd = new File(".");
240         ReporterConfiguration reporterConfiguration = new ReporterConfiguration(cwd, true);
241         TestRequest testSuiteDefinition = new TestRequest(
242                 getSuiteXmlFileStrings(),
243                 getTestSourceDirectory(),
244                 new TestListResolver(USER_REQUESTED_TEST + "#aUserRequestedTestMethod"),
245                 RERUN_FAILING_TEST_COUNT);
246         RunOrderParameters runOrderParameters = new RunOrderParameters(RunOrder.DEFAULT, null);
247         return new ProviderConfiguration(
248                 directoryScannerParameters,
249                 runOrderParameters,
250                 reporterConfiguration,
251                 new TestArtifactInfo("5.0", "ABC"),
252                 testSuiteDefinition,
253                 new HashMap<String, String>(),
254                 TEST_TYPED,
255                 readTestsFromInStream,
256                 cli,
257                 0,
258                 Shutdown.DEFAULT,
259                 0);
260     }
261 
262     private StartupConfiguration getTestStartupConfiguration(ClassLoaderConfiguration classLoaderConfiguration) {
263         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration(true, true);
264 
265         return new StartupConfiguration(
266                 "com.provider",
267                 classpathConfiguration,
268                 classLoaderConfiguration,
269                 ALL,
270                 Collections.<String[]>emptyList());
271     }
272 
273     private File getTestSourceDirectory() {
274         return new File("TestSrc");
275     }
276 
277     private File[] getSuiteXmlFiles() {
278         return new File[] {new File("A1"), new File("A2")};
279     }
280 
281     private List<String> getSuiteXmlFileStrings() {
282         return Arrays.asList("A1", "A2");
283     }
284 }