1
2
3
4
5
6
7
8
9
10 package org.dom4j.dtd;
11
12 import junit.framework.*;
13 import junit.textui.TestRunner;
14
15 /*** Tests the {@link AttributeDecl} functionality. Tests each of the
16 * property access methods and the serialization mechanisms. Correct
17 * parsing is tested by {@link TestDocType}.<p>
18 *
19 * There are several key variations that need to be tested both here
20 * and in {@link TestDocType}, which is responsible for testing
21 * correct parsing of the {@link DocumentType}. Those variations
22 * include the different <code>valueDefault</code> and
23 * <code>value</code> variations so that we can test for correct
24 * acceptance and correct rejection of attribute declarations.<p>
25 *
26 * @todo The dom4j documentation needs to describe what
27 * representation SHOULD be generated by {@link
28 * AttributeDecl#toString()}.
29 *
30 * @todo The dom4j AttributeDecl should expose some methods that make
31 * it easier for people to use the DTD grammar, e.g., isFixed(),
32 * isRequired(), isImplied().
33 *
34 * @author Bryan Thompson
35 * @author Maarten Coene
36 * @version $Revision: 1.3 $
37 */
38
39 public class TestAttributeDecl extends TestCase {
40
41
42 public static void main( String[] args ) {
43 TestRunner.run( suite() );
44 }
45
46 public static Test suite() {
47 return new TestSuite( TestAttributeDecl.class );
48 }
49
50 public TestAttributeDecl(String name) {
51 super(name);
52 }
53
54
55
56
57 /***
58 * Test <pre><!ATTLIST foo bar ID #IMPLIED></pre>.
59 */
60
61 public void test_ID_IMPLIED_none()
62 {
63
64 assertSameAttributeDecl
65 ( new MyTestAttributeDecl
66 ( "foo",
67 "bar",
68 "ID",
69 "#IMPLIED",
70 null,
71 "<!ATTLIST foo bar ID #IMPLIED>"
72 ),
73 new AttributeDecl
74 ( "foo",
75 "bar",
76 "ID",
77 "#IMPLIED",
78 null
79 )
80 );
81 }
82
83 /***
84 * Test <pre><!ATTLIST foo bar CDATA #FIXED \"goo\"></pre>.
85 */
86
87 public void test_CDATA_Fixed_value()
88 {
89
90 assertSameAttributeDecl
91 ( new MyTestAttributeDecl
92 ( "foo",
93 "bar",
94 "CDATA",
95 "#FIXED",
96 "goo",
97 "<!ATTLIST foo bar CDATA #FIXED \"goo\">"
98 ),
99 new AttributeDecl
100 ( "foo",
101 "bar",
102 "CDATA",
103 "#FIXED",
104 "goo"
105 )
106 );
107
108 }
109
110 /***
111 * Test <pre><!ATTLIST foo bar CDATA "goo"></pre>.
112 */
113
114 public void test_CDATA_none_value()
115 {
116
117 assertSameAttributeDecl
118 ( new MyTestAttributeDecl
119 ( "foo",
120 "bar",
121 "CDATA",
122 null,
123 "goo",
124 "<!ATTLIST foo bar CDATA \"goo\">"
125 ),
126 new AttributeDecl
127 ( "foo",
128 "bar",
129 "CDATA",
130 null,
131 "goo"
132 )
133 );
134
135 }
136
137
138
139
140 protected void assertSameAttributeDecl
141 ( MyTestAttributeDecl expected,
142 AttributeDecl actual
143 )
144 {
145
146 assertEquals
147 ( "elementName is correct",
148 expected.getElementName(),
149 actual.getElementName()
150 );
151
152 assertEquals
153 ( "attributeName is correct",
154 expected.getAttributeName(),
155 actual.getAttributeName()
156 );
157
158 assertEquals
159 ( "type is correct",
160 expected.getType(),
161 actual.getType()
162 );
163
164 assertEquals
165 ( "valueDefault is correct",
166 expected.getValueDefault(),
167 actual.getValueDefault()
168 );
169
170 assertEquals
171 ( "toString() is correct",
172 expected.getText(),
173 actual.toString()
174 );
175
176 }
177
178
179 /***
180 * Helper is useful since we are trying to exhaustively test the
181 * ATTLIST variations and their correct serialization.
182 */
183
184 protected static class MyTestAttributeDecl
185 {
186
187 String m_elementName;
188 String m_attributeName;
189 String m_type;
190 String m_valueDefault;
191 String m_value;
192 String m_text;
193
194 public String getElementName() {return m_elementName;}
195 public String getAttributeName() {return m_attributeName;}
196 public String getType() {return m_type;}
197 public String getValueDefault() {return m_valueDefault;}
198 public String getValue() {return m_value;}
199 public String getText() {return m_text;}
200
201 /***
202 * @param elementName The name of the element whose attribute
203 * is being described.
204 *
205 * @param attributeName The name of the attribute.
206 *
207 * @param type The type of the declared attribute, e.g.,
208 * CDATA, ID, IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN,
209 * NKTOKENS
210 *
211 * @param valueDefault The type of default that applies for
212 * this attribute declaration, e.g., #REQUIRED, #IMPLIED,
213 * #FIXED (in which case the <i>value</i> MUST be
214 * non-<code>null</code> and specifies the fixed value for the
215 * attribute</code>, or <code>null</code> if no valueDefault
216 * was specified in the attribute declaration (in which case
217 * the <i>value</i> MUST be non-<code>null</code> and
218 * specifies the default value for the attribute).
219 *
220 * @param value The value of the attribute assigned in the
221 * attribute declaration -or- <code>null</code> if no value
222 * was provided in the attribute declaration. The value MUST
223 * be <code>null</code> unless the <i>valueDefault</i> is
224 * either "#FIXED" or <code>null</code>.
225 *
226 * @param text The text representation of the attribute
227 * declaration, e.g., <code><!ATTLIST foo id ID #IMPLIED></code>.
228 *
229 * @todo The constructor and properties in {@link
230 * AttributeDecl} should have some similar javadoc so that
231 * people more easily understand the interaction and
232 * difference between the <i>valueDefault</i> and <i>value</i>
233 * properties. The constructor SHOULD be clear about whether
234 * and when the <code>valueDefault</code> and
235 * <code>value</code> properties MUST be <code>null</code>.
236 */
237
238 public MyTestAttributeDecl
239 ( String elementName,
240 String attributeName,
241 String type,
242 String valueDefault,
243 String value,
244 String text
245 )
246 {
247
248 m_elementName = elementName;
249
250 m_attributeName = attributeName;
251
252 m_type = type;
253
254 m_valueDefault = valueDefault;
255
256 m_value = value;
257
258 m_text = text;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 }
289
290 }
291
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339