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