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