View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public class DefaultScanResult implements ScanResult {
32      private final List<String> classes;
33  
34      private static final String SCAN_RESULT_NUMBER = "tc.";
35  
36      public DefaultScanResult(List<String> classes) {
37          this.classes = Collections.unmodifiableList(classes);
38      }
39  
40      @Override
41      public int size() {
42          return classes.size();
43      }
44  
45      @Override
46      public String getClassName(int index) {
47          return classes.get(index);
48      }
49  
50      @Override
51      public void writeTo(Map<String, String> properties) {
52          for (int i = 0, size = classes.size(); i < size; i++) {
53              properties.put(SCAN_RESULT_NUMBER + i, classes.get(i));
54          }
55      }
56  
57      public static DefaultScanResult from(Map<String, String> properties) {
58          List<String> result = new ArrayList<>();
59          int i = 0;
60          while (true) {
61              String item = properties.get(SCAN_RESULT_NUMBER + (i++));
62              if (item == null) {
63                  return new DefaultScanResult(result);
64              }
65              result.add(item);
66          }
67      }
68  
69      public boolean isEmpty() {
70          return classes.isEmpty();
71      }
72  
73      public List<String> getClasses() {
74          return classes;
75      }
76  
77      @Override
78      public TestsToRun applyFilter(ScannerFilter scannerFilter, ClassLoader testClassLoader) {
79          Set<Class<?>> result = new LinkedHashSet<>();
80  
81          int size = size();
82          for (int i = 0; i < size; i++) {
83              String className = getClassName(i);
84  
85              Class<?> testClass = loadClass(testClassLoader, className);
86  
87              if (scannerFilter == null || scannerFilter.accept(testClass)) {
88                  result.add(testClass);
89              }
90          }
91  
92          return new TestsToRun(result);
93      }
94  
95      @Override
96      public List<Class<?>> getClassesSkippedByValidation(ScannerFilter scannerFilter, ClassLoader testClassLoader) {
97          List<Class<?>> result = new ArrayList<>();
98  
99          int size = size();
100         for (int i = 0; i < size; i++) {
101             String className = getClassName(i);
102 
103             Class<?> testClass = loadClass(testClassLoader, className);
104 
105             if (scannerFilter != null && !scannerFilter.accept(testClass)) {
106                 result.add(testClass);
107             }
108         }
109 
110         return result;
111     }
112 
113     private static Class<?> loadClass(ClassLoader classLoader, String className) {
114         try {
115             return classLoader.loadClass(className);
116         } catch (ClassNotFoundException e) {
117             throw new RuntimeException("Unable to create test class '" + className + "'", e);
118         }
119     }
120 
121     public DefaultScanResult append(DefaultScanResult other) {
122         if (other != null) {
123             List<String> src = new ArrayList<>(classes);
124             src.addAll(other.classes);
125             return new DefaultScanResult(src);
126         } else {
127             return this;
128         }
129     }
130 }