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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, 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.model;
25
26 import jenkins.model.DependencyDeclarer;
27 import hudson.security.ACL;
28 import hudson.tasks.BuildTrigger;
29 import hudson.tasks.MailMessageIdAction;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33
34 import org.acegisecurity.context.SecurityContextHolder;
35 import org.jvnet.hudson.test.HudsonTestCase;
36 import org.jvnet.hudson.test.Bug;
37 import org.jvnet.hudson.test.MockBuilder;
38 import org.jvnet.hudson.test.recipes.LocalData;
39
40 /**
41 * @author Alan.Harder@sun.com
42 */
43 public class DependencyGraphTest extends HudsonTestCase {
44
45 /**
46 * Tests triggering downstream projects with DependencyGraph.Dependency
47 */
48 public void testTriggerJob() throws Exception {
49 setQuietPeriod(3);
50 Project p = createFreeStyleProject(),
51 down1 = createFreeStyleProject(), down2 = createFreeStyleProject();
52 // Add one standard downstream job:
53 p.getPublishersList().add(
54 new BuildTrigger(Collections.singletonList(down1), Result.SUCCESS));
55 // Add one downstream job with custom Dependency impl:
56 p.getBuildersList().add(new TestDeclarer(Result.UNSTABLE, down2));
57 jenkins.rebuildDependencyGraph();
58 // First build won't trigger down1 (Unstable doesn't meet threshold)
59 // but will trigger down2 (build #1 is odd).
60 Build b = (Build)p.scheduleBuild2(0, new Cause.UserCause()).get();
61 String log = getLog(b);
62 Queue.Item q = jenkins.getQueue().getItem(down1);
63 assertNull("down1 should not be triggered: " + log, q);
64 assertNull("down1 should not be triggered: " + log, down1.getLastBuild());
65 q = jenkins.getQueue().getItem(down2);
66 assertNotNull("down2 should be in queue (quiet period): " + log, q);
67 Run r = (Run)q.getFuture().get(6, TimeUnit.SECONDS);
68 assertNotNull("down2 should be triggered: " + log, r);
69 assertNotNull("down2 should have MailMessageIdAction",
70 r.getAction(MailMessageIdAction.class));
71 // Now change to success result..
72 p.getBuildersList().replace(new TestDeclarer(Result.SUCCESS, down2));
73 jenkins.rebuildDependencyGraph();
74 // ..and next build will trigger down1 (Success meets threshold),
75 // but not down2 (build #2 is even)
76 b = (Build)p.scheduleBuild2(0, new Cause.UserCause()).get();
77 log = getLog(b);
78 q = jenkins.getQueue().getItem(down2);
79 assertNull("down2 should not be triggered: " + log, q);
80 assertEquals("down2 should not be triggered: " + log, 1,
81 down2.getLastBuild().getNumber());
82 q = jenkins.getQueue().getItem(down1);
83 assertNotNull("down1 should be in queue (quiet period): " + log, q);
84 r = (Run)q.getFuture().get(6, TimeUnit.SECONDS);
85 assertNotNull("down1 should be triggered", r);
86 }
87
88 private static class TestDeclarer extends MockBuilder implements DependencyDeclarer {
89 private AbstractProject down;
90 private TestDeclarer(Result buildResult, AbstractProject down) {
91 super(buildResult);
92 this.down = down;
93 }
94 public void buildDependencyGraph(AbstractProject owner, DependencyGraph graph) {
95 graph.addDependency(new DependencyGraph.Dependency(owner, down) {
96 @Override
97 public boolean shouldTriggerBuild(AbstractBuild build, TaskListener listener,
98 List<Action> actions) {
99 // Trigger for ODD build number
100 if (build.getNumber() % 2 == 1) {
101 actions.add(new MailMessageIdAction("foo"));
102 return true;
103 }
104 return false;
105 }
106 });
107 }
108 }
109
110 /**
111 * Tests that all dependencies are found even when some projects have restricted visibility.
112 */
113 @LocalData @Bug(5265)
114 public void testItemReadPermission() throws Exception {
115 // Rebuild dependency graph as anonymous user:
116 jenkins.rebuildDependencyGraph();
117 try {
118 // Switch to full access to check results:
119 ACL.impersonate(ACL.SYSTEM);
120 // @LocalData for this test has jobs w/o anonymous Item.READ
121 AbstractProject up = (AbstractProject) jenkins.getItem("hiddenUpstream");
122 assertNotNull("hiddenUpstream project not found", up);
123 List<AbstractProject> down = jenkins.getDependencyGraph().getDownstream(up);
124 assertEquals("Should have one downstream project", 1, down.size());
125 } finally {
126 SecurityContextHolder.clearContext();
127 }
128 }
129
130 }
131