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<>();
53 scanDependencies.add( "org.surefire.dependency:test-jar" );
54
55 List<String> include = new ArrayList<>();
56 include.add( "**/*A.java" );
57 List<String> exclude = new ArrayList<>();
58
59 List<File> dependenciesToScan = new ArrayList<>();
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<>();
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 try ( ZipOutputStream out = new ZipOutputStream( new FileOutputStream( output ) ) )
84 {
85 out.putNextEntry( new ZipEntry( "org/test/TestA.class" ) );
86 out.closeEntry();
87 out.putNextEntry( new ZipEntry( "org/test/TestB.class" ) );
88 out.closeEntry();
89 return output;
90 }
91 }
92 }