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