1 package org.apache.maven.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29
30
31
32 public class DefaultScanResult
33 implements ScanResult
34 {
35 private final List<String> files;
36
37 private static final String SCAN_RESULT_NUMBER = "tc.";
38
39 public DefaultScanResult( List<String> files )
40 {
41 this.files = Collections.unmodifiableList( files );
42 }
43
44 public int size()
45 {
46 return files.size();
47 }
48
49 public String getClassName( int index )
50 {
51 return files.get( index );
52 }
53
54 public void writeTo( Map<String, String> properties )
55 {
56 for ( int i = 0, size = files.size(); i < size; i++ )
57 {
58 properties.put( SCAN_RESULT_NUMBER + i, files.get( i ) );
59 }
60 }
61
62 public static DefaultScanResult from( Map<String, String> properties )
63 {
64 List<String> result = new ArrayList<String>();
65 int i = 0;
66 while ( true )
67 {
68 String item = properties.get( SCAN_RESULT_NUMBER + ( i++ ) );
69 if ( item == null )
70 {
71 return new DefaultScanResult( result );
72 }
73 result.add( item );
74 }
75 }
76
77 public boolean isEmpty()
78 {
79 return files.isEmpty();
80 }
81
82 public List getFiles()
83 {
84 return files;
85 }
86
87 public TestsToRun applyFilter( ScannerFilter scannerFilter, ClassLoader testClassLoader )
88 {
89 Set<Class<?>> result = new LinkedHashSet<Class<?>>();
90
91 int size = size();
92 for ( int i = 0; i < size; i++ )
93 {
94 String className = getClassName( i );
95
96 Class<?> testClass = loadClass( testClassLoader, className );
97
98 if ( scannerFilter == null || scannerFilter.accept( testClass ) )
99 {
100 result.add( testClass );
101 }
102 }
103
104 return new TestsToRun( result );
105 }
106
107 public List<Class<?>> getClassesSkippedByValidation( ScannerFilter scannerFilter, ClassLoader testClassLoader )
108 {
109 List<Class<?>> result = new ArrayList<Class<?>>();
110
111 int size = size();
112 for ( int i = 0; i < size; i++ )
113 {
114 String className = getClassName( i );
115
116 Class<?> testClass = loadClass( testClassLoader, className );
117
118 if ( scannerFilter != null && !scannerFilter.accept( testClass ) )
119 {
120 result.add( testClass );
121 }
122 }
123
124 return result;
125 }
126
127 private static Class<?> loadClass( ClassLoader classLoader, String className )
128 {
129 try
130 {
131 return classLoader.loadClass( className );
132 }
133 catch ( ClassNotFoundException e )
134 {
135 throw new RuntimeException( "Unable to create test class '" + className + "'", e );
136 }
137 }
138
139 public DefaultScanResult append( DefaultScanResult other )
140 {
141 if ( other != null )
142 {
143 List<String> src = new ArrayList<String>( files );
144 src.addAll( other.files );
145 return new DefaultScanResult( src );
146 }
147 else
148 {
149 return this;
150 }
151 }
152
153 }