KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearchAdvancedFeatures.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.10 $
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.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.main.OpenCms;
38 import org.opencms.report.CmsShellReport;
39 import org.opencms.test.OpenCmsTestCase;
40 import org.opencms.test.OpenCmsTestProperties;
41 import org.opencms.util.CmsDateUtil;
42 import org.opencms.util.CmsStringUtil;
43
44 import java.util.Arrays JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import junit.extensions.TestSetup;
50 import junit.framework.Test;
51 import junit.framework.TestSuite;
52
53 /**
54  * Unit test for advanced search features.<p>
55  *
56  * @author Alexander Kandzior
57  * @version $Revision: 1.10 $
58  */

59 public class TestCmsSearchAdvancedFeatures extends OpenCmsTestCase {
60
61     /** Name of the index used for testing. */
62     public static final String JavaDoc INDEX_OFFLINE = "Offline project (VFS)";
63
64     /** The index used for testing. */
65     public static final String JavaDoc INDEX_ONLINE = "Online project (VFS)";
66
67     /**
68      * Default JUnit constructor.<p>
69      *
70      * @param arg0 JUnit parameters
71      */

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

82     public static Test suite() {
83
84         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
85
86         TestSuite suite = new TestSuite();
87         suite.setName(TestCmsSearchAdvancedFeatures.class.getName());
88
89         suite.addTest(new TestCmsSearchAdvancedFeatures("testSortSearchResults"));
90         suite.addTest(new TestCmsSearchAdvancedFeatures("testSearchCategories"));
91         suite.addTest(new TestCmsSearchAdvancedFeatures("testMultipleSearchRoots"));
92         suite.addTest(new TestCmsSearchAdvancedFeatures("testSearchRestriction"));
93
94         TestSetup wrapper = new TestSetup(suite) {
95
96             protected void setUp() {
97
98                 setupOpenCms("simpletest", "/sites/default/");
99             }
100
101             protected void tearDown() {
102
103                 removeOpenCms();
104             }
105         };
106
107         return wrapper;
108     }
109
110     /**
111      * Tests searching with multiple search roots.<p>
112      *
113      * @throws Exception if the test fails
114      */

115     public void testMultipleSearchRoots() throws Exception JavaDoc {
116
117         CmsObject cms = getCmsObject();
118         echo("Testing searching with multiple search roots");
119
120         CmsSearch searchBean = new CmsSearch();
121         List JavaDoc searchResult;
122         String JavaDoc query = "OpenCms";
123
124         searchBean.init(cms);
125         searchBean.setIndex(INDEX_OFFLINE);
126         searchBean.setMatchesPerPage(1000);
127         searchBean.setQuery(query);
128
129         String JavaDoc[][] roots = new String JavaDoc[][] {
130             new String JavaDoc[] {"/folder1/"},
131             new String JavaDoc[] {"/folder2/"},
132             new String JavaDoc[] {"/types/"},
133             new String JavaDoc[] {"/folder2/", "/types/"},
134             new String JavaDoc[] {"/folder1/", "/types/"},
135             new String JavaDoc[] {"/folder1/", "/folder2/"},
136             new String JavaDoc[] {"/folder1/", "/folder2/", "/types/"}};
137
138         int[] expected = new int[] {7, 4, 1, 5, 8, 11, 12};
139
140         for (int i = 0; i < expected.length; i++) {
141             int expect = expected[i];
142             String JavaDoc[] rootList = roots[i];
143             searchBean.setSearchRoots(rootList);
144             searchResult = searchBean.getSearchResult();
145             Iterator JavaDoc j = searchResult.iterator();
146             System.out.println("Result for search "
147                 + i
148                 + " (found "
149                 + searchResult.size()
150                 + ", expected "
151                 + expect
152                 + ")");
153             while (j.hasNext()) {
154                 CmsSearchResult res = (CmsSearchResult)j.next();
155                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
156                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
157                 System.out.println(" score: " + res.getScore());
158             }
159             assertEquals(expect, searchResult.size());
160         }
161     }
162
163     /**
164      * Tests search category grouping.<p>
165      *
166      * @throws Exception if the test fails
167      */

168     public void testSearchCategories() throws Exception JavaDoc {
169
170         CmsObject cms = getCmsObject();
171         echo("Testing searching for categories");
172
173         // perform a search on the newly generated index
174
CmsSearch searchBean = new CmsSearch();
175         List JavaDoc searchResult;
176         String JavaDoc query = "OpenCms";
177
178         // apply search categories to some folders
179

180         CmsProperty cat1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_1", null, true);
181         CmsProperty cat2 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_2", null, true);
182         CmsProperty cat3 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_3", null, true);
183
184         cms.lockResource("/folder1/");
185         cms.writePropertyObject("/folder1/", cat1);
186         cms.unlockResource("/folder1/");
187         cms.lockResource("/folder2/");
188         cms.writePropertyObject("/folder2/", cat2);
189         cms.unlockResource("/folder2/");
190         cms.lockResource("/types/");
191         cms.writePropertyObject("/types/", cat3);
192         cms.unlockResource("/types/");
193
194         // update the search index used
195
OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, new CmsShellReport(cms.getRequestContext().getLocale()));
196
197         searchBean.init(cms);
198         searchBean.setIndex(INDEX_OFFLINE);
199         searchBean.setQuery(query);
200         searchBean.setMatchesPerPage(1000);
201         searchBean.setCalculateCategories(true);
202
203         // first run is default sort order
204
searchResult = searchBean.getSearchResult();
205         Iterator JavaDoc i = searchResult.iterator();
206         System.out.println("Result sorted by relevance:");
207         while (i.hasNext()) {
208             CmsSearchResult res = (CmsSearchResult)i.next();
209             System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
210             System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
211             System.out.println(" score: " + res.getScore());
212         }
213
214         Map JavaDoc categories = searchBean.getSearchResultCategories();
215         // make sure categories where found
216
assertNotNull(categories);
217         // print the categories
218
System.out.println(CmsSearchCategoryCollector.formatCategoryMap(categories));
219         // make sure the results are as expected
220
assertTrue(categories.containsKey(cat1.getValue()));
221         assertTrue(categories.containsKey(cat2.getValue()));
222         assertTrue(categories.containsKey(cat3.getValue()));
223         assertTrue(categories.containsKey(CmsSearchCategoryCollector.UNKNOWN_CATEGORY));
224         // result must be all 3 categories plus 1 for "unknown"
225
assertEquals(4, categories.size());
226         // assert result count
227
assertEquals(new Integer JavaDoc(7), categories.get(cat1.getValue()));
228         assertEquals(new Integer JavaDoc(4), categories.get(cat2.getValue()));
229         assertEquals(new Integer JavaDoc(1), categories.get(cat3.getValue()));
230         assertEquals(new Integer JavaDoc(1), categories.get(CmsSearchCategoryCollector.UNKNOWN_CATEGORY));
231
232         // count the category results
233
searchBean.setCalculateCategories(false);
234
235         String JavaDoc[][] cats = new String JavaDoc[][] {
236             new String JavaDoc[] {cat1.getValue()},
237             new String JavaDoc[] {cat2.getValue()},
238             new String JavaDoc[] {cat3.getValue()},
239             new String JavaDoc[] {cat1.getValue(), cat3.getValue()},
240             new String JavaDoc[] {cat2.getValue(), cat3.getValue()},
241             new String JavaDoc[] {cat1.getValue(), cat2.getValue()},
242             new String JavaDoc[] {cat1.getValue(), cat2.getValue(), cat3.getValue()}};
243
244         int[] expected = new int[] {7, 4, 1, 8, 5, 11, 12};
245
246         for (int k = 0; k < expected.length; k++) {
247             int expect = expected[k];
248             String JavaDoc[] catList = cats[k];
249             searchBean.setCategories(catList);
250             searchResult = searchBean.getSearchResult();
251             Iterator JavaDoc j = searchResult.iterator();
252             System.out.println("Result for search "
253                 + k
254                 + " (found "
255                 + searchResult.size()
256                 + ", expected "
257                 + expect
258                 + ")");
259             while (j.hasNext()) {
260                 CmsSearchResult res = (CmsSearchResult)j.next();
261                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
262                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
263                 System.out.println(" score: " + res.getScore());
264             }
265             assertEquals(expect, searchResult.size());
266         }
267     }
268
269     /**
270      * Tests searching with restrictions.<p>
271      *
272      * @throws Exception if the test fails
273      */

274     public void testSearchRestriction() throws Exception JavaDoc {
275
276         CmsObject cms = getCmsObject();
277         echo("Testing searching in search results");
278
279         CmsSearch searchBean = new CmsSearch();
280         List JavaDoc searchResult;
281         String JavaDoc query = "OpenCms";
282
283         searchBean.init(cms);
284         searchBean.setIndex(INDEX_OFFLINE);
285         searchBean.setMatchesPerPage(1000);
286         searchBean.setQuery(query);
287
288         // first part of the rest is identical to "testMultipleSearchRoots()"
289
String JavaDoc[][] roots = new String JavaDoc[][] {
290             new String JavaDoc[] {"/folder1/"},
291             new String JavaDoc[] {"/folder2/"},
292             new String JavaDoc[] {"/types/"},
293             new String JavaDoc[] {"/folder2/", "/types/"},
294             new String JavaDoc[] {"/folder1/", "/types/"},
295             new String JavaDoc[] {"/folder1/", "/folder2/"},
296             new String JavaDoc[] {"/folder1/", "/folder2/", "/types/"}};
297
298         int[] expected = new int[] {7, 4, 1, 5, 8, 11, 12};
299
300         for (int i = 0; i < expected.length; i++) {
301             int expect = expected[i];
302             String JavaDoc[] rootList = roots[i];
303             searchBean.setSearchRoots(rootList);
304             searchResult = searchBean.getSearchResult();
305             Iterator JavaDoc j = searchResult.iterator();
306             System.out.println("Result for search "
307                 + i
308                 + " (found "
309                 + searchResult.size()
310                 + ", expected "
311                 + expect
312                 + ")");
313             while (j.hasNext()) {
314                 CmsSearchResult res = (CmsSearchResult)j.next();
315                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
316                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
317                 System.out.println(" score: " + res.getScore());
318             }
319             assertEquals(expect, searchResult.size());
320         }
321
322         // now create a restriction to search for an additional "Alkacon" (effectivly searching for "OpenCms Alkacon")
323
CmsSearchParameters restriction;
324         restriction = new CmsSearchParameters("Alkacon", null, null, null, false, null);
325
326         expected = new int[] {3, 2, 1, 3, 4, 5, 6};
327
328         for (int i = 0; i < expected.length; i++) {
329             int expect = expected[i];
330             String JavaDoc[] rootList = roots[i];
331             searchBean.setSearchRoots(rootList);
332             searchBean.setResultRestriction(restriction);
333             searchResult = searchBean.getSearchResult();
334             Iterator JavaDoc j = searchResult.iterator();
335             System.out.println("Result for search "
336                 + i
337                 + " (found "
338                 + searchResult.size()
339                 + ", expected "
340                 + expect
341                 + ")");
342             while (j.hasNext()) {
343                 CmsSearchResult res = (CmsSearchResult)j.next();
344                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
345                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
346                 System.out.println(" score: " + res.getScore());
347             }
348             assertEquals(expect, searchResult.size());
349         }
350
351         // another run of tests using searching only in the "meta" field
352
restriction = new CmsSearchParameters("Alkacon", Arrays.asList(new String JavaDoc[] {"meta"}), null, null, false, null);
353
354         expected = new int[] {0, 0, 1, 1, 1, 0, 1};
355
356         for (int i = 0; i < expected.length; i++) {
357             int expect = expected[i];
358             String JavaDoc[] rootList = roots[i];
359             searchBean.setSearchRoots(rootList);
360             searchBean.setResultRestriction(restriction);
361             searchResult = searchBean.getSearchResult();
362             Iterator JavaDoc j = searchResult.iterator();
363             System.out.println("Result for search "
364                 + i
365                 + " (found "
366                 + searchResult.size()
367                 + ", expected "
368                 + expect
369                 + ")");
370             while (j.hasNext()) {
371                 CmsSearchResult res = (CmsSearchResult)j.next();
372                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
373                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
374                 System.out.println(" score: " + res.getScore());
375             }
376             assertEquals(expect, searchResult.size());
377         }
378
379         // another run of tests using categories that have been defined in "testSearchCategories()"
380
// reset search bean settings (restriction changed them) to initial values (see start of method)
381
searchBean = new CmsSearch();
382         searchBean.init(cms);
383         searchBean.setIndex(INDEX_OFFLINE);
384         searchBean.setMatchesPerPage(1000);
385         searchBean.setQuery(query);
386         restriction = new CmsSearchParameters(
387             null,
388             null,
389             null,
390             Arrays.asList(new String JavaDoc[] {"category_1", "category_3"}),
391             false,
392             null);
393
394         expected = new int[] {7, 0, 1, 1, 8, 7, 8};
395
396         for (int i = 0; i < expected.length; i++) {
397             int expect = expected[i];
398             String JavaDoc[] rootList = roots[i];
399             searchBean.setSearchRoots(rootList);
400             searchBean.setResultRestriction(restriction);
401             searchResult = searchBean.getSearchResult();
402             Iterator JavaDoc j = searchResult.iterator();
403             System.out.println("Result for search "
404                 + i
405                 + " (found "
406                 + searchResult.size()
407                 + ", expected "
408                 + expect
409                 + ")");
410             while (j.hasNext()) {
411                 CmsSearchResult res = (CmsSearchResult)j.next();
412                 System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
413                 System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
414                 System.out.println(" score: " + res.getScore());
415             }
416             assertEquals(expect, searchResult.size());
417         }
418     }
419
420     /**
421      * Tests sorting of search results.<p>
422      *
423      * @throws Exception if the test fails
424      */

425     public void testSortSearchResults() throws Exception JavaDoc {
426
427         CmsObject cms = getCmsObject();
428         echo("Testing sorting of search results");
429
430         // perform a search on the newly generated index
431
CmsSearch searchBean = new CmsSearch();
432         List JavaDoc searchResult;
433         String JavaDoc query = "OpenCms";
434
435         // update the search index used
436
OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, new CmsShellReport(cms.getRequestContext().getLocale()));
437
438         searchBean.init(cms);
439         searchBean.setIndex(INDEX_OFFLINE);
440         searchBean.setQuery(query);
441
442         // first run is default sort order
443
searchResult = searchBean.getSearchResult();
444         Iterator JavaDoc i = searchResult.iterator();
445         System.out.println("Result sorted by relevance:");
446         while (i.hasNext()) {
447             CmsSearchResult res = (CmsSearchResult)i.next();
448             System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
449             System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
450             System.out.print(CmsDateUtil.getHeaderDate(res.getDateLastModified().getTime()));
451             System.out.println(" score: " + res.getScore());
452         }
453
454         // second run use Title sort order
455
String JavaDoc lastTitle = null;
456         searchBean.setSortOrder(CmsSearchParameters.SORT_TITLE);
457         searchResult = searchBean.getSearchResult();
458         i = searchResult.iterator();
459         System.out.println("Result sorted by title:");
460         while (i.hasNext()) {
461             CmsSearchResult res = (CmsSearchResult)i.next();
462             System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
463             System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
464             System.out.print(CmsDateUtil.getHeaderDate(res.getDateLastModified().getTime()));
465             System.out.println(" score: " + res.getScore());
466             if (lastTitle != null) {
467                 // make sure result is sorted correctly
468
assertTrue(lastTitle.compareTo(res.getTitle()) <= 0);
469             }
470             lastTitle = res.getTitle();
471         }
472
473         // third run use date last modified
474
long lastTime = 0;
475         searchBean.setSortOrder(CmsSearchParameters.SORT_DATE_LASTMODIFIED);
476         searchResult = searchBean.getSearchResult();
477         i = searchResult.iterator();
478         System.out.println("Result sorted by date last modified:");
479         while (i.hasNext()) {
480             CmsSearchResult res = (CmsSearchResult)i.next();
481             System.out.print(CmsStringUtil.padRight(cms.getRequestContext().removeSiteRoot(res.getPath()), 50));
482             System.out.print(CmsStringUtil.padRight(res.getTitle(), 40));
483             System.out.print(CmsDateUtil.getHeaderDate(res.getDateLastModified().getTime()));
484             System.out.println(" score: " + res.getScore());
485             if (lastTime > 0) {
486                 // make sure result is sorted correctly
487
assertTrue(lastTime >= res.getDateLastModified().getTime());
488                 assertTrue(res.getScore() <= 100);
489             }
490             lastTime = res.getDateLastModified().getTime();
491         }
492
493         assertNull(searchBean.getSearchResultCategories());
494     }
495 }
Popular Tags