1 package org.apache.maven.plugin.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.api.testset.TestListResolver;
23 import org.apache.maven.surefire.api.util.ScanResult;
24 import org.hamcrest.Matcher;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.Parameterized;
28
29 import java.io.File;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.hamcrest.Matchers.is;
36 import static org.hamcrest.Matchers.hasSize;
37 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
38 import static org.hamcrest.Matchers.notNullValue;
39 import static org.junit.runners.Parameterized.Parameter;
40 import static org.junit.runners.Parameterized.Parameters;
41
42
43
44
45 @RunWith( Parameterized.class )
46 public class DirectoryScannerTest
47 {
48 @Parameters( name = "\"{0}\" should count {1} classes" )
49 public static Iterable<Object[]> data()
50 {
51 return Arrays.asList( new Object[][]
52 {
53 { "**/*ZT*A.java", is( 3 ) },
54 { "**/*ZT*A.java#testMethod", is( 3 ) },
55 { "**/*ZT?A.java#testMethod, !*ZT2A", is( 2 ) },
56 { "**/*ZT?A.java#testMethod, !*ZT2A#testMethod", is( 3 ) },
57 { "#testMethod", is( greaterThanOrEqualTo( 3 ) ) }
58 } );
59 }
60
61 @Parameter( 0 )
62 @SuppressWarnings( "checkstyle:visibilitymodifier" )
63 public String filter;
64
65 @Parameter( 1 )
66 @SuppressWarnings( "checkstyle:visibilitymodifier" )
67 public Matcher<? super Integer> expectedClassesCount;
68
69 @Test
70 public void locateTestClasses()
71 throws Exception
72 {
73
74 File baseDir = new File( new File( "target/test-classes" ).getCanonicalPath() );
75 TestListResolver resolver = new TestListResolver( filter );
76 DirectoryScanner surefireDirectoryScanner = new DirectoryScanner( baseDir, resolver );
77
78 ScanResult classNames = surefireDirectoryScanner.scan();
79 assertThat( classNames, is( notNullValue() ) );
80 assertThat( classNames.size(), is( expectedClassesCount ) );
81
82 Map<String, String> props = new HashMap<>();
83 classNames.writeTo( props );
84 assertThat( props.values(), hasSize( expectedClassesCount ) );
85 }
86 }