?? testcreatewriteresource.java
字號:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestCreateWriteResource.java,v $
* Date : $Date: 2005/06/28 20:40:34 $
* Version: $Revision: 1.19 $
*
* 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.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceConfigurableFilter;
import org.opencms.test.OpenCmsTestResourceFilter;
import org.opencms.util.CmsUUID;
import java.util.ArrayList;
import java.util.List;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit tests for the create and import methods.<p>
*
* @author Alexander Kandzior
* @version $Revision: 1.19 $
*/
public class TestCreateWriteResource extends OpenCmsTestCase {
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestCreateWriteResource(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(TestCreateWriteResource.class.getName());
suite.addTest(new TestCreateWriteResource("testImportResource"));
suite.addTest(new TestCreateWriteResource("testImportResourceAgain"));
suite.addTest(new TestCreateWriteResource("testImportSibling"));
suite.addTest(new TestCreateWriteResource("testImportFolder"));
suite.addTest(new TestCreateWriteResource("testImportFolderAgain"));
suite.addTest(new TestCreateWriteResource("testCreateResource"));
suite.addTest(new TestCreateWriteResource("testCreateResourceAgain"));
suite.addTest(new TestCreateWriteResource("testCreateFolder"));
suite.addTest(new TestCreateWriteResource("testCreateFolderAgain"));
suite.addTest(new TestCreateWriteResource("testCreateDotnameResources"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
// removeOpenCms();
}
};
return wrapper;
}
/**
* Test creation of invalid resources that have only dots in their name.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCreateDotnameResources() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing creating a resource with only dots in the name");
Exception error = null;
try {
cms.createResource("/folder1/.", CmsResourceTypeFolder.getStaticTypeId(), null, null);
} catch (CmsIllegalArgumentException e) {
assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
error = e;
}
assertNotNull(error);
error = null;
try {
cms.createResource("/folder1/..", CmsResourceTypePlain.getStaticTypeId(), null, null);
} catch (CmsIllegalArgumentException e) {
assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
error = e;
}
assertNotNull(error);
error = null;
try {
cms.createResource("/folder1/.../", CmsResourceTypeFolder.getStaticTypeId(), null, null);
} catch (CmsIllegalArgumentException e) {
assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
error = e;
}
assertNotNull(error);
error = null;
try {
cms.createResource("/folder1/....", CmsResourceTypePlain.getStaticTypeId(), null, null);
} catch (CmsIllegalArgumentException e) {
assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
error = e;
}
assertNotNull(error);
error = null;
try {
cms.createResource("/folder1/...../", CmsResourceTypeFolder.getStaticTypeId(), null, null);
} catch (CmsIllegalArgumentException e) {
assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
error = e;
}
assertNotNull(error);
}
/**
* Test the create resource method for a folder.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCreateFolder() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing creating a folder");
String resourcename = "/folder1/test2/";
long timestamp = System.currentTimeMillis()-1;
cms.createResource(resourcename, CmsResourceTypeFolder.getStaticTypeId(), null, null);
// check the created folder
CmsFolder folder = cms.readFolder(resourcename);
assertEquals(folder.getState(), CmsResource.STATE_NEW);
assertTrue(folder.getDateLastModified() > timestamp);
assertTrue(folder.getDateCreated() > timestamp);
// ensure created resource is a folder
assertIsFolder(cms, resourcename);
// project must be current project
assertProject(cms, resourcename, cms.getRequestContext().currentProject());
// state must be "new"
assertState(cms, resourcename, CmsResource.STATE_NEW);
// date last modified
assertDateLastModifiedAfter(cms, resourcename, timestamp);
// date created
assertDateCreatedAfter(cms, resourcename, timestamp);
// the user last modified must be the current user
assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getId());
cms.publishProject();
assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
}
/**
* Test the create a folder again.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCreateFolderAgain() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing to create an existing folder again");
String resourcename = "/folder1/test2/";
storeResources(cms, resourcename);
long timestamp = System.currentTimeMillis();
assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
cms.lockResource(resourcename);
try {
// resource exists and is not deleted, creation must thrw exception
cms.createResource(resourcename, CmsResourceTypeFolder.getStaticTypeId(), null, null);
} catch (CmsVfsException e) {
if (!(e instanceof CmsVfsResourceAlreadyExistsException)) {
fail("Existing resource '" + resourcename + "' was not detected!");
}
}
// read resource for comparing id's later
CmsResource original = cms.readResource(resourcename);
// delete resource and try again
cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.createResource(resourcename, CmsResourceTypeFolder.getStaticTypeId(), null, null);
// ensure created resource is a folder
assertIsFolder(cms, resourcename);
// project must be current project
assertProject(cms, resourcename, cms.getRequestContext().currentProject());
// state must be "changed"
assertState(cms, resourcename, CmsResource.STATE_CHANGED);
// date last modified
assertDateLastModifiedAfter(cms, resourcename, timestamp);
// the user last modified must be the current user
assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
// date created
assertDateCreated(cms, resourcename, original.getDateCreated());
// the user created must be the current user
assertUserCreated(cms, resourcename, cms.readUser(original.getUserCreated()));
// compare id's
CmsResource created = cms.readResource(resourcename);
if (! created.getResourceId().equals(original.getResourceId())) {
fail("A created folder that replaced a deleted folder must have the same resource id!");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -