View Javadoc
1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Contains all the tests that have been found according to specified include/exclude
35   * specification for a given surefire run.
36   *
37   * @author Kristian Rosenvold (junit core adaption)
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       * Constructor
49       *
50       * @param locatedClasses A set of java.lang.Class objects representing tests to run
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       * @return test classes which have been retrieved by {@link TestsToRun#iterator()}.
65       */
66      public Iterator<Class<?>> iterated()
67      {
68          return newWeakIterator();
69      }
70  
71      /**
72       * Returns an iterator over the located java.lang.Class objects
73       *
74       * @return an unmodifiable iterator
75       */
76      @Override
77      public Iterator<Class<?>> iterator()
78      {
79          return new ClassesIterator();
80      }
81  
82      private final class ClassesIterator
83          extends CloseableIterator<Class<?>>
84      {
85          private final Iterator<Class<?>> it = TestsToRun.this.locatedClasses.iterator();
86  
87          private int iteratedCount;
88  
89          @Override
90          protected boolean isClosed()
91          {
92              return TestsToRun.this.isFinished();
93          }
94  
95          @Override
96          protected boolean doHasNext()
97          {
98              return it.hasNext();
99          }
100 
101         @Override
102         protected Class<?> doNext()
103         {
104             Class<?> nextTest = it.next();
105             TestsToRun.this.iteratedCount = max( ++iteratedCount, TestsToRun.this.iteratedCount );
106             return nextTest;
107         }
108 
109         @Override
110         protected void doRemove()
111         {
112         }
113 
114         @Override
115         public void remove()
116         {
117             throw new UnsupportedOperationException( "unsupported remove" );
118         }
119     }
120 
121     public final void markTestSetFinished()
122     {
123         finished = true;
124     }
125 
126     public final boolean isFinished()
127     {
128         return finished;
129     }
130 
131     @Override
132     public String toString()
133     {
134         StringBuilder sb = new StringBuilder( "TestsToRun: [" );
135         for ( Class<?> clazz : this )
136         {
137             sb.append( ' ' )
138                     .append( clazz.getName() );
139         }
140 
141         sb.append( ']' );
142         return sb.toString();
143     }
144 
145     public boolean containsAtLeast( int atLeast )
146     {
147         return containsAtLeast( iterator(), atLeast );
148     }
149 
150     private boolean containsAtLeast( Iterator<Class<?>> it, int atLeast )
151     {
152         for ( int i = 0; i < atLeast; i++ )
153         {
154             if ( !it.hasNext() )
155             {
156                 return false;
157             }
158 
159             it.next();
160         }
161 
162         return true;
163     }
164 
165     public boolean containsExactly( int items )
166     {
167         Iterator<Class<?>> it = iterator();
168         return containsAtLeast( it, items ) && !it.hasNext();
169     }
170 
171     /**
172      * @return {@code true}, if the classes may be read eagerly. {@code false},
173      *         if the classes must only be read lazy.
174      */
175     public boolean allowEagerReading()
176     {
177         return true;
178     }
179 
180     public Class<?>[] getLocatedClasses()
181     {
182         if ( !allowEagerReading() )
183         {
184             throw new IllegalStateException( "Cannot eagerly read" );
185         }
186         Collection<Class<?>> result = new ArrayList<Class<?>>();
187         for ( Class<?> clazz : this )
188         {
189             result.add( clazz );
190         }
191         return result.toArray( new Class<?>[result.size()] );
192     }
193 
194     /**
195      * Get test class which matches className
196      *
197      * @param className string used to find the test class
198      * @return Class object with the matching name, null if could not find a class with the matching name
199      */
200     public Class<?> getClassByName( String className )
201     {
202         for ( Class<?> clazz : this )
203         {
204             if ( clazz.getName().equals( className ) )
205             {
206                 return clazz;
207             }
208         }
209         return null;
210     }
211 
212     /**
213      * @return snapshot of tests upon constructs of internal iterator.
214      * Therefore weakly consistent while {@link TestsToRun#iterator()} is being iterated.
215      */
216     private Iterator<Class<?>> newWeakIterator()
217     {
218         final Iterator<Class<?>> it = locatedClasses.subList( 0, iteratedCount ).iterator();
219         return new CloseableIterator<Class<?>>()
220         {
221             @Override
222             protected boolean isClosed()
223             {
224                 return TestsToRun.this.isFinished();
225             }
226 
227             @Override
228             protected boolean doHasNext()
229             {
230                 return it.hasNext();
231             }
232 
233             @Override
234             protected Class<?> doNext()
235             {
236                 return it.next();
237             }
238 
239             @Override
240             protected void doRemove()
241             {
242             }
243 
244             @Override
245             public void remove()
246             {
247                 throw new UnsupportedOperationException( "unsupported remove" );
248             }
249         };
250     }
251 }