⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcmssearchadvancedfeatures.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/search/TestCmsSearchAdvancedFeatures.java,v $
 * Date   : $Date: 2007-08-23 12:42:18 $
 * Version: $Revision: 1.13 $
 *
 * 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.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.report.I_CmsReport;
import org.opencms.search.fields.CmsSearchField;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Unit test for advanced search features.<p>
 * 
 * @author Alexander Kandzior 
 * @version $Revision: 1.13 $
 */
public class TestCmsSearchAdvancedFeatures 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 TestCmsSearchAdvancedFeatures(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(TestCmsSearchAdvancedFeatures.class.getName());

        suite.addTest(new TestCmsSearchAdvancedFeatures("testSortSearchResults"));
        suite.addTest(new TestCmsSearchAdvancedFeatures("testSearchCategories"));
        suite.addTest(new TestCmsSearchAdvancedFeatures("testMultipleSearchRoots"));
        suite.addTest(new TestCmsSearchAdvancedFeatures("testSearchRestriction"));
        suite.addTest(new TestCmsSearchAdvancedFeatures("testLimitTimeRanges"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

                setupOpenCms("simpletest", "/sites/default/");
            }

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Tests searching with limiting the time ranges.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testLimitTimeRanges() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing searching with limiting to time ranges");

        CmsSearch searchBean = new CmsSearch();
        List searchResult;
        String query = "OpenCms";

        searchBean.init(cms);
        searchBean.setIndex(INDEX_OFFLINE);
        searchBean.setMatchesPerPage(1000);
        searchBean.setQuery(query);

        searchResult = searchBean.getSearchResult();
        int orgCount = searchResult.size();

        // check min date created
        Date stamp = new Date();
        searchBean.getParameters().setMinDateCreated(stamp.getTime());

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(0, searchResult.size());

        String resName = "search_new.txt";
        cms.createResource(resName, CmsResourceTypePlain.getStaticTypeId(), "OpenCms".getBytes(), null);
        I_CmsReport report = new CmsShellReport(cms.getRequestContext().getLocale());
        OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, report);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(1, searchResult.size());

        // check max date created
        searchBean.getParameters().setMaxDateCreated(stamp.getTime()-1000);
        searchBean.getParameters().setMinDateCreated(Long.MIN_VALUE);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(orgCount, searchResult.size());

        searchBean.getParameters().setMaxDateCreated(Long.MAX_VALUE);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(orgCount + 1, searchResult.size());

        // check min date last modified
        stamp = new Date();
        searchBean.getParameters().setMinDateLastModified(stamp.getTime());
        
        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(0, searchResult.size());
        
        CmsFile file = cms.readFile(resName);
        file.setContents("OpenCms ist toll".getBytes());
        cms.writeFile(file);
        report = new CmsShellReport(cms.getRequestContext().getLocale());
        OpenCms.getSearchManager().rebuildIndex(INDEX_OFFLINE, report);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(1, searchResult.size());

        // check max date last modified
        searchBean.getParameters().setMaxDateLastModified(stamp.getTime()-1000);
        searchBean.getParameters().setMinDateLastModified(Long.MIN_VALUE);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(orgCount, searchResult.size());

        searchBean.getParameters().setMaxDateLastModified(Long.MAX_VALUE);

        searchBean.init(cms);
        searchResult = searchBean.getSearchResult();
        assertEquals(orgCount + 1, searchResult.size());
    }

    /**
     * Tests searching with multiple search roots.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testMultipleSearchRoots() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing searching with multiple search roots");

        CmsSearch searchBean = new CmsSearch();
        List searchResult;
        String query = "OpenCms";

        searchBean.init(cms);
        searchBean.setIndex(INDEX_OFFLINE);
        searchBean.setMatchesPerPage(1000);
        searchBean.setQuery(query);

        String[][] roots = new String[][] {
            new String[] {"/folder1/"},
            new String[] {"/folder2/"},
            new String[] {"/types/"},
            new String[] {"/folder2/", "/types/"},
            new String[] {"/folder1/", "/types/"},
            new String[] {"/folder1/", "/folder2/"},
            new String[] {"/folder1/", "/folder2/", "/types/"}};

        int[] expected = new int[] {7, 4, 1, 5, 8, 11, 12};

        for (int i = 0; i < expected.length; i++) {
            int expect = expected[i];
            String[] rootList = roots[i];
            searchBean.setSearchRoots(rootList);
            searchResult = searchBean.getSearchResult();
            System.out.println("Result for search "
                + i
                + " (found "
                + searchResult.size()
                + ", expected "
                + expect
                + ")");
            TestCmsSearch.printResults(searchResult, cms);
            assertEquals(expect, searchResult.size());
        }
    }

    /**
     * Tests search category grouping.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testSearchCategories() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing searching for categories");

        // perform a search on the newly generated index
        CmsSearch searchBean = new CmsSearch();
        List searchResult;
        String query = "OpenCms";

        // apply search categories to some folders

        CmsProperty cat1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_1", null, true);
        CmsProperty cat2 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_2", null, true);
        CmsProperty cat3 = new CmsProperty(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, "category_3", null, true);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -