-
Notifications
You must be signed in to change notification settings - Fork 584
/
PowerMockIgnorePackagesExtractorImpl.java
118 lines (91 loc) · 4.44 KB
/
PowerMockIgnorePackagesExtractorImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powermock.tests.utils.impl;
import org.powermock.configuration.GlobalConfiguration;
import org.powermock.configuration.PowerMockConfiguration;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.tests.utils.IgnorePackagesExtractor;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class PowerMockIgnorePackagesExtractorImpl implements IgnorePackagesExtractor {
@Override
public String[] getPackagesToIgnore(AnnotatedElement element) {
PowerMockIgnore annotation = element.getAnnotation(PowerMockIgnore.class);
boolean useGlobal = true;
if (annotation != null){
useGlobal = annotation.globalIgnore();
}
Set<String> ignoredPackages = new HashSet<String>();
useGlobal &= extractPackageToIgnore(element, ignoredPackages);
final String[] packageToIgnore = ignoredPackages.toArray(new String[ignoredPackages.size()]);
if (useGlobal) {
return getPackageToIgnoreWithGlobal(packageToIgnore);
} else {
return packageToIgnore;
}
}
private String[] getPackageToIgnoreWithGlobal(final String[] packageToIgnore) {
String[] globalIgnore = getGlobalIgnore();
final String[] allPackageToIgnore;
if (globalIgnore != null) {
allPackageToIgnore = addGlobalIgnore(packageToIgnore, globalIgnore);
} else {
allPackageToIgnore = packageToIgnore;
}
return allPackageToIgnore;
}
private String[] getGlobalIgnore() {
final PowerMockConfiguration powerMockConfiguration = GlobalConfiguration.powerMockConfiguration();
return powerMockConfiguration.getGlobalIgnore();
}
private boolean extractPackageToIgnore(final AnnotatedElement element, final Set<String> ignoredPackages) {
boolean useGlobalFromAnnotation = addValueFromAnnotation(element, ignoredPackages);
boolean useGlobalFromSuperclass = addValuesFromSuperclass((Class<?>) element, ignoredPackages);
return useGlobalFromAnnotation & useGlobalFromSuperclass;
}
private boolean addValuesFromSuperclass(final Class<?> element, final Set<String> ignoredPackages) {
final Collection<Class<?>> superclasses = new ArrayList<Class<?>>();
Collections.addAll(superclasses, element.getSuperclass());
Collections.addAll(superclasses, element.getInterfaces());
boolean useGlobalIgnore = true;
for (Class<?> superclass : superclasses) {
if (superclass != null && !superclass.equals(Object.class)) {
useGlobalIgnore &= extractPackageToIgnore(superclass, ignoredPackages);
}
}
return useGlobalIgnore;
}
private boolean addValueFromAnnotation(final AnnotatedElement element, final Set<String> ignoredPackages) {
PowerMockIgnore annotation = element.getAnnotation(PowerMockIgnore.class);
if (annotation != null) {
String[] ignores = annotation.value();
Collections.addAll(ignoredPackages, ignores);
return annotation.globalIgnore();
}
return true;
}
private String[] addGlobalIgnore(final String[] packageToIgnore, final String[] globalIgnore) {
final String[] allPackageToIgnore;
allPackageToIgnore = new String[globalIgnore.length + packageToIgnore.length];
System.arraycopy(globalIgnore, 0, allPackageToIgnore, 0, globalIgnore.length);
System.arraycopy(packageToIgnore, 0, allPackageToIgnore, globalIgnore.length, packageToIgnore.length);
return allPackageToIgnore;
}
}