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.io.BufferedReader; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.InputStreamReader; |
26 | |
import java.io.PrintStream; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Collections; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
import org.apache.maven.surefire.booter.ForkingRunListener; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class LazyTestsToRun |
43 | |
extends TestsToRun |
44 | |
{ |
45 | 0 | private List workQueue = new ArrayList(); |
46 | |
|
47 | |
private BufferedReader inputReader; |
48 | |
|
49 | 0 | private boolean streamClosed = false; |
50 | |
|
51 | |
private ClassLoader testClassLoader; |
52 | |
|
53 | |
private PrintStream originalOutStream; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public LazyTestsToRun( InputStream testSource, ClassLoader testClassLoader, PrintStream originalOutStream ) |
63 | |
{ |
64 | 0 | super( Collections.emptyList() ); |
65 | |
|
66 | 0 | this.testClassLoader = testClassLoader; |
67 | 0 | this.originalOutStream = originalOutStream; |
68 | |
|
69 | 0 | inputReader = new BufferedReader( new InputStreamReader( testSource ) ); |
70 | 0 | } |
71 | |
|
72 | |
protected void addWorkItem( String className ) |
73 | |
{ |
74 | 0 | synchronized ( workQueue ) |
75 | |
{ |
76 | 0 | workQueue.add( ReflectionUtils.loadClass( testClassLoader, className ) ); |
77 | 0 | } |
78 | 0 | } |
79 | |
|
80 | |
protected void requestNextTest() |
81 | |
{ |
82 | 0 | StringBuffer sb = new StringBuffer(); |
83 | 0 | sb.append( (char) ForkingRunListener.BOOTERCODE_NEXT_TEST ).append( ",0,want more!\n" ); |
84 | 0 | originalOutStream.print( sb.toString() ); |
85 | 0 | } |
86 | |
|
87 | 0 | private class BlockingIterator |
88 | |
implements Iterator |
89 | |
{ |
90 | 0 | private int lastPos = -1; |
91 | |
|
92 | |
public boolean hasNext() |
93 | |
{ |
94 | 0 | int nextPos = lastPos + 1; |
95 | 0 | synchronized ( workQueue ) |
96 | |
{ |
97 | 0 | if ( workQueue.size() > nextPos ) |
98 | |
{ |
99 | 0 | return true; |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | if ( needsToWaitForInput( nextPos ) ) |
104 | |
{ |
105 | 0 | requestNextTest(); |
106 | |
|
107 | |
String nextClassName; |
108 | |
try |
109 | |
{ |
110 | 0 | nextClassName = inputReader.readLine(); |
111 | |
} |
112 | 0 | catch ( IOException e ) |
113 | |
{ |
114 | 0 | streamClosed = true; |
115 | 0 | return false; |
116 | 0 | } |
117 | |
|
118 | 0 | if ( null == nextClassName ) |
119 | |
{ |
120 | 0 | streamClosed = true; |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 0 | addWorkItem( nextClassName ); |
125 | |
} |
126 | |
} |
127 | |
|
128 | 0 | return ( workQueue.size() > nextPos ); |
129 | |
} |
130 | 0 | } |
131 | |
} |
132 | |
|
133 | |
private boolean needsToWaitForInput( int nextPos ) |
134 | |
{ |
135 | 0 | return workQueue.size() == nextPos && !streamClosed; |
136 | |
} |
137 | |
|
138 | |
public Object next() |
139 | |
{ |
140 | 0 | synchronized ( workQueue ) |
141 | |
{ |
142 | 0 | return workQueue.get( ++lastPos ); |
143 | 0 | } |
144 | |
} |
145 | |
|
146 | |
public void remove() |
147 | |
{ |
148 | 0 | throw new UnsupportedOperationException(); |
149 | |
} |
150 | |
|
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public Iterator iterator() |
157 | |
{ |
158 | 0 | return new BlockingIterator(); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public String toString() |
165 | |
{ |
166 | 0 | StringBuffer sb = new StringBuffer( "LazyTestsToRun " ); |
167 | 0 | synchronized ( workQueue ) |
168 | |
{ |
169 | 0 | sb.append( "(more items expected: " ).append( !streamClosed ).append( "): " ); |
170 | 0 | sb.append( workQueue ); |
171 | 0 | } |
172 | |
|
173 | 0 | return sb.toString(); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public boolean allowEagerReading() { |
180 | 0 | return false; |
181 | |
} |
182 | |
|
183 | |
} |