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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.FilePath;
28 import hudson.Launcher;
29 import hudson.model.AbstractBuild;
30 import hudson.model.BuildListener;
31 import hudson.model.FreeStyleProject;
32 import hudson.model.Result;
33 import hudson.tasks.LogRotatorTest.TestsFail;
34 import java.io.File;
35 import static hudson.tasks.LogRotatorTest.build;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import org.jvnet.hudson.test.Bug;
40 import org.jvnet.hudson.test.FailureBuilder;
41 import org.jvnet.hudson.test.HudsonTestCase;
42 import org.jvnet.hudson.test.TestBuilder;
43
44 /**
45 * Verifies that artifacts from the last successful and stable builds of a job will be kept if requested.
46 */
47 public class ArtifactArchiverTest extends HudsonTestCase {
48
49 public void testSuccessVsFailure() throws Exception {
50 FreeStyleProject project = createFreeStyleProject();
51 project.getPublishersList().replaceBy(Collections.singleton(new ArtifactArchiver("f", "", true)));
52 assertEquals("(no artifacts)", Result.FAILURE, build(project)); // #1
53 assertFalse(project.getBuildByNumber(1).getHasArtifacts());
54 project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifact()));
55 assertEquals(Result.SUCCESS, build(project)); // #2
56 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
57 project.getBuildersList().replaceBy(Arrays.asList(new CreateArtifact(), new FailureBuilder()));
58 assertEquals(Result.FAILURE, build(project)); // #3
59 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
60 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
61 assertEquals(Result.FAILURE, build(project)); // #4
62 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
63 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
64 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
65 assertEquals(Result.FAILURE, build(project)); // #5
66 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
67 assertFalse("no better than #4", project.getBuildByNumber(3).getHasArtifacts());
68 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
69 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
70 project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifact()));
71 assertEquals(Result.SUCCESS, build(project)); // #6
72 assertTrue("#2 is still lastSuccessful until #6 is complete", project.getBuildByNumber(2).getHasArtifacts());
73 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
74 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
75 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
76 assertTrue(project.getBuildByNumber(6).getHasArtifacts());
77 assertEquals(Result.SUCCESS, build(project)); // #7
78 assertFalse("lastSuccessful was #6 for ArtifactArchiver", project.getBuildByNumber(2).getHasArtifacts());
79 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
80 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
81 assertFalse(project.getBuildByNumber(5).getHasArtifacts());
82 assertTrue(project.getBuildByNumber(6).getHasArtifacts());
83 assertTrue(project.getBuildByNumber(7).getHasArtifacts());
84 }
85
86 @Bug(2417)
87 public void testStableVsUnstable() throws Exception {
88 FreeStyleProject project = createFreeStyleProject();
89 Publisher artifactArchiver = new ArtifactArchiver("f", "", true);
90 project.getPublishersList().replaceBy(Collections.singleton(artifactArchiver));
91 project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifact()));
92 assertEquals(Result.SUCCESS, build(project)); // #1
93 assertTrue(project.getBuildByNumber(1).getHasArtifacts());
94 project.getPublishersList().replaceBy(Arrays.asList(artifactArchiver, new TestsFail()));
95 assertEquals(Result.UNSTABLE, build(project)); // #2
96 assertTrue(project.getBuildByNumber(1).getHasArtifacts());
97 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
98 assertEquals(Result.UNSTABLE, build(project)); // #3
99 assertTrue(project.getBuildByNumber(1).getHasArtifacts());
100 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
101 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
102 assertEquals(Result.UNSTABLE, build(project)); // #4
103 assertTrue(project.getBuildByNumber(1).getHasArtifacts());
104 assertFalse(project.getBuildByNumber(2).getHasArtifacts());
105 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
106 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
107 project.getPublishersList().replaceBy(Collections.singleton(artifactArchiver));
108 assertEquals(Result.SUCCESS, build(project)); // #5
109 assertTrue(project.getBuildByNumber(1).getHasArtifacts());
110 assertFalse(project.getBuildByNumber(2).getHasArtifacts());
111 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
112 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
113 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
114 assertEquals(Result.SUCCESS, build(project)); // #6
115 assertFalse(project.getBuildByNumber(1).getHasArtifacts());
116 assertFalse(project.getBuildByNumber(2).getHasArtifacts());
117 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
118 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
119 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
120 assertTrue(project.getBuildByNumber(6).getHasArtifacts());
121 }
122
123 @Bug(3227)
124 public void testEmptyDirectories() throws Exception {
125 FreeStyleProject project = createFreeStyleProject();
126 Publisher artifactArchiver = new ArtifactArchiver("dir/", "", false);
127 project.getPublishersList().replaceBy(Collections.singleton(artifactArchiver));
128 project.getBuildersList().replaceBy(Collections.singleton(new TestBuilder() {
129 public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
130 FilePath dir = build.getWorkspace().child("dir");
131 dir.child("subdir1").mkdirs();
132 FilePath subdir2 = dir.child("subdir2");
133 subdir2.mkdirs();
134 subdir2.child("file").write("content", "UTF-8");
135 return true;
136 }
137 }));
138 assertEquals(Result.SUCCESS, build(project)); // #1
139 File artifacts = project.getBuildByNumber(1).getArtifactsDir();
140 File[] kids = artifacts.listFiles();
141 assertEquals(1, kids.length);
142 assertEquals("dir", kids[0].getName());
143 kids = kids[0].listFiles();
144 assertEquals(1, kids.length);
145 assertEquals("subdir2", kids[0].getName());
146 kids = kids[0].listFiles();
147 assertEquals(1, kids.length);
148 assertEquals("file", kids[0].getName());
149 }
150
151 static class CreateArtifact extends TestBuilder {
152 public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
153 build.getWorkspace().child("f").write("content", "UTF-8");
154 return true;
155 }
156 }
157
158 }
159