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.Arrays; |
24 | |
import java.util.Collections; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Set; |
29 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class TestsToRun implements Iterable<Class> |
38 | |
{ |
39 | |
private final List<Class> locatedClasses; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public TestsToRun( List<Class> locatedClasses ) |
47 | 6 | { |
48 | 6 | this.locatedClasses = Collections.unmodifiableList( locatedClasses ); |
49 | 6 | Set<Class> testSets = new HashSet<Class>(); |
50 | |
|
51 | 6 | for ( Class testClass : locatedClasses ) |
52 | |
{ |
53 | 12 | if ( testSets.contains( testClass ) ) |
54 | |
{ |
55 | 0 | throw new RuntimeException( "Duplicate test set '" + testClass.getName() + "'" ); |
56 | |
} |
57 | 12 | testSets.add( testClass ); |
58 | 12 | } |
59 | 6 | } |
60 | |
|
61 | |
public static TestsToRun fromClass( Class clazz ) |
62 | |
throws TestSetFailedException |
63 | |
{ |
64 | 0 | return new TestsToRun( Arrays.<Class>asList( clazz ) ); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public Iterator<Class> iterator() |
73 | |
{ |
74 | 9 | return locatedClasses.iterator(); |
75 | |
} |
76 | |
|
77 | |
public String toString() |
78 | |
{ |
79 | 0 | StringBuilder sb = new StringBuilder(); |
80 | 0 | sb.append( "TestsToRun: [" ); |
81 | 0 | Iterator it = iterator(); |
82 | 0 | while ( it.hasNext() ) |
83 | |
{ |
84 | 0 | Class clazz = (Class) it.next(); |
85 | 0 | sb.append( " " ).append( clazz.getName() ); |
86 | 0 | } |
87 | |
|
88 | 0 | sb.append( ']' ); |
89 | 0 | return sb.toString(); |
90 | |
} |
91 | |
|
92 | |
public boolean containsAtLeast( int atLeast ) |
93 | |
{ |
94 | 2 | return containsAtLeast( iterator(), atLeast ); |
95 | |
} |
96 | |
|
97 | |
private boolean containsAtLeast( Iterator it, int atLeast ) |
98 | |
{ |
99 | 14 | for ( int i = 0; i < atLeast; i++ ) |
100 | |
{ |
101 | 11 | if ( !it.hasNext() ) |
102 | |
{ |
103 | 2 | return false; |
104 | |
} |
105 | |
|
106 | 9 | it.next(); |
107 | |
} |
108 | |
|
109 | 3 | return true; |
110 | |
} |
111 | |
|
112 | |
public boolean containsExactly( int items ) |
113 | |
{ |
114 | 3 | Iterator it = iterator(); |
115 | 3 | return containsAtLeast( it, items ) && !it.hasNext(); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public boolean allowEagerReading() |
123 | |
{ |
124 | 1 | return true; |
125 | |
} |
126 | |
|
127 | |
public Class[] getLocatedClasses() |
128 | |
{ |
129 | 1 | if ( !allowEagerReading() ) |
130 | |
{ |
131 | 0 | throw new IllegalStateException( "Cannot eagerly read" ); |
132 | |
} |
133 | 1 | List<Class> result = new ArrayList<Class>(); |
134 | 1 | Iterator<Class> it = iterator(); |
135 | 3 | while ( it.hasNext() ) |
136 | |
{ |
137 | 2 | result.add( it.next() ); |
138 | |
} |
139 | 1 | return result.toArray( new Class[result.size()] ); |
140 | |
} |
141 | |
} |