1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: TestDocType.java,v 1.10 2004/06/25 08:03:47 maartenc Exp $
8 */
9
10 package org.dom4j;
11
12 import java.net.URL;
13 import java.util.List;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18
19 import org.dom4j.dtd.ElementDecl;
20 import org.dom4j.io.SAXReader;
21
22 /*** Tests the DocType functionality
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25 * @version $Revision: 1.10 $
26 */
27 public class TestDocType extends AbstractTestCase {
28
29 /*** Input XML file to read */
30 protected static String INPUT_XML_FILE = "/xml/dtd/internal.xml";
31
32 public static void main( String[] args ) {
33 TestRunner.run( suite() );
34 }
35
36 public static Test suite() {
37 return new TestSuite( TestDocType.class );
38 }
39
40 public TestDocType(String name) {
41 super(name);
42 }
43
44 // Test case(s)
45 //-------------------------------------------------------------------------
46 public void testDocType() throws Exception {
47 DocumentType docType = document.getDocType();
48 assertTrue("Has DOCTYPE", docType!= null);
49
50 List declarations = docType.getInternalDeclarations();
51 assertTrue("DOCTYPE has declarations", declarations != null && !declarations.isEmpty());
52
53 ElementDecl decl = (ElementDecl) declarations.get(0);
54
55 assertEquals("name is correct", "greeting", decl.getName() );
56 assertEquals("model is correct", "(#PCDATA)", normalize(decl.getModel()));
57
58 String expected = "<!ELEMENT " + decl.getName() + " " + decl.getModel() + ">";
59 assertEquals("toString() is correct", expected, decl.toString());
60 }
61
62 /***
63 * Removes the optional * at the end of (#PCDATA)
64 */
65 private String normalize(String model) {
66 if ("(#PCDATA)*".equals(model)) {
67 return ("(#PCDATA)");
68 }
69 return model;
70 }
71
72 // Implementation methods
73 //-------------------------------------------------------------------------
74 protected void setUp() throws Exception {
75 SAXReader reader = new SAXReader("org.dom4j.io.aelfred2.SAXDriver");
76 reader.setIncludeInternalDTDDeclarations(true);
77
78 URL url = getClass().getResource(INPUT_XML_FILE);
79 document = reader.read(url);
80 }
81 }
82
83
84
85
86 /*
87 * Redistribution and use of this software and associated documentation
88 * ("Software"), with or without modification, are permitted provided
89 * that the following conditions are met:
90 *
91 * 1. Redistributions of source code must retain copyright
92 * statements and notices. Redistributions must also contain a
93 * copy of this document.
94 *
95 * 2. Redistributions in binary form must reproduce the
96 * above copyright notice, this list of conditions and the
97 * following disclaimer in the documentation and/or other
98 * materials provided with the distribution.
99 *
100 * 3. The name "DOM4J" must not be used to endorse or promote
101 * products derived from this Software without prior written
102 * permission of MetaStuff, Ltd. For written permission,
103 * please contact dom4j-info@metastuff.com.
104 *
105 * 4. Products derived from this Software may not be called "DOM4J"
106 * nor may "DOM4J" appear in their names without prior written
107 * permission of MetaStuff, Ltd. DOM4J is a registered
108 * trademark of MetaStuff, Ltd.
109 *
110 * 5. Due credit should be given to the DOM4J Project -
111 * http://www.dom4j.org
112 *
113 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
114 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
115 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
116 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
117 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
118 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
119 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
121 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
122 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
124 * OF THE POSSIBILITY OF SUCH DAMAGE.
125 *
126 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
127 *
128 * $Id: TestDocType.java,v 1.10 2004/06/25 08:03:47 maartenc Exp $
129 */