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

1 /*
2 * The MIT License
3 *
4 * Copyright 2011 Yahoo!, Inc.
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.tasks;
26
27 import hudson.matrix.Axis;
28 import hudson.matrix.AxisList;
29 import hudson.matrix.MatrixProject;
30 import hudson.model.AbstractProject;
31 import hudson.model.Fingerprint;
32 import hudson.model.FreeStyleBuild;
33 import hudson.model.FreeStyleProject;
34 import hudson.model.Hudson;
35 import hudson.model.Result;
36 import hudson.util.RunList;
37
38 import java.io.IOException;
39 import java.util.Collection;
40 import java.util.List;
41
42 import org.jvnet.hudson.test.HudsonTestCase;
43
44 /**
45 *
46 * @author dty
47 */
48 @SuppressWarnings("rawtypes")
49 public class FingerprinterTest extends HudsonTestCase {
50 private static final String[] singleContents = {
51 "abcdef"
52 };
53 private static final String[] singleFiles = {
54 "test.txt"
55 };
56 private static final String[] singleContents2 = {
57 "ghijkl"
58 };
59 private static final String[] singleFiles2 = {
60 "test2.txt"
61 };
62 private static final String[] doubleContents = {
63 "abcdef",
64 "ghijkl"
65 };
66 private static final String[] doubleFiles = {
67 "test.txt",
68 "test2.txt"
69 };
70
71 private static final String renamedProject1 = "renamed project 1";
72 private static final String renamedProject2 = "renamed project 2";
73
74 @Override
75 public void setUp() throws Exception {
76 super.setUp();
77 Fingerprinter.enableFingerprintsInDependencyGraph = true;
78 }
79
80 public void testFingerprintDependencies() throws Exception {
81 FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
82 FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
83
84 assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
85 assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());
86
87 List<AbstractProject> downstreamProjects = upstream.getDownstreamProjects();
88 List<AbstractProject> upstreamProjects = downstream.getUpstreamProjects();
89
90 assertEquals(1, downstreamProjects.size());
91 assertEquals(1, upstreamProjects.size());
92 assertTrue(upstreamProjects.contains(upstream));
93 assertTrue(downstreamProjects.contains(downstream));
94 }
95
96 public void testMultipleUpstreamDependencies() throws Exception {
97 FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
98 FreeStyleProject upstream2 = createFreeStyleProjectWithFingerprints(singleContents2, singleFiles2);
99 FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(doubleContents, doubleFiles);
100
101 assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
102 assertBuildStatusSuccess(upstream2.scheduleBuild2(0).get());
103 assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());
104
105 List<AbstractProject> downstreamProjects = upstream.getDownstreamProjects();
106 List<AbstractProject> downstreamProjects2 = upstream2.getDownstreamProjects();
107 List<AbstractProject> upstreamProjects = downstream.getUpstreamProjects();
108
109 assertEquals(1, downstreamProjects.size());
110 assertEquals(1, downstreamProjects2.size());
111 assertEquals(2, upstreamProjects.size());
112 assertTrue(upstreamProjects.contains(upstream));
113 assertTrue(upstreamProjects.contains(upstream2));
114 assertTrue(downstreamProjects.contains(downstream));
115 }
116
117 public void testMultipleDownstreamDependencies() throws Exception {
118 FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(doubleContents, doubleFiles);
119 FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
120 FreeStyleProject downstream2 = createFreeStyleProjectWithFingerprints(singleContents2, singleFiles2);
121
122 assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
123 assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());
124 assertBuildStatusSuccess(downstream2.scheduleBuild2(0).get());
125
126 List<AbstractProject> downstreamProjects = upstream.getDownstreamProjects();
127 List<AbstractProject> upstreamProjects = downstream.getUpstreamProjects();
128 List<AbstractProject> upstreamProjects2 = downstream2.getUpstreamProjects();
129
130 assertEquals(2, downstreamProjects.size());
131 assertEquals(1, upstreamProjects.size());
132 assertEquals(1, upstreamProjects2.size());
133 assertTrue(upstreamProjects.contains(upstream));
134 assertTrue(upstreamProjects2.contains(upstream));
135 assertTrue(downstreamProjects.contains(downstream));
136 assertTrue(downstreamProjects.contains(downstream2));
137 }
138
139 public void testDependencyExclusion() throws Exception {
140 FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
141 FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
142
143 FreeStyleBuild upstreamBuild = assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
144 assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());
145
146 upstreamBuild.delete();
147
148 Hudson.getInstance().rebuildDependencyGraph();
149
150 List<AbstractProject> upstreamProjects = downstream.getUpstreamProjects();
151 List<AbstractProject> downstreamProjects = upstream.getDownstreamProjects();
152
153 assertEquals(0, upstreamProjects.size());
154 assertEquals(0, downstreamProjects.size());
155 }
156
157 public void testCircularDependency() throws Exception {
158 FreeStyleProject p = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
159
160 assertBuildStatusSuccess(p.scheduleBuild2(0).get());
161 assertBuildStatusSuccess(p.scheduleBuild2(0).get());
162
163 List<AbstractProject> upstreamProjects = p.getUpstreamProjects();
164 List<AbstractProject> downstreamProjects = p.getDownstreamProjects();
165
166 assertEquals(0, upstreamProjects.size());
167 assertEquals(0, downstreamProjects.size());
168 }
169
170 public void testMatrixDependency() throws Exception {
171 MatrixProject matrixProject = createMatrixProject();
172 matrixProject.setAxes(new AxisList(new Axis("foo", "a", "b")));
173 FreeStyleProject freestyleProject = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
174 addFingerprinterToProject(matrixProject, singleContents, singleFiles);
175
176 jenkins.rebuildDependencyGraph();
177
178 buildAndAssertSuccess(matrixProject);
179 buildAndAssertSuccess(freestyleProject);
180 waitUntilNoActivity();
181
182 RunList<FreeStyleBuild> builds = freestyleProject.getBuilds();
183 assertEquals("There should only be one FreestyleBuild", 1, builds.size());
184 FreeStyleBuild build = builds.iterator().next();
185 assertEquals(Result.SUCCESS, build.getResult());
186 List<AbstractProject> downstream = jenkins.getDependencyGraph().getDownstream(matrixProject);
187 assertTrue(downstream.contains(freestyleProject));
188 List<AbstractProject> upstream = jenkins.getDependencyGraph().getUpstream(freestyleProject);
189 assertTrue(upstream.contains(matrixProject));
190 }
191
192 public void testProjectRename() throws Exception {
193 FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
194 FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
195
196 FreeStyleBuild upstreamBuild = assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
197 FreeStyleBuild downstreamBuild = assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());
198
199 String oldUpstreamName = upstream.getName();
200 String oldDownstreamName = downstream.getName();
201
202 // Verify that owner entry in fingerprint record is changed
203 // after source project is renamed
204 upstream.renameTo(renamedProject1);
205 Fingerprinter.FingerprintAction action = upstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
206 assertNotNull(action);
207 Collection<Fingerprint> fingerprints = action.getFingerprints().values();
208 for (Fingerprint f: fingerprints) {
209 assertTrue(f.getOriginal().is(upstream));
210 assertTrue(f.getOriginal().getName().equals(renamedProject1));
211 assertFalse(f.getOriginal().getName().equals(oldUpstreamName));
212 }
213
214 action = downstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
215 assertNotNull(action);
216 fingerprints = action.getFingerprints().values();
217 for (Fingerprint f: fingerprints) {
218 assertTrue(f.getOriginal().is(upstream));
219 assertTrue(f.getOriginal().getName().equals(renamedProject1));
220 assertFalse(f.getOriginal().getName().equals(oldUpstreamName));
221 }
222
223 // Verify that usage entry in fingerprint record is changed after
224 // sink project is renamed
225 downstream.renameTo(renamedProject2);
226 upstream.renameTo(renamedProject1);
227 action = upstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
228 assertNotNull(action);
229 fingerprints = action.getFingerprints().values();
230 for (Fingerprint f: fingerprints) {
231 List<String> jobs = f.getJobs();
232
233 assertTrue(jobs.contains(renamedProject2));
234 assertFalse(jobs.contains(oldDownstreamName));
235 }
236
237 action = downstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
238 assertNotNull(action);
239 fingerprints = action.getFingerprints().values();
240 for (Fingerprint f: fingerprints) {
241 List<String> jobs = f.getJobs();
242
243 assertTrue(jobs.contains(renamedProject2));
244 assertFalse(jobs.contains(oldDownstreamName));
245 }
246 }
247
248 private FreeStyleProject createFreeStyleProjectWithFingerprints(String[] contents, String[] files) throws IOException, Exception {
249 FreeStyleProject project = createFreeStyleProject();
250
251 addFingerprinterToProject(project, contents, files);
252
253 return project;
254 }
255
256 private void addFingerprinterToProject(AbstractProject<?, ?> project, String[] contents, String[] files) throws Exception {
257 StringBuilder targets = new StringBuilder();
258 for (int i = 0; i < contents.length; i++) {
259 if (project instanceof MatrixProject) {
260 ((MatrixProject)project).getBuildersList().add(new Shell("echo " + contents[i] + " > " + files[i]));
261 } else {
262 ((FreeStyleProject)project).getBuildersList().add(new Shell("echo " + contents[i] + " > " + files[i]));
263 }
264
265 targets.append(files[i]).append(',');
266 }
267
268 project.getPublishersList().add(new Fingerprinter(targets.toString(), false));
269 }
270 }
271