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 junit.framework.TestCase;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.DefaultArtifact;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.surefire.testset.TestListResolver;
27  import org.apache.maven.surefire.util.ScanResult;
28  
29  import java.io.File;
30  import java.io.FileOutputStream;
31  import java.util.*;
32  import java.util.zip.ZipEntry;
33  import java.util.zip.ZipOutputStream;
34  
35  /**
36   * @author Aslak Knutsen
37   */
38  public class DependenciesScannerTest
39      extends TestCase
40  {
41      public void testLocateTestClasses()
42          throws Exception
43      {
44          File testFile = writeTestFile();
45  
46          // use target as people can configure ide to compile in an other place than maven
47          Artifact artifact =
48              new DefaultArtifact( "org.surefire.dependency", "test-jar", VersionRange.createFromVersion( "1.0" ), "test",
49                                   "jar", "tests", null );
50          artifact.setFile( testFile );
51  
52          List<String> scanDependencies = new ArrayList<String>();
53          scanDependencies.add( "org.surefire.dependency:test-jar" );
54  
55          List<String> include = new ArrayList<String>();
56          include.add( "**/*A.java" );
57          List<String> exclude = new ArrayList<String>();
58  
59          DependencyScanner scanner =
60              new DependencyScanner( DependencyScanner.filter( Collections.singletonList( artifact ), scanDependencies ),
61                                     new TestListResolver( include, exclude ) );
62  
63          ScanResult classNames = scanner.scan();
64          assertNotNull( classNames );
65          assertEquals( 1, classNames.size() );
66  
67          Map<String, String> props = new HashMap<String, String>();
68          classNames.writeTo( props );
69          assertEquals( 1, props.size() );
70      }
71  
72      private File writeTestFile()
73          throws Exception
74      {
75          File output = new File( "target/DependenciesScannerTest-tests.jar" );
76          output.delete();
77  
78          ZipOutputStream out = new ZipOutputStream( new FileOutputStream( output ) );
79          try
80          {
81              out.putNextEntry( new ZipEntry( "org/test/TestA.class" ) );
82              out.closeEntry();
83              out.putNextEntry( new ZipEntry( "org/test/TestB.class" ) );
84              out.closeEntry();
85              return output;
86          }
87          finally
88          {
89              out.close();
90          }
91      }
92  }