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.Collection;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.apache.maven.surefire.testset.TestSetFailedException;
30
31 import static java.lang.Math.max;
32
33
34
35
36
37
38
39 public class TestsToRun implements Iterable<Class<?>>
40 {
41 private final List<Class<?>> locatedClasses;
42
43 private volatile boolean finished;
44
45 private int iteratedCount;
46
47
48
49
50
51
52 public TestsToRun( Set<Class<?>> locatedClasses )
53 {
54 this.locatedClasses = new ArrayList<Class<?>>( locatedClasses );
55 }
56
57 public static TestsToRun fromClass( Class<?> clazz )
58 throws TestSetFailedException
59 {
60 return new TestsToRun( Collections.<Class<?>>singleton( clazz ) );
61 }
62
63
64
65
66 public Iterator<Class<?>> iterated()
67 {
68 return newWeakIterator();
69 }
70
71
72
73
74
75
76 public Iterator<Class<?>> iterator()
77 {
78 return new ClassesIterator();
79 }
80
81 private final class ClassesIterator
82 extends CloseableIterator<Class<?>>
83 {
84 private final Iterator<Class<?>> it = TestsToRun.this.locatedClasses.iterator();
85
86 private int iteratedCount;
87
88 @Override
89 protected boolean isClosed()
90 {
91 return TestsToRun.this.isFinished();
92 }
93
94 @Override
95 protected boolean doHasNext()
96 {
97 return it.hasNext();
98 }
99
100 @Override
101 protected Class<?> doNext()
102 {
103 Class<?> nextTest = it.next();
104 TestsToRun.this.iteratedCount = max( ++iteratedCount, TestsToRun.this.iteratedCount );
105 return nextTest;
106 }
107
108 @Override
109 protected void doRemove()
110 {
111 }
112
113 @Override
114 public void remove()
115 {
116 throw new UnsupportedOperationException( "unsupported remove" );
117 }
118 }
119
120 public final void markTestSetFinished()
121 {
122 finished = true;
123 }
124
125 public final boolean isFinished()
126 {
127 return finished;
128 }
129
130 public String toString()
131 {
132 StringBuilder sb = new StringBuilder( "TestsToRun: [" );
133 for ( Class<?> clazz : this )
134 {
135 sb.append( ' ' )
136 .append( clazz.getName() );
137 }
138
139 sb.append( ']' );
140 return sb.toString();
141 }
142
143 public boolean containsAtLeast( int atLeast )
144 {
145 return containsAtLeast( iterator(), atLeast );
146 }
147
148 private boolean containsAtLeast( Iterator<Class<?>> it, int atLeast )
149 {
150 for ( int i = 0; i < atLeast; i++ )
151 {
152 if ( !it.hasNext() )
153 {
154 return false;
155 }
156
157 it.next();
158 }
159
160 return true;
161 }
162
163 public boolean containsExactly( int items )
164 {
165 Iterator<Class<?>> it = iterator();
166 return containsAtLeast( it, items ) && !it.hasNext();
167 }
168
169
170
171
172
173 public boolean allowEagerReading()
174 {
175 return true;
176 }
177
178 public Class<?>[] getLocatedClasses()
179 {
180 if ( !allowEagerReading() )
181 {
182 throw new IllegalStateException( "Cannot eagerly read" );
183 }
184 Collection<Class<?>> result = new ArrayList<Class<?>>();
185 for ( Class<?> clazz : this )
186 {
187 result.add( clazz );
188 }
189 return result.toArray( new Class<?>[result.size()] );
190 }
191
192
193
194
195
196
197
198 public Class<?> getClassByName( String className )
199 {
200 for ( Class<?> clazz : this )
201 {
202 if ( clazz.getName().equals( className ) )
203 {
204 return clazz;
205 }
206 }
207 return null;
208 }
209
210
211
212
213
214 private Iterator<Class<?>> newWeakIterator()
215 {
216 final Iterator<Class<?>> it = locatedClasses.subList( 0, iteratedCount ).iterator();
217 return new CloseableIterator<Class<?>>()
218 {
219 @Override
220 protected boolean isClosed()
221 {
222 return TestsToRun.this.isFinished();
223 }
224
225 @Override
226 protected boolean doHasNext()
227 {
228 return it.hasNext();
229 }
230
231 @Override
232 protected Class<?> doNext()
233 {
234 return it.next();
235 }
236
237 @Override
238 protected void doRemove()
239 {
240 }
241
242 @Override
243 public void remove()
244 {
245 throw new UnsupportedOperationException( "unsupported remove" );
246 }
247 };
248 }
249 }