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.List; |
25 | |
import java.util.Properties; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class DefaultScanResult |
31 | |
implements ScanResult |
32 | |
{ |
33 | |
private final List files; |
34 | |
|
35 | |
private static final String scanResultNo = "tc."; |
36 | |
|
37 | |
public DefaultScanResult( List files ) |
38 | 2 | { |
39 | 2 | this.files = files; |
40 | 2 | } |
41 | |
|
42 | |
public int size() |
43 | |
{ |
44 | 0 | return files.size(); |
45 | |
} |
46 | |
|
47 | |
public String getClassName( int index ) |
48 | |
{ |
49 | 0 | return (String) files.get( index ); |
50 | |
} |
51 | |
|
52 | |
public void writeTo( Properties properties ) |
53 | |
{ |
54 | 1 | int size = files.size(); |
55 | 3 | for ( int i = 0; i < size; i++ ) |
56 | |
{ |
57 | 2 | properties.setProperty( scanResultNo + i, (String) files.get( i ) ); |
58 | |
} |
59 | 1 | } |
60 | |
|
61 | |
public static DefaultScanResult from( Properties properties ) |
62 | |
{ |
63 | 1 | List result = new ArrayList(); |
64 | 1 | int i = 0; |
65 | |
while ( true ) |
66 | |
{ |
67 | 3 | String item = properties.getProperty( scanResultNo + ( i++ ) ); |
68 | 3 | if ( item == null ) |
69 | |
{ |
70 | 1 | return new DefaultScanResult( result ); |
71 | |
} |
72 | 2 | result.add( item ); |
73 | 2 | } |
74 | |
} |
75 | |
|
76 | |
public boolean isEmpty() |
77 | |
{ |
78 | 0 | return files.isEmpty(); |
79 | |
} |
80 | |
|
81 | |
public List getFiles() |
82 | |
{ |
83 | 1 | return Collections.unmodifiableList( files ); |
84 | |
} |
85 | |
|
86 | |
public TestsToRun applyFilter( ScannerFilter scannerFilter, ClassLoader testClassLoader ) |
87 | |
{ |
88 | 0 | List result = new ArrayList(); |
89 | |
|
90 | 0 | int size = size(); |
91 | 0 | for ( int i = 0; i < size; i++ ) |
92 | |
{ |
93 | 0 | String className = getClassName( i ); |
94 | |
|
95 | 0 | Class testClass = loadClass( testClassLoader, className ); |
96 | |
|
97 | 0 | if ( scannerFilter == null || scannerFilter.accept( testClass ) ) |
98 | |
{ |
99 | 0 | result.add( testClass ); |
100 | |
} |
101 | |
} |
102 | |
|
103 | 0 | return new TestsToRun( result ); |
104 | |
} |
105 | |
|
106 | |
public List getClassesSkippedByValidation( ScannerFilter scannerFilter, ClassLoader testClassLoader ) |
107 | |
{ |
108 | 0 | List result = new ArrayList(); |
109 | |
|
110 | 0 | int size = size(); |
111 | 0 | for ( int i = 0; i < size; i++ ) |
112 | |
{ |
113 | 0 | String className = getClassName( i ); |
114 | |
|
115 | 0 | Class testClass = loadClass( testClassLoader, className ); |
116 | |
|
117 | 0 | if ( scannerFilter != null && !scannerFilter.accept( testClass ) ) |
118 | |
{ |
119 | 0 | result.add( testClass ); |
120 | |
} |
121 | |
} |
122 | |
|
123 | 0 | return result; |
124 | |
} |
125 | |
|
126 | |
private static Class loadClass( ClassLoader classLoader, String className ) |
127 | |
{ |
128 | |
Class testClass; |
129 | |
try |
130 | |
{ |
131 | 0 | testClass = classLoader.loadClass( className ); |
132 | |
} |
133 | 0 | catch ( ClassNotFoundException e ) |
134 | |
{ |
135 | 0 | throw new NestedRuntimeException( "Unable to create test class '" + className + "'", e ); |
136 | 0 | } |
137 | 0 | return testClass; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
} |