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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.recipes;
25
26 import org.jvnet.hudson.test.HudsonTestCase;
27 import org.jvnet.hudson.test.JenkinsRecipe;
28 import org.jvnet.hudson.test.JenkinsRule;
29 import org.apache.commons.io.FileUtils;
30
31 import hudson.util.JenkinsReloadFailed;
32
33 import java.io.File;
34 import java.lang.annotation.Documented;
35 import static java.lang.annotation.ElementType.METHOD;
36 import java.lang.annotation.Retention;
37 import static java.lang.annotation.RetentionPolicy.RUNTIME;
38 import java.lang.annotation.Target;
39 import java.net.URL;
40
41 /**
42 * Installs the specified plugin before launching Hudson.
43 *
44 * @author Kohsuke Kawaguchi
45 */
46 @Documented
47 @Recipe(WithPlugin.RunnerImpl.class)
48 @JenkinsRecipe(WithPlugin.RuleRunnerImpl.class)
49 @Target(METHOD)
50 @Retention(RUNTIME)
51 public @interface WithPlugin {
52 /**
53 * Name of the plugin.
54 *
55 * For now, this has to be one of the plugins statically available in resources
56 * "/plugins/NAME". TODO: support retrieval through Maven repository.
57 * TODO: load the HPI file from $M2_REPO or $USER_HOME/.m2 by naming e.g. org.jvnet.hudson.plugins:monitoring:hpi:1.34.0
58 * (used in conjunction with the depepdency in POM to ensure it's available)
59 */
60 String value();
61
62 class RunnerImpl extends Recipe.Runner<WithPlugin> {
63 private WithPlugin a;
64
65 @Override
66 public void setup(HudsonTestCase testCase, WithPlugin recipe) throws Exception {
67 a = recipe;
68 testCase.useLocalPluginManager = true;
69 }
70
71 @Override
72 public void decorateHome(HudsonTestCase testCase, File home) throws Exception {
73 URL res = getClass().getClassLoader().getResource("plugins/" + a.value());
74 FileUtils.copyURLToFile(res,new File(home,"plugins/"+a.value()));
75 }
76 }
77
78 class RuleRunnerImpl extends JenkinsRecipe.Runner<WithPlugin> {
79 private WithPlugin a;
80
81 @Override
82 public void setup(JenkinsRule jenkinsRule, WithPlugin recipe) throws Exception {
83 a = recipe;
84 jenkinsRule.useLocalPluginManager = true;
85 }
86
87 @Override
88 public void decorateHome(JenkinsRule jenkinsRule, File home) throws Exception {
89 URL res = getClass().getClassLoader().getResource("plugins/" + a.value());
90 FileUtils.copyURLToFile(res,new File(home,"plugins/"+a.value()));
91 }
92 }
93 }
94