📄 testlinkvalidation.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestLinkValidation.java,v $
* Date : $Date: 2007-09-10 10:11:52 $
* 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.file.history.I_CmsHistoryResource;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypeImage;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.relations.CmsRelation;
import org.opencms.relations.CmsRelationFilter;
import org.opencms.relations.CmsRelationType;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsUUID;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.page.CmsXmlPage;
import org.opencms.xml.page.CmsXmlPageFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
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 tests for OpenCms link validation.<p>
*
* @author Michael Moossen
*
* @version $Revision: 1.5 $
*/
public class TestLinkValidation extends OpenCmsTestCase {
private static final int MODE_XMLCONTENT_BOTH = 1;
private static final int MODE_XMLCONTENT_FILEREF_ONLY = 3;
private static final int MODE_XMLCONTENT_HTML_ONLY = 2;
private static final int MODE_XMLPAGE = 0;
/**
* Default JUnit constructor.<p>
*
* @param arg0 JUnit parameters
*/
public TestLinkValidation(String arg0) {
super(arg0);
}
/**
* Sets the content of a resource.<p>
*
* @param cms the cms context
* @param filename the resource name
* @param content the content to set
*
* @throws CmsException if something goes wrong
*/
public static void setContent(CmsObject cms, String filename, String content) throws CmsException {
CmsResource res = cms.readResource(filename);
CmsFile file = cms.readFile(res);
CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file, true);
if (!page.hasValue("test", Locale.ENGLISH)) {
page.addValue("test", Locale.ENGLISH);
}
page.setStringValue(cms, "test", Locale.ENGLISH, content);
file.setContents(page.marshal());
cms.lockResource(filename);
cms.writeFile(file);
}
/**
* Sets the content of a xmlcontent resource.<p>
*
* @param cms the cms context
* @param filename the resource name
* @param html the content to set in the text field
* @param link the vfs file reference to set
*
* @throws CmsException if something goes wrong
*/
public static void setXmlContent(CmsObject cms, String filename, String html, String link) throws CmsException {
CmsResource res = cms.readResource(filename);
CmsFile file = cms.readFile(res);
CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
if (!content.hasValue("Text", Locale.ENGLISH, 0)) {
content.addValue(cms, "Text", Locale.ENGLISH, 0);
}
content.getValue("Text", Locale.ENGLISH, 0).setStringValue(cms, html);
if (!content.hasValue("Homepage", Locale.ENGLISH, 0)) {
content.addValue(cms, "Homepage", Locale.ENGLISH, 0);
}
content.getValue("Homepage", Locale.ENGLISH, 0).setStringValue(cms, link);
file.setContents(content.marshal());
cms.lockResource(filename);
cms.writeFile(file);
}
/**
* 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(TestLinkValidation.class.getName());
suite.addTest(new TestLinkValidation("testLinkValidationXmlPages"));
suite.addTest(new TestLinkValidation("testLinkValidationXmlContents"));
suite.addTest(new TestLinkValidation("testLinkValidationXmlContentsHtml"));
suite.addTest(new TestLinkValidation("testLinkValidationXmlContentsFileRef"));
suite.addTest(new TestLinkValidation("testBrokenLinkFile"));
suite.addTest(new TestLinkValidation("testBrokenLinkFolder"));
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
setupOpenCms("simpletest", "/sites/default/");
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Test broken link issue with files.<p>
*
* @throws Throwable if something goes wrong
*/
public void testBrokenLinkFile() throws Throwable {
echo("Testing broken link issue with files");
CmsObject cms = getCmsObject();
String resName = "testBrokenLinkFile.html";
String imgName = "testBrokenLinkFile.gif";
CmsResource res = cms.createResource(resName, CmsResourceTypeXmlPage.getStaticTypeId());
List relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
setContent(cms, resName, "<img src='" + imgName + "' >");
relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertEquals(1, relations.size());
CmsRelation expected = new CmsRelation(
res.getStructureId(),
res.getRootPath(),
CmsUUID.getNullUUID(),
cms.getRequestContext().addSiteRoot(imgName),
CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertEquals(0, relations.size());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
CmsResource img = cms.createResource(imgName, CmsResourceTypeImage.getStaticTypeId());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertEquals(1, relations.size());
expected = new CmsRelation(
res.getStructureId(),
res.getRootPath(),
img.getStructureId(),
img.getRootPath(),
CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertEquals(0, relations.size());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
}
/**
* Test broken link issue with folder.<p>
*
* @throws Throwable if something goes wrong
*/
public void testBrokenLinkFolder() throws Throwable {
echo("Testing broken link issue with folder");
CmsObject cms = getCmsObject();
String resName = "testBrokenLinkFile2.html";
String folderName = "testBrokenLinkFolder/";
String folderName2 = "testBrokenLinkFolder2/";
String imgName = "testBrokenLinkFile2.gif";
CmsResource res = cms.createResource(resName, CmsResourceTypeXmlPage.getStaticTypeId());
List relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
setContent(cms, resName, "<img src='" + folderName + imgName + "' >");
relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertEquals(1, relations.size());
CmsRelation expected = new CmsRelation(
res.getStructureId(),
res.getRootPath(),
CmsUUID.getNullUUID(),
cms.getRequestContext().addSiteRoot(folderName + imgName),
CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertEquals(0, relations.size());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
cms.createResource(folderName2, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
cms.createResource(folderName2 + imgName, CmsResourceTypeImage.getStaticTypeId());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertEquals(0, relations.size());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
cms.moveResource(folderName2, folderName);
CmsResource img = cms.readResource(folderName + imgName);
relations = cms.getRelationsForResource(resName, CmsRelationFilter.ALL);
assertEquals(1, relations.size());
expected = new CmsRelation(
res.getStructureId(),
res.getRootPath(),
img.getStructureId(),
img.getRootPath(),
CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(resName, CmsRelationFilter.SOURCES);
assertEquals(0, relations.size());
relations = cms.getRelationsForResource(resName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
assertRelation(expected, (CmsRelation)relations.get(0));
}
/**
* Test link validation for xml contents with html and file references.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLinkValidationXmlContents() throws Throwable {
echo("Testing link validation for xml contents with html and file references");
testLinkValidation(MODE_XMLCONTENT_BOTH);
}
/**
* Test link validation for xml contents with only file references.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLinkValidationXmlContentsFileRef() throws Throwable {
echo("Testing link validation for xml contents with only file references");
testLinkValidation(MODE_XMLCONTENT_FILEREF_ONLY);
}
/**
* Test link validation for xml contents with only html.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLinkValidationXmlContentsHtml() throws Throwable {
echo("Testing link validation for xml contents with only html");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -