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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Tom Huybrechts
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
25 package hudson.tools;
26
27 import hudson.Functions;
28 import hudson.model.*;
29 import hudson.model.labels.LabelAtom;
30 import hudson.slaves.DumbSlave;
31 import hudson.tasks.Maven;
32 import hudson.tasks.BatchFile;
33 import hudson.tasks.Ant;
34 import hudson.tasks.Shell;
35 import hudson.tasks.Ant.AntInstallation;
36 import hudson.tasks.Maven.MavenInstallation;
37 import hudson.EnvVars;
38 import hudson.maven.MavenModuleSet;
39
40 import java.io.IOException;
41
42 import jenkins.model.Jenkins;
43 import junit.framework.Assert;
44
45 import org.jvnet.hudson.test.HudsonTestCase;
46 import org.jvnet.hudson.test.SingleFileSCM;
47 import org.jvnet.hudson.test.ExtractResourceSCM;
48
49 import com.gargoylesoftware.htmlunit.html.HtmlForm;
50 import com.gargoylesoftware.htmlunit.html.HtmlPage;
51
52 /**
53 * This class tests that environment variables from node properties are applied,
54 * and that the priority is maintained: parameters > slave node properties >
55 * master node properties
56 */
57 public class ToolLocationNodePropertyTest extends HudsonTestCase {
58
59 private DumbSlave slave;
60 private FreeStyleProject project;
61
62 public void testFormRoundTrip() throws Exception {
63
64 MavenInstallation.DescriptorImpl mavenDescriptor = jenkins.getDescriptorByType(MavenInstallation.DescriptorImpl.class);
65 mavenDescriptor.setInstallations(new MavenInstallation("maven", "XXX", NO_PROPERTIES));
66 AntInstallation.DescriptorImpl antDescriptor = jenkins.getDescriptorByType(AntInstallation.DescriptorImpl.class);
67 antDescriptor.setInstallations(new AntInstallation("ant", "XXX", NO_PROPERTIES));
68 JDK.DescriptorImpl jdkDescriptor = jenkins.getDescriptorByType(JDK.DescriptorImpl.class);
69 jdkDescriptor.setInstallations(new JDK("jdk", "XXX"));
70
71 ToolLocationNodeProperty property = new ToolLocationNodeProperty(
72 new ToolLocationNodeProperty.ToolLocation(jdkDescriptor, "jdk", "foobar"),
73 new ToolLocationNodeProperty.ToolLocation(mavenDescriptor, "maven", "barzot"),
74 new ToolLocationNodeProperty.ToolLocation(antDescriptor, "ant", "zotfoo"));
75 slave.getNodeProperties().add(property);
76
77 WebClient webClient = new WebClient();
78 HtmlPage page = webClient.getPage(slave, "configure");
79 HtmlForm form = page.getFormByName("config");
80 submit(form);
81
82 Assert.assertEquals(1, slave.getNodeProperties().toList().size());
83
84 ToolLocationNodeProperty prop = slave.getNodeProperties().get(ToolLocationNodeProperty.class);
85 Assert.assertEquals(3, prop.getLocations().size());
86
87 ToolLocationNodeProperty.ToolLocation location = prop.getLocations().get(0);
88 Assert.assertEquals(jdkDescriptor, location.getType());
89 Assert.assertEquals("jdk", location.getName());
90 Assert.assertEquals("foobar", location.getHome());
91
92 location = prop.getLocations().get(1);
93 Assert.assertEquals(mavenDescriptor, location.getType());
94 Assert.assertEquals("maven", location.getName());
95 Assert.assertEquals("barzot", location.getHome());
96
97 location = prop.getLocations().get(2);
98 Assert.assertEquals(antDescriptor, location.getType());
99 Assert.assertEquals("ant", location.getName());
100 Assert.assertEquals("zotfoo", location.getHome());
101 }
102
103 public void testMaven() throws Exception {
104 MavenInstallation maven = configureDefaultMaven();
105 String mavenPath = maven.getHome();
106 Jenkins.getInstance().getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(new MavenInstallation("maven", "THIS IS WRONG", NO_PROPERTIES));
107
108 project.getBuildersList().add(new Maven("--version", "maven"));
109 configureDumpEnvBuilder();
110
111 Build build = project.scheduleBuild2(0).get();
112 assertBuildStatus(Result.FAILURE, build);
113
114 ToolLocationNodeProperty property = new ToolLocationNodeProperty(
115 new ToolLocationNodeProperty.ToolLocation(jenkins.getDescriptorByType(MavenInstallation.DescriptorImpl.class), "maven", mavenPath));
116 slave.getNodeProperties().add(property);
117
118 build = project.scheduleBuild2(0).get();
119 assertBuildStatus(Result.SUCCESS, build);
120 }
121
122 private void configureDumpEnvBuilder() throws IOException {
123 if(Functions.isWindows())
124 project.getBuildersList().add(new BatchFile("set"));
125 else
126 project.getBuildersList().add(new Shell("export"));
127 }
128
129 public void testAnt() throws Exception {
130 Ant.AntInstallation ant = configureDefaultAnt();
131 String antPath = ant.getHome();
132 Jenkins.getInstance().getDescriptorByType(Ant.DescriptorImpl.class).setInstallations(new AntInstallation("ant", "THIS IS WRONG"));
133
134 project.setScm(new SingleFileSCM("build.xml", "<project name='foo'/>"));
135 project.getBuildersList().add(new Ant("-version", "ant", null,null,null));
136 configureDumpEnvBuilder();
137
138 Build build = project.scheduleBuild2(0).get();
139 assertBuildStatus(Result.FAILURE, build);
140
141 ToolLocationNodeProperty property = new ToolLocationNodeProperty(
142 new ToolLocationNodeProperty.ToolLocation(jenkins.getDescriptorByType(AntInstallation.DescriptorImpl.class), "ant", antPath));
143 slave.getNodeProperties().add(property);
144
145 build = project.scheduleBuild2(0).get();
146 System.out.println(build.getLog());
147 assertBuildStatus(Result.SUCCESS, build);
148 }
149
150 public void testNativeMaven() throws Exception {
151 MavenInstallation maven = configureDefaultMaven();
152 String mavenPath = maven.getHome();
153 Jenkins.getInstance().getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(new MavenInstallation("maven", "THIS IS WRONG", NO_PROPERTIES));
154
155 MavenModuleSet project = createMavenProject();
156 project.setScm(new ExtractResourceSCM(getClass().getResource(
157 "/simple-projects.zip")));
158 project.setAssignedLabel(slave.getSelfLabel());
159 project.setJDK(jenkins.getJDK("default"));
160
161 project.setMaven("maven");
162 project.setGoals("clean");
163
164 Run build = project.scheduleBuild2(0).get();
165 assertBuildStatus(Result.FAILURE, build);
166
167 ToolLocationNodeProperty property = new ToolLocationNodeProperty(
168 new ToolLocationNodeProperty.ToolLocation(jenkins.getDescriptorByType(MavenInstallation.DescriptorImpl.class), "maven", mavenPath));
169 slave.getNodeProperties().add(property);
170
171 build = project.scheduleBuild2(0).get();
172 System.out.println(build.getLog());
173 assertBuildStatus(Result.SUCCESS, build);
174
175 }
176
177 // //////////////////////// setup //////////////////////////////////////////
178
179 public void setUp() throws Exception {
180 super.setUp();
181 EnvVars env = new EnvVars();
182 // we don't want Maven, Ant, etc. to be discovered in the path for this test to work,
183 // but on Unix these tools rely on other basic Unix tools (like env) for its operation,
184 // so empty path breaks the test.
185 env.put("PATH", "/bin:/usr/bin");
186 env.put("M2_HOME", "empty");
187 slave = createSlave(new LabelAtom("slave"), env);
188 project = createFreeStyleProject();
189 project.setAssignedLabel(slave.getSelfLabel());
190 }
191 }
192