View Javadoc
1   package org.apache.maven.plugin.surefire;
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.artifact.Artifact;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
26  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
27  import org.apache.maven.surefire.booter.Classpath;
28  import org.apache.maven.surefire.booter.ModularClasspathConfiguration;
29  import org.apache.maven.surefire.booter.StartupConfiguration;
30  import org.apache.maven.surefire.suite.RunResult;
31  import org.apache.maven.surefire.util.DefaultScanResult;
32  import org.codehaus.plexus.languages.java.jpms.LocationManager;
33  import org.codehaus.plexus.languages.java.jpms.ResolvePathsRequest;
34  import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
35  import org.codehaus.plexus.logging.Logger;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.junit.runner.RunWith;
39  import org.mockito.ArgumentCaptor;
40  import org.mockito.Mock;
41  import org.powermock.core.classloader.annotations.PrepareForTest;
42  import org.powermock.modules.junit4.PowerMockRunner;
43  
44  import java.io.File;
45  import java.io.IOException;
46  import java.nio.file.Path;
47  import java.util.LinkedHashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  import static java.io.File.separatorChar;
52  import static java.util.Arrays.asList;
53  import static java.util.Collections.singleton;
54  import static org.apache.commons.lang3.JavaVersion.JAVA_1_7;
55  import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
56  import static org.apache.maven.surefire.booter.SystemUtils.isBuiltInJava7AtLeast;
57  import static org.fest.assertions.Assertions.assertThat;
58  import static org.junit.Assume.assumeTrue;
59  import static org.mockito.ArgumentMatchers.anyString;
60  import static org.mockito.ArgumentMatchers.eq;
61  import static org.mockito.Mockito.times;
62  import static org.mockito.Mockito.verify;
63  import static org.mockito.Mockito.when;
64  import static org.powermock.api.mockito.PowerMockito.doReturn;
65  import static org.powermock.api.mockito.PowerMockito.doNothing;
66  import static org.powermock.api.mockito.PowerMockito.mock;
67  import static org.powermock.api.mockito.PowerMockito.mockStatic;
68  import static org.powermock.api.mockito.PowerMockito.spy;
69  import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
70  import static org.powermock.api.mockito.PowerMockito.verifyStatic;
71  import static org.powermock.reflect.Whitebox.invokeMethod;
72  
73  /**
74   * Test for {@link AbstractSurefireMojo}.
75   */
76  @RunWith( PowerMockRunner.class )
77  @PrepareForTest( { AbstractSurefireMojo.class, ResolvePathsRequest.class } )
78  public class AbstractSurefireMojoJava7PlusTest
79  {
80      @Mock
81      private LocationManager locationManager;
82  
83      @BeforeClass
84      public static void withJava7Plus()
85      {
86          assumeTrue( JAVA_RECENT.atLeast( JAVA_1_7 ) );
87      }
88  
89      @Test
90      public void shouldHaveStartupConfigForModularClasspath()
91              throws Exception
92      {
93          AbstractSurefireMojo mojo = spy( new Mojo() );
94          doReturn( locationManager )
95                  .when( mojo, "getLocationManager" );
96  
97          Classpath testClasspath = new Classpath( asList( "non-modular.jar", "modular.jar",
98                  "target" + separatorChar + "classes", "junit.jar", "hamcrest.jar" ) );
99  
100         doReturn( testClasspath ).when( mojo, "generateTestClasspath" );
101         doReturn( 1 ).when( mojo, "getEffectiveForkCount" );
102         doReturn( true ).when( mojo, "effectiveIsEnableAssertions" );
103         when( mojo.isChildDelegation() ).thenReturn( false );
104         when( mojo.getTestClassesDirectory() ).thenReturn( new File( "target" + separatorChar + "test-classes" ) );
105 
106         DefaultScanResult scanResult = mock( DefaultScanResult.class );
107         when( scanResult.getClasses() ).thenReturn( asList( "org.apache.A", "org.apache.B" ) );
108 
109         ClassLoaderConfiguration classLoaderConfiguration = new ClassLoaderConfiguration( false, true );
110 
111         Classpath providerClasspath = new Classpath( singleton( "surefire-provider.jar" ) );
112 
113         File moduleInfo = new File( "target" + separatorChar + "classes" + separatorChar + "module-info.class" );
114 
115         @SuppressWarnings( "unchecked" )
116         ResolvePathsRequest<String> req = mock( ResolvePathsRequest.class );
117         mockStatic( ResolvePathsRequest.class );
118         when( ResolvePathsRequest.withStrings( eq( testClasspath.getClassPath() ) ) ).thenReturn( req );
119         when( req.setMainModuleDescriptor( eq( moduleInfo.getAbsolutePath() ) ) ).thenReturn( req );
120 
121         @SuppressWarnings( "unchecked" )
122         ResolvePathsResult<String> res = mock( ResolvePathsResult.class );
123         when( res.getClasspathElements() ).thenReturn( asList( "non-modular.jar", "junit.jar", "hamcrest.jar" ) );
124         Map<String, ResolvePathsResult.ModuleNameSource> mod = new LinkedHashMap<String, ResolvePathsResult.ModuleNameSource>();
125         mod.put( "modular.jar", null );
126         mod.put( "target" + separatorChar + "classes", null );
127         when( res.getModulepathElements() ).thenReturn( mod );
128         when( locationManager.resolvePaths( eq( req ) ) ).thenReturn( res );
129 
130         Logger logger = mock( Logger.class );
131         when( logger.isDebugEnabled() ).thenReturn( true );
132         doNothing().when( logger ).debug( anyString() );
133         when( mojo.getConsoleLogger() ).thenReturn( new PluginConsoleLogger( logger ) );
134 
135         StartupConfiguration conf = invokeMethod( mojo, "newStartupConfigForModularClasspath",
136                 classLoaderConfiguration, providerClasspath, "org.asf.Provider", moduleInfo, scanResult );
137 
138         verify( mojo, times( 1 ) ).effectiveIsEnableAssertions();
139         verify( mojo, times( 1 ) ).isChildDelegation();
140         verifyPrivate( mojo, times( 1 ) ).invoke( "generateTestClasspath" );
141         verify( mojo, times( 1 ) ).getEffectiveForkCount();
142         verify( mojo, times( 1 ) ).getTestClassesDirectory();
143         verify( scanResult, times( 1 ) ).getClasses();
144         verifyStatic( ResolvePathsRequest.class, times( 1 ) );
145         ResolvePathsRequest.withStrings( eq( testClasspath.getClassPath() ) );
146         verify( req, times( 1 ) ).setMainModuleDescriptor( eq( moduleInfo.getAbsolutePath() ) );
147         verify( res, times( 1 ) ).getClasspathElements();
148         verify( res, times( 1 ) ).getModulepathElements();
149         verify( locationManager, times( 1 ) ).resolvePaths( eq( req ) );
150         ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
151         verify( logger, times( 6 ) ).debug( argument.capture() );
152         assertThat( argument.getAllValues() )
153                 .containsExactly( "test classpath:  non-modular.jar  junit.jar  hamcrest.jar",
154                         "test modulepath:  modular.jar  target" + separatorChar + "classes",
155                         "provider classpath:  surefire-provider.jar",
156                         "test(compact) classpath:  non-modular.jar  junit.jar  hamcrest.jar",
157                         "test(compact) modulepath:  modular.jar  classes",
158                         "provider(compact) classpath:  surefire-provider.jar"
159                 );
160 
161         assertThat( conf ).isNotNull();
162         assertThat( conf.isShadefire() ).isFalse();
163         assertThat( conf.isProviderMainClass() ).isFalse();
164         assertThat( conf.isManifestOnlyJarRequestedAndUsable() ).isFalse();
165         assertThat( conf.getClassLoaderConfiguration() ).isSameAs( classLoaderConfiguration );
166         assertThat( conf.getProviderClassName() ).isEqualTo( "org.asf.Provider" );
167         assertThat( conf.getActualClassName() ).isEqualTo( "org.asf.Provider" );
168         assertThat( conf.getClasspathConfiguration() ).isNotNull();
169         assertThat( ( Object ) conf.getClasspathConfiguration().getTestClasspath() )
170                 .isEqualTo( new Classpath( res.getClasspathElements() ) );
171         assertThat( ( Object ) conf.getClasspathConfiguration().getProviderClasspath() ).isSameAs( providerClasspath );
172         assertThat( conf.getClasspathConfiguration() ).isInstanceOf( ModularClasspathConfiguration.class );
173         ModularClasspathConfiguration mcc = ( ModularClasspathConfiguration ) conf.getClasspathConfiguration();
174         assertThat( mcc.getModularClasspath().getModuleDescriptor() ).isEqualTo( moduleInfo );
175         assertThat( mcc.getModularClasspath().getPackages() ).containsOnly( "org.apache" );
176         assertThat( mcc.getModularClasspath().getPatchFile() )
177                 .isEqualTo( new File( "target" + separatorChar + "test-classes" ) );
178         assertThat( mcc.getModularClasspath().getModulePath() )
179                 .containsExactly( "modular.jar", "target" + separatorChar + "classes" );
180         assertThat( ( Object ) mcc.getTestClasspath() ).isEqualTo( new Classpath( res.getClasspathElements() ) );
181     }
182 
183     @Test
184     public void shouldHaveTmpDirectory() throws IOException
185     {
186         Path path = ( Path ) AbstractSurefireMojo.createTmpDirectoryWithJava7( "surefire" );
187 
188         assertThat( path )
189                 .isNotNull();
190 
191         assertThat( path.startsWith( System.getProperty( "java.io.tmpdir" ) ) )
192                 .isTrue();
193 
194         String dir = path.getName( path.getNameCount() - 1 ).toString();
195 
196         assertThat( dir )
197                 .startsWith( "surefire" );
198 
199         assertThat( dir )
200                 .matches( "^surefire[\\d]+$" );
201     }
202 
203     @Test
204     public void shouldHaveTmpDirectoryName() throws IOException
205     {
206         String dir = AbstractSurefireMojo.createTmpDirectoryNameWithJava7( "surefire" );
207 
208         assertThat( dir )
209                 .isNotNull();
210 
211         assertThat( dir )
212                 .startsWith( "surefire" );
213 
214         assertThat( dir )
215                 .matches( "^surefire[\\d]+$" );
216     }
217 
218     @Test
219     public void shouldTestIsJava7()
220     {
221         assertThat( isBuiltInJava7AtLeast() )
222                 .isTrue();
223     }
224 
225     public static class Mojo
226             extends AbstractSurefireMojo
227     {
228 
229         @Override
230         protected String getPluginName()
231         {
232             return null;
233         }
234 
235         @Override
236         protected int getRerunFailingTestsCount()
237         {
238             return 0;
239         }
240 
241         @Override
242         public boolean isSkipTests()
243         {
244             return false;
245         }
246 
247         @Override
248         public void setSkipTests( boolean skipTests )
249         {
250 
251         }
252 
253         @Override
254         public boolean isSkipExec()
255         {
256             return false;
257         }
258 
259         @Override
260         public void setSkipExec( boolean skipExec )
261         {
262 
263         }
264 
265         @Override
266         public boolean isSkip()
267         {
268             return false;
269         }
270 
271         @Override
272         public void setSkip( boolean skip )
273         {
274 
275         }
276 
277         @Override
278         public File getBasedir()
279         {
280             return null;
281         }
282 
283         @Override
284         public void setBasedir( File basedir )
285         {
286 
287         }
288 
289         @Override
290         public File getTestClassesDirectory()
291         {
292             return null;
293         }
294 
295         @Override
296         public void setTestClassesDirectory( File testClassesDirectory )
297         {
298 
299         }
300 
301         @Override
302         public File getClassesDirectory()
303         {
304             return null;
305         }
306 
307         @Override
308         public void setClassesDirectory( File classesDirectory )
309         {
310 
311         }
312 
313         @Override
314         public File getReportsDirectory()
315         {
316             return null;
317         }
318 
319         @Override
320         public void setReportsDirectory( File reportsDirectory )
321         {
322 
323         }
324 
325         @Override
326         public String getTest()
327         {
328             return null;
329         }
330 
331         @Override
332         public void setTest( String test )
333         {
334 
335         }
336 
337         @Override
338         public List<String> getIncludes()
339         {
340             return null;
341         }
342 
343         @Override
344         public File getIncludesFile()
345         {
346             return null;
347         }
348 
349         @Override
350         public void setIncludes( List<String> includes )
351         {
352 
353         }
354 
355         @Override
356         public boolean isPrintSummary()
357         {
358             return false;
359         }
360 
361         @Override
362         public void setPrintSummary( boolean printSummary )
363         {
364 
365         }
366 
367         @Override
368         public String getReportFormat()
369         {
370             return null;
371         }
372 
373         @Override
374         public void setReportFormat( String reportFormat )
375         {
376 
377         }
378 
379         @Override
380         public boolean isUseFile()
381         {
382             return false;
383         }
384 
385         @Override
386         public void setUseFile( boolean useFile )
387         {
388 
389         }
390 
391         @Override
392         public String getDebugForkedProcess()
393         {
394             return null;
395         }
396 
397         @Override
398         public void setDebugForkedProcess( String debugForkedProcess )
399         {
400 
401         }
402 
403         @Override
404         public int getForkedProcessTimeoutInSeconds()
405         {
406             return 0;
407         }
408 
409         @Override
410         public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
411         {
412 
413         }
414 
415         @Override
416         public int getForkedProcessExitTimeoutInSeconds()
417         {
418             return 0;
419         }
420 
421         @Override
422         public void setForkedProcessExitTimeoutInSeconds( int forkedProcessTerminationTimeoutInSeconds )
423         {
424 
425         }
426 
427         @Override
428         public double getParallelTestsTimeoutInSeconds()
429         {
430             return 0;
431         }
432 
433         @Override
434         public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
435         {
436 
437         }
438 
439         @Override
440         public double getParallelTestsTimeoutForcedInSeconds()
441         {
442             return 0;
443         }
444 
445         @Override
446         public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
447         {
448 
449         }
450 
451         @Override
452         public boolean isUseSystemClassLoader()
453         {
454             return false;
455         }
456 
457         @Override
458         public void setUseSystemClassLoader( boolean useSystemClassLoader )
459         {
460 
461         }
462 
463         @Override
464         public boolean isUseManifestOnlyJar()
465         {
466             return false;
467         }
468 
469         @Override
470         public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
471         {
472 
473         }
474 
475         @Override
476         public String getEncoding()
477         {
478             return null;
479         }
480 
481         @Override
482         public void setEncoding( String encoding )
483         {
484 
485         }
486 
487         @Override
488         public Boolean getFailIfNoSpecifiedTests()
489         {
490             return null;
491         }
492 
493         @Override
494         public void setFailIfNoSpecifiedTests( boolean failIfNoSpecifiedTests )
495         {
496 
497         }
498 
499         @Override
500         public int getSkipAfterFailureCount()
501         {
502             return 0;
503         }
504 
505         @Override
506         public String getShutdown()
507         {
508             return null;
509         }
510 
511         @Override
512         public File getExcludesFile()
513         {
514             return null;
515         }
516 
517         @Override
518         protected List<File> suiteXmlFiles()
519         {
520             return null;
521         }
522 
523         @Override
524         protected boolean hasSuiteXmlFiles()
525         {
526             return false;
527         }
528 
529         @Override
530         public File[] getSuiteXmlFiles()
531         {
532             return new File[0];
533         }
534 
535         @Override
536         public void setSuiteXmlFiles( File[] suiteXmlFiles )
537         {
538 
539         }
540 
541         @Override
542         public String getRunOrder()
543         {
544             return null;
545         }
546 
547         @Override
548         public void setRunOrder( String runOrder )
549         {
550 
551         }
552 
553         @Override
554         protected void handleSummary( RunResult summary, Exception firstForkException )
555                 throws MojoExecutionException, MojoFailureException
556         {
557 
558         }
559 
560         @Override
561         protected boolean isSkipExecution()
562         {
563             return false;
564         }
565 
566         @Override
567         protected String[] getDefaultIncludes()
568         {
569             return new String[0];
570         }
571 
572         @Override
573         protected String getReportSchemaLocation()
574         {
575             return null;
576         }
577 
578         @Override
579         protected Artifact getMojoArtifact()
580         {
581             return null;
582         }
583     }
584 }