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.*;
24  import org.apache.maven.surefire.cli.CommandLineOption;
25  import org.apache.maven.surefire.report.ReporterConfiguration;
26  import org.apache.maven.surefire.testset.*;
27  import org.apache.maven.surefire.util.RunOrder;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.HashMap;
35  import java.util.List;
36  
37  import static org.apache.maven.surefire.cli.CommandLineOption.LOGGING_LEVEL_DEBUG;
38  import static org.apache.maven.surefire.cli.CommandLineOption.REACTOR_FAIL_FAST;
39  import static org.apache.maven.surefire.cli.CommandLineOption.SHOW_ERRORS;
40  
41  /**
42   * Performs roundtrip testing of serialization/deserialization of The StartupConfiguration
43   *
44   * @author Kristian Rosenvold
45   */
46  public class BooterDeserializerStartupConfigurationTest
47      extends TestCase
48  {
49      private final ClasspathConfiguration classpathConfiguration = createClasspathConfiguration();
50  
51      private final List<CommandLineOption> cli =
52          Arrays.asList( LOGGING_LEVEL_DEBUG, SHOW_ERRORS, REACTOR_FAIL_FAST );
53  
54      public void testProvider()
55          throws IOException
56      {
57          assertEquals( "com.provider", getReloadedStartupConfiguration().getProviderClassName() );
58      }
59  
60      public void testClassPathConfiguration()
61          throws IOException
62      {
63          ClasspathConfiguration reloadedClasspathConfiguration =
64              getReloadedStartupConfiguration().getClasspathConfiguration();
65          assertEquals( classpathConfiguration, reloadedClasspathConfiguration );
66      }
67  
68      private void assertEquals( ClasspathConfiguration expectedConfiguration,
69                                 ClasspathConfiguration actualConfiguration )
70      {
71          assertEquals( expectedConfiguration.getTestClasspath().getClassPath(),
72                        actualConfiguration.getTestClasspath().getClassPath() );
73          assertEquals( expectedConfiguration.isEnableAssertions(), actualConfiguration.isEnableAssertions() );
74          assertEquals(  expectedConfiguration.isChildDelegation(), actualConfiguration.isChildDelegation() );
75          assertEquals(  expectedConfiguration.getProviderClasspath(), actualConfiguration.getProviderClasspath() );
76          assertEquals(  expectedConfiguration.getTestClasspath(), actualConfiguration.getTestClasspath() );
77      }
78  
79      public void testClassLoaderConfiguration()
80          throws IOException
81      {
82          assertFalse( getReloadedStartupConfiguration().isManifestOnlyJarRequestedAndUsable() );
83      }
84  
85      public void testClassLoaderConfigurationTrues()
86          throws IOException
87      {
88          final StartupConfiguration testStartupConfiguration =
89              getTestStartupConfiguration( getManifestOnlyJarForkConfiguration() );
90          boolean current = testStartupConfiguration.isManifestOnlyJarRequestedAndUsable();
91          assertEquals( current, saveAndReload( testStartupConfiguration ).isManifestOnlyJarRequestedAndUsable() );
92      }
93  
94      private ClasspathConfiguration createClasspathConfiguration()
95      {
96          Classpath testClassPath = new Classpath( Arrays.asList( "CP1", "CP2" ) );
97          Classpath providerClasspath = new Classpath( Arrays.asList( "SP1", "SP2" ) );
98          return new ClasspathConfiguration( testClassPath, providerClasspath, Classpath.emptyClasspath(), true, true );
99      }
100 
101     public static ClassLoaderConfiguration getSystemClassLoaderConfiguration()
102     {
103         return new ClassLoaderConfiguration( true, false );
104     }
105 
106     public static ClassLoaderConfiguration getManifestOnlyJarForkConfiguration()
107     {
108         return new ClassLoaderConfiguration( true, true );
109     }
110 
111 
112     private StartupConfiguration getReloadedStartupConfiguration()
113         throws IOException
114     {
115         ClassLoaderConfiguration classLoaderConfiguration = getSystemClassLoaderConfiguration();
116         return saveAndReload( getTestStartupConfiguration( classLoaderConfiguration ) );
117     }
118 
119     private StartupConfiguration saveAndReload( StartupConfiguration startupConfiguration )
120         throws IOException
121     {
122         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration( null, null );
123         PropertiesWrapper props = new PropertiesWrapper( new HashMap<String, String>() );
124         BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
125         String aTest = "aTest";
126         final File propsTest =
127             booterSerializer.serialize( props, getProviderConfiguration(), startupConfiguration, aTest, false );
128         BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
129         return booterDeserializer.getProviderConfiguration();
130     }
131 
132     private ProviderConfiguration getProviderConfiguration()
133     {
134 
135         File cwd = new File( "." );
136         DirectoryScannerParameters directoryScannerParameters =
137             new DirectoryScannerParameters( cwd, new ArrayList<String>(), new ArrayList<String>(),
138                                             new ArrayList<String>(), true, "hourly" );
139         ReporterConfiguration reporterConfiguration = new ReporterConfiguration( cwd, true );
140         TestRequest testSuiteDefinition =
141             new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(),
142                              new TestListResolver( "aUserRequestedTest#aUserRequestedTestMethod" ));
143 
144         RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
145         return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, true, reporterConfiguration,
146                 new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new HashMap<String, String>(),
147                 BooterDeserializerProviderConfigurationTest.aTestTyped, true, cli, 0, Shutdown.DEFAULT );
148     }
149 
150     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
151     {
152         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false,
153                                          false );
154     }
155 
156     private File getTestSourceDirectory()
157     {
158         return new File( "TestSrc" );
159     }
160 
161     private String[] getSuiteXmlFileStrings()
162     {
163         return new String[]{ "A1", "A2" };
164     }
165 }