?? testcmssearch.java
字號(hào):
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearch.java,v $
* Date : $Date: 2006/03/27 14:52:51 $
* Version: $Revision: 1.16 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 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.types.CmsResourceTypeBinary;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.i18n.CmsEncoder;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.report.I_CmsReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsStringUtil;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit test for the cms search indexer.<p>
*
* @author Carsten Weinholz
* @version $Revision: 1.16 $
*/
public class TestCmsSearch extends OpenCmsTestCase {
/** Name of the index used for testing. */
public static final String INDEX_OFFLINE = "Offline project (VFS)";
/** Name of the search index created using API. */
public static final String INDEX_TEST = "Test new index";
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestCmsSearch(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(TestCmsSearch.class.getName());
suite.addTest(new TestCmsSearch("testCmsSearchIndexer"));
suite.addTest(new TestCmsSearch("testCmsSearchDocumentTypes"));
suite.addTest(new TestCmsSearch("testCmsSearchXmlContent"));
suite.addTest(new TestCmsSearch("testIndexGeneration"));
suite.addTest(new TestCmsSearch("testQueryEncoding"));
suite.addTest(new TestCmsSearch("testSearchIssueWithSpecialFoldernames"));
// This test is intended only for performance/resource monitoring
// suite.addTest(new TestCmsSearch("testCmsSearchLargeResult"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Tests searching in various document types.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCmsSearchDocumentTypes() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing search for various document types");
CmsSearch cmsSearchBean = new CmsSearch();
cmsSearchBean.init(cms);
cmsSearchBean.setIndex(INDEX_OFFLINE);
cmsSearchBean.setSearchRoot("/types/");
List results;
cmsSearchBean.setQuery("+Alkacon +OpenCms +Text");
results = cmsSearchBean.getSearchResult();
assertEquals(1, results.size());
assertEquals("/sites/default/types/text.txt", ((CmsSearchResult)results.get(0)).getPath());
}
/**
* Test the cms search indexer.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCmsSearchIndexer() throws Throwable {
I_CmsReport report = new CmsShellReport(Locale.ENGLISH);
OpenCms.getSearchManager().rebuildAllIndexes(report);
}
/**
* Tests the cms search with a larger result set.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCmsSearchLargeResult() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing search with large result set");
// create test folder
cms.createResource("/test/", CmsResourceTypeFolder.RESOURCE_TYPE_ID, null, null);
cms.unlockResource("/test/");
// create master resource
importTestResource(
cms,
"org/opencms/search/pdf-test-112.pdf",
"/test/master.pdf",
CmsResourceTypeBinary.getStaticTypeId(),
Collections.EMPTY_LIST);
// create a copy
cms.copyResource("/test/master.pdf", "/test/copy.pdf");
cms.chacc("/test/copy.pdf", "group", "Users", "-r");
// create siblings
for (int i = 0; i < 100; i++) {
cms.createSibling("/test/master.pdf", "/test/sibling" + i + ".pdf", null);
}
// publish the project and update the search index
I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, report);
// search for "pdf"
CmsSearch cmsSearchBean = new CmsSearch();
cmsSearchBean.init(cms);
cmsSearchBean.setIndex(INDEX_OFFLINE);
List results;
cms.createUser("test", "test", "", null);
cms.loginUser("test", "test");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cmsSearchBean.setQuery("pdf");
echo("With Permission check, with excerpt");
OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.PERMISSIONS, CmsStringUtil.TRUE);
OpenCms.getSearchManager().getIndex(INDEX_OFFLINE).addConfigurationParameter(CmsSearchIndex.EXCERPT, CmsStringUtil.TRUE);
cmsSearchBean.setSearchPage(1);
long duration = -System.currentTimeMillis();
results = cmsSearchBean.getSearchResult();
duration += System.currentTimeMillis();
echo("Search1: " + cmsSearchBean.getSearchResultCount() + " results found, total duration: " + duration + " ms");
for (Iterator i = results.iterator(); i.hasNext();) {
CmsSearchResult res = (CmsSearchResult)i.next();
echo(res.getPath() + res.getExcerpt());
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -