File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\search\SearchTest.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.search;
25
26 import hudson.model.ListView;
27 import hudson.model.FreeStyleProject;
28
29 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
30 import com.gargoylesoftware.htmlunit.Page;
31 import com.gargoylesoftware.htmlunit.AlertHandler;
32
33 import net.sf.json.JSONArray;
34 import net.sf.json.JSONObject;
35 import net.sf.json.JSONSerializer;
36
37 import org.junit.Assert;
38 import org.jvnet.hudson.test.HudsonTestCase;
39 import org.jvnet.hudson.test.Bug;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 /**
45 * @author Kohsuke Kawaguchi
46 */
47 public class SearchTest extends HudsonTestCase {
48 /**
49 * No exact match should result in a failure status code.
50 */
51 public void testFailure() throws Exception {
52 try {
53 search("no-such-thing");
54 fail("404 expected");
55 } catch (FailingHttpStatusCodeException e) {
56 assertEquals(404,e.getResponse().getStatusCode());
57 }
58 }
59
60 /**
61 * Makes sure the script doesn't execute.
62 */
63 @Bug(3415)
64 public void testXSS() throws Exception {
65 try {
66 WebClient wc = new WebClient();
67 wc.setAlertHandler(new AlertHandler() {
68 public void handleAlert(Page page, String message) {
69 throw new AssertionError();
70 }
71 });
72 wc.search("<script>alert('script');</script>");
73 fail("404 expected");
74 } catch (FailingHttpStatusCodeException e) {
75 assertEquals(404,e.getResponse().getStatusCode());
76 }
77 }
78
79 public void testSearchByProjectName() throws Exception {
80 final String projectName = "testSearchByProjectName";
81
82 createFreeStyleProject(projectName);
83
84 Page result = search(projectName);
85 Assert.assertNotNull(result);
86 assertGoodStatus(result);
87
88 // make sure we've fetched the testSearchByDisplayName project page
89 String contents = result.getWebResponse().getContentAsString();
90 Assert.assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", projectName)));
91 }
92
93 public void testSearchByDisplayName() throws Exception {
94 final String displayName = "displayName9999999";
95
96 FreeStyleProject project = createFreeStyleProject("testSearchByDisplayName");
97 project.setDisplayName(displayName);
98
99 Page result = search(displayName);
100 Assert.assertNotNull(result);
101 assertGoodStatus(result);
102
103 // make sure we've fetched the testSearchByDisplayName project page
104 String contents = result.getWebResponse().getContentAsString();
105 Assert.assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", displayName)));
106 }
107
108 public void testSearch2ProjectsWithSameDisplayName() throws Exception {
109 // create 2 freestyle projects with the same display name
110 final String projectName1 = "projectName1";
111 final String projectName2 = "projectName2";
112 final String projectName3 = "projectName3";
113 final String displayName = "displayNameFoo";
114 final String otherDisplayName = "otherDisplayName";
115
116 FreeStyleProject project1 = createFreeStyleProject(projectName1);
117 project1.setDisplayName(displayName);
118 FreeStyleProject project2 = createFreeStyleProject(projectName2);
119 project2.setDisplayName(displayName);
120 FreeStyleProject project3 = createFreeStyleProject(projectName3);
121 project3.setDisplayName(otherDisplayName);
122
123 // make sure that on search we get back one of the projects, it doesn't
124 // matter which one as long as the one that is returned has displayName
125 // as the display name
126 Page result = search(displayName);
127 Assert.assertNotNull(result);
128 assertGoodStatus(result);
129
130 // make sure we've fetched the testSearchByDisplayName project page
131 String contents = result.getWebResponse().getContentAsString();
132 Assert.assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", displayName)));
133 Assert.assertFalse(contents.contains(otherDisplayName));
134 }
135
136 public void testProjectNamePrecedesDisplayName() throws Exception {
137 final String project1Name = "foo";
138 final String project1DisplayName = "project1DisplayName";
139 final String project2Name = "project2Name";
140 final String project2DisplayName = project1Name;
141 final String project3Name = "project3Name";
142 final String project3DisplayName = "project3DisplayName";
143
144 // create 1 freestyle project with the name foo
145 FreeStyleProject project1 = createFreeStyleProject(project1Name);
146 project1.setDisplayName(project1DisplayName);
147
148 // create another with the display name foo
149 FreeStyleProject project2 = createFreeStyleProject(project2Name);
150 project2.setDisplayName(project2DisplayName);
151
152 // create a third project and make sure it's not picked up by search
153 FreeStyleProject project3 = createFreeStyleProject(project3Name);
154 project3.setDisplayName(project3DisplayName);
155
156 // search for foo
157 Page result = search(project1Name);
158 Assert.assertNotNull(result);
159 assertGoodStatus(result);
160
161 // make sure we get the project with the name foo
162 String contents = result.getWebResponse().getContentAsString();
163 Assert.assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", project1DisplayName)));
164 // make sure projects 2 and 3 were not picked up
165 Assert.assertFalse(contents.contains(project2Name));
166 Assert.assertFalse(contents.contains(project3Name));
167 Assert.assertFalse(contents.contains(project3DisplayName));
168 }
169
170 public void testGetSuggestionsHasBothNamesAndDisplayNames() throws Exception {
171 final String projectName = "project name";
172 final String displayName = "display name";
173
174 FreeStyleProject project1 = createFreeStyleProject(projectName);
175 project1.setDisplayName(displayName);
176
177 WebClient wc = new WebClient();
178 Page result = wc.goTo("search/suggest?query=name", "application/json");
179 Assert.assertNotNull(result);
180 assertGoodStatus(result);
181
182 String content = result.getWebResponse().getContentAsString();
183 System.out.println(content);
184 JSONObject jsonContent = (JSONObject)JSONSerializer.toJSON(content);
185 Assert.assertNotNull(jsonContent);
186 JSONArray jsonArray = jsonContent.getJSONArray("suggestions");
187 Assert.assertNotNull(jsonArray);
188
189 Assert.assertEquals(2, jsonArray.size());
190
191 boolean foundProjectName = false;
192 boolean foundDispayName = false;
193 for(Object suggestion : jsonArray) {
194 JSONObject jsonSuggestion = (JSONObject)suggestion;
195
196 String name = (String)jsonSuggestion.get("name");
197 if(projectName.equals(name)) {
198 foundProjectName = true;
199 }
200 else if(displayName.equals(name)) {
201 foundDispayName = true;
202 }
203 }
204
205 Assert.assertTrue(foundProjectName);
206 Assert.assertTrue(foundDispayName);
207 }
208
209 /**
210 * Disable/enable status shouldn't affect the search
211 */
212 @Bug(13148)
213 public void testDisabledJobShouldBeSearchable() throws Exception {
214 FreeStyleProject j = createFreeStyleProject("foo-bar");
215 assertTrue(suggest(jenkins.getSearchIndex(), "foo").contains(j));
216
217 j.disable();
218 assertTrue(suggest(jenkins.getSearchIndex(), "foo").contains(j));
219 }
220
221 /**
222 * All top-level jobs should be searchable, not just jobs in the current view.
223 */
224 @Bug(13148)
225 public void testCompletionOutsideView() throws Exception {
226 FreeStyleProject j = createFreeStyleProject("foo-bar");
227 ListView v = new ListView("empty1",jenkins);
228 ListView w = new ListView("empty2",jenkins);
229 jenkins.addView(v);
230 jenkins.addView(w);
231 jenkins.setPrimaryView(w);
232
233 // new view should be empty
234 assertFalse(v.contains(j));
235 assertFalse(w.contains(j));
236 assertFalse(jenkins.getPrimaryView().contains(j));
237
238 assertTrue(suggest(jenkins.getSearchIndex(),"foo").contains(j));
239 }
240
241 private List<SearchItem> suggest(SearchIndex index, String term) {
242 List<SearchItem> result = new ArrayList<SearchItem>();
243 index.suggest(term, result);
244 return result;
245 }
246 }
247