📄 testpublishissues.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPublishIssues.java,v $
* Date : $Date: 2007-08-13 16:29:57 $
* Version: $Revision: 1.23 $
*
* 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.CmsPublishList;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.lock.CmsLockType;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsUUID;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Unit tests for special publish issues.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.23 $
*/
/**
* Comment for <code>TestPermissions</code>.<p>
*/
public class TestPublishIssues extends OpenCmsTestCase {
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestPublishIssues(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(TestPublishIssues.class.getName());
suite.addTest(new TestPublishIssues("testPublishScenarioA"));
suite.addTest(new TestPublishIssues("testPublishScenarioB"));
suite.addTest(new TestPublishIssues("testPublishScenarioC"));
suite.addTest(new TestPublishIssues("testMultipleProjectCreation"));
suite.addTest(new TestPublishIssues("testMultipleProjectCreationGalore"));
suite.addTest(new TestPublishIssues("testDirectPublishWithSiblings"));
suite.addTest(new TestPublishIssues("testPublishScenarioD"));
suite.addTest(new TestPublishIssues("testPublishScenarioE"));
suite.addTest(new TestPublishIssues("testPublishScenarioF"));
suite.addTest(new TestPublishIssues("testPublishScenarioG"));
suite.addTest(new TestPublishIssues("testPublishScenarioH"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Tests publish scenario "publish all Siblings".<p>
*
* This scenario is described as follows:
* If a direct publish is made, and the option "publish all siblings" is selected,
* a file that contains siblings will be added multiple times to the publish list,
* causing a primary key collision in the publish history table.<p>
*
* @throws Throwable if something goes wrong
*/
public void testDirectPublishWithSiblings() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing publish scenario using 'publish all Siblings'");
cms.lockResource("/folder1/");
cms.setDateLastModified("/folder1/", System.currentTimeMillis(), true);
cms.unlockResource("/folder1/");
// publish the project (this did cause an exception because of primary key violation!)
CmsUUID publishId = OpenCms.getPublishManager().publishResource(cms, "/folder1/", true, new CmsShellReport(
cms.getRequestContext().getLocale()));
OpenCms.getPublishManager().waitWhileRunning();
// read the published resources from the history
List publishedResources = cms.readPublishedResources(publishId);
// make sure the publish history contains the required amount of resources
assertEquals(35, publishedResources.size());
}
/**
* Tests multiple creation of a project with the same name.<p>
*
* @throws Exception in case the test fails
*/
public void testMultipleProjectCreation() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing multiple creation of a project with the same name");
String projectName = "projectX";
// try to read a non-existant project
try {
cms.readProject(projectName);
fail("Required exception was not thrown!");
} catch (CmsException e) {
// ok
}
// create the project
cms.createProject(
projectName,
"Test project",
OpenCms.getDefaultUsers().getGroupUsers(),
OpenCms.getDefaultUsers().getGroupUsers());
CmsProject project = cms.readProject(projectName);
cms.getRequestContext().setCurrentProject(project);
cms.copyResourceToProject("/folder1/");
// check if the project was created as planned
List resources = cms.readProjectResources(project);
assertEquals(1, resources.size());
assertEquals("/sites/default/folder1/", (String)resources.get(0));
// copy the root folder of the sito to the project - this must remove the "/folder1/" folder
cms.copyResourceToProject("/");
resources = cms.readProjectResources(project);
assertEquals(1, resources.size());
assertEquals("/sites/default/", (String)resources.get(0));
// now create the project again - this must NOT throw an exception since projects may have the same name
CmsProject newProject = cms.createProject(
projectName,
"Test project 2nd time",
OpenCms.getDefaultUsers().getGroupUsers(),
OpenCms.getDefaultUsers().getGroupUsers());
// check if the projects have different ids
CmsUUID id1 = project.getUuid();
CmsUUID id2 = newProject.getUuid();
if (id1 == id2) {
fail("Two different projects created with same name have the same id!");
}
// read the projects again and asserts same name but differnet description
project = cms.readProject(id1);
newProject = cms.readProject(id2);
assertEquals(project.getName(), newProject.getName());
if (project.getDescription().equals(newProject.getDescription())) {
fail("Projects should have differnet descriptions!");
}
}
/**
* Tries to create very many projects with the same name.<p>
*
* This displays an issue in the OpenCms v6 project driver, where a project is created with
* a UNIQUE INDEX based on the given name and the current system time. In rare instances the
* system time may be identical and an error occurs.<p>
*
* The issue was found in the MySQL project driver, but others DBs may be affected as well.<p>
*
* @throws Exception in case the test fails
*/
public void testMultipleProjectCreationGalore() throws Exception {
CmsObject cms = getCmsObject();
final String name = "multipleProject";
final String group = OpenCms.getDefaultUsers().getGroupUsers();
final String description = "";
// usually creating the project 20x is enough to replicate the issue
for (int i = 0; i < 20; i++) {
cms.createProject(name, description, group, group);
}
}
/**
* Tests publish scenario "A".<p>
*
* This scenario is described as follows:
* We have users "test1" and "test2".
* We have two projects, "project1" and "project2".
* Project "project1" consists of the folder "/".
* Project "project2" consists of the folder "/folder1/subfolder11/".
* User "test2" edits the file "/folder1/subfolder11/index.html".
* After this, user "test1" locks the folder "/folder1" in "project1", and unlocks it again.
* User "test2" logs in and now publishes "project2".<p>
*
* Wanted result: the changed resource "/folder1/subfolder11/index.html" is published
* with "project2".<p>
*
* The test illustrates a change in the logic from OpenCms 5 to OpenCms 6:
* In OpenCms 5, locking a file caused it to switch to the current users project.
* In OpenCms 6, this is no longer true, at last not if you just lock a parent folder.
* So in OpenCms 5, this test would fail, since the resource would be in "project1", not "project2".<p>
*
* @throws Throwable if something goes wrong
*/
public void testPublishScenarioA() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing publish scenario A");
String projectRes1 = "/folder1/subfolder11/";
String resource1 = projectRes1 + "index.html";
String resource2 = "/folder1/";
long timestamp = System.currentTimeMillis();
// we use the default "Offline" project as "project1"
CmsProject project1 = cms.readProject("Offline");
// create "project2" as Admin user
cms.createProject(
"project2",
"Test project 2 for scenario A",
OpenCms.getDefaultUsers().getGroupUsers(),
OpenCms.getDefaultUsers().getGroupUsers());
CmsProject project2 = cms.readProject("project2");
cms.getRequestContext().setCurrentProject(project2);
cms.copyResourceToProject(projectRes1);
// check if the project was created as planned
List resources = cms.readProjectResources(project2);
assertEquals(1, resources.size());
assertEquals("/sites/default" + projectRes1, (String)resources.get(0));
// login as user "test2"
cms.loginUser("test2", "test2");
cms.getRequestContext().setCurrentProject(project2);
// perform some edit on the file
cms.lockResource(resource1);
cms.setDateLastModified(resource1, System.currentTimeMillis(), false);
// assert some basic status info
assertDateLastModifiedAfter(cms, resource1, timestamp);
assertProject(cms, resource1, project2);
assertLock(cms, resource1, CmsLockType.EXCLUSIVE);
assertState(cms, resource1, CmsResource.STATE_CHANGED);
// now login as user "test1" (default will be in the "Offline" project)
cms.loginUser("test1", "test1");
cms.getRequestContext().setCurrentProject(project1);
// lock the folder
cms.lockResource(resource2);
// assert some basic status info
assertProject(cms, resource1, project2);
assertLock(cms, resource1, CmsLockType.INHERITED);
// now unlock the folder again
cms.unlockResource(resource2);
// back to the user "test2"
cms.loginUser("test2", "test2");
cms.getRequestContext().setCurrentProject(project2);
// get the publish list
CmsPublishList publishList = OpenCms.getPublishManager().getPublishList(cms);
assertEquals(1, publishList.getFileList().size());
// project should have no locked resources
int resourceProjectCount = cms.countLockedResources(project2.getUuid());
assertEquals(0, resourceProjectCount);
// unlock the project
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// ensure the file was published - state must be "unchanged"
assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
}
/**
* Tests publish scenario "B".<p>
*
* This scenario is described as follows:
* We have users "test1" and "test2" and projects "projectA" and "projectB".
* Both projects contain all resources.
* We have two folders "/foldera/" and "/folderb/".
* There is a resource "test.txt" that has a sibling in both folders.
* User "test1" locks folder "/foldera/" and user "test2" locks folder "/folderb".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -