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.Collections;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * @author Kristian Rosenvold
31   */
32  public class DefaultScanResult
33      implements ScanResult
34  {
35      private final List<String> classes;
36  
37      private static final String SCAN_RESULT_NUMBER = "tc.";
38  
39      public DefaultScanResult( List<String> classes )
40      {
41          this.classes = Collections.unmodifiableList( classes );
42      }
43  
44      @Override
45      public int size()
46      {
47          return classes.size();
48      }
49  
50      @Override
51      public String getClassName( int index )
52      {
53          return classes.get( index );
54      }
55  
56      @Override
57      public void writeTo( Map<String, String> properties )
58      {
59          for ( int i = 0, size = classes.size(); i < size; i++ )
60          {
61              properties.put( SCAN_RESULT_NUMBER + i, classes.get( i ) );
62          }
63      }
64  
65      public static DefaultScanResult from( Map<String, String> properties )
66      {
67          List<String> result = new ArrayList<String>();
68          int i = 0;
69          while ( true )
70          {
71              String item = properties.get( SCAN_RESULT_NUMBER + ( i++ ) );
72              if ( item == null )
73              {
74                  return new DefaultScanResult( result );
75              }
76              result.add( item );
77          }
78      }
79  
80      public boolean isEmpty()
81      {
82          return classes.isEmpty();
83      }
84  
85      public List<String> getClasses()
86      {
87          return classes;
88      }
89  
90      @Override
91      public TestsToRun applyFilter( ScannerFilter scannerFilter, ClassLoader testClassLoader )
92      {
93          Set<Class<?>> result = new LinkedHashSet<Class<?>>();
94  
95          int size = size();
96          for ( int i = 0; i < size; i++ )
97          {
98              String className = getClassName( i );
99  
100             Class<?> testClass = loadClass( testClassLoader, className );
101 
102             if ( scannerFilter == null || scannerFilter.accept( testClass ) )
103             {
104                 result.add( testClass );
105             }
106         }
107 
108         return new TestsToRun( result );
109     }
110 
111     @Override
112     public List<Class<?>> getClassesSkippedByValidation( ScannerFilter scannerFilter, ClassLoader testClassLoader )
113     {
114         List<Class<?>> result = new ArrayList<Class<?>>();
115 
116         int size = size();
117         for ( int i = 0; i < size; i++ )
118         {
119             String className = getClassName( i );
120 
121             Class<?> testClass = loadClass( testClassLoader, className );
122 
123             if ( scannerFilter != null && !scannerFilter.accept( testClass ) )
124             {
125                 result.add( testClass );
126             }
127         }
128 
129         return result;
130     }
131 
132     private static Class<?> loadClass( ClassLoader classLoader, String className )
133     {
134         try
135         {
136             return classLoader.loadClass( className );
137         }
138         catch ( ClassNotFoundException e )
139         {
140             throw new RuntimeException( "Unable to create test class '" + className + "'", e );
141         }
142     }
143 
144     public DefaultScanResult append( DefaultScanResult other )
145     {
146         if ( other != null )
147         {
148             List<String> src = new ArrayList<String>( classes );
149             src.addAll( other.classes );
150             return new DefaultScanResult( src );
151         }
152         else
153         {
154             return this;
155         }
156     }
157 
158 }