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