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.responders.search;
004:
005: import fitnesse.testutil.*;
006: import fitnesse.http.*;
007: import fitnesse.wiki.*;
008: import fitnesse.FitNesseContext;
009: import fitnesse.components.*;
010:
011: public class SearchResponderTest extends AbstractRegex {
012: private WikiPage root;
013:
014: private PageCrawler crawler;
015:
016: private SearchResponder responder;
017:
018: private MockRequest request;
019:
020: public void setUp() throws Exception {
021: root = InMemoryPage.makeRoot("RooT");
022: crawler = root.getPageCrawler();
023: crawler.addPage(root, PathParser.parse("SomePage"),
024: "has something in it");
025: responder = new SearchResponder();
026: request = new MockRequest();
027: request.addInput("searchString", "blah");
028: request.addInput("searchType", "blah");
029: }
030:
031: public void tearDown() throws Exception {
032: }
033:
034: public void testHtml() throws Exception {
035: String content = getResponseContentUsingSearchString("something");
036:
037: assertHasRegexp("something", content);
038: assertHasRegexp("SomePage", content);
039: }
040:
041: public void testEscapesSearchString() throws Exception {
042: String content = getResponseContentUsingSearchString("!+-<&>");
043: assertSubString("!+-<&>", content);
044: }
045:
046: private String getResponseContentUsingSearchString(
047: String searchString) throws Exception {
048: request.addInput("searchString", searchString);
049:
050: Response response = responder.makeResponse(new FitNesseContext(
051: root), request);
052: MockResponseSender sender = new MockResponseSender();
053: response.readyToSend(sender);
054: sender.waitForClose(5000);
055: return sender.sentData();
056: }
057:
058: public void testTitle() throws Exception {
059: request.addInput("searchType",
060: "something with the word title in it");
061: responder.setRequest(request);
062: String title = responder.getTitle();
063: assertSubString("Title Search Results", title);
064:
065: request.addInput("searchType",
066: "something with the word content in it");
067: title = responder.getTitle();
068: assertSubString("Content Search Results", title);
069: }
070:
071: public void testActivatingProperSearch() throws Exception {
072: TestableSearcher searcher = new TestableSearcher();
073: responder.setSearcher(searcher);
074: responder.setRequest(request);
075:
076: request.addInput("searchType",
077: "something with the word title in it");
078: new MockResponseSender(responder.makeResponse(
079: new FitNesseContext(root), request));
080: assertTrue(searcher.titleSearchCalled);
081:
082: request.addInput("searchType",
083: "something with the word content in it");
084: new MockResponseSender(responder.makeResponse(
085: new FitNesseContext(root), request));
086: assertTrue(searcher.contentSearchCalled);
087: }
088:
089: private static class TestableSearcher extends Searcher {
090: boolean contentSearchCalled = false;
091:
092: boolean titleSearchCalled = false;
093:
094: public TestableSearcher() throws Exception {
095: super ("", null);
096: }
097:
098: public void searchContent(SearchObserver observer)
099: throws Exception {
100: contentSearchCalled = true;
101: }
102:
103: public void searchTitles(SearchObserver observer) {
104: titleSearchCalled = true;
105: }
106: }
107: }
|