File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\tasks\test\TestResultProjectAction.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 package hudson.tasks.test;
25
26 import hudson.model.AbstractBuild;
27 import hudson.model.AbstractProject;
28 import hudson.model.Action;
29 import hudson.tasks.junit.JUnitResultArchiver;
30 import org.kohsuke.stapler.Ancestor;
31 import org.kohsuke.stapler.StaplerRequest;
32 import org.kohsuke.stapler.StaplerResponse;
33
34 import javax.servlet.ServletException;
35 import javax.servlet.http.Cookie;
36 import javax.servlet.http.HttpServletResponse;
37 import java.io.IOException;
38 import java.util.List;
39
40 /**
41 * Project action object from test reporter, such as {@link JUnitResultArchiver},
42 * which displays the trend report on the project top page.
43 *
44 * <p>
45 * This works with any {@link AbstractTestResultAction} implementation.
46 *
47 * @author Kohsuke Kawaguchi
48 */
49 public class TestResultProjectAction implements Action {
50 /**
51 * Project that owns this action.
52 */
53 public final AbstractProject<?,?> project;
54
55 public TestResultProjectAction(AbstractProject<?,?> project) {
56 this.project = project;
57 }
58
59 /**
60 * No task list item.
61 */
62 public String getIconFileName() {
63 return null;
64 }
65
66 public String getDisplayName() {
67 return "Test Report";
68 }
69
70 public String getUrlName() {
71 return "test";
72 }
73
74 public AbstractTestResultAction getLastTestResultAction() {
75 final AbstractBuild<?,?> tb = project.getLastSuccessfulBuild();
76
77 AbstractBuild<?,?> b=project.getLastBuild();
78 while(b!=null) {
79 AbstractTestResultAction a = b.getTestResultAction();
80 if(a!=null && (!b.isBuilding())) return a;
81 if(b==tb)
82 // if even the last successful build didn't produce the test result,
83 // that means we just don't have any tests configured.
84 return null;
85 b = b.getPreviousBuild();
86 }
87
88 return null;
89 }
90
91 /**
92 * Display the test result trend.
93 */
94 public void doTrend( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
95 AbstractTestResultAction a = getLastTestResultAction();
96 if(a!=null)
97 a.doGraph(req,rsp);
98 else
99 rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
100 }
101
102 /**
103 * Generates the clickable map HTML fragment for {@link #doTrend(StaplerRequest, StaplerResponse)}.
104 */
105 public void doTrendMap( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
106 AbstractTestResultAction a = getLastTestResultAction();
107 if(a!=null)
108 a.doGraphMap(req,rsp);
109 else
110 rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
111 }
112
113 /**
114 * Changes the test result report display mode.
115 */
116 public void doFlipTrend( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
117 boolean failureOnly = false;
118
119 // check the current preference value
120 Cookie[] cookies = req.getCookies();
121 if(cookies!=null) {
122 for (Cookie cookie : cookies) {
123 if(cookie.getName().equals(FAILURE_ONLY_COOKIE))
124 failureOnly = Boolean.parseBoolean(cookie.getValue());
125 }
126 }
127
128 // flip!
129 failureOnly = !failureOnly;
130
131 // set the updated value
132 Cookie cookie = new Cookie(FAILURE_ONLY_COOKIE,String.valueOf(failureOnly));
133 List anc = req.getAncestors();
134 Ancestor a = (Ancestor) anc.get(anc.size()-2);
135 cookie.setPath(a.getUrl()); // just for this project
136 cookie.setMaxAge(60*60*24*365); // 1 year
137 rsp.addCookie(cookie);
138
139 // back to the project page
140 rsp.sendRedirect("..");
141 }
142
143 private static final String FAILURE_ONLY_COOKIE = "TestResultAction_failureOnly";
144 }
145