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

1 package hudson.model;
2
3 import hudson.search.SearchIndex;
4 import hudson.search.SearchIndexBuilder;
5 import hudson.search.SearchItem;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.List;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.mockito.Mockito;
14
15 public class ViewTest {
16
17 /*
18 * This test verifies that urls and displaynames in the TopLevelItem list
19 * are added to the SearchIndexBuilder
20 */
21 @Test
22 public void testAddDisplayNamesToSearchIndex() {
23 final String url1 = "url1";
24 final String displayName1 = "displayName1";
25 final String url2 = "url2";
26 final String displayName2 = "displayName2";
27
28 SearchIndexBuilder sib = new SearchIndexBuilder();
29 // mock the items to be indexed
30 TopLevelItem item1 = Mockito.mock(TopLevelItem.class);
31 Mockito.when(item1.getSearchUrl()).thenReturn(url1);
32 Mockito.when(item1.getDisplayName()).thenReturn(displayName1);
33 TopLevelItem item2 = Mockito.mock(TopLevelItem.class);
34 Mockito.when(item2.getSearchUrl()).thenReturn(url2);
35 Mockito.when(item2.getDisplayName()).thenReturn(displayName2);
36 Collection<TopLevelItem> items = new ArrayList<TopLevelItem>();
37 items.add(item1);
38 items.add(item2);
39
40 // mock the view class except for the addDisplayNamesToSearchIndex() call as that
41 // is what we are testing
42 View view = Mockito.mock(View.class);
43 Mockito.doCallRealMethod().when(view).addDisplayNamesToSearchIndex(sib, items);
44
45 // now make the actual call to index items
46 view.addDisplayNamesToSearchIndex(sib, items);
47
48 // make and index with sib
49 SearchIndex index = sib.make();
50
51 // now make sure we can fetch item1 from the index
52 List<SearchItem> result = new ArrayList<SearchItem>();
53 index.find(displayName1, result);
54 Assert.assertEquals(1, result.size());
55 SearchItem actual = result.get(0);
56 Assert.assertEquals(actual.getSearchName(), item1.getDisplayName());
57 Assert.assertEquals(actual.getSearchUrl(), item1.getSearchUrl());
58
59 // clear the result array for the next search result to test
60 result.clear();
61 // make sure we can fetch item 2 from the index
62 index.find(displayName2, result);
63 Assert.assertEquals(1, result.size());
64 actual = result.get(0);
65 Assert.assertEquals(actual.getSearchName(), item2.getDisplayName());
66 Assert.assertEquals(actual.getSearchUrl(), item2.getSearchUrl());
67 }
68 }
69