KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > TestCmsSearch


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearch.java,v $
3  * Date : $Date: 2006/10/26 10:22:11 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.types.CmsResourceTypeBinary;
36 import org.opencms.file.types.CmsResourceTypeFolder;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.main.OpenCms;
39 import org.opencms.report.CmsShellReport;
40 import org.opencms.report.I_CmsReport;
41 import org.opencms.test.OpenCmsTestCase;
42 import org.opencms.test.OpenCmsTestProperties;
43 import org.opencms.util.CmsStringUtil;
44
45 import java.util.Collections JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Locale JavaDoc;
49
50 import junit.extensions.TestSetup;
51 import junit.framework.Test;
52 import junit.framework.TestSuite;
53
54 /**
55  * Unit test for the cms search indexer.<p>
56  *
57  * @author Carsten Weinholz
58  * @version $Revision: 1.17 $
59  */

60 public class TestCmsSearch extends OpenCmsTestCase {
61
62     /** Name of the index used for testing. */
63     public static final String JavaDoc INDEX_OFFLINE = "Offline project (VFS)";
64
65     /** Name of the search index created using API. */
66     public static final String JavaDoc INDEX_TEST = "Test new index";
67
68     /**
69      * Default JUnit constructor.<p>
70      *
71      * @param arg0 JUnit parameters
72      */

73     public TestCmsSearch(String JavaDoc arg0) {
74
75         super(arg0);
76     }
77
78     /**
79      * Test suite for this test class.<p>
80      *
81      * @return the test suite
82      */

83     public static Test suite() {
84
85         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
86
87         TestSuite suite = new TestSuite();
88         suite.setName(TestCmsSearch.class.getName());
89
90         suite.addTest(new TestCmsSearch("testCmsSearchIndexer"));
91         suite.addTest(new TestCmsSearch("testCmsSearchUppercaseFolderName"));
92         suite.addTest(new TestCmsSearch("testCmsSearchDocumentTypes"));
93         suite.addTest(new TestCmsSearch("testCmsSearchXmlContent"));
94         suite.addTest(new TestCmsSearch("testIndexGeneration"));
95         suite.addTest(new TestCmsSearch("testQueryEncoding"));
96         suite.addTest(new TestCmsSearch("testSearchIssueWithSpecialFoldernames"));
97
98         // This test is intended only for performance/resource monitoring
99
// suite.addTest(new TestCmsSearch("testCmsSearchLargeResult"));
100

101         TestSetup wrapper = new TestSetup(suite) {
102
103             protected void setUp() {
104
105                 setupOpenCms("simpletest", "/sites/default/");
106             }
107
108             protected void tearDown() {
109
110                 removeOpenCms();
111             }
112         };
113
114         return wrapper;
115     }
116
117     /**
118      * Tests the CmsSearch with folder names with uppercase letters.<p>
119      *
120      * @throws Throwable if something goes wrong
121      */

122     public void testCmsSearchUppercaseFolderName() throws Throwable JavaDoc {
123         
124         CmsObject cms = getCmsObject();
125         echo("Testing search with uppercase folder names");
126
127         // create test folder
128
cms.createResource("/testUPPERCASE/", CmsResourceTypeFolder.RESOURCE_TYPE_ID, null, null);
129         cms.unlockResource("/testUPPERCASE/");
130
131         // create master resource
132
importTestResource(
133             cms,
134             "org/opencms/search/pdf-test-112.pdf",
135             "/testUPPERCASE/master.pdf",
136             CmsResourceTypeBinary.getStaticTypeId(),
137             Collections.EMPTY_LIST);
138         
139         // publish the project and update the search index
140
I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
141         OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, report);
142         
143         // search for "pdf"
144
CmsSearch cmsSearchBean = new CmsSearch();
145         cmsSearchBean.init(cms);
146         cmsSearchBean.setIndex(INDEX_OFFLINE);
147         cmsSearchBean.setQuery("pdf");
148         
149         CmsSearchParameters parameters = cmsSearchBean.getParameters();
150         parameters.setSearchRoots("/testUPPERCASE/");
151         cmsSearchBean.setParameters(parameters);
152         
153         List JavaDoc results = cmsSearchBean.getSearchResult();
154         assertEquals(1, results.size());
155     }
156     
157     /**
158      * Tests searching in various document types.<p>
159      *
160      * @throws Throwable if something goes wrong
161      */

162     public void testCmsSearchDocumentTypes() throws Throwable JavaDoc {
163
164         CmsObject cms = getCmsObject();
165         echo("Testing search for various document types");
166
167         CmsSearch cmsSearchBean = new CmsSearch();
168         cmsSearchBean.init(cms);
169         cmsSearchBean.setIndex(INDEX_OFFLINE);
170         cmsSearchBean.setSearchRoot("/types/");
171         List JavaDoc results;
172
173         cmsSearchBean.setQuery("+Alkacon +OpenCms +Text");
174         results = cmsSearchBean.getSearchResult();
175         assertEquals(1, results.size());
176         assertEquals("/sites/default/types/text.txt", ((CmsSearchResult)results.get(0)).getPath());
177     }
178
179     /**
180      * Test the cms search indexer.<p>
181      *
182      * @throws Throwable if something goes wrong
183      */

184     public void testCmsSearchIndexer() throws Throwable JavaDoc {
185
186         I_CmsReport report = new CmsShellReport(Locale.ENGLISH);
187         OpenCms.getSearchManager().rebuildAllIndexes(report);
188     }
189
190     /**
191      * Tests the cms search with a larger result set.<p>
192      *
193      * @throws Throwable if something goes wrong
194      */

195     public void testCmsSearchLargeResult() throws Throwable JavaDoc {
196
197         CmsObject cms = getCmsObject();
198         echo("Testing search with large result set");
199
200         // create test folder
201
cms.createResource("/test/", CmsResourceTypeFolder.RESOURCE_TYPE_ID, null, null);
202         cms.unlockResource("/test/");
203
204         // create master resource
205
importTestResource(
206             cms,
207             "org/opencms/search/pdf-test-112.pdf",
208             "/test/master.pdf",
209             CmsResourceTypeBinary.getStaticTypeId(),
210             Collections.EMPTY_LIST);
211
212         // create a copy
213
cms.copyResource("/test/master.pdf", "/test/copy.pdf");
214         cms.chacc("/test/copy.pdf", "group", "Users", "-r");
215
216         // create siblings
217
for (int i = 0; i < 100; i++) {
218             cms.createSibling("/test/master.pdf", "/test/sibling" + i + ".pdf", null);
219         }
220
221         // publish the project and update the search index
222
I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
223         OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, report);
224
225         // search for "pdf"
226
CmsSearch cmsSearchBean = new CmsSearch();
227         cmsSearchBean.init(cms);
228         cmsSearchBean.setIndex(INDEX_OFFLINE);
229         List JavaDoc results;
230
231         cms.createUser("test", "test", "", null);
232         cms.loginUser("test", "test");
233         cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
234
235         cmsSearchBean.setQuery("pdf");
236
237         echo("With Permission check, with excerpt");
238         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.PERMISSIONS, CmsStringUtil.TRUE);
239         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.EXCERPT, CmsStringUtil.TRUE);
240
241         cmsSearchBean.setSearchPage(1);
242         long duration = -System.currentTimeMillis();
243         results = cmsSearchBean.getSearchResult();
244         duration += System.currentTimeMillis();
245         echo("Search1: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
246
247         for (Iterator JavaDoc i = results.iterator(); i.hasNext();) {
248             CmsSearchResult res = (CmsSearchResult)i.next();
249             echo(res.getPath() + res.getExcerpt());
250         }
251
252         cmsSearchBean.setSearchPage(2);
253         duration = -System.currentTimeMillis();
254         results = cmsSearchBean.getSearchResult();
255         duration += System.currentTimeMillis();
256         echo("Search2: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
257
258         for (Iterator JavaDoc i = results.iterator(); i.hasNext();) {
259             CmsSearchResult res = (CmsSearchResult)i.next();
260             echo(res.getPath() + res.getExcerpt());
261         }
262
263         echo("With Permission check, without excerpt");
264         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.PERMISSIONS, CmsStringUtil.TRUE);
265         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.EXCERPT, CmsStringUtil.FALSE);
266
267         cmsSearchBean.setSearchPage(1);
268         duration = -System.currentTimeMillis();
269         results = cmsSearchBean.getSearchResult();
270         duration += System.currentTimeMillis();
271         echo("Search1: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
272
273         cmsSearchBean.setSearchPage(2);
274         duration = -System.currentTimeMillis();
275         results = cmsSearchBean.getSearchResult();
276         duration += System.currentTimeMillis();
277         echo("Search2: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
278
279         echo("Without Permission check, with excerpt");
280         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(
281             CmsSearchIndex.PERMISSIONS,
282             CmsStringUtil.FALSE);
283         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.EXCERPT, CmsStringUtil.TRUE);
284
285         cmsSearchBean.setSearchPage(1);
286         duration = -System.currentTimeMillis();
287         results = cmsSearchBean.getSearchResult();
288         duration += System.currentTimeMillis();
289         echo("Search1: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
290
291         cmsSearchBean.setSearchPage(2);
292         duration = -System.currentTimeMillis();
293         results = cmsSearchBean.getSearchResult();
294         duration += System.currentTimeMillis();
295         echo("Search2: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
296
297         echo("Without Permission check, without excerpt");
298         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(
299             CmsSearchIndex.PERMISSIONS,
300             CmsStringUtil.FALSE);
301         OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.EXCERPT, CmsStringUtil.FALSE);
302
303         cmsSearchBean.setSearchPage(1);
304         duration = -System.currentTimeMillis();
305         results = cmsSearchBean.getSearchResult();
306         duration += System.currentTimeMillis();
307         echo("Search1: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
308
309         cmsSearchBean.setSearchPage(2);
310         duration = -System.currentTimeMillis();
311         results = cmsSearchBean.getSearchResult();
312         duration += System.currentTimeMillis();
313         echo("Search2: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
314     }
315
316     /**
317      * Test the cms search indexer.<p>
318      *
319      * @throws Throwable if something goes wrong
320      */

321     public void testCmsSearchXmlContent() throws Throwable JavaDoc {
322
323         CmsObject cms = getCmsObject();
324         echo("Testing search for xml contents");
325
326         CmsSearch cmsSearchBean = new CmsSearch();
327         cmsSearchBean.init(cms);
328         cmsSearchBean.setIndex(INDEX_OFFLINE);
329         List JavaDoc results;
330
331         cmsSearchBean.setQuery(">>SearchEgg1<<");
332         results = cmsSearchBean.getSearchResult();
333         assertEquals(1, results.size());
334         assertEquals("/sites/default/xmlcontent/article_0001.html", ((CmsSearchResult)results.get(0)).getPath());
335
336         cmsSearchBean.setQuery(">>SearchEgg2<<");
337         results = cmsSearchBean.getSearchResult();
338         assertEquals(1, results.size());
339         assertEquals("/sites/default/xmlcontent/article_0002.html", ((CmsSearchResult)results.get(0)).getPath());
340
341         cmsSearchBean.setQuery(">>SearchEgg3<<");
342         results = cmsSearchBean.getSearchResult();
343         assertEquals(1, results.size());
344         assertEquals("/sites/default/xmlcontent/article_0003.html", ((CmsSearchResult)results.get(0)).getPath());
345     }
346
347     /**
348      * Tests index generation with different analyzers.<p>
349      *
350      * This test was added in order to verify proper generation of resource "root path" information
351      * in the index.
352      *
353      * @throws Throwable if something goes wrong
354      */

355     public void testIndexGeneration() throws Throwable JavaDoc {
356
357         CmsSearchIndex searchIndex = new CmsSearchIndex(INDEX_TEST);
358         searchIndex.setProjectName("Offline");
359         // important: use german locale for a special treat on term analyzing
360
searchIndex.setLocale(Locale.GERMAN.toString());
361         searchIndex.setRebuildMode(CmsSearchIndex.REBUILD_MODE_AUTO);
362         // available pre-configured in the test configuration files opencms-search.xml
363
searchIndex.addSourceName("source1");
364
365         // initialize the new index
366
searchIndex.initialize();
367
368         // add the search index to the manager
369
OpenCms.getSearchManager().addSearchIndex(searchIndex);
370
371         I_CmsReport report = new CmsShellReport(Locale.ENGLISH);
372         OpenCms.getSearchManager().rebuildAllIndexes(report);
373
374         // perform a search on the newly generated index
375
CmsSearch searchBean = new CmsSearch();
376         List JavaDoc searchResult;
377
378         searchBean.init(getCmsObject());
379         searchBean.setIndex(INDEX_TEST);
380         searchBean.setQuery(">>SearchEgg1<<");
381
382         // assert one file is found in the default site
383
searchResult = searchBean.getSearchResult();
384         assertEquals(1, searchResult.size());
385         assertEquals("/sites/default/xmlcontent/article_0001.html", ((CmsSearchResult)searchResult.get(0)).getPath());
386
387         // change seach root and assert no more files are found
388
searchBean.setSearchRoot("/folder1/");
389         searchResult = searchBean.getSearchResult();
390         assertEquals(0, searchResult.size());
391     }
392
393     /**
394      * Tests if <code>{@link CmsSearch#setQuery(String)}</code> modifies
395      * the query in an undesireable way (changes url encoded Strings). <p>
396      */

397     public void testQueryEncoding() {
398
399         // without encoding
400
String JavaDoc query = "Ölmühlmäher";
401         CmsSearch test = new CmsSearch();
402         test.setQuery(query);
403         assertEquals(query, test.getQuery());
404
405         // with encoding
406
query = CmsEncoder.encode(query, CmsEncoder.ENCODING_UTF_8);
407         test.setQuery(query);
408         assertEquals(query, test.getQuery());
409     }
410     
411     /**
412      * Tests an issue where no results are found in folders that have names
413      * like <code>/basisdienstleistungen_-_zka/</code>.<p>
414      *
415      * @throws Exception if the test fails
416      */

417     public void testSearchIssueWithSpecialFoldernames() throws Exception JavaDoc {
418         
419         CmsObject cms = getCmsObject();
420         echo("Testing search issue with special folder name");
421         
422         String JavaDoc folderName = "/basisdienstleistungen_-_zka/";
423         cms.copyResource("/types/", folderName);
424         cms.unlockProject(cms.getRequestContext().currentProject().getId());
425         cms.publishProject();
426         
427         CmsSearch cmsSearchBean = new CmsSearch();
428         cmsSearchBean.init(cms);
429         cmsSearchBean.setIndex(INDEX_TEST);
430         List JavaDoc results;
431
432         cmsSearchBean.setSearchRoot("/");
433         cmsSearchBean.setQuery("+Alkacon +OpenCms");
434         results = cmsSearchBean.getSearchResult();
435         printResults(results);
436         assertEquals(8, results.size());
437         assertEquals("/sites/default" + folderName + "text.txt", ((CmsSearchResult)results.get(0)).getPath());
438
439         cmsSearchBean.setSearchRoot(folderName);
440         cmsSearchBean.setQuery("+Alkacon +OpenCms");
441         results = cmsSearchBean.getSearchResult();
442         printResults(results);
443         assertEquals(1, results.size());
444         assertEquals("/sites/default" + folderName + "text.txt", ((CmsSearchResult)results.get(0)).getPath());
445     }
446     
447     /**
448      * Prints the result form the search to System.out.<p>
449      *
450      * @param results the result List to iterate
451      */

452     private void printResults(List JavaDoc results) {
453         
454         Iterator JavaDoc i = results.iterator();
455         int count = 0;
456         while (i.hasNext()) {
457             CmsSearchResult result = (CmsSearchResult)i.next();
458             System.out.println(++count + ": " + result.getPath() + " - " + result.getTitle());
459         }
460     }
461 }
Popular Tags