001: package com.ecyrd.jspwiki.search;
002:
003: import java.io.File;
004: import java.util.Collection;
005: import java.util.Properties;
006:
007: import junit.framework.Test;
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010:
011: import com.ecyrd.jspwiki.SearchResult;
012: import com.ecyrd.jspwiki.TestEngine;
013: import com.ecyrd.jspwiki.TestHttpServletRequest;
014: import com.ecyrd.jspwiki.WikiContext;
015:
016: public class SearchManagerTest extends TestCase {
017: TestEngine m_engine;
018: SearchManager m_mgr;
019:
020: protected void setUp() throws Exception {
021: super .setUp();
022:
023: Properties props = new Properties();
024: props.load(TestEngine.findTestProperties());
025:
026: props.setProperty(SearchManager.PROP_SEARCHPROVIDER,
027: "LuceneSearchProvider");
028: props.setProperty("jspwiki.lucene.initialdelay", "1");
029:
030: String tmpdir = props.getProperty("jspwiki.workDir");
031:
032: assertNotNull(tmpdir);
033: // Empty the lucene work directory
034: TestEngine.deleteAll(new File(tmpdir, "lucene"));
035:
036: m_engine = new TestEngine(props);
037: m_mgr = m_engine.getSearchManager();
038: }
039:
040: protected void tearDown() throws Exception {
041: super .tearDown();
042:
043: TestEngine.deleteTestPage("TestPage");
044: }
045:
046: public void testDefaultProvider() {
047: assertEquals("com.ecyrd.jspwiki.search.LuceneSearchProvider",
048: m_mgr.getSearchEngine().getClass().getName());
049: }
050:
051: public void testSimpleSearch() throws Exception {
052: String txt = "It was the dawn of the third age of mankind, ten years after the Earth-Minbari War.";
053:
054: m_engine.saveText("TestPage", txt);
055:
056: Thread.yield();
057:
058: Thread.sleep(2000L); // Should cover for both index and initial delay
059:
060: Collection res = m_mgr.findPages("mankind");
061:
062: assertNotNull("null result", res);
063: assertEquals("no pages", 1, res.size());
064:
065: assertEquals("page", "TestPage", ((SearchResult) res.iterator()
066: .next()).getPage().getName());
067: }
068:
069: public void testSimpleSearch2() throws Exception {
070: String txt = "It was the dawn of the third age of mankind, ten years after the Earth-Minbari War.";
071:
072: m_engine.saveText("TestPage", txt);
073:
074: m_engine.saveText("TestPage", txt + " 2");
075:
076: Thread.yield();
077:
078: Thread.sleep(2000L); // Should cover for both index and initial delay
079:
080: Collection res = m_mgr.findPages("mankind");
081:
082: assertNotNull("null result", res);
083: assertEquals("no pages", 1, res.size());
084:
085: assertEquals("page", "TestPage", ((SearchResult) res.iterator()
086: .next()).getPage().getName());
087: }
088:
089: public void testSimpleSearch3() throws Exception {
090: String txt = "It was the dawn of the third age of mankind, ten years after the Earth-Minbari War.";
091:
092: TestHttpServletRequest request = new TestHttpServletRequest();
093: request.setParameter("page", "TestPage");
094:
095: WikiContext ctx = m_engine.createContext(request,
096: WikiContext.EDIT);
097:
098: m_engine.saveText(ctx, txt);
099:
100: m_engine
101: .saveText(
102: ctx,
103: "The Babylon Project was a dream given form. Its goal: to prevent another war by creating a place where humans and aliens could work out their differences peacefully.");
104:
105: Thread.yield();
106:
107: Thread.sleep(2000L); // Should cover for both index and initial delay
108:
109: Collection res = m_mgr.findPages("mankind");
110:
111: assertNotNull("found results", res);
112: assertEquals("empty results", 0, res.size());
113:
114: res = m_mgr.findPages("Babylon");
115: assertNotNull("null result", res);
116: assertEquals("no pages", 1, res.size());
117:
118: assertEquals("page", "TestPage", ((SearchResult) res.iterator()
119: .next()).getPage().getName());
120: }
121:
122: public void testTitleSearch() throws Exception {
123: String txt = "Nonsensical content that should not match";
124:
125: m_engine.saveText("TestPage", txt);
126:
127: Thread.yield();
128:
129: Thread.sleep(2000L); // Should cover for both index and initial delay
130:
131: Collection res = m_mgr.findPages("Test");
132:
133: assertNotNull("null result", res);
134: assertEquals("no pages", 1, res.size());
135:
136: assertEquals("page", "TestPage", ((SearchResult) res.iterator()
137: .next()).getPage().getName());
138: }
139:
140: public void testTitleSearch2() throws Exception {
141: String txt = "Nonsensical content that should not match";
142:
143: m_engine.saveText("TestPage", txt);
144:
145: Thread.yield();
146:
147: Thread.sleep(2000L); // Should cover for both index and initial delay
148:
149: Collection res = m_mgr.findPages("TestPage");
150:
151: assertNotNull("null result", res);
152: assertEquals("no pages", 1, res.size());
153:
154: assertEquals("page", "TestPage", ((SearchResult) res.iterator()
155: .next()).getPage().getName());
156: }
157:
158: public static Test suite() {
159: return new TestSuite(SearchManagerTest.class);
160: }
161: }
|