View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Kristian Rosenvold
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          // use target as people can configure ide to compile in an other place than maven
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  }