testcmsxmlcontentschemamodifications.java

来自「找了很久才找到到源代码」· Java 代码 · 共 504 行 · 第 1/2 页

JAVA
504
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/xml/content/TestCmsXmlContentSchemaModifications.java,v $
 * Date   : $Date: 2007-08-13 16:30:14 $
 * Version: $Revision: 1.3 $
 *
 * 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.xml.content;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.i18n.CmsEncoder;
import org.opencms.main.CmsEvent;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsFileUtil;
import org.opencms.xml.CmsXmlContentDefinition;
import org.opencms.xml.CmsXmlEntityResolver;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;

import java.util.Collections;
import java.util.HashMap;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Tests for XML content schema changes.<p>
 *
 * @author Alexander Kandzior 
 * @version $Revision: 1.3 $
 */
public class TestCmsXmlContentSchemaModifications extends OpenCmsTestCase {

    private static final String SCHEMA_SYSTEM_ID_1 = "http://www.opencms.org/test1.xsd";

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCmsXmlContentSchemaModifications(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(TestCmsXmlContentSchemaModifications.class.getName());

        suite.addTest(new TestCmsXmlContentSchemaModifications("testVfsFile"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testUsageDemo"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testAddSchemaNodes"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testRemoveSchemaNodes"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testReArrangeSchemaNodes"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testCombinedChangeSchemaNodes"));
        suite.addTest(new TestCmsXmlContentSchemaModifications("testNestedChangeSchemaNodes"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

                setupOpenCms("simpletest", "/sites/default/");
            }

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Demo test for using the XML content correcting API.<p>
     * 
     * @throws Exception in case the test fails
     */
    public void testVfsFile() throws Exception {

        CmsObject cms = getCmsObject();
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);

        // filenames to use
        String changedSchema = "org/opencms/xml/content/xmlcontent-definition-2.mod.xsd";
        String originalFile = "org/opencms/xml/content/xmlcontent-2.mod.xml";
        String filename = "/testVfsFile.html";
        // cache the changed content definition
        cacheSchema(resolver, "http://www.opencms.org/test2.xsd", changedSchema);
        // read the XML content from the test directory, usually this would be from the VFS
        String content = CmsFileUtil.readFile(originalFile, CmsEncoder.ENCODING_UTF_8);

        // unmarshal the XML content (the schema is already using the "filereference")
        CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);

        // output the XML content (unmodified version)
        System.out.println(xmlcontent.toString());

        // validate the XML structure - must be invalid because of the schema change
        try {
            xmlcontent.validateXmlStructure(resolver);
            fail("xml content should not be valid");
        } catch (CmsXmlException e) {
            // ignore
        }

        // enable "auto correction mode" - this is required or the XML structure will not be fully corrected
        xmlcontent.setAutoCorrectionEnabled(true);
        // now correct the XML        
        xmlcontent.correctXmlStructure(cms);

        // output the XML content (modified version)
        System.out.println(xmlcontent.toString());

        // check again if the XML is correct - this time it must work without exception
        xmlcontent.validateXmlStructure(resolver);
        
        // write the content to the VFS
        cms.createResource(
            filename,
            OpenCms.getResourceManager().getResourceType("xmlcontent").getTypeId(),
            CmsFileUtil.readFile(originalFile),
            Collections.EMPTY_LIST);

        // assumption: a file is to be corrected automatically while writing it to the VFS
        // for this, a special OpenCms request context attribute has been introduced
        // if this is set to a Boolean.TRUE object, the XML content is always corrected while saving it to the VFS
        CmsFile file = cms.readFile(filename);

        // this is not normally required, but illustrates the differences in the API behaviour
        // trying to write the file now (with the changed schema but XML still using the old schema) is not possible 
        // this is because of the build-in validation while writing
        try {
            cms.writeFile(file);
            fail("should fail to write the old xml file");
        } catch (CmsXmlException e) {
            // ok, ignore
        }

        // now set the "automatic correction" request context attribute
        cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE);

        // write the file again - the correction will now be done automatically while writing (no exceptions)
        file = cms.writeFile(file);

        // output the XML content (modified version)
        System.out.println(new String(file.getContents()));
    }

    /**
     * Test adding new nodes to the XML schema.<p>
     * 
     * @throws Exception in case the test fails
     */
    public void testAddSchemaNodes() throws Exception {

        echo("Testing adding new nodes to XML schema");

        runTestWithChangedSchema(
            SCHEMA_SYSTEM_ID_1,
            "/testAddSchemaNodes.html",
            "xmlcontent-definition-1.xsd",
            "xmlcontent-1.xml",
            "xmlcontent-definition-1.mod1.xsd",
            "xmlcontent-1.mod1.xml");
    }

    /**
     * Combined modification test for simple (non-nested) XML schema.<p>
     * 
     * @throws Exception in case the test fails
     */
    public void testCombinedChangeSchemaNodes() throws Exception {

        echo("Combined modification test for simple (non-nested) XML schema");

        runTestWithChangedSchema(
            SCHEMA_SYSTEM_ID_1,
            "/testCombinedChangeSchemaNodesA.html",
            "xmlcontent-definition-1.xsd",
            "xmlcontent-1.xml",
            "xmlcontent-definition-1.mod4.xsd",
            "xmlcontent-1.mod4.xml");

        runTestWithChangedSchema(
            SCHEMA_SYSTEM_ID_1,
            "/testCombinedChangeSchemaNodesB.html",
            "xmlcontent-definition-1.mod4.xsd",
            "xmlcontent-1.mod4.xml",
            "xmlcontent-definition-1.mod5.xsd",
            "xmlcontent-1.mod5.xml");
    }

    /**
     * Combined modification test for a nested XML schema.<p>
     * 
     * @throws Exception in case the test fails
     */
    public void testNestedChangeSchemaNodes() throws Exception {

        echo("Combined modification test for a nested XML schema");

        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(getCmsObject());
        
        cacheSchema(
            resolver,
            "http://www.opencms.org/test3.xsd",
            "org/opencms/xml/content/xmlcontent-definition-3.xsd");
        
        cacheSchema(
            resolver,
            "http://www.opencms.org/test4.xsd",
            "org/opencms/xml/content/xmlcontent-definition-4.xsd");
        
        runTestWithChangedSchema(

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?