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          List<File> dependenciesToScan = new ArrayList<File>();
60          for ( Artifact a : DependencyScanner.filter( Collections.singletonList( artifact ), scanDependencies ) )
61          {
62              dependenciesToScan.add( a.getFile() );
63          }
64  
65          DependencyScanner scanner =
66              new DependencyScanner( dependenciesToScan, new TestListResolver( include, exclude ) );
67  
68          ScanResult classNames = scanner.scan();
69          assertNotNull( classNames );
70          assertEquals( 1, classNames.size() );
71  
72          Map<String, String> props = new HashMap<String, String>();
73          classNames.writeTo( props );
74          assertEquals( 1, props.size() );
75      }
76  
77      private File writeTestFile()
78          throws Exception
79      {
80          File output = new File( "target/DependenciesScannerTest-tests.jar" );
81          output.delete();
82  
83          ZipOutputStream out = new ZipOutputStream( new FileOutputStream( output ) );
84          try
85          {
86              out.putNextEntry( new ZipEntry( "org/test/TestA.class" ) );
87              out.closeEntry();
88              out.putNextEntry( new ZipEntry( "org/test/TestB.class" ) );
89              out.closeEntry();
90              return output;
91          }
92          finally
93          {
94              out.close();
95          }
96      }
97  }