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