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.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   * Contains all the tests that have been found according to specified include/exclude
33   * specification for a given surefire run.
34   *
35   * @author Kristian Rosenvold (junit core adaption)
36   */
37  public class TestsToRun
38  {
39      private final List locatedClasses;
40  
41      /**
42       * Constructor
43       *
44       * @param locatedClasses A list of java.lang.Class objects representing tests to run
45       */
46      public TestsToRun( List locatedClasses )
47      {
48          this.locatedClasses = Collections.unmodifiableList( locatedClasses );
49          Set testSets = new HashSet();
50  
51          for ( Iterator iterator = locatedClasses.iterator(); iterator.hasNext(); )
52          {
53              Class testClass = (Class) iterator.next();
54              if ( testSets.contains( testClass ) )
55              {
56                  throw new RuntimeException( "Duplicate test set '" + testClass.getName() + "'" );
57              }
58              testSets.add( testClass );
59          }
60      }
61  
62      public static TestsToRun fromClass( Class clazz )
63          throws TestSetFailedException
64      {
65          return new TestsToRun( Arrays.asList( new Class[]{ clazz } ) );
66      }
67  
68      /**
69       * Returns an iterator over the located java.lang.Class objects
70       *
71       * @return an unmodifiable iterator
72       */
73      public Iterator iterator()
74      {
75          return locatedClasses.iterator();
76      }
77  
78      public String toString()
79      {
80          StringBuffer sb = new StringBuffer();
81          sb.append( "TestsToRun: [" );
82          Iterator it = iterator();
83          while ( it.hasNext() )
84          {
85              Class clazz = (Class) it.next();
86              sb.append( " " ).append( clazz.getName() );
87          }
88  
89          sb.append( ']' );
90          return sb.toString();
91      }
92  
93      public boolean containsAtLeast( int atLeast )
94      {
95          return containsAtLeast( iterator(), atLeast );
96      }
97  
98      private boolean containsAtLeast( Iterator it, int atLeast )
99      {
100         for ( int i = 0; i < atLeast; i++ )
101         {
102             if ( !it.hasNext() )
103             {
104                 return false;
105             }
106 
107             it.next();
108         }
109 
110         return true;
111     }
112 
113     public boolean containsExactly( int items )
114     {
115         Iterator it = iterator();
116         return containsAtLeast( it, items ) && !it.hasNext();
117     }
118 
119     /**
120      * @return {@code true}, if the classes may be read eagerly. {@code false},
121      *         if the classes must only be read lazy.
122      */
123     public boolean allowEagerReading()
124     {
125         return true;
126     }
127 
128     public Class[] getLocatedClasses()
129     {
130         if ( !allowEagerReading() )
131         {
132             throw new IllegalStateException( "Cannot eagerly read" );
133         }
134         List result = new ArrayList();
135         Iterator it = iterator();
136         while ( it.hasNext() )
137         {
138             result.add( it.next() );
139         }
140         return (Class[]) result.toArray( new Class[result.size()] );
141     }
142 }