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.Arrays;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Set;
28 import org.apache.maven.surefire.testset.TestSetFailedException;
29
30 /**
31 * Contains all the tests that have been found according to specified include/exclude
32 * specification for a given surefire run.
33 *
34 * @author Kristian Rosenvold (junit core adaption)
35 */
36 public class TestsToRun
37 {
38 private final List locatedClasses;
39
40 /**
41 * Constructor
42 *
43 * @param locatedClasses A list of java.lang.Class objects representing tests to run
44 */
45 public TestsToRun( List locatedClasses )
46 {
47 this.locatedClasses = Collections.unmodifiableList( locatedClasses );
48 Set testSets = new HashSet();
49
50 for ( Iterator iterator = locatedClasses.iterator(); iterator.hasNext(); )
51 {
52 Class testClass = (Class) iterator.next();
53 if ( testSets.contains( testClass ) )
54 {
55 throw new RuntimeException( "Duplicate test set '" + testClass.getName() + "'" );
56 }
57 testSets.add( testClass );
58 }
59 }
60
61 public static TestsToRun fromClass( Class clazz )
62 throws TestSetFailedException
63 {
64 return new TestsToRun( Arrays.asList( new Class[]{ clazz } ) );
65 }
66
67 public int size()
68 {
69 return locatedClasses.size();
70 }
71
72 public Class[] getLocatedClasses()
73 {
74 return (Class[]) locatedClasses.toArray( new Class[locatedClasses.size()] );
75 }
76
77 /**
78 * Returns an iterator over the located java.lang.Class objects
79 *
80 * @return an unmodifiable iterator
81 */
82 public Iterator iterator()
83 {
84 return locatedClasses.iterator();
85 }
86
87 public String toString()
88 {
89 StringBuffer sb = new StringBuffer();
90 sb.append( "TestsToRun: [" );
91 Iterator it = iterator();
92 while ( it.hasNext() )
93 {
94 Class clazz = (Class) it.next();
95 sb.append( " " ).append( clazz.getName() );
96 }
97
98 sb.append( ']' );
99 return sb.toString();
100 }
101
102 }