001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.components;
004:
005: import junit.framework.*;
006: import fitnesse.wiki.*;
007: import fitnesse.testutil.FitNesseUtil;
008: import java.util.*;
009:
010: public class SearcherTest extends TestCase implements SearchObserver {
011: WikiPage root;
012:
013: private WikiPage pageTwo;
014:
015: private List hits = new ArrayList();
016:
017: private PageCrawler crawler;
018:
019: private Searcher searcher;
020:
021: public void hit(WikiPage page) throws Exception {
022: hits.add(page);
023: }
024:
025: public void setUp() throws Exception {
026: root = InMemoryPage.makeRoot("RooT");
027: crawler = root.getPageCrawler();
028: crawler.addPage(root, PathParser.parse("PageOne"),
029: "has PageOne content");
030: crawler.addPage(root, PathParser.parse("PageOne.PageOneChild"),
031: "PageChild is a child of PageOne");
032: pageTwo = crawler.addPage(root, PathParser.parse("PageTwo"),
033: "PageTwo has a bit of content too\n^PageOneChild");
034: PageData data = pageTwo.getData();
035: data.setAttribute(WikiPageProperties.VIRTUAL_WIKI_ATTRIBUTE,
036: "http://localhost:" + FitNesseUtil.port + "/PageOne");
037: pageTwo.commit(data);
038: }
039:
040: public void testSearcher() throws Exception {
041: searcher = new Searcher("has", root);
042: getResults();
043: assertEquals(2, hits.size());
044: hasNamedPageAtIndex(hits, "PageOne", 0);
045: hasNamedPageAtIndex(hits, "PageTwo", 1);
046: }
047:
048: public void testSearcherAgain() throws Exception {
049: searcher = new Searcher("a", root);
050: getResults();
051: assertEquals(3, hits.size());
052: hasNamedPageAtIndex(hits, "PageOne", 0);
053: hasNamedPageAtIndex(hits, "PageOneChild", 1);
054: hasNamedPageAtIndex(hits, "PageTwo", 2);
055: }
056:
057: public void testDontSearchProxyPages() throws Exception {
058: searcher = new Searcher("a", pageTwo);
059: getResults();
060: assertEquals(2, hits.size());
061: }
062:
063: public void testObserving() throws Exception {
064: Searcher searcher = new Searcher("has", root);
065: searcher.searchContent(this );
066: assertEquals(2, hits.size());
067: }
068:
069: public void testTitleSearch() throws Exception {
070: searcher = new Searcher("one", root);
071: hits.clear();
072: searcher.searchTitles(this );
073: Collections.sort(hits, new Comparer());
074:
075: assertEquals(2, hits.size());
076: assertEquals("PageOne", ((WikiPage) hits.get(0)).getName());
077: assertEquals("PageOneChild", ((WikiPage) hits.get(1)).getName());
078: }
079:
080: private void hasNamedPageAtIndex(List results, String name,
081: int index) throws Exception {
082: WikiPage p = (WikiPage) results.get(index);
083: assertEquals(name, p.getName());
084: }
085:
086: public void getResults() throws Exception {
087: hits.clear();
088: searcher.searchContent(this );
089: Collections.sort(hits, new Comparer());
090: }
091:
092: class Comparer implements Comparator {
093: public int compare(Object o1, Object o2) {
094: try {
095: WikiPage page1 = (WikiPage) o1;
096: WikiPage page2 = (WikiPage) o2;
097: return page1.getName().compareTo(page2.getName());
098: } catch (Exception e) {
099: return 0;
100: }
101: }
102: }
103: }
|