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