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.ClasspathConfiguration;
25  import org.apache.maven.surefire.booter.ProviderConfiguration;
26  import org.apache.maven.surefire.booter.StartupConfiguration;
27  import org.apache.maven.surefire.booter.SystemPropertyManager;
28  import org.apache.maven.surefire.report.ReporterConfiguration;
29  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
30  import org.apache.maven.surefire.testset.TestArtifactInfo;
31  import org.apache.maven.surefire.testset.TestRequest;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.List;
39  import java.util.Properties;
40  
41  import junit.framework.Assert;
42  import junit.framework.TestCase;
43  
44  /**
45   * Performs roundtrip testing of serialization/deserialization of the ProviderConfiguration
46   *
47   * @author Kristian Rosenvold
48   */
49  public class BooterDeserializerProviderConfigurationTest
50      extends TestCase
51  {
52  
53      private final String aTest = "aTest";
54  
55      private final String aUserRequestedTest = "aUserRequestedTest";
56      
57      private final String aUserRequestedTestMethod = "aUserRequestedTestMethod";
58  
59      public static ClassLoaderConfiguration getForkConfiguration()
60          throws IOException
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 includes = new ArrayList();
72          List excludes = new ArrayList();
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( new ArrayList() );
81          ProviderConfiguration read = saveAndReload( providerConfiguration, testStartupConfiguration );
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 = getDirectoryScannerParameters();
95          ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
96          List reports = new ArrayList();
97          final String first = "abc";
98          final String second = "cde";
99          final String third = "efg";
100         reports.add( first );
101         reports.add( second );
102         reports.add( third );
103 
104         ProviderConfiguration providerConfiguration =
105             getTestProviderConfiguration( directoryScannerParameters, reports );
106 
107         final ReporterConfiguration reporterConfiguration = providerConfiguration.getReporterConfiguration();
108         reporterConfiguration.getReports().add( first );
109         reporterConfiguration.getReports().add( second );
110         reporterConfiguration.getReports().add( third );
111 
112         final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
113         ProviderConfiguration reloaded = saveAndReload( providerConfiguration, testProviderConfiguration );
114 
115         Assert.assertEquals( first, reloaded.getReporterConfiguration().getReports().get( 0 ) );
116         Assert.assertEquals( second, reloaded.getReporterConfiguration().getReports().get( 1 ) );
117         Assert.assertEquals( third, reloaded.getReporterConfiguration().getReports().get( 2 ) );
118     }
119 
120     public void testTestArtifact()
121         throws IOException
122     {
123         ProviderConfiguration reloaded = getReloadedProviderConfiguration( new ArrayList() );
124 
125         Assert.assertEquals( "5.0", reloaded.getTestArtifact().getVersion() );
126         Assert.assertEquals( "ABC", reloaded.getTestArtifact().getClassifier() );
127     }
128 
129     public void testTestRequest()
130         throws IOException
131     {
132         ProviderConfiguration reloaded = getReloadedProviderConfiguration( new ArrayList() );
133 
134         TestRequest testSuiteDefinition = reloaded.getTestSuiteDefinition();
135         List suiteXmlFiles = testSuiteDefinition.getSuiteXmlFiles();
136         File[] expected = getSuiteXmlFiles();
137         Assert.assertEquals( expected[0], suiteXmlFiles.get( 0 ) );
138         Assert.assertEquals( expected[1], suiteXmlFiles.get( 1 ) );
139         Assert.assertEquals( getTestSourceDirectory(), testSuiteDefinition.getTestSourceDirectory() );
140         Assert.assertEquals( aUserRequestedTest, testSuiteDefinition.getRequestedTest() );
141     }
142 
143     public void testTestForFork()
144         throws IOException
145     {
146         final ProviderConfiguration reloaded = getReloadedProviderConfiguration( new ArrayList() );
147         Assert.assertEquals( aTest, reloaded.getTestForForkString() );
148 
149     }
150 
151     public void testFailIfNoTests()
152         throws IOException
153     {
154         ProviderConfiguration reloaded = getReloadedProviderConfiguration( new ArrayList() );
155         assertTrue( reloaded.isFailIfNoTests().booleanValue() );
156 
157     }
158 
159     private ProviderConfiguration getReloadedProviderConfiguration( ArrayList reports )
160         throws IOException
161     {
162         DirectoryScannerParameters directoryScannerParameters = getDirectoryScannerParameters();
163         ClassLoaderConfiguration forkConfiguration = getForkConfiguration();
164         ProviderConfiguration booterConfiguration = getTestProviderConfiguration( directoryScannerParameters, reports );
165         final StartupConfiguration testProviderConfiguration = getTestStartupConfiguration( forkConfiguration );
166         return saveAndReload( booterConfiguration, testProviderConfiguration );
167     }
168 
169     private DirectoryScannerParameters getDirectoryScannerParameters()
170     {
171         File aDir = new File( "." );
172         List includes = new ArrayList();
173         List excludes = new ArrayList();
174         includes.add( "abc" );
175         includes.add( "cde" );
176         excludes.add( "xx1" );
177         excludes.add( "xx2" );
178 
179         return new DirectoryScannerParameters( aDir, includes, excludes, Boolean.TRUE, null );
180     }
181 
182     private ProviderConfiguration saveAndReload( ProviderConfiguration booterConfiguration,
183                                                  StartupConfiguration testProviderConfiguration )
184         throws IOException
185     {
186         final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration();
187         Properties props = new Properties();
188         BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, props );
189         booterSerializer.serialize( booterConfiguration, testProviderConfiguration, aTest );
190         final File propsTest =
191             SystemPropertyManager.writePropertiesFile( props, forkConfiguration.getTempDirectory(), "propsTest", true );
192         BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
193         return booterDeserializer.deserialize();
194     }
195 
196     private ProviderConfiguration getTestProviderConfiguration( DirectoryScannerParameters directoryScannerParameters,
197                                                                 List reports )
198         throws IOException
199     {
200 
201         File cwd = new File( "." );
202         ReporterConfiguration reporterConfiguration =
203             new ReporterConfiguration( reports, cwd, Boolean.TRUE, null );
204         TestRequest testSuiteDefinition =
205             new TestRequest( getSuiteXmlFileStrings(), getTestSourceDirectory(), aUserRequestedTest, aUserRequestedTestMethod );
206         return new ProviderConfiguration( directoryScannerParameters, true, reporterConfiguration,
207                                           new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new Properties(),
208                                           aTest );
209     }
210 
211     private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
212     {
213         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( true, true );
214 
215         return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false, false,
216                                          false );
217     }
218 
219     private File getTestSourceDirectory()
220     {
221         return new File( "TestSrc" );
222     }
223 
224     private File[] getSuiteXmlFiles()
225     {
226         return new File[]{ new File( "A1" ), new File( "A2" ) };
227     }
228 
229     private List getSuiteXmlFileStrings()
230     {
231         return Arrays.asList( new Object[]{ "A1", "A2" } );
232     }
233 }