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