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.artifact.DefaultArtifact;
24  import org.apache.maven.artifact.handler.ArtifactHandler;
25  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
29  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
30  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
31  import org.apache.maven.surefire.extensions.ForkNodeFactory;
32  import org.apache.maven.surefire.api.suite.RunResult;
33  import org.apache.maven.surefire.api.util.DefaultScanResult;
34  import org.apache.maven.toolchain.Toolchain;
35  import org.junit.Test;
36  
37  import java.io.File;
38  import java.io.FileOutputStream;
39  import java.util.List;
40  import java.util.zip.ZipEntry;
41  import java.util.zip.ZipOutputStream;
42  
43  import static java.util.Arrays.asList;
44  import static java.util.Collections.singletonList;
45  import static org.fest.assertions.Assertions.assertThat;
46  import static org.junit.Assert.fail;
47  import static org.powermock.reflect.Whitebox.invokeMethod;
48  import static org.powermock.reflect.Whitebox.setInternalState;
49  
50  /**
51   *
52   */
53  public class MojoMocklessTest
54  {
55      @Test
56      public void testGetStartupReportConfiguration() throws Exception
57      {
58          AbstractSurefireMojo surefirePlugin = new Mojo( null, null );
59          StartupReportConfiguration config = invokeMethod( surefirePlugin, "getStartupReportConfiguration", "", false );
60  
61          assertThat( config.getXmlReporter() )
62                  .isNotNull()
63                  .isInstanceOf( SurefireStatelessReporter.class );
64  
65          assertThat( config.getConsoleOutputReporter() )
66                  .isNotNull()
67                  .isInstanceOf( SurefireConsoleOutputReporter.class );
68  
69          assertThat( config.getTestsetReporter() )
70                  .isNotNull()
71                  .isInstanceOf( SurefireStatelessTestsetInfoReporter.class );
72      }
73  
74      @Test
75      public void testGetStartupReportConfiguration2() throws Exception
76      {
77          AbstractSurefireMojo surefirePlugin = new Mojo( null, null );
78          SurefireStatelessReporter xmlReporter = new SurefireStatelessReporter( false, "3.0" );
79          SurefireConsoleOutputReporter consoleReporter = new SurefireConsoleOutputReporter();
80          SurefireStatelessTestsetInfoReporter testsetInfoReporter = new SurefireStatelessTestsetInfoReporter();
81          setInternalState( surefirePlugin, "statelessTestsetReporter", xmlReporter );
82          setInternalState( surefirePlugin, "consoleOutputReporter", consoleReporter );
83          setInternalState( surefirePlugin, "statelessTestsetInfoReporter", testsetInfoReporter );
84  
85          StartupReportConfiguration config = invokeMethod( surefirePlugin, "getStartupReportConfiguration", "", false );
86  
87          assertThat( config.getXmlReporter() )
88                  .isNotNull()
89                  .isSameAs( xmlReporter );
90  
91          assertThat( config.getConsoleOutputReporter() )
92                  .isNotNull()
93                  .isSameAs( consoleReporter );
94  
95          assertThat( config.getTestsetReporter() )
96                  .isNotNull()
97                  .isSameAs( testsetInfoReporter );
98      }
99  
100     @Test
101     public void testForkMode()
102     {
103         AbstractSurefireMojo surefirePlugin = new Mojo( null, null );
104         setInternalState( surefirePlugin, "toolchain", new MyToolChain() );
105         setInternalState( surefirePlugin, "forkMode", "never" );
106         assertThat( surefirePlugin.getEffectiveForkMode() )
107                 .isEqualTo( "once" );
108     }
109 
110     @Test
111     @SuppressWarnings( "checkstyle:magicnumber" )
112     public void testForkCountComputation()
113     {
114         AbstractSurefireMojo surefirePlugin = new Mojo( null, null );
115         assertConversionFails( surefirePlugin, "nothing" );
116 
117         assertConversionFails( surefirePlugin, "5,0" );
118         assertConversionFails( surefirePlugin, "5.0" );
119         assertConversionFails( surefirePlugin, "5,0C" );
120         assertConversionFails( surefirePlugin, "5.0CC" );
121 
122         assertForkCount( surefirePlugin, 5, "5" );
123 
124         int availableProcessors = Runtime.getRuntime().availableProcessors();
125         assertForkCount( surefirePlugin, 3 * availableProcessors, "3C" );
126         assertForkCount( surefirePlugin, (int) ( 2.5 * availableProcessors ), "2.5C" );
127         assertForkCount( surefirePlugin, availableProcessors, "1.0001 C" );
128         assertForkCount( surefirePlugin, 1, 1d / ( (double) availableProcessors + 1 ) + "C" );
129         assertForkCount( surefirePlugin, 0, "0 C" );
130     }
131 
132     private static void assertForkCount( AbstractSurefireMojo surefirePlugin, int expected, String value )
133     {
134         assertThat( surefirePlugin.convertWithCoreCount( value ) )
135                 .isEqualTo( expected );
136     }
137 
138     private static void assertConversionFails( AbstractSurefireMojo surefirePlugin, String value )
139     {
140         try
141         {
142             surefirePlugin.convertWithCoreCount( value );
143         }
144         catch ( NumberFormatException e )
145         {
146             return;
147         }
148         fail( "Expected NumberFormatException when converting " + value );
149     }
150 
151     private static class MyToolChain implements Toolchain
152     {
153         @Override
154         public String getType()
155         {
156             return null;
157         }
158 
159         @Override
160         public String findTool( String s )
161         {
162             return null;
163         }
164     }
165 
166     @Test
167     public void scanDependenciesShouldReturnNull()
168             throws MojoFailureException
169     {
170         Mojo mojo = new Mojo( null, null );
171         DefaultScanResult result = mojo.scanDependencies();
172         assertThat( result )
173                 .isNull();
174     }
175 
176     @Test
177     public void scanDependenciesShouldReturnNullAfterMissingBuildArtifact()
178             throws MojoFailureException
179     {
180         VersionRange version = VersionRange.createFromVersion( "1.0" );
181         ArtifactHandler handler = new DefaultArtifactHandler();
182         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler );
183         List<Artifact> projectTestArtifacts = singletonList( testDeps );
184         String[] dependenciesToScan = { "g:a" };
185         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
186         DefaultScanResult result = mojo.scanDependencies();
187         assertThat( result )
188                 .isNull();
189     }
190 
191     @Test
192     public void scanDependenciesShouldReturnNullWithWAR()
193             throws MojoFailureException
194     {
195         VersionRange version = VersionRange.createFromVersion( "1.0" );
196         ArtifactHandler handler = new DefaultArtifactHandler();
197         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "war", null, handler );
198         testDeps.setFile( new File( new File( "target" ), "a-1.0.war" ) );
199         List<Artifact> projectTestArtifacts = singletonList( testDeps );
200         String[] dependenciesToScan = { "g:a" };
201         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
202         DefaultScanResult result = mojo.scanDependencies();
203         assertThat( result )
204                 .isNull();
205     }
206 
207     @Test
208     public void scanDependenciesShouldReturnNullWithExistingWAR()
209             throws Exception
210     {
211         VersionRange version = VersionRange.createFromVersion( "1.0" );
212         ArtifactHandler handler = new DefaultArtifactHandler();
213         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "war", null, handler );
214         File artifactFile = File.createTempFile( "surefire", ".war" );
215         testDeps.setFile( artifactFile );
216         List<Artifact> projectTestArtifacts = singletonList( testDeps );
217         String[] dependenciesToScan = { "g:a" };
218         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
219         DefaultScanResult result = mojo.scanDependencies();
220         assertThat( result )
221                 .isNull();
222     }
223 
224     @Test
225     public void scanDependenciesShouldReturnClassWithExistingTestJAR()
226             throws Exception
227     {
228         VersionRange version = VersionRange.createFromVersion( "1.0" );
229         ArtifactHandler handler = new DefaultArtifactHandler();
230         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );
231 
232         File artifactFile = File.createTempFile( "surefire", ".jar" );
233         testDeps.setFile( artifactFile );
234         try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile ) ) )
235         {
236             os.putNextEntry( new ZipEntry( "pkg/" ) );
237             os.closeEntry();
238             os.putNextEntry( new ZipEntry( "pkg/MyTest.class" ) );
239             os.closeEntry();
240             os.finish();
241         }
242 
243         List<Artifact> projectTestArtifacts = singletonList( testDeps );
244         String[] dependenciesToScan = { "g:a" };
245         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
246         DefaultScanResult result = mojo.scanDependencies();
247 
248         assertThat( result )
249                 .isNotNull();
250 
251         assertThat( result.isEmpty() )
252                 .isFalse();
253 
254         assertThat( result.getClasses() )
255                 .contains( "pkg.MyTest" );
256     }
257 
258     @Test
259     public void scanDependenciesShouldReturnNullWithEmptyTestJAR()
260             throws Exception
261     {
262         VersionRange version = VersionRange.createFromVersion( "1.0" );
263         ArtifactHandler handler = new DefaultArtifactHandler();
264         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "jar", null, handler );
265 
266         File artifactFile = File.createTempFile( "surefire", ".jar" );
267         testDeps.setFile( artifactFile );
268         try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile ) ) )
269         {
270             os.putNextEntry( new ZipEntry( "pkg/" ) );
271             os.closeEntry();
272             os.finish();
273         }
274 
275         List<Artifact> projectTestArtifacts = singletonList( testDeps );
276         String[] dependenciesToScan = { "g:a" };
277         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
278         DefaultScanResult result = mojo.scanDependencies();
279 
280         assertThat( result )
281                 .isNotNull();
282 
283         assertThat( result.isEmpty() )
284                 .isTrue();
285     }
286 
287     @Test
288     public void scanDependenciesShouldReturnClassWithDirectory()
289             throws Exception
290     {
291         VersionRange version = VersionRange.createFromVersion( "1.0" );
292         ArtifactHandler handler = new DefaultArtifactHandler();
293         Artifact testDeps = new DefaultArtifact( "g", "a", version, "compile", "test-jar", null, handler );
294 
295         File artifactFile = File.createTempFile( "surefire", "-classes" );
296         String classDir = artifactFile.getCanonicalPath();
297         assertThat( artifactFile.delete() ).isTrue();
298         File classes = new File( classDir );
299         assertThat( classes.mkdir() ).isTrue();
300 
301         testDeps.setFile( classes );
302 
303         assertThat( new File( classes, "AnotherTest.class" ).createNewFile() )
304                 .isTrue();
305 
306         List<Artifact> projectTestArtifacts = singletonList( testDeps );
307         String[] dependenciesToScan = { "g:a" };
308         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
309         DefaultScanResult result = mojo.scanDependencies();
310 
311         assertThat( result )
312                 .isNotNull();
313 
314         assertThat( result.isEmpty() )
315                 .isFalse();
316 
317         assertThat( result.getClasses() )
318                 .contains( "AnotherTest" );
319     }
320 
321     @Test
322     public void scanMultipleDependencies()
323             throws Exception
324     {
325         VersionRange version = VersionRange.createFromVersion( "1.0" );
326         ArtifactHandler handler = new DefaultArtifactHandler();
327         Artifact testDep1 = new DefaultArtifact( "g", "x", version, "compile", "jar", null, handler );
328 
329         File artifactFile1 = File.createTempFile( "surefire", "-classes" );
330         String classDir = artifactFile1.getCanonicalPath();
331         assertThat( artifactFile1.delete() ).isTrue();
332         File classes = new File( classDir );
333         assertThat( classes.mkdir() ).isTrue();
334 
335         testDep1.setFile( classes );
336 
337         assertThat( new File( classes, "AnotherTest.class" ).createNewFile() )
338                 .isTrue();
339 
340         Artifact testDep2 = new DefaultArtifact( "g", "a", version, "test", "jar", null, handler );
341         File artifactFile2 = File.createTempFile( "surefire", ".jar" );
342         testDep2.setFile( artifactFile2 );
343         try ( ZipOutputStream os = new ZipOutputStream( new FileOutputStream( artifactFile2 ) ) )
344         {
345             os.putNextEntry( new ZipEntry( "pkg/" ) );
346             os.closeEntry();
347             os.putNextEntry( new ZipEntry( "pkg/MyTest.class" ) );
348             os.closeEntry();
349             os.finish();
350         }
351 
352         List<Artifact> projectTestArtifacts = asList( testDep1, testDep2 );
353         String[] dependenciesToScan = { "g:a" };
354         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
355         DefaultScanResult result = mojo.scanDependencies();
356 
357         assertThat( result )
358                 .isNotNull();
359 
360         assertThat( result.isEmpty() )
361                 .isFalse();
362 
363         assertThat( result.getClasses() )
364                 .hasSize( 1 );
365 
366         assertThat( result.getClasses() )
367                 .contains( "pkg.MyTest" );
368     }
369 
370     private static final class Mojo
371             extends AbstractSurefireMojo
372     {
373         private final List<Artifact> projectTestArtifacts;
374         private final String[] dependenciesToScan;
375 
376         Mojo( List<Artifact> projectTestArtifacts, String[] dependenciesToScan )
377         {
378             this.projectTestArtifacts = projectTestArtifacts;
379             this.dependenciesToScan = dependenciesToScan;
380         }
381 
382         @Override
383         protected String getPluginName()
384         {
385             return null;
386         }
387 
388         @Override
389         protected int getRerunFailingTestsCount()
390         {
391             return 0;
392         }
393 
394         @Override
395         public boolean isSkipTests()
396         {
397             return false;
398         }
399 
400         @Override
401         public void setSkipTests( boolean skipTests )
402         {
403 
404         }
405 
406         @Override
407         public boolean isSkipExec()
408         {
409             return false;
410         }
411 
412         @Override
413         public void setSkipExec( boolean skipExec )
414         {
415 
416         }
417 
418         @Override
419         public boolean isSkip()
420         {
421             return false;
422         }
423 
424         @Override
425         public void setSkip( boolean skip )
426         {
427 
428         }
429 
430         @Override
431         public File getBasedir()
432         {
433             return null;
434         }
435 
436         @Override
437         public void setBasedir( File basedir )
438         {
439 
440         }
441 
442         @Override
443         public File getTestClassesDirectory()
444         {
445             return null;
446         }
447 
448         @Override
449         public void setTestClassesDirectory( File testClassesDirectory )
450         {
451 
452         }
453 
454         @Override
455         public File getMainBuildPath()
456         {
457             return null;
458         }
459 
460         @Override
461         public void setMainBuildPath( File mainBuildPath )
462         {
463 
464         }
465 
466         @Override
467         public File getReportsDirectory()
468         {
469             return null;
470         }
471 
472         @Override
473         public void setReportsDirectory( File reportsDirectory )
474         {
475 
476         }
477 
478         @Override
479         public String getTest()
480         {
481             return null;
482         }
483 
484         @Override
485         public void setTest( String test )
486         {
487 
488         }
489 
490         @Override
491         public List<String> getIncludes()
492         {
493             return null;
494         }
495 
496         @Override
497         public File getIncludesFile()
498         {
499             return null;
500         }
501 
502         @Override
503         public void setIncludes( List<String> includes )
504         {
505 
506         }
507 
508         @Override
509         public boolean isPrintSummary()
510         {
511             return false;
512         }
513 
514         @Override
515         public void setPrintSummary( boolean printSummary )
516         {
517 
518         }
519 
520         @Override
521         public String getReportFormat()
522         {
523             return null;
524         }
525 
526         @Override
527         public void setReportFormat( String reportFormat )
528         {
529 
530         }
531 
532         @Override
533         public boolean isUseFile()
534         {
535             return false;
536         }
537 
538         @Override
539         public void setUseFile( boolean useFile )
540         {
541 
542         }
543 
544         @Override
545         public String getDebugForkedProcess()
546         {
547             return null;
548         }
549 
550         @Override
551         public void setDebugForkedProcess( String debugForkedProcess )
552         {
553 
554         }
555 
556         @Override
557         public int getForkedProcessTimeoutInSeconds()
558         {
559             return 0;
560         }
561 
562         @Override
563         public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
564         {
565 
566         }
567 
568         @Override
569         public int getForkedProcessExitTimeoutInSeconds()
570         {
571             return 0;
572         }
573 
574         @Override
575         public void setForkedProcessExitTimeoutInSeconds( int forkedProcessTerminationTimeoutInSeconds )
576         {
577 
578         }
579 
580         @Override
581         public double getParallelTestsTimeoutInSeconds()
582         {
583             return 0;
584         }
585 
586         @Override
587         public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
588         {
589 
590         }
591 
592         @Override
593         public double getParallelTestsTimeoutForcedInSeconds()
594         {
595             return 0;
596         }
597 
598         @Override
599         public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
600         {
601 
602         }
603 
604         @Override
605         public boolean isUseSystemClassLoader()
606         {
607             return false;
608         }
609 
610         @Override
611         public void setUseSystemClassLoader( boolean useSystemClassLoader )
612         {
613 
614         }
615 
616         @Override
617         public boolean isUseManifestOnlyJar()
618         {
619             return false;
620         }
621 
622         @Override
623         public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
624         {
625 
626         }
627 
628         @Override
629         public String getEncoding()
630         {
631             return null;
632         }
633 
634         @Override
635         public void setEncoding( String encoding )
636         {
637 
638         }
639 
640         @Override
641         public Boolean getFailIfNoSpecifiedTests()
642         {
643             return null;
644         }
645 
646         @Override
647         public void setFailIfNoSpecifiedTests( boolean failIfNoSpecifiedTests )
648         {
649 
650         }
651 
652         @Override
653         public int getSkipAfterFailureCount()
654         {
655             return 0;
656         }
657 
658         @Override
659         public String getShutdown()
660         {
661             return null;
662         }
663 
664         @Override
665         public File getExcludesFile()
666         {
667             return null;
668         }
669 
670         @Override
671         protected List<File> suiteXmlFiles()
672         {
673             return null;
674         }
675 
676         @Override
677         protected boolean hasSuiteXmlFiles()
678         {
679             return false;
680         }
681 
682         @Override
683         protected String[] getExcludedEnvironmentVariables()
684         {
685             return new String[0];
686         }
687 
688         @Override
689         public File[] getSuiteXmlFiles()
690         {
691             return new File[0];
692         }
693 
694         @Override
695         public void setSuiteXmlFiles( File[] suiteXmlFiles )
696         {
697 
698         }
699 
700         @Override
701         public String getRunOrder()
702         {
703             return null;
704         }
705 
706         @Override
707         public void setRunOrder( String runOrder )
708         {
709 
710         }
711 
712         @Override
713         public String[] getDependenciesToScan()
714         {
715             return dependenciesToScan;
716         }
717 
718         @Override
719         protected void handleSummary( RunResult summary, Exception firstForkException )
720         {
721 
722         }
723 
724         @Override
725         protected boolean isSkipExecution()
726         {
727             return false;
728         }
729 
730         @Override
731         protected String[] getDefaultIncludes()
732         {
733             return new String[0];
734         }
735 
736         @Override
737         protected String getReportSchemaLocation()
738         {
739             return null;
740         }
741 
742         @Override
743         protected boolean useModulePath()
744         {
745             return false;
746         }
747 
748         @Override
749         protected void setUseModulePath( boolean useModulePath )
750         {
751 
752         }
753 
754         @Override
755         protected ForkNodeFactory getForkNode()
756         {
757             return null;
758         }
759 
760         @Override
761         protected String getEnableProcessChecker()
762         {
763             return null;
764         }
765 
766         @Override
767         protected Artifact getMojoArtifact()
768         {
769             return null;
770         }
771 
772         @Override
773         List<Artifact> getProjectTestArtifacts()
774         {
775             return projectTestArtifacts;
776         }
777 
778         @Override
779         public File getSystemPropertiesFile()
780         {
781             return null;
782         }
783 
784         @Override
785         public void setSystemPropertiesFile( File systemPropertiesFile )
786         {
787 
788         }
789     }
790 }