1 /*
2 * Copyright 2001-2005 (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
8 package org.dom4j.dtd;
9
10 import junit.textui.TestRunner;
11
12 import org.dom4j.AbstractTestCase;
13
14 /**
15 * Tests the {@link AttributeDecl}functionality. Tests each of the property
16 * access methods and the serialization mechanisms. Correct parsing is tested by
17 * {@link DocTypeTest}.
18 *
19 * <p>
20 * There are several key variations that need to be tested both here and in
21 * {@link DocTypeTest}, which is responsible for testing correct parsing of the
22 * {@link DocumentType}. Those variations include the different
23 * <code>valueDefault</code> and <code>value</code> variations so that we
24 * can test for correct acceptance and correct rejection of attribute
25 * declarations.
26 * </p>
27 *
28 * <p>
29 * </p>
30 *
31 * @author Bryan Thompson
32 * @author Maarten Coene
33 * @version $Revision: 1.3 $
34 *
35 * @todo The dom4j documentation needs to describe what representation SHOULD be
36 * generated by {@link AttributeDecl#toString()}.
37 * @todo The dom4j AttributeDecl should expose some methods that make it easier
38 * for people to use the DTD grammar, e.g., isFixed(), isRequired(),
39 * isImplied().
40 */
41 public class AttributeDeclTest extends AbstractTestCase {
42 public static void main(String[] args) {
43 TestRunner.run(AttributeDeclTest.class);
44 }
45
46 // Test case(s)
47 // -------------------------------------------------------------------------
48
49 /**
50 * Test
51 *
52 * <pre>
53 *
54 * <!ATTLIST foo bar ID #IMPLIED>
55 *
56 * </pre>.
57 */
58 public void testIdImpliedNone() {
59 MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
60 "bar", // attributeName
61 "ID", // type
62 "#IMPLIED", // valueDefault
63 null, // value
64 "<!ATTLIST foo bar ID #IMPLIED>");
65 assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "ID",
66 "#IMPLIED", null));
67 }
68
69 /**
70 * Test
71 *
72 * <pre>
73 *
74 * <!ATTLIST foo bar CDATA #FIXED \"goo\">
75 *
76 * </pre>.
77 */
78 public void testCDataFixedValue() {
79 MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
80 "bar", // attributeName
81 "CDATA", // type
82 "#FIXED", // valueDefault
83 "goo", // value
84 "<!ATTLIST foo bar CDATA #FIXED \"goo\">");
85 assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "CDATA",
86 "#FIXED", "goo"));
87 }
88
89 /**
90 * Test
91 *
92 * <pre>
93 *
94 * <!ATTLIST foo bar CDATA "goo">
95 *
96 * </pre>.
97 */
98 public void testCDataNoneValue() {
99 MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
100 "bar", // attributeName
101 "CDATA", // type
102 null, // valueDefault
103 "goo", // value
104 "<!ATTLIST foo bar CDATA \"goo\">");
105 assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "CDATA",
106 null, "goo"));
107 }
108
109 // Implementation methods
110 // -------------------------------------------------------------------------
111 protected void assertSameAttributeDecl(MyTestAttributeDecl expected,
112 AttributeDecl actual) {
113 assertEquals("elementName is correct", expected.getElementName(),
114 actual.getElementName());
115
116 assertEquals("attributeName is correct", expected.getAttributeName(),
117 actual.getAttributeName());
118
119 assertEquals("type is correct", expected.getType(), actual.getType());
120
121 assertEquals("valueDefault is correct", expected.getValueDefault(),
122 actual.getValueDefault());
123
124 assertEquals("toString() is correct", expected.getText(), actual
125 .toString());
126 }
127
128 /**
129 * Helper is useful since we are trying to exhaustively test the ATTLIST
130 * variations and their correct serialization.
131 */
132 protected static class MyTestAttributeDecl {
133 private String elName;
134
135 private String attName;
136
137 private String declType;
138
139 private String defaultValue;
140
141 private String declValue;
142
143 private String txt;
144
145 /**
146 * DOCUMENT ME!
147 *
148 * @param elementName
149 * The name of the element whose attribute is being
150 * described.
151 * @param attributeName
152 * The name of the attribute.
153 * @param type
154 * The type of the declared attribute, e.g., CDATA, ID,
155 * IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN, NKTOKENS
156 * @param valueDefault
157 * The type of default that applies for this attribute
158 * declaration, e.g., #REQUIRED, #IMPLIED, #FIXED (in which
159 * case the <i>value </i> MUST be non- <code>null</code>
160 * and specifies the fixed value for the attribute, or
161 * <code>null</code> if no valueDefault was specified in
162 * the attribute declaration (in which case the <i>value </i>
163 * MUST be non- <code>null</code> and specifies the default
164 * value for the attribute).
165 * @param value
166 * The value of the attribute assigned in the attribute
167 * declaration -or- <code>null</code> if no value was
168 * provided in the attribute declaration. The value MUST be
169 * <code>null</code> unless the <i>valueDefault </i> is
170 * either "#FIXED" or <code>null</code>.
171 * @param text
172 * The text representation of the attribute declaration,
173 * e.g., <code><!ATTLIST foo id ID #IMPLIED></code>.
174 *
175 * @todo The constructor and properties in {@link AttributeDecl}should
176 * have some similar javadoc so that people more easily understand
177 * the interaction and difference between the <i>valueDefault </i>
178 * and <i>value </i> properties. The constructor SHOULD be clear
179 * about whether and when the <code>valueDefault</code> and
180 * <code>value</code> properties MUST be <code>null</code>.
181 */
182 public MyTestAttributeDecl(String elementName, String attributeName,
183 String type, String valueDefault, String value, String text) {
184 elName = elementName;
185
186 attName = attributeName;
187
188 declType = type;
189
190 defaultValue = valueDefault;
191
192 declValue = value;
193
194 txt = text;
195 }
196
197 public String getElementName() {
198 return elName;
199 }
200
201 public String getAttributeName() {
202 return attName;
203 }
204
205 public String getType() {
206 return declType;
207 }
208
209 public String getValueDefault() {
210 return defaultValue;
211 }
212
213 public String getValue() {
214 return declValue;
215 }
216
217 public String getText() {
218 return txt;
219 }
220 } // Class TestAttributeDecl
221 }
222
223 /*
224 * Redistribution and use of this software and associated documentation
225 * ("Software"), with or without modification, are permitted provided that the
226 * following conditions are met:
227 *
228 * 1. Redistributions of source code must retain copyright statements and
229 * notices. Redistributions must also contain a copy of this document.
230 *
231 * 2. Redistributions in binary form must reproduce the above copyright notice,
232 * this list of conditions and the following disclaimer in the documentation
233 * and/or other materials provided with the distribution.
234 *
235 * 3. The name "DOM4J" must not be used to endorse or promote products derived
236 * from this Software without prior written permission of MetaStuff, Ltd. For
237 * written permission, please contact dom4j-info@metastuff.com.
238 *
239 * 4. Products derived from this Software may not be called "DOM4J" nor may
240 * "DOM4J" appear in their names without prior written permission of MetaStuff,
241 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
242 *
243 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
244 *
245 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
246 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
247 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
249 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
250 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
253 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
254 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
255 * POSSIBILITY OF SUCH DAMAGE.
256 *
257 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
258 */