1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.search;
18  
19  import java.io.File;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  
25  import org.apache.jetspeed.search.ParsedObject;
26  import org.apache.jetspeed.search.SearchEngine;
27  import org.apache.jetspeed.search.SearchResults;
28  import org.apache.jetspeed.search.handlers.HandlerFactoryImpl;
29  import org.apache.jetspeed.search.lucene.SearchEngineImpl;
30  
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  import junit.framework.TestSuite;
34  
35  /***
36   * @author jford
37   *
38   */
39  public class TestSearch extends TestCase
40  {
41      
42      private final static String INDEX_DIRECTORY = "./target/search_index";
43  
44      private File indexRoot;
45      SearchEngine searchEngine;
46      
47      private URL jetspeedHomePage = null;
48      
49      public TestSearch(String name)
50      {
51          super(name);
52          
53          try {
54              jetspeedHomePage = new URL("http://portals.apache.org/jetspeed-1/");
55          } catch (MalformedURLException e) {
56              e.printStackTrace();
57          }
58          
59          indexRoot = new File(INDEX_DIRECTORY);
60      }
61      
62      /***
63       * Start the tests.
64       *
65       * @param args the arguments. Not used
66       */
67      public static void main(String args[]) 
68      {
69          junit.awtui.TestRunner.main( new String[] { TestSearch.class.getName() } );
70      }
71   
72      /***
73       * Creates the test suite.
74       *
75       * @return a test suite (<code>TestSuite</code>) that includes all methods
76       *         starting with "test"
77       */
78      public static Test suite() 
79      {
80          // All methods starting with "test" will be executed in the test suite.
81          return new TestSuite( TestSearch.class );
82      }
83      
84      protected void setUp() throws Exception
85      {
86          super.setUp();
87          HashMap mapping = new HashMap();
88          mapping.put("java.net.URL", "org.apache.jetspeed.search.handlers.URLToDocHandler");
89          
90          HandlerFactoryImpl hfi = new HandlerFactoryImpl(mapping);
91          
92          searchEngine = new SearchEngineImpl(indexRoot.getPath(), null, true, hfi);
93      }
94      
95      protected void tearDown() throws Exception
96      {
97          File[] indexFiles = indexRoot.listFiles();
98          if(indexFiles != null)
99          {
100 	        for(int i=0; i<indexFiles.length; i++)
101 	        {
102 	            File file = indexFiles[i];
103 	            file.delete();
104 	        }
105         }
106         
107         indexRoot.delete();
108     }
109     
110     public void testRemoveWebPage() throws Exception
111     {
112         //System.out.println("search home = " + JetspeedResources.getString("services.SearchService.directory"));
113         
114         assertNotNull("Created URL to Jetspeed Home Page",  jetspeedHomePage);
115         assertTrue("Removing non-existent index entry", searchEngine.remove(jetspeedHomePage) == false);
116         assertTrue("Adding to index", searchEngine.add(jetspeedHomePage));
117         assertTrue("Removing from index", searchEngine.remove(jetspeedHomePage));
118     }
119     
120     public void testPutWebPage() throws Exception
121     {
122         //System.out.println("search home = " + JetspeedResources.getString("services.SearchService.directory"));
123         
124         assertNotNull("Created URL to Jetspeed Home Page",  jetspeedHomePage);
125         assertTrue("Adding to index", searchEngine.add(jetspeedHomePage));
126         assertTrue("Adding to index", searchEngine.add(new URL("http://www.java.net")));
127         assertTrue("Adding to index", searchEngine.add(new URL("http://portals.apache.org")));
128     }
129     
130     /***
131      *
132      * @throws Exception
133      */
134     public void testVerifyJetspeedSearch() throws Exception
135     {
136         //because tear down deletes files, need to do add again
137         testPutWebPage();
138         
139         SearchResults results  = searchEngine.search("YourResultsBelongToUs");
140         //System.out.println("Query 'YourResultsBelongToUs' hits = " + results.size());
141         assertTrue(" Hit count == 0", results.size() == 0);
142         Iterator resultIter = results.iterator();
143         while (resultIter.hasNext())
144         {
145             ParsedObject result = (ParsedObject) resultIter.next();
146             
147             System.out.println("Score = " + result.getScore());
148             System.out.println("title = " + result.getTitle());
149             System.out.println("summary = " + result.getDescription());
150             System.out.println("url = " + result.getURL());
151         }
152     }
153     
154     public void testVerifyJetspeedSearch1() throws Exception
155     {
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         {
165             ParsedObject result = (ParsedObject) resultIter.next();
166             System.out.println("Score = " + result.getScore());
167             System.out.println("title = " + result.getTitle());
168             System.out.println("summary = " + result.getDescription());
169             System.out.println("url = " + result.getURL());
170         }
171     }
172     
173     public void testVerifyJetspeedSearch2() throws Exception
174     {
175 //      because tear down deletes files, need to do add again
176         testPutWebPage();
177         
178         SearchResults results  = searchEngine.search("community");
179         assertTrue(" Hit count == 0", results.size() > 0);
180         
181         Iterator resultIter = results.iterator();
182         while (resultIter.hasNext())
183         {
184             ParsedObject result = (ParsedObject) resultIter.next();
185             System.out.println("Score = " + result.getScore());
186             System.out.println("title = " + result.getTitle());
187             System.out.println("summary = " + result.getDescription());
188             System.out.println("url = " + result.getURL());
189         }
190     }
191 }