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.Collections;
28  import java.util.List;
29  import java.util.Properties;
30  import org.apache.maven.surefire.booter.BooterDeserializer;
31  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
32  import org.apache.maven.surefire.booter.ClasspathConfiguration;
33  import org.apache.maven.surefire.booter.PropertiesWrapper;
34  import org.apache.maven.surefire.booter.ProviderConfiguration;
35  import org.apache.maven.surefire.booter.StartupConfiguration;
36  import org.apache.maven.surefire.booter.TypeEncodedValue;
37  import org.apache.maven.surefire.report.ReporterConfiguration;
38  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
39  import org.apache.maven.surefire.testset.RunOrderParameters;
40  import org.apache.maven.surefire.testset.TestArtifactInfo;
41  import org.apache.maven.surefire.testset.TestRequest;
42  import org.apache.maven.surefire.util.RunOrder;
43  
44  import junit.framework.Assert;
45  import junit.framework.TestCase;
46  
47  /**
48   * Performs roundtrip testing of serialization/deserialization of the ProviderConfiguration
49   *
50   * @author Kristian Rosenvold
51   */
52  public class BooterDeserializerProviderConfigurationTest
53      extends TestCase
54  {
55  
56      public static final TypeEncodedValue aTestTyped = new TypeEncodedValue( String.class.getName(), "aTest" );
57  
58      private final String aUserRequestedTest = "aUserRequestedTest";
59  
60      private static ClassLoaderConfiguration getForkConfiguration()
61      {
62          return new ClassLoaderConfiguration( true, false );
63      }
64  
65      // ProviderConfiguration methods
66      public void testDirectoryScannerParams()
67          throws IOException
68      {
69  
70          File aDir = new File( "." );
71          List<String> includes = new ArrayList<String>();
72          List<String> excludes = new ArrayList<String>();
73          includes.add( "abc" );
74          includes.add( "cde" );
75          excludes.add( "xx1" );
76          excludes.add( "xx2" );
77  
78          ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
79          final StartupConfiguration testStartupConfiguration = getTestStartupConfiguration( forkConfiguration );
80          ProviderConfiguration providerConfiguration = getReloadedProviderConfiguration();
81          ProviderConfiguration read = saveAndReload( providerConfiguration, testStartupConfiguration, false );
82  
83          Assert.assertEquals( aDir, read.getBaseDir() );
84          Assert.assertEquals( includes.get( 0 ), read.getIncludes().get( 0 ) );
85          Assert.assertEquals( includes.get( 1 ), read.getIncludes().get( 1 ) );
86          Assert.assertEquals( excludes.get( 0 ), read.getExcludes().get( 0 ) );
87          Assert.assertEquals( excludes.get( 1 ), read.getExcludes().get( 1 ) );
88  
89      }
90  
91      public void testReporterConfiguration()
92          throws IOException
93      {
94          DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
95          ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
96  
97          ProviderConfiguration providerConfiguration = getTestProviderConfiguration( directoryScannerParameters, false );
98  
99          final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
100         ProviderConfiguration reloaded = saveAndReload( providerConfiguration, testProviderConfiguration, false );
101 
102         assertTrue( reloaded.getReporterConfiguration().isTrimStackTrace().booleanValue() );
103         assertNotNull( reloaded.getReporterConfiguration().getReportsDirectory() );
104     }
105 
106     public void testTestArtifact()
107         throws IOException
108     {
109         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
110 
111         Assert.assertEquals( "5.0", reloaded.getTestArtifact().getVersion() );
112         Assert.assertEquals( "ABC", reloaded.getTestArtifact().getClassifier() );
113     }
114 
115     public void testTestRequest()
116         throws IOException
117     {
118         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
119 
120         TestRequest testSuiteDefinition = reloaded.getTestSuiteDefinition();
121         List<?> suiteXmlFiles = testSuiteDefinition.getSuiteXmlFiles();
122         File[] expected = getSuiteXmlFiles();
123         Assert.assertEquals( expected[0], suiteXmlFiles.get( 0 ) );
124         Assert.assertEquals( expected[1], suiteXmlFiles.get( 1 ) );
125         Assert.assertEquals( getTestSourceDirectory(), testSuiteDefinition.getTestSourceDirectory() );
126         Assert.assertEquals( aUserRequestedTest, testSuiteDefinition.getRequestedTest() );
127     }
128 
129     public void testTestForFork()
130         throws IOException
131     {
132         final ProviderConfiguration reloaded = getReloadedProviderConfiguration();
133         Assert.assertEquals( aTestTyped, reloaded.getTestForFork() );
134 
135     }
136 
137     public void testTestForForkWithMultipleFiles()
138         throws IOException
139     {
140         final ProviderConfiguration reloaded = getReloadedProviderConfigurationForReadFromInStream();
141         Assert.assertNull( reloaded.getTestForFork() );
142         Assert.assertTrue( reloaded.isReadTestsFromInStream() );
143 
144     }
145 
146     public void testFailIfNoTests()
147         throws IOException
148     {
149         ProviderConfiguration reloaded = getReloadedProviderConfiguration();
150         assertTrue( reloaded.isFailIfNoTests().booleanValue() );
151 
152     }
153 
154     private ProviderConfiguration getReloadedProviderConfigurationForReadFromInStream()
155         throws IOException
156     {
157         return getReloadedProviderConfiguration( true );
158     }
159 
160     private ProviderConfiguration getReloadedProviderConfiguration()
161         throws IOException
162     {
163         return getReloadedProviderConfiguration( false );
164     }
165 
166     private ProviderConfiguration getReloadedProviderConfiguration( boolean readTestsFromInStream )
167         throws IOException
168     {
169         DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParametersWithoutSpecificTests();
170         ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
171         ProviderConfiguration booterConfiguration =
172             getTestProviderConfiguration( directoryScannerParameters, readTestsFromInStream );
173         final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
174         return saveAndReload( booterConfiguration, testProviderConfiguration, readTestsFromInStream );
175     }
176 
177     private DirectoryScannerParameters getDirectoryScannerParametersWithoutSpecificTests()
178     {
179         File aDir = new File( "." );
180         List<String> includes = new ArrayList<String>();
181         List<String> excludes = new ArrayList<String>();
182         includes.add( "abc" );
183         includes.add( "cde" );
184         excludes.add( "xx1" );
185         excludes.add( "xx2" );
186 
187         return new DirectoryScannerParameters( aDir, includes, excludes, Collections.emptyList(), Boolean.TRUE,
188                                                RunOrder.asString( RunOrder.DEFAULT ) );
189     }
190 
191     private ProviderConfiguration saveAndReload( ProviderConfiguration booterConfiguration,
192                                                  StartupConfiguration testProviderConfiguration,
193                                                  boolean readTestsFromInStream )
194         throws IOException
195     {
196         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration( null, null );
197         PropertiesWrapper props = new PropertiesWrapper( new Properties() );
198         BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
199         Object test;
200         if ( readTestsFromInStream )
201         {
202             test = null;
203         }
204         else
205         {
206             test = "aTest";
207         }
208         final File propsTest = booterSerializer.serialize( props, booterConfiguration, testProviderConfiguration, test,
209                                                            readTestsFromInStream );
210         BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
211         return booterDeserializer.deserialize();
212     }
213 
214     private ProviderConfiguration getTestProviderConfiguration( DirectoryScannerParameters directoryScannerParameters,
215                                                                 boolean readTestsFromInStream )
216     {
217 
218         File cwd = new File( "." );
219         ReporterConfiguration reporterConfiguration = new ReporterConfiguration( cwd, Boolean.TRUE );
220         String aUserRequestedTestMethod = "aUserRequestedTestMethod";
221         TestRequest testSuiteDefinition =
222             new TestRequest( getSuiteXmlFileStrings(), getTestSourceDirectory(), aUserRequestedTest,
223                              aUserRequestedTestMethod );
224         RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, null );
225         return new ProviderConfiguration( directoryScannerParameters, runOrderParameters, true, reporterConfiguration,
226                                           new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new Properties(),
227                                           aTestTyped, readTestsFromInStream );
228     }
229 
230     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
231     {
232         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( true, true );
233 
234         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false,
235                                          false );
236     }
237 
238     private File getTestSourceDirectory()
239     {
240         return new File( "TestSrc" );
241     }
242 
243     private File[] getSuiteXmlFiles()
244     {
245         return new File[]{ new File( "A1" ), new File( "A2" ) };
246     }
247 
248     private List<String> getSuiteXmlFileStrings()
249     {
250         return Arrays.asList( new String[]{ "A1", "A2" } );
251     }
252 }