Skip to content

Commit 44db6ae

Browse files
committedMar 9, 2015
JENKINS-24433 Searchbox doesn't work properly inside a folder
1 parent b5ad71f commit 44db6ae

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed
 

‎core/src/main/java/hudson/search/Search.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException,
7676
SuggestedItem target = find(index, query, smo);
7777
if(target!=null) {
7878
// found
79-
rsp.sendRedirect2(a.getUrl()+target.getUrl());
79+
rsp.sendRedirect2(req.getContextPath()+target.getUrl());
8080
return;
8181
}
8282
}

‎core/src/main/java/hudson/search/SuggestedItem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public String getUrl() {
8181

8282
private static SuggestedItem build(SearchableModelObject searchContext, Item top) {
8383
ItemGroup<? extends Item> parent = top.getParent();
84-
if (parent instanceof Item && parent != searchContext) {
84+
if (parent instanceof Item) {
8585
Item parentItem = (Item)parent;
8686
return new SuggestedItem(build(searchContext, parentItem), top);
8787
}

‎test/src/test/java/hudson/search/SearchTest.java

+123
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ public void testSearchByProjectName() throws Exception {
105105
assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", projectName)));
106106
}
107107

108+
@Issue("JENKINS-24433")
109+
@Test
110+
public void testSearchByProjectNameBehindAFolder() throws Exception {
111+
final String projectName = "testSearchByProjectName";
112+
j.createFreeStyleProject(projectName);
113+
j.createFolder("my-folder-1").createProject(FreeStyleProject.class, "my-job-1");
114+
115+
Page result = j.createWebClient().goTo("job/my-folder-1/search?q="+ projectName);
116+
117+
assertNotNull(result);
118+
j.assertGoodStatus(result);
119+
120+
// make sure we've fetched the testSearchByDisplayName project page
121+
String contents = result.getWebResponse().getContentAsString();
122+
assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", projectName)));
123+
}
124+
125+
@Issue("JENKINS-24433")
126+
@Test
127+
public void testSearchByProjectNameInAFolder() throws Exception {
128+
final String projectName = "testSearchByProjectName";
129+
j.createFreeStyleProject(projectName);
130+
j.createFolder("my-folder-1").createProject(FreeStyleProject.class, "my-job-1");
131+
132+
Page result = j.createWebClient().goTo("job/my-folder-1/search?q="+ "my-folder-1/my-job-1");
133+
134+
assertNotNull(result);
135+
j.assertGoodStatus(result);
136+
137+
// make sure we've fetched the testSearchByDisplayName project page
138+
String contents = result.getWebResponse().getContentAsString();
139+
assertTrue(contents.contains(String.format("<title>%s [Jenkins]</title>", "my-job-1 [my-folder-1]")));
140+
}
141+
108142
@Test
109143
public void testSearchByDisplayName() throws Exception {
110144
final String displayName = "displayName9999999";
@@ -225,6 +259,95 @@ else if(displayName.equals(name)) {
225259
assertTrue(foundDispayName);
226260
}
227261

262+
@Issue("JENKINS-24433")
263+
@Test
264+
public void testProjectNameBehindAFolderDisplayName() throws Exception {
265+
final String projectName1 = "job-1";
266+
final String displayName1 = "job-1";
267+
268+
final String projectName2 = "job-2";
269+
final String displayName2 = "job-2";
270+
271+
FreeStyleProject project1 = j.createFreeStyleProject(projectName1);
272+
project1.setDisplayName(displayName1);
273+
274+
FreeStyleProject project2 = j.createFolder("my-folder-1").createProject(FreeStyleProject.class, projectName2);
275+
project2.setDisplayName(displayName2);
276+
277+
WebClient wc = j.createWebClient();
278+
Page result = wc.goTo("job/my-folder-1/search/suggest?query=" + projectName1, "application/json");
279+
assertNotNull(result);
280+
j.assertGoodStatus(result);
281+
282+
String content = result.getWebResponse().getContentAsString();
283+
JSONObject jsonContent = (JSONObject)JSONSerializer.toJSON(content);
284+
assertNotNull(jsonContent);
285+
JSONArray jsonArray = jsonContent.getJSONArray("suggestions");
286+
assertNotNull(jsonArray);
287+
288+
assertEquals(1, jsonArray.size());
289+
290+
boolean foundProjectName = false;
291+
boolean foundDisplayName = false;
292+
for(Object suggestion : jsonArray) {
293+
JSONObject jsonSuggestion = (JSONObject)suggestion;
294+
295+
String name = (String)jsonSuggestion.get("name");
296+
if(projectName1.equals(name)) {
297+
foundProjectName = true;
298+
}
299+
if(displayName1.equals(name)) {
300+
foundDisplayName = true;
301+
}
302+
}
303+
304+
assertTrue(foundProjectName);
305+
assertTrue(foundDisplayName);
306+
}
307+
308+
@Issue("JENKINS-24433")
309+
@Test
310+
public void testProjectNameInAFolderDisplayName() throws Exception {
311+
final String projectName1 = "job-1";
312+
final String displayName1 = "job-1";
313+
314+
final String projectName2 = "job-2";
315+
final String displayName2 = "my-folder-1 job-2";
316+
317+
FreeStyleProject project1 = j.createFreeStyleProject(projectName1);
318+
project1.setDisplayName(displayName1);
319+
320+
FreeStyleProject project2 = j.createFolder("my-folder-1").createProject(FreeStyleProject.class, projectName2);
321+
project2.setDisplayName(displayName2);
322+
323+
324+
WebClient wc = j.createWebClient();
325+
Page result = wc.goTo("job/my-folder-1/search/suggest?query=" + projectName2, "application/json");
326+
assertNotNull(result);
327+
j.assertGoodStatus(result);
328+
329+
String content = result.getWebResponse().getContentAsString();
330+
JSONObject jsonContent = (JSONObject)JSONSerializer.toJSON(content);
331+
assertNotNull(jsonContent);
332+
JSONArray jsonArray = jsonContent.getJSONArray("suggestions");
333+
assertNotNull(jsonArray);
334+
335+
assertEquals(1, jsonArray.size());
336+
337+
boolean foundDisplayName = false;
338+
for(Object suggestion : jsonArray) {
339+
JSONObject jsonSuggestion = (JSONObject)suggestion;
340+
341+
String name = (String)jsonSuggestion.get("name");
342+
343+
if(displayName2.equals(name)) {
344+
foundDisplayName = true;
345+
}
346+
}
347+
348+
assertTrue(foundDisplayName);
349+
}
350+
228351
/**
229352
* Disable/enable status shouldn't affect the search
230353
*/

0 commit comments

Comments
 (0)
Please sign in to comment.