1
2
3
4
5
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
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",
60 "bar",
61 "ID",
62 "#IMPLIED",
63 null,
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",
80 "bar",
81 "CDATA",
82 "#FIXED",
83 "goo",
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",
100 "bar",
101 "CDATA",
102 null,
103 "goo",
104 "<!ATTLIST foo bar CDATA \"goo\">");
105 assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "CDATA",
106 null, "goo"));
107 }
108
109
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 }
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258