File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\TestExtensionLoader.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package org.jvnet.hudson.test;
25
26 import hudson.Extension;
27 import hudson.ExtensionFinder.GuiceExtensionAnnotation;
28
29 import java.lang.reflect.AnnotatedElement;
30 import java.lang.reflect.Field;
31 import java.lang.reflect.Method;
32 import org.junit.runner.Description;
33
34 /**
35 * Loads {@link TestExtension}s.
36 *
37 * @author Kohsuke Kawaguchi
38 */
39 @Extension
40 public class TestExtensionLoader extends GuiceExtensionAnnotation<TestExtension> {
41 public TestExtensionLoader() {
42 super(TestExtension.class);
43 }
44
45 @Override
46 protected boolean isOptional(TestExtension annotation) {
47 return false;
48 }
49
50 @Override
51 protected double getOrdinal(TestExtension annotation) {
52 return 0;
53 }
54
55 @Override
56 protected boolean isActive(AnnotatedElement e) {
57 TestEnvironment env = TestEnvironment.get();
58 if (env == null) {
59 return false;
60 }
61
62 TestExtension a = e.getAnnotation(TestExtension.class);
63 if (a==null) return false; // stale index
64 String testName = a.value();
65 Description description = env.description();
66 if (testName.length()>0 && !testName.equals(description.getMethodName()))
67 return false; // doesn't apply to this test
68 String className = description.getClassName();
69 if (e instanceof Class) {
70 for (Class<?> outer = (Class) e; outer != null; outer = outer.getEnclosingClass()) {
71 if (outer.getName().equals(className)) {
72 return true; // enclosed
73 }
74 }
75 return false;
76 }
77 if (e instanceof Field) {
78 Field f = (Field) e;
79 return f.getDeclaringClass().getName().equals(className);
80 }
81 if (e instanceof Method) {
82 Method m = (Method) e;
83 return m.getDeclaringClass().getName().equals(className);
84 }
85 return false;
86 }
87
88 }
89