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.TestCase;
31  import org.apache.maven.surefire.api.booter.Shutdown;
32  import org.apache.maven.surefire.api.cli.CommandLineOption;
33  import org.apache.maven.surefire.api.report.ReporterConfiguration;
34  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
35  import org.apache.maven.surefire.api.testset.RunOrderParameters;
36  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
37  import org.apache.maven.surefire.api.testset.TestListResolver;
38  import org.apache.maven.surefire.api.testset.TestRequest;
39  import org.apache.maven.surefire.api.util.RunOrder;
40  import org.apache.maven.surefire.booter.AbstractPathConfiguration;
41  import org.apache.maven.surefire.booter.BooterDeserializer;
42  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
43  import org.apache.maven.surefire.booter.Classpath;
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.shared.io.FileUtils;
49  import org.junit.After;
50  import org.junit.Before;
51  
52  import static org.apache.maven.surefire.api.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
53  import static org.apache.maven.surefire.api.cli.CommandLineOption.REACTOR_FAIL_FAST;
54  import static org.apache.maven.surefire.api.cli.CommandLineOption.SHOW_ERRORS;
55  import static org.apache.maven.surefire.booter.ProcessCheckerType.ALL;
56  
57  /**
58   * Performs roundtrip testing of serialization/deserialization of The StartupConfiguration
59   *
60   * @author Kristian Rosenvold
61   */
62  public class BooterDeserializerStartupConfigurationTest extends TestCase {
63  
64      private static int idx = 0;
65  
66      private File basedir;
67  
68      private final ClasspathConfiguration classpathConfiguration = createClasspathConfiguration();
69  
70      private final List<CommandLineOption> cli = Arrays.asList(LOGGING_LEVEL_DEBUG, SHOW_ERRORS, REACTOR_FAIL_FAST);
71  
72      @Before
73      public void setupDirectories() throws IOException {
74          File target = new File(System.getProperty("user.dir"), "target");
75          basedir = new File(target, "BooterDeserializerProviderConfigurationTest-" + ++idx);
76          FileUtils.deleteDirectory(basedir);
77          assertTrue(basedir.mkdirs());
78      }
79  
80      @After
81      public void deleteDirectories() throws IOException {
82          FileUtils.deleteDirectory(basedir);
83      }
84  
85      public void testProvider() throws IOException {
86          assertEquals("com.provider", getReloadedStartupConfiguration().getProviderClassName());
87      }
88  
89      public void testClassPathConfiguration() throws IOException {
90          AbstractPathConfiguration reloadedClasspathConfiguration =
91                  getReloadedStartupConfiguration().getClasspathConfiguration();
92  
93          assertTrue(reloadedClasspathConfiguration instanceof ClasspathConfiguration);
94          assertCpConfigEquals(classpathConfiguration, (ClasspathConfiguration) reloadedClasspathConfiguration);
95      }
96  
97      public void testProcessChecker() throws IOException {
98          assertEquals(ALL, getReloadedStartupConfiguration().getProcessChecker());
99      }
100 
101     private void assertCpConfigEquals(
102             ClasspathConfiguration expectedConfiguration, ClasspathConfiguration actualConfiguration) {
103         assertEquals(
104                 expectedConfiguration.getTestClasspath().getClassPath(),
105                 actualConfiguration.getTestClasspath().getClassPath());
106         assertEquals(expectedConfiguration.isEnableAssertions(), actualConfiguration.isEnableAssertions());
107         assertEquals(expectedConfiguration.isChildDelegation(), actualConfiguration.isChildDelegation());
108         assertEquals(expectedConfiguration.getProviderClasspath(), actualConfiguration.getProviderClasspath());
109         assertEquals(expectedConfiguration.getTestClasspath(), actualConfiguration.getTestClasspath());
110     }
111 
112     public void testClassLoaderConfiguration() throws IOException {
113         assertFalse(getReloadedStartupConfiguration().isManifestOnlyJarRequestedAndUsable());
114     }
115 
116     public void testClassLoaderConfigurationTrues() throws IOException {
117         final StartupConfiguration testStartupConfiguration =
118                 getTestStartupConfiguration(getManifestOnlyJarForkConfiguration());
119         boolean current = testStartupConfiguration.isManifestOnlyJarRequestedAndUsable();
120         assertEquals(current, saveAndReload(testStartupConfiguration).isManifestOnlyJarRequestedAndUsable());
121     }
122 
123     public void testProcessCheckerAll() throws IOException {
124         assertEquals(ALL, getReloadedStartupConfiguration().getProcessChecker());
125     }
126 
127     public void testProcessCheckerNull() throws IOException {
128         StartupConfiguration startupConfiguration = new StartupConfiguration(
129                 "com.provider",
130                 classpathConfiguration,
131                 getManifestOnlyJarForkConfiguration(),
132                 null,
133                 Collections.<String[]>emptyList());
134         assertNull(saveAndReload(startupConfiguration).getProcessChecker());
135     }
136 
137     private ClasspathConfiguration createClasspathConfiguration() {
138         Classpath testClassPath = new Classpath(Arrays.asList("CP1", "CP2"));
139         Classpath providerClasspath = new Classpath(Arrays.asList("SP1", "SP2"));
140         return new ClasspathConfiguration(testClassPath, providerClasspath, Classpath.emptyClasspath(), true, true);
141     }
142 
143     private static ClassLoaderConfiguration getSystemClassLoaderConfiguration() {
144         return new ClassLoaderConfiguration(true, false);
145     }
146 
147     private static ClassLoaderConfiguration getManifestOnlyJarForkConfiguration() {
148         return new ClassLoaderConfiguration(true, true);
149     }
150 
151     private StartupConfiguration getReloadedStartupConfiguration() throws IOException {
152         ClassLoaderConfiguration classLoaderConfiguration = getSystemClassLoaderConfiguration();
153         return saveAndReload(getTestStartupConfiguration(classLoaderConfiguration));
154     }
155 
156     private StartupConfiguration saveAndReload(StartupConfiguration startupConfiguration) throws IOException {
157         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration(basedir, null);
158         PropertiesWrapper props = new PropertiesWrapper(new HashMap<String, String>());
159         BooterSerializer booterSerializer = new BooterSerializer(forkConfiguration);
160         String aTest = "aTest";
161         File propsTest = booterSerializer.serialize(
162                 props,
163                 getProviderConfiguration(),
164                 startupConfiguration,
165                 aTest,
166                 false,
167                 null,
168                 1,
169                 "tcp://localhost:63003");
170         BooterDeserializer booterDeserializer = new BooterDeserializer(new FileInputStream(propsTest));
171         assertNull(booterDeserializer.getPluginPid());
172         assertEquals("tcp://localhost:63003", booterDeserializer.getConnectionString());
173         return booterDeserializer.getStartupConfiguration();
174     }
175 
176     private ProviderConfiguration getProviderConfiguration() {
177         File cwd = new File(".");
178         DirectoryScannerParameters directoryScannerParameters = new DirectoryScannerParameters(
179                 cwd, new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), "hourly");
180         ReporterConfiguration reporterConfiguration = new ReporterConfiguration(cwd, true);
181         TestRequest testSuiteDefinition = new TestRequest(
182                 Arrays.asList(getSuiteXmlFileStrings()),
183                 getTestSourceDirectory(),
184                 new TestListResolver("aUserRequestedTest#aUserRequestedTestMethod"));
185 
186         RunOrderParameters runOrderParameters = new RunOrderParameters(RunOrder.DEFAULT, null);
187         return new ProviderConfiguration(
188                 directoryScannerParameters,
189                 runOrderParameters,
190                 reporterConfiguration,
191                 new TestArtifactInfo("5.0", "ABC"),
192                 testSuiteDefinition,
193                 new HashMap<String, String>(),
194                 BooterDeserializerProviderConfigurationTest.TEST_TYPED,
195                 true,
196                 cli,
197                 0,
198                 Shutdown.DEFAULT,
199                 0);
200     }
201 
202     private StartupConfiguration getTestStartupConfiguration(ClassLoaderConfiguration classLoaderConfiguration) {
203         return new StartupConfiguration(
204                 "com.provider",
205                 classpathConfiguration,
206                 classLoaderConfiguration,
207                 ALL,
208                 Collections.<String[]>emptyList());
209     }
210 
211     private File getTestSourceDirectory() {
212         return new File("TestSrc");
213     }
214 
215     private String[] getSuiteXmlFileStrings() {
216         return new String[] {"A1", "A2"};
217     }
218 }