PageRenderTime 45ms CodeModel.GetById 17ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/tasks/test/TestResultProjectAction.java

http://github.com/hudson/hudson
Java | 149 lines | 76 code | 19 blank | 54 comment | 7 complexity | 1da6f0242c39ad2895f6d3d9d1fc8a63 MD5 | raw file
  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 */
 24package hudson.tasks.test;
 25
 26import hudson.model.AbstractBuild;
 27import hudson.model.AbstractProject;
 28import hudson.model.Action;
 29import hudson.tasks.junit.JUnitResultArchiver;
 30import org.kohsuke.stapler.Ancestor;
 31import org.kohsuke.stapler.StaplerRequest;
 32import org.kohsuke.stapler.StaplerResponse;
 33
 34import javax.servlet.ServletException;
 35import javax.servlet.http.Cookie;
 36import javax.servlet.http.HttpServletResponse;
 37import java.io.IOException;
 38import 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 */
 49public class TestResultProjectAction implements Action {
 50    /**
 51     * Project that owns this action.
 52     */
 53    //TODO: review and check whether we can do it private
 54    public final AbstractProject<?,?> project;
 55
 56    public TestResultProjectAction(AbstractProject<?,?> project) {
 57        this.project = project;
 58    }
 59
 60    /**
 61     * No task list item.
 62     */
 63    public String getIconFileName() {
 64        return null;
 65    }
 66
 67    public String getDisplayName() {
 68        return "Test Report";
 69    }
 70
 71    public String getUrlName() {
 72        return "test";
 73    }
 74
 75    public AbstractProject<?, ?> getProject() {
 76        return project;
 77    }
 78
 79    public AbstractTestResultAction getLastTestResultAction() {
 80        final AbstractBuild<?,?> tb = project.getLastSuccessfulBuild();
 81
 82        AbstractBuild<?,?> b=project.getLastBuild();
 83        while(b!=null) {
 84            AbstractTestResultAction a = b.getTestResultAction();
 85            if(a!=null) return a;
 86            if(b==tb)
 87                // if even the last successful build didn't produce the test result,
 88                // that means we just don't have any tests configured.
 89                return null;
 90            b = b.getPreviousBuild();
 91        }
 92
 93        return null;
 94    }
 95
 96    /**
 97     * Display the test result trend.
 98     */
 99    public void doTrend( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
100        AbstractTestResultAction a = getLastTestResultAction();
101        if(a!=null)
102            a.doGraph(req,rsp);
103        else
104            rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
105    }
106
107    /**
108     * Generates the clickable map HTML fragment for {@link #doTrend(StaplerRequest, StaplerResponse)}.
109     */
110    public void doTrendMap( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
111        AbstractTestResultAction a = getLastTestResultAction();
112        if(a!=null)
113            a.doGraphMap(req,rsp);
114        else
115            rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
116    }
117
118    /**
119     * Changes the test result report display mode.
120     */
121    public void doFlipTrend( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
122        boolean failureOnly = false;
123
124        // check the current preference value
125        Cookie[] cookies = req.getCookies();
126        if(cookies!=null) {
127            for (Cookie cookie : cookies) {
128                if(cookie.getName().equals(FAILURE_ONLY_COOKIE))
129                    failureOnly = Boolean.parseBoolean(cookie.getValue());
130            }
131        }
132
133        // flip!
134        failureOnly = !failureOnly;
135
136        // set the updated value
137        Cookie cookie = new Cookie(FAILURE_ONLY_COOKIE,String.valueOf(failureOnly));
138        List anc = req.getAncestors();
139        Ancestor a = (Ancestor) anc.get(anc.size()-2);
140        cookie.setPath(a.getUrl()); // just for this project
141        cookie.setMaxAge(60*60*24*365); // 1 year
142        rsp.addCookie(cookie);
143
144        // back to the project page
145        rsp.sendRedirect("..");
146    }
147
148    private static final String FAILURE_ONLY_COOKIE = "TestResultAction_failureOnly";
149}