001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.search;
018:
019: import java.io.File;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: import org.apache.jetspeed.search.ParsedObject;
026: import org.apache.jetspeed.search.SearchEngine;
027: import org.apache.jetspeed.search.SearchResults;
028: import org.apache.jetspeed.search.handlers.HandlerFactoryImpl;
029: import org.apache.jetspeed.search.lucene.SearchEngineImpl;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: * @author jford
037: *
038: */
039: public class TestSearch extends TestCase {
040:
041: private final static String INDEX_DIRECTORY = "./target/search_index";
042:
043: private File indexRoot;
044: SearchEngine searchEngine;
045:
046: private URL jetspeedHomePage = null;
047:
048: public TestSearch(String name) {
049: super (name);
050:
051: try {
052: jetspeedHomePage = new URL(
053: "http://portals.apache.org/jetspeed-1/");
054: } catch (MalformedURLException e) {
055: e.printStackTrace();
056: }
057:
058: indexRoot = new File(INDEX_DIRECTORY);
059: }
060:
061: /**
062: * Start the tests.
063: *
064: * @param args the arguments. Not used
065: */
066: public static void main(String args[]) {
067: junit.awtui.TestRunner.main(new String[] { TestSearch.class
068: .getName() });
069: }
070:
071: /**
072: * Creates the test suite.
073: *
074: * @return a test suite (<code>TestSuite</code>) that includes all methods
075: * starting with "test"
076: */
077: public static Test suite() {
078: // All methods starting with "test" will be executed in the test suite.
079: return new TestSuite(TestSearch.class);
080: }
081:
082: protected void setUp() throws Exception {
083: super .setUp();
084: HashMap mapping = new HashMap();
085: mapping.put("java.net.URL",
086: "org.apache.jetspeed.search.handlers.URLToDocHandler");
087:
088: HandlerFactoryImpl hfi = new HandlerFactoryImpl(mapping);
089:
090: searchEngine = new SearchEngineImpl(indexRoot.getPath(), null,
091: true, hfi);
092: }
093:
094: protected void tearDown() throws Exception {
095: File[] indexFiles = indexRoot.listFiles();
096: if (indexFiles != null) {
097: for (int i = 0; i < indexFiles.length; i++) {
098: File file = indexFiles[i];
099: file.delete();
100: }
101: }
102:
103: indexRoot.delete();
104: }
105:
106: public void testRemoveWebPage() throws Exception {
107: //System.out.println("search home = " + JetspeedResources.getString("services.SearchService.directory"));
108:
109: assertNotNull("Created URL to Jetspeed Home Page",
110: jetspeedHomePage);
111: assertTrue("Removing non-existent index entry", searchEngine
112: .remove(jetspeedHomePage) == false);
113: assertTrue("Adding to index", searchEngine
114: .add(jetspeedHomePage));
115: assertTrue("Removing from index", searchEngine
116: .remove(jetspeedHomePage));
117: }
118:
119: public void testPutWebPage() throws Exception {
120: //System.out.println("search home = " + JetspeedResources.getString("services.SearchService.directory"));
121:
122: assertNotNull("Created URL to Jetspeed Home Page",
123: jetspeedHomePage);
124: assertTrue("Adding to index", searchEngine
125: .add(jetspeedHomePage));
126: assertTrue("Adding to index", searchEngine.add(new URL(
127: "http://www.java.net")));
128: assertTrue("Adding to index", searchEngine.add(new URL(
129: "http://portals.apache.org")));
130: }
131:
132: /**
133: *
134: * @throws Exception
135: */
136: public void testVerifyJetspeedSearch() throws Exception {
137: //because tear down deletes files, need to do add again
138: testPutWebPage();
139:
140: SearchResults results = searchEngine
141: .search("YourResultsBelongToUs");
142: //System.out.println("Query 'YourResultsBelongToUs' hits = " + results.size());
143: assertTrue(" Hit count == 0", results.size() == 0);
144: Iterator resultIter = results.iterator();
145: while (resultIter.hasNext()) {
146: ParsedObject result = (ParsedObject) resultIter.next();
147:
148: System.out.println("Score = " + result.getScore());
149: System.out.println("title = " + result.getTitle());
150: System.out.println("summary = " + result.getDescription());
151: System.out.println("url = " + result.getURL());
152: }
153: }
154:
155: public void testVerifyJetspeedSearch1() throws Exception {
156: // because tear down deletes files, need to do add again
157: testPutWebPage();
158:
159: SearchResults results = searchEngine.search("Jetspeed");
160: assertTrue(" Hit count == 0", results.size() > 0);
161:
162: Iterator resultIter = results.iterator();
163: while (resultIter.hasNext()) {
164: ParsedObject result = (ParsedObject) resultIter.next();
165: System.out.println("Score = " + result.getScore());
166: System.out.println("title = " + result.getTitle());
167: System.out.println("summary = " + result.getDescription());
168: System.out.println("url = " + result.getURL());
169: }
170: }
171:
172: public void testVerifyJetspeedSearch2() throws Exception {
173: // because tear down deletes files, need to do add again
174: testPutWebPage();
175:
176: SearchResults results = searchEngine.search("community");
177: assertTrue(" Hit count == 0", results.size() > 0);
178:
179: Iterator resultIter = results.iterator();
180: while (resultIter.hasNext()) {
181: ParsedObject result = (ParsedObject) resultIter.next();
182: System.out.println("Score = " + result.getScore());
183: System.out.println("title = " + result.getTitle());
184: System.out.println("summary = " + result.getDescription());
185: System.out.println("url = " + result.getURL());
186: }
187: }
188: }
|