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