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

📄 testproperty.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestProperty.java,v $
 * Date   : $Date: 2007-08-13 16:29:56 $
 * Version: $Revision: 1.26 $
 *
 * 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.file;

import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceFilter;
import org.opencms.util.CmsStringUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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

/**
 * Unit test for the "writeProperty" method of the CmsObject.<p>
 * 
 * @author Michael Emmerich 
 * @version $Revision: 1.26 $
 */
public class TestProperty extends OpenCmsTestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestProperty(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(TestProperty.class.getName());

        suite.addTest(new TestProperty("testFrozenProperty"));
        suite.addTest(new TestProperty("testNullProperty"));
        suite.addTest(new TestProperty("testSharedPropertyIssue1"));
        suite.addTest(new TestProperty("testPropertyLists"));
        suite.addTest(new TestProperty("testPropertyMaps"));
        suite.addTest(new TestProperty("testWriteProperty"));
        suite.addTest(new TestProperty("testWriteProperties"));
        suite.addTest(new TestProperty("testRemoveProperty"));
        suite.addTest(new TestProperty("testRemoveProperties"));
        suite.addTest(new TestProperty("testCreateProperty"));
        suite.addTest(new TestProperty("testCreateProperties"));
        suite.addTest(new TestProperty("testWritePropertyOnFolder"));
        suite.addTest(new TestProperty("testDefaultPropertyCreation"));
        suite.addTest(new TestProperty("testCaseSensitiveProperties"));
        suite.addTest(new TestProperty("testReadResourcesWithProperty"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

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

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Tests reading and writing property lists.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testPropertyLists() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing reading and writing property lists");

        String source = "/xmlcontent/article_0001.html";
        cms.lockResource(source);

        CmsProperty prop;
        prop = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false);

        // basic asserts so we know for sure where we start
        assertEquals("Sample article 1  (>>SearchEgg1<<)", prop.getValue());
        assertEquals("Sample article 1  (>>SearchEgg1<<)", prop.getStructureValue());
        assertNull(prop.getResourceValue());

        // simple list asserts
        assertEquals(1, prop.getValueList().size());
        assertEquals(1, prop.getStructureValueList().size());
        assertNull(prop.getResourceValueList());

        // now set the title as a list        
        List list = new ArrayList();
        String value = "";
        for (int i = 1; i <= 10; i++) {
            String s = "Title " + i;
            list.add(s);
            value += s;
            if (i < 10) {
                value += CmsProperty.VALUE_LIST_DELIMITER;
            }
        }
        prop.setStructureValueList(list);

        // asserts on non-written property
        assertEquals(value, prop.getValue());
        assertEquals(value, prop.getStructureValue());
        assertNull(prop.getResourceValue());
        assertEquals(10, prop.getValueList().size());
        assertEquals(10, prop.getStructureValueList().size());
        assertNull(prop.getResourceValueList());
        list = prop.getValueList();
        for (int i = 1; i <= 10; i++) {
            String s = "Title " + i;
            assertEquals(s, list.get(i - 1).toString());
        }

        // write the property object
        cms.writePropertyObject(source, prop);

        // read and check the property
        CmsProperty prop2 = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false);

        // asserts on written property
        assertEquals(value, prop2.getValue());
        assertEquals(value, prop2.getStructureValue());
        assertNull(prop2.getResourceValue());
        assertEquals(10, prop2.getValueList().size());
        assertEquals(10, prop2.getStructureValueList().size());
        assertNull(prop2.getResourceValueList());
        list = prop2.getValueList();
        for (int i = 1; i <= 10; i++) {
            String s = "Title " + i;
            assertEquals(s, list.get(i - 1).toString());
        }

        // setting a list via the single string value
        prop.setStructureValue(null);
        value = "Test|Toast|Hi|Ho";
        prop.setValue(value, CmsProperty.TYPE_SHARED);

        assertEquals(value, prop.getValue());
        assertEquals(value, prop.getResourceValue());
        assertNull(prop.getStructureValue());
        assertEquals(4, prop.getValueList().size());
        assertEquals(4, prop.getResourceValueList().size());
        assertNull(prop.getStructureValueList());
        assertEquals("Test", prop.getResourceValueList().get(0));
        assertEquals("Toast", prop.getResourceValueList().get(1));
        assertEquals("Hi", prop.getResourceValueList().get(2));
        assertEquals("Ho", prop.getResourceValueList().get(3));

        // check the behaviour if a list entry contains a delimiter character
        list = new ArrayList(3);
        String s = "delimtest|1";
        list.add(s);
        list.add("othervalue");
        list.add("thirdvalue");
        prop.setStructureValueList(list);
        s = CmsStringUtil.substitute(
            s,
            String.valueOf(CmsProperty.VALUE_LIST_DELIMITER),
            CmsProperty.VALUE_LIST_DELIMITER_REPLACEMENT);
        s += CmsProperty.VALUE_LIST_DELIMITER + "othervalue" + CmsProperty.VALUE_LIST_DELIMITER + "thirdvalue";
        assertEquals(s, prop.getStructureValue());

    }

    /**
     * Tests reading and writing property maps.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testPropertyMaps() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing reading and writing property maps");

        String source = "/xmlcontent/article_0001.html";
        cms.lockResource(source);

        CmsProperty prop;
        prop = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false);

        // set the value to a simple key/value list
        prop.setValue("a=1|b=2", CmsProperty.TYPE_INDIVIDUAL);
        assertEquals(2, prop.getStructureValueMap().size());
        assertEquals("1", (String)prop.getStructureValueMap().get("a"));

        // now set the title as a map        
        Map map = new HashMap(3);
        map.put("k1", "v1");
        map.put("k2", "v2|=");
        map.put("k3", "v3");
        prop.setStructureValueMap(map);

        // write the property object
        cms.writePropertyObject(source, prop);

        // read and check the property
        CmsProperty prop2 = cms.readPropertyObject(source, CmsPropertyDefinition.PROPERTY_TITLE, false);

        // check the created map
        assertEquals(3, prop2.getStructureValueMap().size());
        assertEquals("v1", (String)prop2.getStructureValueMap().get("k1"));
        assertEquals("v2|=", (String)prop2.getStructureValueMap().get("k2"));
        assertEquals("v3", (String)prop2.getStructureValueMap().get("k3"));
        assertNull(prop.getResourceValueMap());

    }

    /**
     * Test the writeProperty method to create a list of properties.<p>
     * @param tc the OpenCmsTestCase
     * @param cms the CmsObject
     * @param resource1 the resource to create the properies
     * @param propertyList1 the properties to create

⌨️ 快捷键说明

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