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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Alan Harder
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.tasks;
25
26 import com.gargoylesoftware.htmlunit.html.HtmlForm;
27 import com.gargoylesoftware.htmlunit.html.HtmlPage;
28 import hudson.maven.MavenModuleSet;
29 import hudson.maven.MavenModuleSetBuild;
30 import hudson.model.FreeStyleBuild;
31 import hudson.model.FreeStyleProject;
32 import hudson.model.Result;
33 import hudson.model.Run;
34 import org.jvnet.hudson.test.ExtractResourceSCM;
35 import org.jvnet.hudson.test.HudsonTestCase;
36 import org.jvnet.hudson.test.MockBuilder;
37
38 /**
39 * Tests for hudson.tasks.BuildTrigger
40 * @author Alan.Harder@sun.com
41 */
42 public class BuildTriggerTest extends HudsonTestCase {
43
44 private FreeStyleProject createDownstreamProject() throws Exception {
45 FreeStyleProject dp = createFreeStyleProject("downstream");
46
47 // Hm, no setQuietPeriod, have to submit form..
48 WebClient webClient = new WebClient();
49 HtmlPage page = webClient.getPage(dp,"configure");
50 HtmlForm form = page.getFormByName("config");
51 form.getInputByName("hasCustomQuietPeriod").click();
52 form.getInputByName("quiet_period").setValueAttribute("0");
53 submit(form);
54 assertEquals("set quiet period", 0, dp.getQuietPeriod());
55
56 return dp;
57 }
58
59 private void doTriggerTest(boolean evenWhenUnstable, Result triggerResult,
60 Result dontTriggerResult) throws Exception {
61 FreeStyleProject p = createFreeStyleProject(),
62 dp = createDownstreamProject();
63 p.getPublishersList().add(new BuildTrigger("downstream", evenWhenUnstable));
64 p.getBuildersList().add(new MockBuilder(dontTriggerResult));
65 jenkins.rebuildDependencyGraph();
66
67 // First build should not trigger downstream job
68 FreeStyleBuild b = p.scheduleBuild2(0).get();
69 assertNoDownstreamBuild(dp, b);
70
71 // Next build should trigger downstream job
72 p.getBuildersList().replace(new MockBuilder(triggerResult));
73 b = p.scheduleBuild2(0).get();
74 assertDownstreamBuild(dp, b);
75 }
76
77 private void assertNoDownstreamBuild(FreeStyleProject dp, Run<?,?> b) throws Exception {
78 for (int i = 0; i < 3; i++) {
79 Thread.sleep(200);
80 assertTrue("downstream build should not run! upstream log: " + getLog(b),
81 !dp.isInQueue() && !dp.isBuilding() && dp.getLastBuild()==null);
82 }
83 }
84
85 private void assertDownstreamBuild(FreeStyleProject dp, Run<?,?> b) throws Exception {
86 // Wait for downstream build
87 for (int i = 0; dp.getLastBuild()==null && i < 20; i++) Thread.sleep(100);
88 assertNotNull("downstream build didn't run.. upstream log: " + getLog(b), dp.getLastBuild());
89 }
90
91 public void testBuildTrigger() throws Exception {
92 doTriggerTest(false, Result.SUCCESS, Result.UNSTABLE);
93 }
94
95 public void testTriggerEvenWhenUnstable() throws Exception {
96 doTriggerTest(true, Result.UNSTABLE, Result.FAILURE);
97 }
98
99 private void doMavenTriggerTest(boolean evenWhenUnstable) throws Exception {
100 FreeStyleProject dp = createDownstreamProject();
101 configureDefaultMaven();
102 MavenModuleSet m = createMavenProject();
103 m.getPublishersList().add(new BuildTrigger("downstream", evenWhenUnstable));
104 if (!evenWhenUnstable) {
105 // Configure for UNSTABLE
106 m.setGoals("clean test");
107 m.setScm(new ExtractResourceSCM(getClass().getResource("maven-test-failure.zip")));
108 } // otherwise do nothing which gets FAILURE
109 // First build should not trigger downstream project
110 MavenModuleSetBuild b = m.scheduleBuild2(0).get();
111 assertNoDownstreamBuild(dp, b);
112
113 if (evenWhenUnstable) {
114 // Configure for UNSTABLE
115 m.setGoals("clean test");
116 m.setScm(new ExtractResourceSCM(getClass().getResource("maven-test-failure.zip")));
117 } else {
118 // Configure for SUCCESS
119 m.setGoals("clean");
120 m.setScm(new ExtractResourceSCM(getClass().getResource("maven-empty.zip")));
121 }
122 // Next build should trigger downstream project
123 b = m.scheduleBuild2(0).get();
124 assertDownstreamBuild(dp, b);
125 }
126
127 public void testMavenBuildTrigger() throws Exception {
128 doMavenTriggerTest(false);
129 }
130
131 public void testMavenTriggerEvenWhenUnstable() throws Exception {
132 doMavenTriggerTest(true);
133 }
134 }
135