File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\tasks\LogRotatorTest.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.Launcher;
28 import hudson.model.AbstractBuild;
29 import hudson.model.BuildListener;
30 import hudson.model.Descriptor;
31 import hudson.model.FreeStyleProject;
32 import hudson.model.Result;
33 import hudson.model.Run;
34 import hudson.tasks.ArtifactArchiverTest.CreateArtifact;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.concurrent.TimeUnit;
38 import org.jvnet.hudson.test.Bug;
39 import org.jvnet.hudson.test.FailureBuilder;
40 import org.jvnet.hudson.test.HudsonTestCase;
41
42 /**
43 * Verifies that the last successful and stable builds of a job will be kept if requested.
44 */
45 public class LogRotatorTest extends HudsonTestCase {
46
47 public void testSuccessVsFailure() throws Exception {
48 FreeStyleProject project = createFreeStyleProject();
49 project.setLogRotator(new LogRotator(-1, 2, -1, -1));
50 assertEquals(Result.SUCCESS, build(project)); // #1
51 project.getBuildersList().replaceBy(Collections.singleton(new FailureBuilder()));
52 assertEquals(Result.FAILURE, build(project)); // #2
53 assertEquals(Result.FAILURE, build(project)); // #3
54 assertEquals(1, numberOf(project.getLastSuccessfulBuild()));
55 project.getBuildersList().replaceBy(Collections.<Builder>emptySet());
56 assertEquals(Result.SUCCESS, build(project)); // #4
57 assertEquals(4, numberOf(project.getLastSuccessfulBuild()));
58 assertEquals(null, project.getBuildByNumber(1));
59 assertEquals(null, project.getBuildByNumber(2));
60 assertEquals(3, numberOf(project.getLastFailedBuild()));
61 }
62
63 @Bug(2417)
64 public void testStableVsUnstable() throws Exception {
65 FreeStyleProject project = createFreeStyleProject();
66 project.setLogRotator(new LogRotator(-1, 2, -1, -1));
67 assertEquals(Result.SUCCESS, build(project)); // #1
68 project.getPublishersList().replaceBy(Collections.singleton(new TestsFail()));
69 assertEquals(Result.UNSTABLE, build(project)); // #2
70 assertEquals(Result.UNSTABLE, build(project)); // #3
71 assertEquals(1, numberOf(project.getLastStableBuild()));
72 project.getPublishersList().replaceBy(Collections.<Publisher>emptySet());
73 assertEquals(Result.SUCCESS, build(project)); // #4
74 assertEquals(null, project.getBuildByNumber(1));
75 assertEquals(null, project.getBuildByNumber(2));
76 }
77
78 @Bug(834)
79 public void testArtifactDelete() throws Exception {
80 FreeStyleProject project = createFreeStyleProject();
81 project.setLogRotator(new LogRotator(-1, 6, -1, 2));
82 project.getPublishersList().replaceBy(Collections.singleton(new ArtifactArchiver("f", "", true)));
83 assertEquals("(no artifacts)", Result.FAILURE, build(project)); // #1
84 assertFalse(project.getBuildByNumber(1).getHasArtifacts());
85 project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifact()));
86 assertEquals(Result.SUCCESS, build(project)); // #2
87 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
88 project.getBuildersList().replaceBy(Arrays.asList(new CreateArtifact(), new FailureBuilder()));
89 assertEquals(Result.FAILURE, build(project)); // #3
90 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
91 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
92 assertEquals(Result.FAILURE, build(project)); // #4
93 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
94 assertTrue(project.getBuildByNumber(3).getHasArtifacts());
95 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
96 assertEquals(Result.FAILURE, build(project)); // #5
97 assertTrue(project.getBuildByNumber(2).getHasArtifacts());
98 assertFalse("no better than #4", project.getBuildByNumber(3).getHasArtifacts());
99 assertTrue(project.getBuildByNumber(4).getHasArtifacts());
100 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
101 project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifact()));
102 assertEquals(Result.SUCCESS, build(project)); // #6
103 assertFalse("#2 is still lastSuccessful until #6 is complete", project.getBuildByNumber(2).getHasArtifacts());
104 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
105 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
106 assertTrue(project.getBuildByNumber(5).getHasArtifacts());
107 assertTrue(project.getBuildByNumber(6).getHasArtifacts());
108 assertEquals(Result.SUCCESS, build(project)); // #7
109 assertEquals(null, project.getBuildByNumber(1));
110 assertNotNull(project.getBuildByNumber(2));
111 assertFalse("lastSuccessful was #6 for ArtifactArchiver", project.getBuildByNumber(2).getHasArtifacts());
112 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
113 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
114 assertFalse(project.getBuildByNumber(5).getHasArtifacts());
115 assertTrue(project.getBuildByNumber(6).getHasArtifacts());
116 assertTrue(project.getBuildByNumber(7).getHasArtifacts());
117 assertEquals(Result.SUCCESS, build(project)); // #8
118 assertEquals(null, project.getBuildByNumber(2));
119 assertNotNull(project.getBuildByNumber(3));
120 assertFalse(project.getBuildByNumber(3).getHasArtifacts());
121 assertFalse(project.getBuildByNumber(4).getHasArtifacts());
122 assertFalse(project.getBuildByNumber(5).getHasArtifacts());
123 assertFalse(project.getBuildByNumber(6).getHasArtifacts());
124 assertTrue(project.getBuildByNumber(7).getHasArtifacts());
125 assertTrue(project.getBuildByNumber(8).getHasArtifacts());
126 }
127
128
129 static Result build(FreeStyleProject project) throws Exception {
130 return project.scheduleBuild2(0).get(10, TimeUnit.SECONDS).getResult();
131 }
132
133 private static int numberOf(Run<?,?> run) {
134 return run != null ? run.getNumber() : -1;
135 }
136
137 static class TestsFail extends Publisher {
138 public @Override boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) {
139 build.setResult(Result.UNSTABLE);
140 return true;
141 }
142
143 public BuildStepMonitor getRequiredMonitorService() {
144 return BuildStepMonitor.NONE;
145 }
146
147 public Descriptor<Publisher> getDescriptor() {
148 return new Descriptor<Publisher>(TestsFail.class) {
149 public String getDisplayName() {
150 return "TestsFail";
151 }
152 };
153 }
154 }
155
156 }
157