📄 testproperty.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestProperty.java,v $
* Date : $Date: 2006/07/20 09:53:57 $
* Version: $Revision: 1.24 $
*
* 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.file;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.main.CmsRuntimeException;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
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.24 $
*/
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("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 shure where we start
assertEquals("Sample Article 1", prop.getValue());
assertEquals("Sample Article 1", 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));
}
/**
* 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
* @throws Throwable if something goes wrong
*/
public static void createProperties(OpenCmsTestCase tc, CmsObject cms, String resource1, List propertyList1) throws Throwable {
tc.storeResources(cms, resource1);
long timestamp = System.currentTimeMillis();
cms.lockResource(resource1);
cms.writePropertyObjects(resource1, propertyList1);
cms.unlockResource(resource1);
// now evaluate the result
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY);
// project must be current project
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
// state must be "changed"
tc.assertState(cms, resource1, tc.getPreCalculatedState(resource1));
// date last modified must be after the test timestamp
tc.assertDateLastModifiedAfter(cms, resource1, timestamp);
// the user last modified must be the current user
tc.assertUserLastModified(cms, resource1, cms.getRequestContext().currentUser());
// the properties must be new
tc.assertPropertyNew(cms, resource1, propertyList1);
}
/**
* Test the writeProperty method to create one property.<p>
* @param tc the OpenCmsTestCase
* @param cms the CmsObject
* @param resource1 the resource to add a propery
* @param property1 the property to create
* @throws Throwable if something goes wrong
*/
public static void createProperty(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsProperty property1) throws Throwable {
tc.storeResources(cms, resource1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -