View Javadoc
1   package org.apache.maven.plugin.surefire.util;
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.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   * @author Kristian Rosenvold
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          // use target as people can configure ide to compile in an other place than maven
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  }