Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart9.png 98% of files have more coverage
52   166   21   5,78
20   118   0,4   3
9     2,33  
3    
 
  FilteringRule       Line # 33 2 0% 2 0 100% 1.0
  FilteringClassLoader       Line # 47 39 0% 16 10 84,4% 0.84375
  FilteringStatement       Line # 135 11 0% 3 1 92,3% 0.9230769
 
  (2)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.tests2;
18   
19    import java.io.ByteArrayOutputStream;
20    import java.io.IOException;
21    import java.io.InputStream;
22    import java.lang.reflect.InvocationTargetException;
23    import java.lang.reflect.Method;
24    import java.util.Arrays;
25    import java.util.Collection;
26    import java.util.HashMap;
27    import java.util.Map;
28   
29    import org.junit.rules.MethodRule;
30    import org.junit.runners.model.FrameworkMethod;
31    import org.junit.runners.model.Statement;
32   
 
33    public class FilteringRule implements MethodRule {
34   
35    private final String[] filteredPackages;
36   
 
37  8 toggle public FilteringRule(final String... filteredPackages) {
38  8 this.filteredPackages = filteredPackages;
39    }
40   
 
41  4 toggle public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
42  4 return new FilteringStatement(base, method, target, filteredPackages);
43    }
44   
45    }
46   
 
47    class FilteringClassLoader extends ClassLoader {
48   
49    private static final String[] packagesToBeDeferred = new String[] { "org.hamcrest.", "java.", "sun.",
50    "org.junit." };
51   
52    private final Collection<String> ignoredPackages;
53   
54    private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
55   
 
56  4 toggle public FilteringClassLoader(final Collection<String> ignoredPackages) {
57  4 this.ignoredPackages = ignoredPackages;
58    }
59   
 
60  946 toggle @Override
61    protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
62  946 if (isIgnored(name)) {
63  2 throw new ClassNotFoundException(name);
64    }
65  944 Class<?> clazz = classes.get(name);
66  944 if (clazz != null) {
67  0 return clazz;
68    }
69   
70  944 if (shouldBeDeferred(name)) {
71  422 return super.loadClass(name, resolve);
72    }
73   
74  522 try {
75  522 clazz = loadClass0(name);
76    } catch (final IOException e) {
77  0 throw new ClassNotFoundException("Can't load " + name, e);
78    }
79   
80  522 if (resolve) {
81  0 resolveClass(clazz);
82    }
83   
84  522 classes.put(name, clazz);
85  522 return clazz;
86    }
87   
 
88  944 toggle private boolean shouldBeDeferred(final String name) {
89  944 for (final String pack : packagesToBeDeferred) {
90  2956 if (name.startsWith(pack)) {
91  422 return true;
92    }
93    }
94  522 return false;
95    }
96   
 
97  522 toggle private Class<?> loadClass0(final String name) throws IOException {
98  522 final String path = name.replace('.', '/') + ".class";
99  522 InputStream in = null;
100  522 ByteArrayOutputStream out = null;
101   
102  522 try {
103  522 in = getResourceAsStream(path);
104  522 out = new ByteArrayOutputStream();
105   
106  522 int one;
107  ? while ((one = in.read()) != -1) {
108  1614304 out.write((byte) one);
109    }
110   
111  522 out.flush();
112   
113  522 final byte bytes[] = out.toByteArray();
114  522 return bytes == null ? null : defineClass(name, bytes, 0, bytes.length);
115    } finally {
116  522 if (in != null) {
117  522 in.close();
118    }
119  522 if (out != null) {
120  522 out.close();
121    }
122    }
123    }
124   
 
125  946 toggle private boolean isIgnored(final String name) {
126  946 for (final String s : ignoredPackages) {
127  1890 if (name.startsWith(s)) {
128  2 return true;
129    }
130    }
131  944 return false;
132    }
133    }
134   
 
135    class FilteringStatement extends Statement {
136   
137    private final Statement innerStatement;
138   
139    private final FrameworkMethod method; // The tested method
140   
141    private final Object target; // The test class
142   
143    private final String[] filteredPackages;
144   
 
145  4 toggle public FilteringStatement(final Statement base, final FrameworkMethod method, final Object target,
146    final String[] filteredPackages) {
147  4 this.innerStatement = base;
148  4 this.method = method;
149  4 this.target = target;
150  4 this.filteredPackages = filteredPackages;
151    }
152   
 
153  4 toggle @Override
154    public void evaluate() throws Throwable {
155  4 final FilteringClassLoader cl = new FilteringClassLoader(Arrays.asList(filteredPackages));
156  4 final Class<?> c = cl.loadClass(target.getClass().getName());
157  4 final Object test = c.newInstance();
158  4 final Method m = c.getMethod(method.getName());
159  4 try {
160  4 m.invoke(test);
161    } catch (final InvocationTargetException e) {
162  0 throw e.getTargetException();
163    }
164    }
165   
166    }