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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, 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 hudson;
25
26 import com.google.inject.AbstractModule;
27 import hudson.model.PageDecorator;
28 import org.jvnet.hudson.test.HudsonTestCase;
29 import org.jvnet.hudson.test.TestEnvironment;
30 import org.jvnet.hudson.test.TestExtension;
31
32 import javax.inject.Inject;
33 import javax.inject.Qualifier;
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36
37 /**
38 * @author Kohsuke Kawaguchi
39 */
40 public class ExtensionFinderTest extends HudsonTestCase {
41 /**
42 * It's OK for some extensions to fail to load. The system needs to tolerate that.
43 */
44 public void testFailingInstance() {
45 FailingExtension i = PageDecorator.all().get(FailingExtension.class);
46 assertNull("Instantiation should have failed",i);
47 assertTrue("Instantiation should have been attempted", FailingExtension.error);
48 }
49
50 @TestExtension("testFailingInstance")
51 public static class FailingExtension extends PageDecorator {
52 public FailingExtension() {
53 super(FailingExtension.class);
54 error = true;
55 throw new LinkageError(); // this component fails to load
56 }
57 public static boolean error;
58 }
59
60
61
62
63
64 /**
65 * Extensions are Guice components, so it should support injection.
66 */
67 public void testInjection() {
68 InjectingExtension i = PageDecorator.all().get(InjectingExtension.class);
69 assertNotNull(i.foo);
70 assertEquals("lion king",i.value);
71 }
72
73 @TestExtension("testInjection")
74 public static class InjectingExtension extends PageDecorator {
75 @Inject
76 Foo foo;
77
78 @Inject @LionKing
79 String value;
80
81
82 public InjectingExtension() {
83 super(InjectingExtension.class);
84 }
85 public static class Foo {}
86 }
87
88
89 @Retention(RetentionPolicy.RUNTIME) @Qualifier
90 public @interface LionKing {}
91
92 @Extension
93 public static class ModuleImpl extends AbstractModule {
94 protected void configure() {
95 if (TestEnvironment.get().testCase instanceof ExtensionFinderTest) {
96 bind(String.class).annotatedWith(LionKing.class).toInstance("lion king");
97 }
98 }
99 }
100
101
102 /**
103 * Tests the error recovery behaviour.
104 *
105 * One failure in binding definition shouldn't prevent Jenkins from booting.
106 */
107 public void testErrorRecovery() {
108 BrokenExtension i = PageDecorator.all().get(BrokenExtension.class);
109 assertNull(i);
110 }
111
112 @TestExtension("testErrorRecovery")
113 public static class BrokenExtension extends PageDecorator {
114 public BrokenExtension() {
115 super(InjectingExtension.class);
116
117 throw new Error();
118 }
119 }
120 }
121