1   package org.bejug.javacareers.jobs.search.lucene;
2   
3   import java.io.File;
4   import java.util.List;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.apache.lucene.index.Term;
9   import org.bejug.javacareers.jobs.service.LucenePdfService;
10  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
11  
12  /***
13   Copyright (C) 2005 The Java Community
14  
15   This program is free software; you can redistribute it and/or modify  it under
16   the terms of the GNU General Public License as published by  the Free Software
17   Foundation; either version 2 of the License, or  (at your option) any later
18   version.
19  
20   This program is distributed in the hope that it will be useful,  but WITHOUT
21   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
22   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
23  
24   You should have received a copy of the GNU General Public License  along with
25   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
26   Place, Suite 330, Boston, MA 02111-1307 USA.
27   */
28  
29  /***
30   * @author Bavo Bruylandt (Last modified by $Author: shally $)
31   * @version $Revision: 1.13 $ - $Date: 2005/12/21 11:38:43 $
32   */
33  public class SearchTests extends AbstractDependencyInjectionSpringContextTests {
34  
35      private static final Log LOG = LogFactory.getLog(SearchTests.class);
36  
37      /***
38       *
39       */
40      private LucenePdfService lucenePdfService;
41      private File file4;
42  
43      /***
44       * @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#getConfigLocations()
45       * @return locations
46       */
47      protected String[] getConfigLocations() {
48          return new String[]{"/org/bejug/javacareers/applicationContext-test.xml"};
49      }
50  
51  
52      /***
53       * @param lucenePdfService LucenePdfService.
54       */
55      public void setLucenePdfService(LucenePdfService lucenePdfService) {
56          this.lucenePdfService = lucenePdfService;
57      }
58  
59      /***
60       *
61       */
62      public void testLuceneIndexing() {
63          LOG.info("Debug: Starting index test");
64  
65  
66          StringBuffer path = new StringBuffer(100);
67          path.append("test");
68          path.append(File.separator);
69          path.append("file");
70          path.append(File.separator);
71  
72          File file1 = new File(path + "timesheet.pdf");
73          File file2 = new File(path + "ch15.pdf");
74          File file3 = new File(path + "ch14.pdf");
75  
76  
77  
78  
79          try {
80              lucenePdfService.addToIndex(file1.getAbsolutePath(),"user");
81              lucenePdfService.addToIndex(file2.getAbsolutePath(),"user");
82              lucenePdfService.addToIndex(file3.getAbsolutePath(),"user");
83          } catch (PdfException e) {
84              LOG.debug(e);
85              assertTrue("Exception occurred", false);
86          }
87      }
88  
89      /***
90       *
91       */
92      public void testLuceneSearching() {
93          LOG.info("Debug: Starting search test");
94          //LucenePdfService service = new LucenePdfServiceImpl();
95  
96          List searchResultList = null;
97          lucenePdfService.setHighlightTags("<i>", "</i>");
98          try {
99              searchResultList = lucenePdfService.getSearchResultList("database*");
100             assertTrue(searchResultList.size() > 0);
101             for (int i = 0; i < searchResultList.size(); i++) {
102                 SearchResult result = (SearchResult) searchResultList.get(i);
103                 LOG.info("Debug: Lucene returned: " + result.toString());
104 
105                 assertNotNull(result.getFile());
106                 assertNotNull(result.getContext());
107                 //assertTrue(result.getContext().get(0).length() > 0);
108             }
109         } catch (PdfException e) {
110             LOG.debug(e);
111             assertTrue("Exception occurred searching", false);
112         }
113 
114         try {
115             searchResultList = lucenePdfService.getSearchResultList("unknown AND word AND test");
116             assertFalse(searchResultList.size() > 0);
117             for (int i = 0; i < searchResultList.size(); i++) {
118                 SearchResult result = (SearchResult) searchResultList.get(i);
119                 LOG.info("Debug: Lucene returned: " + result.toString());
120                 assertNull(result.getFile());
121                 assertNull(result.getContext());
122             }
123         } catch (PdfException e) {
124             LOG.debug(e);
125             assertTrue("Exception occurred searching", false);
126         }
127     }
128 
129     /***
130      * 
131      */
132     public void testSearchAndDelete() {
133         StringBuffer path = new StringBuffer(100);
134 
135         path.append("test");
136         path.append(File.separator);
137         path.append("file");
138         path.append(File.separator);
139         file4 = new File(path + "ch16.pdf");
140         String abspath = file4.getAbsolutePath();
141         try {
142             lucenePdfService.addToIndex(abspath,"testuser");
143             List results = lucenePdfService.getSearchResultList("testuser");
144             assertEquals(results.size(),1);
145 
146         } catch (PdfException e) {
147             LOG.debug(e);
148             assertTrue("Error adding and reading back",true);
149         }
150 
151             Term term = new Term("user","testuser");
152             //Field field = new Field()
153 
154             LOG.info("Trying to delete from index: "+term.text());
155         try {
156             lucenePdfService.removeFromIndex("testuser");
157             List results = lucenePdfService.getSearchResultList("testuser");
158             assertEquals(results.size(),0);
159             LOG.info("Debug: Deleted from index");
160         } catch (PdfException e) {
161             LOG.debug(e);
162             assertTrue("Error deleting and reading back",true);
163         }
164 
165         }
166 
167 
168 
169     }
170 
171 /***
172  * $Log: SearchTests.java,v $
173  * Revision 1.13  2005/12/21 11:38:43  shally
174  * *** empty log message ***
175  *
176  * Revision 1.12  2005/12/08 14:53:46  shally
177  * Opkuis voor checkstyle.
178  *
179  * Revision 1.11  2005/09/30 14:38:08  bavo_jcs
180  * Fixed URL
181  *
182  * Revision 1.10  2005/09/13 08:11:06  schauwvliege
183  * organize imports
184  *
185  * Revision 1.9  2005/08/31 09:53:40  bavo_jcs
186  * Lucene delete fix
187  *
188  * Revision 1.8  2005/08/26 15:08:03  bavo_jcs
189  * Search fix
190  *
191  * Revision 1.7  2005/08/25 15:12:42  bavo_jcs
192  * Lucene file removal
193  *
194  * Revision 1.6  2005/07/20 11:05:25  ge0ffrey
195  * JAVACAREERS-137
196  *
197  * Revision 1.5  2005/07/12 14:49:22  bavo_jcs
198  * Ajax Job search
199  *
200  * Revision 1.4  2005/06/29 09:00:26  psong09
201  * comment component updated
202  *
203  * Revision 1.3  2005/06/14 12:05:52  schauwvliege
204  * CheckStyle and fixing tests
205  *
206  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
207  * Fix initial import
208  *
209  * Revision 1.6  2005/06/07 14:38:48  bbr
210  * Lucene highlightterms added
211  *
212  * Revision 1.5  2005/06/06 14:13:23  bbr
213  * lowercased context files
214  *
215  * Revision 1.4  2005/05/31 13:30:49  bbr
216  * reorganized contexts for tests
217  *
218  * Revision 1.3  2005/05/26 14:59:31  ssc
219  * added Searchcriteria tests
220  *
221  * Revision 1.2  2005/05/25 13:54:34  bbr
222  * lucene bean
223  *
224  * Revision 1.1  2005/05/23 18:10:19  sja
225  * Corrected path generation.
226  *
227  * Revision 1.1  2005/05/23 15:42:00  bbr
228  * added weight to lucene
229  *
230  * Revision 1.1  2005/05/20 07:45:39  bavo_jcs
231  * -search changes
232  *
233  * Revision 1.1  2005/05/18 15:46:39  bavo_jcs
234  * -adeed search service
235  *
236  */