1 package org.apache.maven.plugin.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38 public class DependenciesScannerTest
39 extends TestCase
40 {
41 public void testLocateTestClasses()
42 throws Exception
43 {
44 File testFile = writeTestFile();
45
46
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 }