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

📄 testelement.java

📁 一个java操作xml的完整示例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
package org.jdom.test.cases;

/*--

 Copyright (C) 2000 Brett McLaughlin & Jason Hunter.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
	notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
	notice, this list of conditions, and the disclaimer that follows
	these conditions in the documentation and/or other materials
	provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
	derived from this software without prior written permission.  For
	written permission, please contact license@jdom.org.

 4. Products derived from this software may not be called "JDOM", nor
	may "JDOM" appear in their name, without prior written permission
	from the JDOM Project Management (pm@jdom.org).

 In addition, we request (but do not require) that you include in the
 end-user documentation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
	 "This product includes software developed by the
	  JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many
 individuals on behalf of the JDOM Project and was originally
 created by Brett McLaughlin <brett@jdom.org> and
 Jason Hunter <jhunter@jdom.org>.  For more information on the
 JDOM Project, please see <http://www.jdom.org/>.

 */

/**
 * Please put a description of your test here.
 *
 * @author unascribed
 * @version 0.1
 */

import junit.framework.*;
import java.util.*;

import org.jdom.*;
import java.io.*;
import java.util.*;
import org.jdom.output.*;
import org.omg.PortableInterceptor.SUCCESSFUL;

public final class TestElement
        extends junit.framework.TestCase {

    /**
     * Resource Bundle for various testing resources
     */
    private ResourceBundle rb = ResourceBundle.getBundle("org.jdom.test.Test");

    /**
     * the directory where needed resource files will be kept
     */
    private String resourceDir = "";

    /**
     *  a directory for temporary storage of files
     */
    private String scratchDir = "";

    /**
     *  Construct a new instance.
     */
    public TestElement(String name) {
        super(name);
    }

    /**
     * The main method runs all the tests in the text ui
     */
    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * This method is called before a test is executed.
     */
    public void setUp() {
        resourceDir = rb.getString("test.resourceRoot");
        scratchDir = rb.getString("test.scratchDirectory");
    }

    /**
     * The suite method runs all the tests
     */
    public static Test suite() {
        TestSuite suite = new TestSuite(TestElement.class);
        /*TestSuite suite = new TestSuite();
        suite.addTest(new TestElement("test_TCU__testAttributeNamespaces"));
        suite.addTest(new TestElement("test_TCU__testDefaultNamespaces"));
        suite.addTest(new TestElement("test_TCU__testSerialization"));
        */
        return suite;
    }

    /**
     * This method is called after a test is executed.
     */
    public void tearDown() {
        // your code goes here.
    }

    /**
     * Test the constructor for an empty element
     */
    public void test_TCC___String() {

        //create a new empty element
        Element el = new Element("theElement");
        assertTrue("wrong element name after constructor", el.getName().equals("theElement"));
        assertTrue("expected NO_NAMESPACE", el.getNamespace().equals(Namespace.NO_NAMESPACE));
        assertTrue("expected no child elements", el.getChildren().equals(Collections.EMPTY_LIST));
        assertTrue("expected no attributes", el.getAttributes().equals(Collections.EMPTY_LIST));

        //must have a name
        try {
            el = new Element("");
            fail("allowed creation of an element with no name");
        }
        catch (IllegalNameException e) {
        }

        //name can't be null
        try {
            el = new Element(null);
            fail("allowed creation of an element with null name");
        }
        catch (IllegalNameException e) {
        }

        //we can assume the Verifier has been called by now so we are done
    }

    /**
     * Test the Element constructor with an name and namespace
     */
    public void test_TCC___String_OrgJdomNamespace() {
        //create a new empty element with a namespace

        Namespace ns = Namespace.getNamespace("urn:foo");
        Element el = new Element("theElement", ns);
        assertTrue("wrong element name after constructor", el.getName().equals("theElement"));
        assertTrue("expected urn:foo namespace", el.getNamespace().equals(Namespace.getNamespace("urn:foo")));
        assertTrue("expected no child elements", el.getChildren().equals(Collections.EMPTY_LIST));
        assertTrue("expected no attributes", el.getAttributes().equals(Collections.EMPTY_LIST));

        //must have a name
        try {
            el = new Element("", ns);
            fail("allowed creation of an element with no name");
        }
        catch (IllegalNameException e) {
        }

        //name can't be null
        try {
            el = new Element(null, ns);
            fail("allowed creation of an element with null name");
        }
        catch (IllegalNameException e) {
        }

        //we can assume the Verifier has been called by now so we are done
    }

    /**
     * Test the Element constructor with a string default namespace
     */
    public void test_TCC___String_String() {
        //create a new empty element with a namespace

        Element el = new Element("theElement", "urn:foo");
        assertTrue("wrong element name after constructor", el.getName().equals("theElement"));
        assertTrue("expected urn:foo namespace", el.getNamespace().equals(Namespace.getNamespace("urn:foo")));
        assertTrue("expected no child elements", el.getChildren().equals(Collections.EMPTY_LIST));
        assertTrue("expected no attributes", el.getAttributes().equals(Collections.EMPTY_LIST));

        //must have a name
        try {
            el = new Element("", "urn:foo");
            fail("allowed creation of an element with no name");
        }
        catch (IllegalNameException e) {
        }

        //name can't be null
        try {
            el = new Element(null, "urn:foo");
            fail("allowed creation of an element with null name");
        }
        catch (IllegalNameException e) {
        }

        //we can assume the Verifier has been called by now so we are done
    }

    /**
     * Test the Element constructor with a namespace uri and prefix
     */
    public void test_TCC___String_String_String() {
        //create a new empty element with a namespace


        Element el = new Element("theElement", "x", "urn:foo");
        assertTrue("wrong element name after constructor", el.getName().equals("theElement"));
        assertTrue("expected urn:foo namespace", el.getNamespace().equals(Namespace.getNamespace("x", "urn:foo")));
        assertTrue("expected no child elements", el.getChildren().equals(Collections.EMPTY_LIST));
        assertTrue("expected no attributes", el.getAttributes().equals(Collections.EMPTY_LIST));

        //must have a name
        try {
            el = new Element("", "x", "urn:foo");
            fail("allowed creation of an element with no name");
        }
        catch (IllegalNameException e) {
        }

        //name can't be null
        try {
            el = new Element(null, "x", "urn:foo");
            fail("allowed creation of an element with null name");
        }
        catch (IllegalNameException e) {
        }

        //we can assume the Verifier has been called by now so we are done

    }

    /**
     * Test the equals compares only object instances
     */
    public void test_TCM__boolean_equals_Object() {
        Element el = new Element("theElement", "x", "urn:foo");
        Element el2 = new Element("theElement", "x", "urn:foo");

        assertTrue("incorrect equals evaluation", ((Object) el).equals(el));
        assertTrue("incorrect equals evaluation", !((Object) el2).equals(el));
    }

    /**
     * Test that hasChildren only reports true for actual child elements.
     */
/*
    public void test_TCM__boolean_hasChildren() {
        //set up an element to test with
        Element element = new Element("element", Namespace.getNamespace("http://foo"));
        assertTrue("reported children when there are none", !element.hasChildren());

        Attribute att1 = new Attribute("anAttribute", "boo");
        element.setAttribute(att1);
        assertTrue("reported children when there are none", !element.hasChildren());

        //add some text
        element.addContent("the text");
        assertTrue("reported children when there are none", !element.hasChildren());

        //add some CDATA
        element.addContent(new CDATA("the text"));
        assertTrue("reported children when there are none", !element.hasChildren());

        //add a PI
        element.addContent(new ProcessingInstruction("pi", "the text"));
        assertTrue("reported children when there are none", !element.hasChildren());

        //add Comment
        element.addContent(new Comment("the text"));
        assertTrue("reported children when there are none", !element.hasChildren());

        //finally a child element
        Element child1 = new Element("child1");
        element.addContent(child1);
        assertTrue("reported no children when there is a child element", element.hasChildren());
    }
*/

    /**
     * Test that hasMixedContent works for varying types of possible
     * child and other content
     */
    public void test_TCM__boolean_hasMixedContent() {
        /** No op because the method is deprecated

         //set up an element to test with
         Element element= new Element("element", Namespace.getNamespace("http://foo"));
         assertTrue("reported mixed content when there is none", ! element.hasMixedContent());


         Attribute att1 = new Attribute("anAttribute", "boo");
         element.setAttribute(att1);
         assertTrue("reported mixed content when there is none", ! element.hasMixedContent());

         //add some text
         element.addContent("the text");
         assertTrue("reported mixed content when there is none", ! element.hasMixedContent());

⌨️ 快捷键说明

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