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.testset.TestListResolver;
23  import org.apache.maven.surefire.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.*;
36  import static org.junit.runners.Parameterized.*;
37  
38  /**
39   * @author Kristian Rosenvold
40   */
41  @RunWith( Parameterized.class )
42  public class DirectoryScannerTest
43  {
44      @Parameters( name = "\"{0}\" should count {1} classes" )
45      public static Iterable<Object[]> data() {
46          return Arrays.asList( new Object[][] {
47              { "**/*ZT*A.java", is( 3 ) },
48              { "**/*ZT*A.java#testMethod", is( 3 ) },
49              { "**/*ZT?A.java#testMethod, !*ZT2A", is( 2 ) },
50              { "**/*ZT?A.java#testMethod, !*ZT2A#testMethod", is( 3 ) },
51              { "#testMethod", is( greaterThanOrEqualTo( 3 ) ) },
52          } );
53      }
54  
55      @Parameter( 0 )
56      public String filter;
57  
58      @Parameter( 1 )
59      public Matcher<? super Integer> expectedClassesCount;
60  
61      @Test
62      public void locateTestClasses()
63          throws Exception
64      {
65          // use target as people can configure ide to compile in an other place than maven
66          File baseDir = new File( new File( "target/test-classes" ).getCanonicalPath() );
67          TestListResolver resolver = new TestListResolver( filter );
68          DirectoryScanner surefireDirectoryScanner = new DirectoryScanner( baseDir, resolver );
69  
70          ScanResult classNames = surefireDirectoryScanner.scan();
71          assertThat( classNames, is( notNullValue() ) );
72          assertThat( classNames.size(), is( expectedClassesCount ) );
73  
74          Map<String, String> props = new HashMap<String, String>();
75          classNames.writeTo( props );
76          assertThat( props.values(), hasSize( expectedClassesCount ) );
77      }
78  }