📄 testmoverename2.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestMoveRename2.java,v $
* Date : $Date: 2007-09-07 12:02:46 $
* Version: $Revision: 1.5 $
*
* 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.db.CmsResourceState;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.lock.CmsLockFilter;
import org.opencms.lock.CmsLockType;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.security.I_CmsPrincipal;
import org.opencms.staticexport.CmsLinkTable;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceFilter;
import org.opencms.xml.page.CmsXmlPage;
import org.opencms.xml.page.CmsXmlPageFactory;
import java.util.Locale;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit tests for move/rename operation.<p>
*
* @author Alexander Kandzior
* @author Michael Moossen
*
* @version $Revision: 1.5 $
*/
public class TestMoveRename2 extends OpenCmsTestCase {
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestMoveRename2(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(TestMoveRename2.class.getName());
suite.addTest(new TestMoveRename2("testMoveSibling"));
suite.addTest(new TestMoveRename2("testRenameNewFolder"));
suite.addTest(new TestMoveRename2("testRenameFileUpperLowerCase"));
suite.addTest(new TestMoveRename2("testRenameFolderUpperLowerCase"));
suite.addTest(new TestMoveRename2("testMoveLostAndFound"));
suite.addTest(new TestMoveRename2("testMoveOverDeleted"));
suite.addTest(new TestMoveRename2("testMoveFolderWithNewResource"));
suite.addTest(new TestMoveRename2("testPublishMovedDeletedFolderWithMovedResource"));
suite.addTest(new TestMoveRename2("testMoveFolderWithPermissionCheck"));
suite.addTest(new TestMoveRename2("testMoveFolderWithInvisibleResources"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Tests to move a folder structure with invisible resources inside.<p>
*
* @throws Exception if the test fails
*/
public void testMoveFolderWithInvisibleResources() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing to move a folder structure with invisible resources inside");
// Creating paths
String folder = "/mytestfolder_moved/";
String folder_moved = "/mytestfolder_moved2/";
String file = "index.html";
cms.lockResource(folder + file);
cms.chacc(folder + file, I_CmsPrincipal.PRINCIPAL_USER, "test2", "-r+v+i");
cms.unlockResource(folder + file);
// lock the whole folder as test2
cms.loginUser("test2", "test2");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.lockResource(folder);
// move the folder
cms.moveResource(folder, folder_moved);
// check lock
assertFalse(cms.getLockedResources("/", CmsLockFilter.FILTER_ALL).contains(folder));
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
assertTrue(cms.existsResource(folder_moved + file));
}
/**
* Tests renaming a folder containing a new resource.<p>
*
* @throws Throwable if something goes wrong
*/
public void testMoveFolderWithNewResource() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing rename a new folder with content");
String folder = "/myfolder/";
String movedFolder = "/myfolder_mov/";
String file = "file.txt";
String newFile = "newfile.txt";
cms.createResource(folder, CmsResourceTypeFolder.getStaticTypeId());
cms.createResource(folder + file, CmsResourceTypePlain.getStaticTypeId());
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
cms.createResource(folder + newFile, CmsResourceTypePlain.getStaticTypeId());
storeResources(cms, folder, true);
cms.lockResource(folder);
cms.moveResource(folder, movedFolder);
// check lock
assertFalse(cms.getLockedResources("/", CmsLockFilter.FILTER_ALL).contains(folder));
CmsResource res = cms.readResource(movedFolder + file);
CmsResource newRes = cms.readResource(movedFolder + newFile);
setMapping(movedFolder + file, folder + file);
assertFilter(cms, res, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
assertState(cms, movedFolder + file, CmsResource.STATE_CHANGED);
setMapping(movedFolder + newFile, folder + newFile);
assertFilter(cms, newRes, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
assertState(cms, movedFolder + newFile, CmsResource.STATE_NEW);
}
/**
* Tests to move a deep folder structure with real permission check.<p>
*
* @throws Exception if the test fails
*/
public void testMoveFolderWithPermissionCheck() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing to move a deep folder structure with real permission check");
// Creating paths
String folder = "/mytestfolder/";
String folder_moved = "/mytestfolder_moved/";
String file = "index.html";
cms.loginUser("test2", "test2");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.createResource(folder, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
cms.createResource(folder + file, CmsResourceTypePlain.getStaticTypeId());
cms.moveResource(folder, folder_moved);
cms.unlockResource(folder_moved);
// check lock
assertFalse(cms.getLockedResources("/", CmsLockFilter.FILTER_ALL).contains(folder));
}
/**
* Tests the move to lost and found folder operation.<p>
*
* @throws Exception if the test fails
*/
public void testMoveLostAndFound() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the move to lost and found folder operation");
String filename = "/file.txt";
String link = "link.html";
CmsResource res = cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId());
CmsResource res1 = cms.createResource(link, CmsResourceTypeXmlPage.getStaticTypeId());
CmsFile file = cms.readFile(res1);
CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file, true);
if (!page.hasValue("test", Locale.ENGLISH)) {
page.addValue("test", Locale.ENGLISH);
}
page.setStringValue(cms, "test", Locale.ENGLISH, "<a href='file.txt'>file</a>");
file.setContents(page.marshal());
cms.lockResource(link);
cms.writeFile(file);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
cms.lockResource(filename);
String newName = cms.moveToLostAndFound(filename);
CmsResource newRes = cms.readResource(newName);
CmsResource oldRes = cms.readResource(filename, CmsResourceFilter.ALL);
assertSiblingCount(cms, newName, 2);
assertState(cms, newName, CmsResource.STATE_NEW);
assertNotSame(res.getStructureId(), newRes.getStructureId());
assertEquals(res.getStructureId(), oldRes.getStructureId());
assertSiblingCount(cms, filename, 2);
assertState(cms, filename, CmsResource.STATE_DELETED);
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId());
file = cms.readFile(res1);
page = CmsXmlPageFactory.unmarshal(cms, file, true);
CmsLinkTable links = page.getLinkTable("test", Locale.ENGLISH);
assertEquals(links.size(), 1);
assertEquals(links.getLink("link0").getVfsUri(), filename);
}
/**
* Tests to move a file over a deleted one.<p>
*
* @throws Exception if the test fails
*/
public void testMoveOverDeleted() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -