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