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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., CloudBees, 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.model;
25
26 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
27 import com.gargoylesoftware.htmlunit.HttpMethod;
28 import com.gargoylesoftware.htmlunit.Page;
29 import com.gargoylesoftware.htmlunit.WebRequestSettings;
30 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
31 import com.gargoylesoftware.htmlunit.html.HtmlElement;
32 import com.gargoylesoftware.htmlunit.html.HtmlForm;
33 import com.gargoylesoftware.htmlunit.html.HtmlPage;
34 import hudson.model.Node.Mode;
35 import hudson.search.SearchTest;
36 import hudson.security.AuthorizationStrategy;
37 import hudson.security.SecurityRealm;
38 import hudson.tasks.Ant;
39 import hudson.tasks.BuildStep;
40 import hudson.tasks.Ant.AntInstallation;
41 import jenkins.model.Jenkins;
42 import org.jvnet.hudson.test.Bug;
43 import org.jvnet.hudson.test.Email;
44 import org.jvnet.hudson.test.HudsonTestCase;
45 import org.jvnet.hudson.test.recipes.LocalData;
46
47 import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
48 import java.lang.reflect.Field;
49 import java.net.URL;
50 import java.util.List;
51
52 /**
53 * @author Kohsuke Kawaguchi
54 */
55 public class HudsonTest extends HudsonTestCase {
56 /**
57 * Tests the basic UI sanity and HtmlUnit set up.
58 */
59 public void testGlobalConfigRoundtrip() throws Exception {
60 jenkins.setQuietPeriod(10);
61 jenkins.setScmCheckoutRetryCount(9);
62 jenkins.setNumExecutors(8);
63 configRoundtrip();
64 assertEquals(10,jenkins.getQuietPeriod());
65 assertEquals(9,jenkins.getScmCheckoutRetryCount());
66 assertEquals(8,jenkins.getNumExecutors());
67 }
68
69 /**
70 * Performs a very basic round-trip of a non-empty system configuration screen.
71 * This makes sure that the structured form submission is working (to some limited extent.)
72 */
73 @LocalData
74 @Email("http://www.nabble.com/Hudson.configure-calling-deprecated-Descriptor.configure-td19051815.html")
75 public void testSimpleConfigSubmit() throws Exception {
76 // just load the page and resubmit
77 HtmlPage configPage = new WebClient().goTo("configure");
78 HtmlForm form = configPage.getFormByName("config");
79 submit(form);
80
81 // make sure all the pieces are intact
82 assertEquals(2, jenkins.getNumExecutors());
83 assertSame(Mode.NORMAL, jenkins.getMode());
84 assertSame(SecurityRealm.NO_AUTHENTICATION, jenkins.getSecurityRealm());
85 assertSame(AuthorizationStrategy.UNSECURED, jenkins.getAuthorizationStrategy());
86 assertEquals(5, jenkins.getQuietPeriod());
87
88 List<JDK> jdks = jenkins.getJDKs();
89 assertEquals(3,jdks.size()); // Hudson adds one more
90 assertJDK(jdks.get(0),"jdk1","/tmp");
91 assertJDK(jdks.get(1),"jdk2","/tmp");
92
93 AntInstallation[] ants = jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations();
94 assertEquals(2,ants.length);
95 assertAnt(ants[0],"ant1","/tmp");
96 assertAnt(ants[1],"ant2","/tmp");
97 }
98
99 private void assertAnt(AntInstallation ant, String name, String home) {
100 assertEquals(ant.getName(),name);
101 assertEquals(ant.getHome(),home);
102 }
103
104 private void assertJDK(JDK jdk, String name, String home) {
105 assertEquals(jdk.getName(),name);
106 assertEquals(jdk.getHome(),home);
107 }
108
109 /**
110 * Makes sure that the search index includes job names.
111 *
112 * @see SearchTest#testFailure
113 * This test makes sure that a failure will result in an exception
114 */
115 public void testSearchIndex() throws Exception {
116 FreeStyleProject p = createFreeStyleProject();
117 Page jobPage = search(p.getName());
118
119 URL url = jobPage.getWebResponse().getUrl();
120 System.out.println(url);
121 assertTrue(url.getPath().endsWith("/job/"+p.getName()+"/"));
122 }
123
124 /**
125 * Top page should only have one item in the breadcrumb.
126 */
127 public void testBreadcrumb() throws Exception {
128 HtmlPage root = new WebClient().goTo("");
129 HtmlElement navbar = root.getElementById("breadcrumbs");
130 assertEquals(1,navbar.selectNodes("LI/A").size());
131 }
132
133 /**
134 * Configure link from "/computer/(master)/" should work.
135 */
136 @Email("http://www.nabble.com/Master-slave-refactor-td21361880.html")
137 public void testComputerConfigureLink() throws Exception {
138 HtmlPage page = new WebClient().goTo("computer/(master)/configure");
139 submit(page.getFormByName("config"));
140 }
141
142 /**
143 * Configure link from "/computer/(master)/" should work.
144 */
145 @Email("http://www.nabble.com/Master-slave-refactor-td21361880.html")
146 public void testDeleteHudsonComputer() throws Exception {
147 HudsonTestCase.WebClient wc = new WebClient();
148 HtmlPage page = wc.goTo("computer/(master)/");
149 for (HtmlAnchor a : page.getAnchors())
150 assertFalse(a.getHrefAttribute(),a.getHrefAttribute().endsWith("delete"));
151
152 // try to delete it by hitting the final URL directly
153 WebRequestSettings req = new WebRequestSettings(new URL(wc.getContextPath()+"computer/(master)/doDelete"), HttpMethod.POST);
154 try {
155 wc.getPage(req);
156 fail("Error code expected");
157 } catch (FailingHttpStatusCodeException e) {
158 assertEquals(SC_FORBIDDEN,e.getStatusCode());
159 }
160
161 // the master computer object should be still here
162 wc.goTo("computer/(master)/");
163 }
164
165 /**
166 * Legacy descriptors should be visible in the /descriptor/xyz URL.
167 */
168 @Email("http://www.nabble.com/1.286-version-and-description-The-requested-resource-%28%29-is-not--available.-td22233801.html")
169 public void testLegacyDescriptorLookup() throws Exception {
170 Descriptor dummy = new Descriptor(HudsonTest.class) {
171 public String getDisplayName() {
172 return "dummy";
173 }
174 };
175
176 BuildStep.PUBLISHERS.addRecorder(dummy);
177 assertSame(dummy, jenkins.getDescriptor(HudsonTest.class.getName()));
178
179 BuildStep.PUBLISHERS.remove(dummy);
180 assertNull(jenkins.getDescriptor(HudsonTest.class.getName()));
181 }
182
183 /**
184 * Verify null/invalid primaryView setting doesn't result in infinite loop.
185 */
186 @Bug(6938)
187 public void testInvalidPrimaryView() throws Exception {
188 Field pv = Jenkins.class.getDeclaredField("primaryView");
189 pv.setAccessible(true);
190 String value = null;
191 pv.set(jenkins, value);
192 assertNull("null primaryView", jenkins.getView(value));
193 value = "some bogus name";
194 pv.set(jenkins, value);
195 assertNull("invalid primaryView", jenkins.getView(value));
196 }
197 }
198