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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Properties;
28  import org.apache.maven.surefire.booter.BooterDeserializer;
29  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
30  import org.apache.maven.surefire.booter.Classpath;
31  import org.apache.maven.surefire.booter.ClasspathConfiguration;
32  import org.apache.maven.surefire.booter.PropertiesWrapper;
33  import org.apache.maven.surefire.booter.ProviderConfiguration;
34  import org.apache.maven.surefire.booter.StartupConfiguration;
35  import org.apache.maven.surefire.report.ReporterConfiguration;
36  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
37  import org.apache.maven.surefire.testset.RunOrderParameters;
38  import org.apache.maven.surefire.testset.TestArtifactInfo;
39  import org.apache.maven.surefire.testset.TestRequest;
40  import org.apache.maven.surefire.util.RunOrder;
41  
42  import junit.framework.TestCase;
43  
44  /**
45   * Performs roundtrip testing of serialization/deserialization of The StartupConfiguration
46   *
47   * @author Kristian Rosenvold
48   */
49  public class BooterDeserializerStartupConfigurationTest
50      extends TestCase
51  {
52      private final ClasspathConfiguration classpathConfiguration = createClasspathConfiguration();
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 Properties() );
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>(), Boolean.TRUE, "hourly" );
139         ReporterConfiguration reporterConfiguration = new ReporterConfiguration( cwd, Boolean.TRUE );
140         String aUserRequestedTest = "aUserRequestedTest";
141         String aUserRequestedTestMethod = "aUserRequestedTestMethod";
142         TestRequest testSuiteDefinition =
143             new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(), aUserRequestedTest,
144                              aUserRequestedTestMethod );
145 
146         RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
147         return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, true, reporterConfiguration,
148                                           new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new Properties(),
149                                           BooterDeserializerProviderConfigurationTest.aTestTyped, true );
150     }
151 
152     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
153     {
154         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false,
155                                          false );
156     }
157 
158     private File getTestSourceDirectory()
159     {
160         return new File( "TestSrc" );
161     }
162 
163     private Object[] getSuiteXmlFileStrings()
164     {
165         return new Object[]{ "A1", "A2" };
166     }
167 }