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 org.apache.maven.surefire.booter.BooterDeserializer;
23  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
24  import org.apache.maven.surefire.booter.Classpath;
25  import org.apache.maven.surefire.booter.ClasspathConfiguration;
26  import org.apache.maven.surefire.booter.PropertiesWrapper;
27  import org.apache.maven.surefire.booter.ProviderConfiguration;
28  import org.apache.maven.surefire.booter.StartupConfiguration;
29  import org.apache.maven.surefire.booter.SystemPropertyManager;
30  import org.apache.maven.surefire.report.ReporterConfiguration;
31  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
32  import org.apache.maven.surefire.testset.TestArtifactInfo;
33  import org.apache.maven.surefire.testset.TestRequest;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.Properties;
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      private final String aTest = "aTest";
55  
56      public void testProvider()
57          throws IOException
58      {
59          assertEquals( "com.provider", getReloadedStartupConfiguration().getProviderClassName() );
60      }
61  
62      public void testClassPathConfiguration()
63          throws IOException
64      {
65          ClasspathConfiguration reloadedClasspathConfiguration =
66              getReloadedStartupConfiguration().getClasspathConfiguration();
67          assertEquals( classpathConfiguration, reloadedClasspathConfiguration );
68      }
69  
70      private void assertEquals( ClasspathConfiguration expectedConfiguration, ClasspathConfiguration actualConfiguration )
71      {
72          assertEquals( expectedConfiguration.getTestClasspath().getClassPath(),
73                        actualConfiguration.getTestClasspath().getClassPath() );
74          Properties propertiesForExpectedConfiguration = getPropertiesForClasspathConfiguration( expectedConfiguration );
75          Properties propertiesForActualConfiguration = getPropertiesForClasspathConfiguration( actualConfiguration );
76          assertEquals( propertiesForExpectedConfiguration, propertiesForActualConfiguration );
77      }
78  
79      private Properties getPropertiesForClasspathConfiguration( ClasspathConfiguration configuration )
80      {
81          Properties properties = new Properties();
82          configuration.setForkProperties( new PropertiesWrapper( properties ));
83          return properties;
84      }
85  
86      public void testClassLoaderConfiguration()
87          throws IOException
88      {
89          assertFalse( getReloadedStartupConfiguration().isManifestOnlyJarRequestedAndUsable() );
90      }
91  
92      public void testClassLoaderConfigurationTrues()
93          throws IOException
94      {
95          final StartupConfiguration testStartupConfiguration =
96              getTestStartupConfiguration( getManifestOnlyJarForkConfiguration() );
97          boolean current = testStartupConfiguration.isManifestOnlyJarRequestedAndUsable();
98          assertEquals( current, saveAndReload( testStartupConfiguration ).isManifestOnlyJarRequestedAndUsable() );
99      }
100 
101     private ClasspathConfiguration createClasspathConfiguration()
102     {
103         Classpath testClassPath = new Classpath( Arrays.asList( new String[]{"CP1" , "CP2" } ) );
104         Classpath providerClasspath = new Classpath( Arrays.asList( new String[]{"SP1" , "SP2" } ) );
105         return new ClasspathConfiguration( testClassPath, providerClasspath, true, true );
106     }
107 
108     public static ClassLoaderConfiguration getSystemClassLoaderConfiguration()
109         throws IOException
110     {
111         return new ClassLoaderConfiguration( true, false );
112     }
113 
114     public static ClassLoaderConfiguration getManifestOnlyJarForkConfiguration()
115         throws IOException
116     {
117         return new ClassLoaderConfiguration( true, true );
118     }
119 
120 
121     private StartupConfiguration getReloadedStartupConfiguration()
122         throws IOException
123     {
124         ClassLoaderConfiguration classLoaderConfiguration = getSystemClassLoaderConfiguration();
125         return saveAndReload( getTestStartupConfiguration( classLoaderConfiguration ) );
126     }
127 
128     private StartupConfiguration saveAndReload( StartupConfiguration startupConfiguration )
129         throws IOException
130     {
131         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration();
132         Properties props = new Properties();
133         BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, props );
134         booterSerializer.serialize( getProviderConfiguration(), startupConfiguration, aTest );
135         final File propsTest =
136             SystemPropertyManager.writePropertiesFile( props, forkConfiguration.getTempDirectory(), "propsTest", true );
137         BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
138         return booterDeserializer.getProviderConfiguration();
139     }
140 
141     private ProviderConfiguration getProviderConfiguration()
142         throws IOException
143     {
144 
145         File cwd = new File( "." );
146         DirectoryScannerParameters directoryScannerParameters =
147             new DirectoryScannerParameters( cwd, new ArrayList(), new ArrayList(), Boolean.TRUE, "hourly" );
148         ReporterConfiguration reporterConfiguration =
149             new ReporterConfiguration( new ArrayList(), cwd, Boolean.TRUE, null );
150         String aUserRequestedTest = "aUserRequestedTest";
151         String aUserRequestedTestMethod = "aUserRequestedTestMethod";
152         TestRequest testSuiteDefinition =
153             new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(), aUserRequestedTest,
154                              aUserRequestedTestMethod );
155         return new ProviderConfiguration( directoryScannerParameters, true, reporterConfiguration,
156                                           new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new Properties(),
157                                           aTest );
158     }
159 
160     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
161     {
162         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false, false,
163                                          false );
164     }
165 
166     private File getTestSourceDirectory()
167     {
168         return new File( "TestSrc" );
169     }
170 
171     private Object[] getSuiteXmlFileStrings()
172     {
173         return new Object[]{ "A1", "A2" };
174     }
175 }