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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2009, Yahoo!, Inc.
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.junit;
25
26 import org.jvnet.hudson.test.HudsonTestCase;
27 import hudson.model.FreeStyleBuild;
28 import hudson.model.FreeStyleProject;
29 import hudson.model.Result;
30
31 import java.util.concurrent.TimeUnit;
32
33 import org.jvnet.hudson.test.TouchBuilder;
34 import org.jvnet.hudson.test.recipes.LocalData;
35
36 import com.gargoylesoftware.htmlunit.html.HtmlPage;
37 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
38 import com.gargoylesoftware.htmlunit.Page;
39
40 /**
41 * User: Benjamin Shine bshine@yahoo-inc.com
42 * Date: Dec 7, 2009
43 * Time: 7:52:55 PM
44 */
45 public class TestResultLinksTest extends HudsonTestCase {
46
47 private FreeStyleProject project;
48 private JUnitResultArchiver archiver;
49
50
51 @Override
52 protected void setUp() throws Exception {
53 super.setUp();
54 project = createFreeStyleProject("taqueria");
55 archiver = new JUnitResultArchiver("*.xml");
56 project.getPublishersList().add(archiver);
57 project.getBuildersList().add(new TouchBuilder());
58 }
59
60 @LocalData
61 public void testFailureLinks() throws Exception {
62 FreeStyleBuild build = project.scheduleBuild2(0).get(10, TimeUnit.SECONDS);
63 assertBuildStatus(Result.UNSTABLE, build);
64
65 TestResult theOverallTestResult = build.getAction(TestResultAction.class).getResult();
66 CaseResult theFailedTestCase = theOverallTestResult.getFailedTests().get(0);
67 String relativePath = theFailedTestCase.getRelativePathFrom(theOverallTestResult);
68 System.out.println("relative path seems to be: " + relativePath);
69
70 HudsonTestCase.WebClient wc = new HudsonTestCase.WebClient();
71
72 String testReportPageUrl = project.getLastBuild().getUrl() + "/testReport";
73 HtmlPage testReportPage = wc.goTo( testReportPageUrl );
74
75 Page packagePage = testReportPage.getFirstAnchorByText("tacoshack.meals").click();
76 assertGoodStatus(packagePage); // I expect this to work; just checking that my use of the APIs is correct.
77
78 // Now we're on that page. We should be able to find a link to the failed test in there.
79 HtmlAnchor anchor = testReportPage.getFirstAnchorByText("tacoshack.meals.NachosTest.testBeanDip");
80 String href = anchor.getHrefAttribute();
81 System.out.println("link is : " + href);
82 Page failureFromLink = anchor.click();
83 assertGoodStatus(failureFromLink);
84
85 // Now check the >>> link -- this is harder, because we can't do the javascript click handler properly
86 // The summary page is just tack on /summary to the url for the test
87
88 }
89
90 // Exercises the b-is-not-a-descendant-of-a path.
91 @LocalData
92 public void testNonDescendantRelativePath() throws Exception {
93 FreeStyleBuild build = project.scheduleBuild2(0).get(10, TimeUnit.MINUTES); // leave time for interactive debugging
94 assertBuildStatus(Result.UNSTABLE, build);
95 TestResult theOverallTestResult = build.getAction(TestResultAction.class).getResult();
96 CaseResult theFailedTestCase = theOverallTestResult.getFailedTests().get(0);
97 String relativePath = theFailedTestCase.getRelativePathFrom(theOverallTestResult);
98 System.out.println("relative path seems to be: " + relativePath);
99 assertNotNull("relative path exists", relativePath);
100 assertFalse("relative path doesn't start with a slash", relativePath.startsWith("/"));
101
102 // Now ask for the relative path from the child to the parent -- we should get an absolute path
103 String relativePath2 = theOverallTestResult.getRelativePathFrom(theFailedTestCase);
104 System.out.println("relative path2 seems to be: " + relativePath2);
105 // I know that in a HudsonTestCase we don't have a meaningful root url, so I expect an empty string here.
106 // If somehow we start being able to produce a root url, then I'll also tolerate a url that starts with that.
107 boolean pathIsEmptyOrNull = relativePath2 == null || relativePath2.isEmpty();
108 boolean pathStartsWithRootUrl = !pathIsEmptyOrNull && relativePath2.startsWith(jenkins.getRootUrl());
109 assertTrue("relative path is empty OR begins with the app root", pathIsEmptyOrNull || pathStartsWithRootUrl );
110 }
111 }
112