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

1 /*
2 * The MIT License
3 *
4 * Copyright 2012 Jesse Glick.
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.triggers;
26
27 import hudson.model.AbstractProject;
28 import hudson.model.FreeStyleProject;
29 import hudson.model.Item;
30 import hudson.model.Items;
31 import java.io.ByteArrayInputStream;
32 import java.io.ObjectStreamException;
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.List;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.stream.StreamSource;
38 import static org.junit.Assert.*;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.jvnet.hudson.test.Bug;
42 import org.jvnet.hudson.test.JenkinsRule;
43 import org.jvnet.hudson.test.TestExtension;
44 import org.kohsuke.stapler.DataBoundConstructor;
45
46 @Bug(14759)
47 public class TriggerStartTest {
48
49 @Rule public JenkinsRule j = new JenkinsRule();
50
51 @Test public void loadCallsStartFalse() throws Exception {
52 FreeStyleProject p = j.createFreeStyleProject();
53 MockTrigger t = new MockTrigger();
54 p.addTrigger(t);
55 p.save();
56 p = (FreeStyleProject) Items.load(p.getParent(), p.getRootDir());
57 t = p.getTrigger(MockTrigger.class);
58 assertNotNull(t);
59 assertEquals("[false]", t.calls.toString());
60 }
61
62 @Test public void submitCallsStartTrue() throws Exception {
63 FreeStyleProject p = j.createFreeStyleProject();
64 MockTrigger t = new MockTrigger();
65 p.addTrigger(t);
66 p.save();
67 p = (FreeStyleProject)j.configRoundtrip((Item)p);
68 t = p.getTrigger(MockTrigger.class);
69 assertNotNull(t);
70 assertEquals("[true]", t.calls.toString());
71 }
72
73 @Test public void updateByXmlCallsStartTrue() throws Exception {
74 FreeStyleProject p = j.createFreeStyleProject();
75 String xml = p.getConfigFile().asString();
76 xml = xml.replace(" <triggers class=\"vector\"/>\n", triggersSection());
77 Source newXML = new StreamSource(new StringReader(xml));
78 p.updateByXml(newXML);
79 MockTrigger t = p.getTrigger(MockTrigger.class);
80 assertNotNull(t);
81 assertEquals("[true]", t.calls.toString());
82 }
83
84 @Test public void createProjectFromXmlCallsStartTrue() throws Exception {
85 FreeStyleProject p = (FreeStyleProject) j.jenkins.createProjectFromXML("whatever", new ByteArrayInputStream(("<project>\n <builders/>\n <publishers/>\n <buildWrappers/>\n" + triggersSection() + "</project>").getBytes()));
86 MockTrigger t = p.getTrigger(MockTrigger.class);
87 assertNotNull(t);
88 assertEquals("[true]", t.calls.toString());
89 }
90
91 @Test public void copyCallsStartTrue() throws Exception {
92 AbstractProject<?,?> p = j.createFreeStyleProject();
93 MockTrigger t = new MockTrigger();
94 p.addTrigger(t);
95 p.save();
96 p = j.jenkins.copy(p, "nue");
97 t = p.getTrigger(MockTrigger.class);
98 assertNotNull(t);
99 assertEquals("[true]", t.calls.toString());
100 }
101
102 private String triggersSection() {
103 String tagname = MockTrigger.class.getName().replace("$", "_-");
104 return " <triggers class=\"vector\">\n <" + tagname + ">\n <spec/>\n </" + tagname + ">\n </triggers>\n";
105 }
106
107 public static class MockTrigger extends Trigger<Item> {
108
109 public transient List<Boolean> calls = new ArrayList<Boolean>();
110
111 @DataBoundConstructor public MockTrigger() {}
112
113 @Override public void start(Item project, boolean newInstance) {
114 super.start(project, newInstance);
115 calls.add(newInstance);
116 }
117
118 @Override protected Object readResolve() throws ObjectStreamException {
119 calls = new ArrayList<Boolean>();
120 return super.readResolve();
121 }
122
123 @TestExtension
124 public static class DescriptorImpl extends TriggerDescriptor {
125
126 @Override public boolean isApplicable(Item item) {
127 return true;
128 }
129
130 @Override public String getDisplayName() {
131 return "mock trigger";
132 }
133
134 }
135
136 }
137
138 }
139