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