View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.maven.surefire.booter.AbstractPathConfiguration;
24  import org.apache.maven.surefire.booter.BooterDeserializer;
25  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
26  import org.apache.maven.surefire.booter.Classpath;
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.api.cli.CommandLineOption;
33  import org.apache.maven.surefire.api.report.ReporterConfiguration;
34  import org.apache.maven.surefire.shared.io.FileUtils;
35  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
36  import org.apache.maven.surefire.api.testset.RunOrderParameters;
37  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
38  import org.apache.maven.surefire.api.testset.TestListResolver;
39  import org.apache.maven.surefire.api.testset.TestRequest;
40  import org.apache.maven.surefire.api.util.RunOrder;
41  import org.junit.After;
42  import org.junit.Before;
43  
44  import java.io.File;
45  import java.io.FileInputStream;
46  import java.io.IOException;
47  import java.util.ArrayList;
48  import java.util.Arrays;
49  import java.util.Collections;
50  import java.util.HashMap;
51  import java.util.List;
52  
53  import static org.apache.maven.surefire.booter.ProcessCheckerType.ALL;
54  import static org.apache.maven.surefire.api.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
55  import static org.apache.maven.surefire.api.cli.CommandLineOption.REACTOR_FAIL_FAST;
56  import static org.apache.maven.surefire.api.cli.CommandLineOption.SHOW_ERRORS;
57  
58  /**
59   * Performs roundtrip testing of serialization/deserialization of The StartupConfiguration
60   *
61   * @author Kristian Rosenvold
62   */
63  public class BooterDeserializerStartupConfigurationTest
64      extends TestCase
65  {
66  
67      private static int idx = 0;
68  
69      private File basedir;
70  
71      private final ClasspathConfiguration classpathConfiguration = createClasspathConfiguration();
72  
73      private final List<CommandLineOption> cli =
74          Arrays.asList( LOGGING_LEVEL_DEBUG, SHOW_ERRORS, REACTOR_FAIL_FAST );
75  
76      @Before
77      public void setupDirectories() throws IOException
78      {
79          File target = new File( System.getProperty( "user.dir" ), "target" );
80          basedir = new File( target, "BooterDeserializerProviderConfigurationTest-" + ++idx );
81          FileUtils.deleteDirectory( basedir );
82          assertTrue( basedir.mkdirs() );
83      }
84  
85      @After
86      public void deleteDirectories() throws IOException
87      {
88          FileUtils.deleteDirectory( basedir );
89      }
90  
91      public void testProvider()
92          throws IOException
93      {
94          assertEquals( "com.provider", getReloadedStartupConfiguration().getProviderClassName() );
95      }
96  
97      public void testClassPathConfiguration()
98          throws IOException
99      {
100         AbstractPathConfiguration reloadedClasspathConfiguration =
101             getReloadedStartupConfiguration().getClasspathConfiguration();
102 
103         assertTrue( reloadedClasspathConfiguration instanceof ClasspathConfiguration );
104         assertCpConfigEquals( classpathConfiguration, (ClasspathConfiguration) reloadedClasspathConfiguration );
105     }
106 
107     public void testProcessChecker() throws IOException
108     {
109         assertEquals( ALL, getReloadedStartupConfiguration().getProcessChecker() );
110     }
111 
112     private void assertCpConfigEquals( ClasspathConfiguration expectedConfiguration,
113                                ClasspathConfiguration actualConfiguration )
114     {
115         assertEquals( expectedConfiguration.getTestClasspath().getClassPath(),
116                       actualConfiguration.getTestClasspath().getClassPath() );
117         assertEquals( expectedConfiguration.isEnableAssertions(), actualConfiguration.isEnableAssertions() );
118         assertEquals(  expectedConfiguration.isChildDelegation(), actualConfiguration.isChildDelegation() );
119         assertEquals(  expectedConfiguration.getProviderClasspath(), actualConfiguration.getProviderClasspath() );
120         assertEquals(  expectedConfiguration.getTestClasspath(), actualConfiguration.getTestClasspath() );
121     }
122 
123     public void testClassLoaderConfiguration()
124         throws IOException
125     {
126         assertFalse( getReloadedStartupConfiguration().isManifestOnlyJarRequestedAndUsable() );
127     }
128 
129     public void testClassLoaderConfigurationTrues()
130         throws IOException
131     {
132         final StartupConfiguration testStartupConfiguration =
133             getTestStartupConfiguration( getManifestOnlyJarForkConfiguration() );
134         boolean current = testStartupConfiguration.isManifestOnlyJarRequestedAndUsable();
135         assertEquals( current, saveAndReload( testStartupConfiguration ).isManifestOnlyJarRequestedAndUsable() );
136     }
137 
138     public void testProcessCheckerAll() throws IOException
139     {
140         assertEquals( ALL, getReloadedStartupConfiguration().getProcessChecker() );
141     }
142 
143     public void testProcessCheckerNull() throws IOException
144     {
145         StartupConfiguration startupConfiguration = new StartupConfiguration( "com.provider", classpathConfiguration,
146                 getManifestOnlyJarForkConfiguration(), null, Collections.<String[]>emptyList() );
147         assertNull( saveAndReload( startupConfiguration ).getProcessChecker() );
148     }
149 
150     private ClasspathConfiguration createClasspathConfiguration()
151     {
152         Classpath testClassPath = new Classpath( Arrays.asList( "CP1", "CP2" ) );
153         Classpath providerClasspath = new Classpath( Arrays.asList( "SP1", "SP2" ) );
154         return new ClasspathConfiguration( testClassPath, providerClasspath, Classpath.emptyClasspath(), true, true );
155     }
156 
157     private static ClassLoaderConfiguration getSystemClassLoaderConfiguration()
158     {
159         return new ClassLoaderConfiguration( true, false );
160     }
161 
162     private static ClassLoaderConfiguration getManifestOnlyJarForkConfiguration()
163     {
164         return new ClassLoaderConfiguration( true, true );
165     }
166 
167     private StartupConfiguration getReloadedStartupConfiguration()
168         throws IOException
169     {
170         ClassLoaderConfiguration classLoaderConfiguration = getSystemClassLoaderConfiguration();
171         return saveAndReload( getTestStartupConfiguration( classLoaderConfiguration ) );
172     }
173 
174     private StartupConfiguration saveAndReload( StartupConfiguration startupConfiguration )
175         throws IOException
176     {
177         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration( basedir, null );
178         PropertiesWrapper props = new PropertiesWrapper( new HashMap<String, String>() );
179         BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
180         String aTest = "aTest";
181         File propsTest = booterSerializer.serialize( props, getProviderConfiguration(), startupConfiguration, aTest,
182                 false, null, 1, "tcp://127.0.0.1:63003" );
183         BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
184         assertNull( booterDeserializer.getPluginPid() );
185         assertEquals( "tcp://127.0.0.1:63003", booterDeserializer.getConnectionString() );
186         return booterDeserializer.getStartupConfiguration();
187     }
188 
189     private ProviderConfiguration getProviderConfiguration()
190     {
191         File cwd = new File( "." );
192         DirectoryScannerParameters directoryScannerParameters =
193             new DirectoryScannerParameters( cwd, new ArrayList<String>(), new ArrayList<String>(),
194                                             new ArrayList<String>(), true, "hourly" );
195         ReporterConfiguration reporterConfiguration = new ReporterConfiguration( cwd, true );
196         TestRequest testSuiteDefinition =
197             new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(),
198                              new TestListResolver( "aUserRequestedTest#aUserRequestedTestMethod" ) );
199 
200         RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
201         return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, true, reporterConfiguration,
202                 new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new HashMap<String, String>(),
203                 BooterDeserializerProviderConfigurationTest.TEST_TYPED, true, cli, 0, Shutdown.DEFAULT, 0 );
204     }
205 
206     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
207     {
208         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, ALL,
209             Collections.<String[]>emptyList() );
210     }
211 
212     private File getTestSourceDirectory()
213     {
214         return new File( "TestSrc" );
215     }
216 
217     private String[] getSuiteXmlFileStrings()
218     {
219         return new String[]{ "A1", "A2" };
220     }
221 }