001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 18/07/2007 14:03:15
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.search;
045:
046: import java.util.ArrayList;
047: import java.util.Date;
048: import java.util.List;
049:
050: import junit.framework.Assert;
051: import junit.framework.TestCase;
052: import net.jforum.entities.Post;
053:
054: import org.apache.log4j.xml.DOMConfigurator;
055: import org.apache.lucene.analysis.standard.StandardAnalyzer;
056: import org.apache.lucene.search.Hits;
057: import org.apache.lucene.search.Query;
058:
059: /**
060: * @author Rafael Steil
061: * @version $Id: LuceneSearchTestCase.java,v 1.31 2007/09/09 22:53:36 rafaelsteil Exp $
062: */
063: public class LuceneSearchTestCase extends TestCase {
064: private static boolean logInitialized;
065:
066: private LuceneSearch search;
067: private LuceneSettings settings;
068: private LuceneIndexer indexer;
069:
070: public void testFivePostsInTwoForumsSearchOneForumAndTwoValidTermsAndOneInvalidTermExpectThreeResults() {
071: List l = this .createThreePosts();
072: ((Post) l.get(0)).setForumId(1);
073: ((Post) l.get(1)).setForumId(2);
074: ((Post) l.get(2)).setForumId(1);
075:
076: this .indexer.create((Post) l.get(0));
077: this .indexer.create((Post) l.get(1));
078: this .indexer.create((Post) l.get(2));
079:
080: // Post 4
081: Post p = this .newPost();
082: p
083: .setText("It introduces you to searching, sorting, filtering and highlighting [...]");
084: p.setForumId(1);
085: this .indexer.create(p);
086:
087: // Post 5
088: p = this .newPost();
089: p.setText("How to integrate lucene into your applications");
090: p.setForumId(2);
091: l.add(p);
092:
093: this .indexer.create(p);
094:
095: // Search
096: SearchArgs args = new SearchArgs();
097: args.setForumId(1);
098: args.setKeywords("open lucene xpto authoritative");
099:
100: List results = this .search.search(args).records();
101:
102: Assert.assertEquals(3, results.size());
103: }
104:
105: public void testORExpressionUsingThreePostsSearchTwoTermsExpectThreeResults() {
106: List l = this .createThreePosts();
107:
108: this .indexer.create((Post) l.get(0));
109: this .indexer.create((Post) l.get(1));
110: this .indexer.create((Post) l.get(2));
111:
112: // Search
113: SearchArgs args = new SearchArgs();
114: args.setKeywords("open lucene");
115:
116: List results = this .search.search(args).records();
117:
118: Assert.assertEquals(3, results.size());
119: }
120:
121: private List createThreePosts() {
122: List l = new ArrayList();
123:
124: // 1
125: Post p = this .newPost();
126: p.setText("lucene is a gem in the open source world");
127: l.add(p);
128:
129: // 2
130: p = this .newPost();
131: p
132: .setText("lucene in action is the authoritative guide to lucene");
133: l.add(p);
134:
135: // 3
136: p = this .newPost();
137: p
138: .setText("Powers search in surprising places [...] open to everyone");
139: l.add(p);
140:
141: return l;
142: }
143:
144: public void testANDExpressionUsingTwoPostsWithOneCommonWordSearchTwoTermsExpectOneResult() {
145: // 1
146: Post p = this .newPost();
147: p.setText("a regular text with some magic word");
148: this .indexer.create(p);
149:
150: // 2
151: p = this .newPost();
152: p.setText("say shazan to see the magic happen");
153: this .indexer.create(p);
154:
155: // Search
156: SearchArgs args = new SearchArgs();
157: args.matchAllKeywords();
158: args.setKeywords("magic regular");
159:
160: List results = this .search.search(args).records();
161:
162: Assert.assertEquals(1, results.size());
163: }
164:
165: public void testThreePostsSearchContentsExpectOneResult() {
166: // 1
167: Post p = this .newPost();
168: p.setSubject("java");
169: this .indexer.create(p);
170:
171: // 2
172: p = this .newPost();
173: p.setSubject("something else");
174: this .indexer.create(p);
175:
176: // 3
177: p = this .newPost();
178: p.setSubject("debug");
179: this .indexer.create(p);
180:
181: // Search
182: SearchArgs args = new SearchArgs();
183: args.setKeywords("java");
184:
185: List results = this .search.search(args).records();
186:
187: Assert.assertEquals(1, results.size());
188: }
189:
190: public void testTwoDifferentForumsSearchOneExpectOneResult() {
191: Post p1 = this .newPost();
192: p1.setForumId(1);
193: this .indexer.create(p1);
194:
195: Post p2 = this .newPost();
196: p2.setForumId(2);
197: this .indexer.create(p2);
198:
199: SearchArgs args = new SearchArgs();
200: args.setForumId(1);
201:
202: List results = this .search.search(args).records();
203:
204: Assert.assertEquals(1, results.size());
205: }
206:
207: private Post newPost() {
208: Post p = new Post();
209:
210: p.setText("");
211: p.setTime(new Date());
212: p.setSubject("");
213: p.setPostUsername("");
214:
215: return p;
216: }
217:
218: protected void setUp() throws Exception {
219: if (!logInitialized) {
220: DOMConfigurator.configure(this .getClass().getResource(
221: "/log4j.xml"));
222: logInitialized = true;
223: }
224:
225: this .settings = new LuceneSettings(new StandardAnalyzer());
226:
227: this .settings.useRAMDirectory();
228:
229: this .indexer = new LuceneIndexer(this .settings);
230:
231: this .search = new LuceneSearch(this .settings,
232: new FakeResultCollector());
233:
234: this .indexer.watchNewDocuDocumentAdded(this .search);
235: }
236:
237: private static class FakeResultCollector implements
238: LuceneResultCollector {
239: public List collect(SearchArgs args, Hits hits, Query query) {
240: List l = new ArrayList();
241:
242: for (int i = 0; i < hits.length(); i++) {
243: // We really don't care about the results, only how many they are
244: l.add("");
245: }
246:
247: return l;
248: }
249: }
250: }
|