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

📄 testdocument.java

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

/* Please run replic.pl on me ! */
/**
 * Please put a description of your test here.
 * 
 * @author unascribed
 * @version 0.1
 */
import junit.framework.*;

import org.jdom.*;
import java.io.*;
import java.util.*;
import org.jdom.output.*;
import org.jdom.input.*;

public final class TestDocument
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 TestDocument(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(TestDocument.class);
        return suite;
    }
    /**
     * This method is called after a test is executed.
     */
    public void tearDown() {
        // your code goes here.
    }
	/**
	 * Test constructor of Document with a List of content including the root element.
	 */
	public void test_TCC___List() {
                Element bogus = new Element("bogus-root");
		Element element = new Element("element");
		Comment comment = new Comment("comment");
		List list = new ArrayList();

		list.add(element);
		list.add(comment);
		Document doc = new Document(list);
                // Get a live list back
                list = doc.getContent();
		assertEquals("incorrect root element returned", element, doc.getRootElement());

		//no root element
                element.detach();
		try {
                    doc.getRootElement();
                    fail("didn't catch missing root element");
                } catch (IllegalStateException e) {
		}

                //set root back, then try to add another element to our
                //live list
                doc.setRootElement(element);
                try {
                    list.add(bogus);
                    fail("didn't catch duplicate root element");
                } catch (IllegalAddException e) {
                }
                assertEquals("incorrect root element returned", element, doc.getRootElement());
  
                //how about replacing it in our live list
                try {
                    Element oldRoot = doc.getRootElement();
                    int i = doc.indexOf(oldRoot);
                    list.set(i, bogus);
                } catch (Exception e) {
                    fail("Root replacement shouldn't have throw a exception");
                }
                //and through the document
                try {
                    doc.setRootElement(element);
                } catch (Exception e) {
                    fail("Root replacement shouldn't have throw a exception");
                }
 	
		list = null;
		try {
			doc = new Document(list);
		} catch (IllegalAddException e) {
			fail("didn't handle null list");
		} catch (NullPointerException e) {
			fail("didn't handle null list");
		}
		
	}
	/**
	 * Test constructor of a Document with a List of content and a DocType.
	 */
	public void test_TCC___List_OrgJdomDocType() {
                Element bogus = new Element("bogus-root");
		Element element = new Element("element");
		Comment comment = new Comment("comment");
		DocType docType = new DocType("element");
		List list = new ArrayList();

        list.add(docType);
		list.add(element);
		list.add(comment);
		Document doc = new Document(list);
                // Get a live list back
                list = doc.getContent();
		assertEquals("incorrect root element returned", element, doc.getRootElement());
		assertEquals("incorrect doc type returned", docType, doc.getDocType());

                element.detach();
                try {
                        doc.getRootElement();
                        fail("didn't catch missing root element");
                } catch (IllegalStateException e) {
                }
                
                //set root back, then try to add another element to our
                //live list
                doc.setRootElement(element);
		try {
			list.add(bogus);
                        fail("didn't catch duplicate root element");
		} catch (IllegalAddException e) {
		}
                assertEquals("incorrect root element returned", element, doc.getRootElement());
 
                //how about replacing it in our live list
                try {
                    Element oldRoot = doc.getRootElement();
                    int i = doc.indexOf(oldRoot);
                    list.set(i,bogus);
                } catch (Exception e) {
                    fail("Root replacement shouldn't have throw a exception");
                }
                //and through the document
                try {
                    doc.setRootElement(element);
                } catch (Exception e) {
                    fail("Root replacement shouldn't have throw a exception");
                }
		
		list = null;
		try {
			doc = new Document(list);
		} catch (IllegalAddException e) {
            fail("didn't handle null list");
		} catch (NullPointerException e) {
			fail("didn't handle null list");
		}
		
	}
	/**
	 * Test the constructor with only a root element.
	 */
	public void test_TCC___OrgJdomElement() {
		Element element = new Element("element");
		
		Document doc = new Document(element);
		assertEquals("incorrect root element returned", element, doc.getRootElement());

		element = null;
		try {
			doc = new Document(element);
		} catch (IllegalAddException e) {
			fail("didn't handle null element");
		} catch (NullPointerException e) {
			fail("didn't handle null element");
		}
		
	}
	/**
	 * Test constructor of Document with and Element and Doctype
	 */
	public void test_TCC___OrgJdomElement_OrgJdomDocType() {
		Element element = new Element("element");
		DocType docType = new DocType("element");
		
		Document doc = new Document(element, docType);
		assertEquals("incorrect root element returned", element, doc.getRootElement());
		assertEquals("incorrect doc type returned", docType, doc.getDocType());

		docType = new DocType("element");
		element = null;
		try {
			doc = new Document(element, docType);
		} catch (IllegalAddException e) {
			fail("didn't handle null element");
		} catch (NullPointerException e) {
			fail("didn't handle null element");
		}
		
	}
	/**
	 * Test object equality.
	 */
	public void test_TCM__boolean_equals_Object() {
		Element element = new Element("element");
		
		Document doc = new Document(element);
		assertEquals("invalid object equality", doc, (Object)doc);

	}
	/**
	 * Test removeContent for a given Comment
	 */
	public void test_TCM__boolean_removeContent_OrgJdomComment() {
		Element element = new Element("element");
		Comment comment = new Comment("comment");
		ArrayList list = new ArrayList();

		list.add(element);
		list.add(comment);
		Document doc = new Document(list);
		assertTrue("incorrect comment removed",! doc.removeContent(new Comment("hi")));
		assertTrue("didn't remove comment", doc.removeContent(comment));
			
		assertTrue("comment not removed", doc.getContent().size() == 1);
	}
	/**
	 * Test removeContent with the supplied ProcessingInstruction.
	 */
	public void test_TCM__boolean_removeContent_OrgJdomProcessingInstruction() {
		Element element = new Element("element");
		ProcessingInstruction pi = new ProcessingInstruction("test", "comment");
		ArrayList list = new ArrayList();

		list.add(element);
		list.add(pi);
		Document doc = new Document(list);
		assertTrue("incorrect pi removed",! doc.removeContent(new ProcessingInstruction("hi", "there")));
		assertTrue("didn't remove pi", doc.removeContent(pi));
			
		assertTrue("PI not removed", doc.getContent().size() == 1);
	

⌨️ 快捷键说明

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