📄 testcmssearchindocuments.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearchInDocuments.java,v $
* Date : $Date: 2007-08-20 13:06:59 $
* Version: $Revision: 1.15 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.search;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.types.CmsResourceTypeBinary;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.i18n.CmsEncoder;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.report.I_CmsReport;
import org.opencms.search.documents.A_CmsVfsDocument;
import org.opencms.search.fields.CmsSearchField;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit test for searching in extracted document text.<p>
*
* @author Alexander Kandzior
* @version $Revision: 1.15 $
*/
public class TestCmsSearchInDocuments extends OpenCmsTestCase {
/** Name of the index used for testing. */
public static final String INDEX_OFFLINE = "Offline project (VFS)";
/** The index used for testing. */
public static final String INDEX_ONLINE = "Online project (VFS)";
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestCmsSearchInDocuments(String arg0) {
super(arg0);
}
/**
* Test suite for this test class.<p>
*
* @return the test suite
*/
public static Test suite() {
OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
TestSuite suite = new TestSuite();
suite.setName(TestCmsSearchInDocuments.class.getName());
suite.addTest(new TestCmsSearchInDocuments("testSearchIndexGeneration"));
suite.addTest(new TestCmsSearchInDocuments("testSearchInDocuments"));
suite.addTest(new TestCmsSearchInDocuments("testExceptGeneration"));
suite.addTest(new TestCmsSearchInDocuments("testSearchBoost"));
suite.addTest(new TestCmsSearchInDocuments("testSearchBoostInMeta"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Tests search boosting.<p>
*
* @throws Exception if the test fails
*/
public void testSearchBoost() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing search boosting");
// perform a search on the newly generated index
CmsSearch searchBean = new CmsSearch();
List searchResult;
// count depend on the number of documents indexed
int expected = 6;
searchBean.init(cms);
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setSearchRoot("/search/");
searchBean.setQuery("OpenCms");
searchResult = searchBean.getSearchResult();
System.out.println("\n\n----- Results searching OFFLINE (no boost factors set)");
TestCmsSearch.printResults(searchResult, cms);
assertEquals(expected, searchResult.size());
CmsSearchResult res1 = (CmsSearchResult)searchResult.get(searchResult.size() - 1);
CmsSearchResult res2 = (CmsSearchResult)searchResult.get(searchResult.size() - 2);
CmsSearchResult res3 = (CmsSearchResult)searchResult.get(0);
String path1 = cms.getRequestContext().removeSiteRoot(res1.getPath());
String path2 = cms.getRequestContext().removeSiteRoot(res2.getPath());
String path3 = cms.getRequestContext().removeSiteRoot(res3.getPath());
CmsProperty maxBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
A_CmsVfsDocument.SEARCH_PRIORITY_MAX_VALUE,
null,
true);
CmsProperty highBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
A_CmsVfsDocument.SEARCH_PRIORITY_HIGH_VALUE,
null,
true);
CmsProperty lowBoost = new CmsProperty(
CmsPropertyDefinition.PROPERTY_SEARCH_PRIORITY,
A_CmsVfsDocument.SEARCH_PRIORITY_LOW_VALUE,
null,
true);
cms.lockResource(path1);
cms.writePropertyObject(path1, maxBoost);
cms.unlockResource(path1);
cms.lockResource(path2);
cms.writePropertyObject(path2, highBoost);
cms.unlockResource(path2);
cms.lockResource(path3);
cms.writePropertyObject(path3, lowBoost);
cms.unlockResource(path3);
// update the search indexes
I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
// this call does not throws the rebuild index event
OpenCms.getSearchManager().rebuildAllIndexes(report);
// perform the same search again in the online index - must be same result as before
searchBean.setIndex(INDEX_ONLINE);
searchBean.setQuery("OpenCms");
searchResult = searchBean.getSearchResult();
System.out.println("\n\n----- Results searching ONLINE (no boost factors set)");
TestCmsSearch.printResults(searchResult, cms);
assertEquals(expected, searchResult.size());
assertEquals(res1.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 1)).getPath());
assertEquals(res2.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 2)).getPath());
assertEquals(res3.getPath(), ((CmsSearchResult)searchResult.get(0)).getPath());
// now the search in the offline index - the boosted docs should now be on top
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setQuery("OpenCms");
searchResult = searchBean.getSearchResult();
System.out.println("\n\n----- Results searching OFFLINE (using changed boost factors)");
TestCmsSearch.printResults(searchResult, cms);
assertEquals(expected, searchResult.size());
// ensure boosted results are on top
assertEquals(res1.getPath(), ((CmsSearchResult)searchResult.get(0)).getPath());
assertEquals(res2.getPath(), ((CmsSearchResult)searchResult.get(1)).getPath());
// low boosted document should be on last position
assertEquals(res3.getPath(), ((CmsSearchResult)searchResult.get(searchResult.size() - 1)).getPath());
}
/**
* Tests search boosting when seachrching in meta information only.<p>
*
* @throws Exception if the test fails
*/
public void testSearchBoostInMeta() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing search boosting in meta information");
// perform a search on the newly generated index
CmsSearch searchBean = new CmsSearch();
List searchResult;
// count depend on the number of documents indexed
int expected = 6;
String path = "/search/";
String query = "OpenCms by Alkacon";
searchBean.init(cms);
searchBean.setIndex(INDEX_OFFLINE);
searchBean.setSearchRoot(path);
searchBean.setQuery(query);
// ensure only meta information is searched
searchBean.setField(new String[] {CmsSearchField.FIELD_META});
searchResult = searchBean.getSearchResult();
// since no resource has any description, no results should be found
System.out.println("\n\n----- No results should be displayed below");
TestCmsSearch.printResults(searchResult, cms);
assertEquals(0, searchResult.size());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -