⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 domtest.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//

package test.xmldb.levelzero;

import test.xmldb.*;
import junit.framework.*;

import org.xmldb.api.modules.XMLResource;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.apache.log4j.Logger;
/**
 * @author  Per Nyfelt
 */
public class DOMTest extends XMLDBTestCase implements LevelZeroTestConstants {

    private Logger logger = Logger.getLogger(DOMTest.class);

    /** Creates new DOMTest */
    public DOMTest(String name) {
        super(name);
    }

    public static Test suite() {
        return new TestSuite(DOMTest.class);
    }    
    
    /** 
     * test all scenarios for using XML as DOM 
     */
    public void testDOM() {
        try {          
            logger.debug("\nLevelZeroTest.testDOM() - started\n");
            insertDOMDocument(id, super.document);
            Node node = retrieveDOMNode(id);
            super.assertNotNull("LevelZeroTest.testDOM()", node); 
            
            insertDOMDocument(id2, document);
            Node node2 = retrieveDOMNode(id2);
            super.assertNotNull("LevelZeroTest.testDOM()", node2);  
            
            // DOM level 2, npot supported by Ozone yet
            //super.assertEquals("LevelZeroTest.testDOM()", node.getLocalName(), node2.getLocalName());
            
            logger.debug("DOMTest.testDOM() - updateDOMDocument() is uncommented!");
            //updateDOMDocument(id);            
            node = retrieveDOMNode(id);
            super.assertNotNull("LevelZeroTest.testDOM()", node); 
    
        } catch (Exception e) {
            fail( e.getMessage( ) );
        }   
    }
    
    private void insertDOMDocument(String id, Document document) throws Exception {
         
        XMLResource resource = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);

        resource.setContentAsDOM(document);
        col.storeResource(resource);
               
   }
    
    private void updateDOMDocument(String id) throws Exception {     
        try {
            XMLResource resource = (XMLResource) col.getResource(id);

            Document document = (Document) resource.getContentAsDOM();
            logger.debug("LevelZeroTest.updateDOMDocument() - " + document);
            assertNotNull("LevelZeroTest.updateDOMDocument()", document);
            Element root = document.getDocumentElement();
            // Change document by appending to an element data

            Text nameNode = document.createTextNode("updateAddition");
            NodeList list = root.getElementsByTagName("testName");
            Node parent = list.item(0);
            parent.appendChild(nameNode);

            // insert a new element
            list = root.getElementsByTagName("levelZeroTests");
            Node levelZeroTests = list.item(0);
            Element testName = document.createElement("testName");
            levelZeroTests.appendChild(testName);
            Node name = document.createTextNode("testSAX");
            testName.appendChild(name);
                     
            resource.setContentAsDOM(document);
            col.storeResource(resource); 
            super.assertEquals(document, resource.getContentAsDOM());
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
   }
   
   private Node retrieveDOMNode(String id) throws Exception {
        XMLResource resource = 
           (XMLResource) col.getResource(id);

        Node node = resource.getContentAsDOM();
        return node;

   }
   
   private void retrieveDOMDocument(String id) throws Exception {
        XMLResource resource = 
           (XMLResource) col.getResource(id);

        Document doc = (Document) resource.getContentAsDOM();       
    }
   
    
}

⌨️ 快捷键说明

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