KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > NodeCreateRuleTestCase


1 /* $Id: NodeCreateRuleTestCase.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester;
20
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.DocumentFragment JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42
43 /**
44  * <p>Test case for the <code>NodeCreateRule</code>.
45  *
46  * @author Christopher Lenz
47  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
48  */

49
50 public class NodeCreateRuleTestCase extends TestCase {
51
52
53     // ----------------------------------------------------- Instance Variables
54

55     /**
56      * Simple test xml document used in the tests.
57      */

58     protected final static String JavaDoc TEST_XML =
59         "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha>" +
60         "<beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>";
61
62
63     /**
64      * The digester instance we will be processing.
65      */

66     protected Digester digester = null;
67
68
69     // ----------------------------------------------------------- Constructors
70

71
72     /**
73      * Construct a new instance of this test case.
74      *
75      * @param name Name of the test case
76      */

77     public NodeCreateRuleTestCase(String JavaDoc name) {
78
79         super(name);
80
81     }
82
83
84     // --------------------------------------------------- Overall Test Methods
85

86
87     /**
88      * Set up instance variables required by this test case.
89      */

90     public void setUp() {
91
92         digester = new Digester();
93
94     }
95
96
97     /**
98      * Return the tests included in this test suite.
99      */

100     public static Test suite() {
101
102         return (new TestSuite(NodeCreateRuleTestCase.class));
103
104     }
105
106
107     /**
108      * Tear down instance variables required by this test case.
109      */

110     public void tearDown() {
111
112         digester = null;
113
114     }
115
116
117
118     // ------------------------------------------------ Individual Test Methods
119

120
121     /**
122      * Tests simple element construction, using the {@link TEST_XML TEST_XML}
123      * XML input data.
124      */

125     public void testInvalidNodeTypes()
126         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
127
128         try {
129             Rule rule = new NodeCreateRule(Node.ATTRIBUTE_NODE);
130             fail("IllegalArgumentException expected for type ATTRIBUTE_NODE");
131         } catch (IllegalArgumentException JavaDoc iae) {
132             // expected
133
}
134         try {
135             Rule rule = new NodeCreateRule(Node.CDATA_SECTION_NODE);
136             fail("IllegalArgumentException expected for type " +
137                  "CDATA_SECTION_NODE");
138         } catch (IllegalArgumentException JavaDoc iae) {
139             // expected
140
}
141         try {
142             Rule rule = new NodeCreateRule(Node.COMMENT_NODE);
143             fail("IllegalArgumentException expected for type COMMENT_NODE");
144         } catch (IllegalArgumentException JavaDoc iae) {
145             // expected
146
}
147         try {
148             Rule rule = new NodeCreateRule(Node.DOCUMENT_NODE);
149             fail("IllegalArgumentException expected for type DOCUMENT_NODE");
150         } catch (IllegalArgumentException JavaDoc iae) {
151             // expected
152
}
153         try {
154             Rule rule = new NodeCreateRule(Node.DOCUMENT_TYPE_NODE);
155             fail("IllegalArgumentException expected for type " +
156                  "DOCUMENT_TYPE_NODE");
157         } catch (IllegalArgumentException JavaDoc iae) {
158             // expected
159
}
160         try {
161             Rule rule = new NodeCreateRule(Node.ENTITY_NODE);
162             fail("IllegalArgumentException expected for type ENTITY_NODE");
163         } catch (IllegalArgumentException JavaDoc iae) {
164             // expected
165
}
166         try {
167             Rule rule = new NodeCreateRule(Node.ENTITY_REFERENCE_NODE);
168             fail("IllegalArgumentException expected for type " +
169                  "ENTITY_REFERENCE_NODE");
170         } catch (IllegalArgumentException JavaDoc iae) {
171             // expected
172
}
173         try {
174             Rule rule = new NodeCreateRule(Node.NOTATION_NODE);
175             fail("IllegalArgumentException expected for type NOTATION_NODE");
176         } catch (IllegalArgumentException JavaDoc iae) {
177             // expected
178
}
179         try {
180             Rule rule = new NodeCreateRule(Node.PROCESSING_INSTRUCTION_NODE);
181             fail("IllegalArgumentException expected for type " +
182                  "PROCESSING_INSTRUCTION_NODE");
183         } catch (IllegalArgumentException JavaDoc iae) {
184             // expected
185
}
186         try {
187             Rule rule = new NodeCreateRule(Node.TEXT_NODE);
188             fail("IllegalArgumentException expected for type TEXT_NODE");
189         } catch (IllegalArgumentException JavaDoc iae) {
190             // expected
191
}
192
193     }
194
195     /**
196      * Tests simple element construction, using the {@link TEST_XML TEST_XML}
197      * XML input data.
198      */

199     public void testElement()
200         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
201
202         digester.addRule("root/alpha", new NodeCreateRule());
203         Object JavaDoc result = digester.parse(new StringReader JavaDoc(TEST_XML));
204
205         assertNotNull(result);
206         assertTrue(result instanceof Element JavaDoc);
207         Element JavaDoc element = (Element JavaDoc)result;
208         assertEquals("alpha", element.getNodeName());
209         assertNull(((Element JavaDoc)element).getLocalName());
210         assertNull(((Element JavaDoc)element).getNamespaceURI());
211         assertEquals(1, element.getChildNodes().getLength());
212         assertEquals("ALPHA BODY", element.getFirstChild().getNodeValue());
213
214     }
215
216
217     /**
218      * Tests simple fragment construction, using the {@link TEST_XML TEST_XML}
219      * XML input data.
220      */

221     public void testDocumentFragment()
222         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
223
224         digester.addRule("root",
225                          new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
226         Object JavaDoc result = digester.parse(new StringReader JavaDoc(TEST_XML));
227
228         assertNotNull(result);
229         assertTrue(result instanceof DocumentFragment JavaDoc);
230         DocumentFragment JavaDoc fragment = (DocumentFragment JavaDoc)result;
231         assertEquals(4, fragment.getChildNodes().getLength());
232
233         Node JavaDoc rootBody = fragment.getFirstChild();
234         assertEquals(Node.TEXT_NODE, rootBody.getNodeType());
235         assertEquals("ROOT BODY", rootBody.getNodeValue());
236
237         Node JavaDoc alpha = fragment.getChildNodes().item(1);
238         assertEquals(Node.ELEMENT_NODE, alpha.getNodeType());
239         assertEquals("alpha", alpha.getNodeName());
240         assertNull(((Element JavaDoc)alpha).getLocalName());
241         assertNull(((Element JavaDoc)alpha).getNamespaceURI());
242         assertEquals(1, alpha.getChildNodes().getLength());
243         assertEquals("ALPHA BODY", alpha.getFirstChild().getNodeValue());
244
245         Node JavaDoc beta = fragment.getChildNodes().item(2);
246         assertEquals(Node.ELEMENT_NODE, beta.getNodeType());
247         assertEquals("beta", beta.getNodeName());
248         assertNull(((Element JavaDoc)beta).getLocalName());
249         assertNull(((Element JavaDoc)beta).getNamespaceURI());
250         assertEquals(1, beta.getChildNodes().getLength());
251         assertEquals("BETA BODY", beta.getFirstChild().getNodeValue());
252
253         Node JavaDoc gamma = fragment.getChildNodes().item(3);
254         assertEquals(Node.ELEMENT_NODE, gamma.getNodeType());
255         assertEquals("gamma", gamma.getNodeName());
256         assertNull(((Element JavaDoc)gamma).getLocalName());
257         assertNull(((Element JavaDoc)gamma).getNamespaceURI());
258         assertEquals(1, gamma.getChildNodes().getLength());
259         assertEquals("GAMMA BODY", gamma.getFirstChild().getNodeValue());
260
261     }
262
263
264     /**
265      * Tests whether control is returned to digester after fragment
266      * construction.
267      */

268     public void testNested()
269         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
270
271         digester.addObjectCreate("root", ArrayList JavaDoc.class);
272         digester.addRule("root/a/b",
273                          new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
274         digester.addSetRoot("root/a/b", "add");
275         digester.addObjectCreate("root/b", String JavaDoc.class);
276         digester.addSetRoot("root/b", "add");
277         Object JavaDoc result = digester.parse(getInputStream("Test4.xml"));
278
279         assertNotNull(result);
280         assertTrue(result instanceof List JavaDoc);
281         List JavaDoc list = (List JavaDoc)result;
282         assertEquals(2, list.size());
283
284         assertTrue(list.get(0) instanceof DocumentFragment JavaDoc);
285         DocumentFragment JavaDoc fragment = (DocumentFragment JavaDoc)list.get(0);
286
287         assertEquals(Node.ELEMENT_NODE,
288                      fragment.getFirstChild().getNodeType());
289         Element JavaDoc a = (Element JavaDoc)fragment.getFirstChild();
290         assertEquals("a", a.getNodeName());
291         assertEquals(1, a.getAttributes().getLength());
292         assertEquals("THREE", a.getAttribute("name"));
293
294         assertTrue(list.get(1) instanceof String JavaDoc);
295
296     }
297
298
299     /**
300      * Tests whether attributes are correctly imported into the fragment, using
301      * the example in the Test1 XML file.
302      */

303     public void testAttributes()
304         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
305
306         digester.addRule("employee",
307                          new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
308         Object JavaDoc result = digester.parse(getInputStream("Test1.xml"));
309
310         assertNotNull(result);
311         assertTrue(result instanceof DocumentFragment JavaDoc);
312         DocumentFragment JavaDoc fragment = (DocumentFragment JavaDoc)result;
313         assertEquals(2, fragment.getChildNodes().getLength());
314
315         assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType());
316         Element JavaDoc address1 = (Element JavaDoc)fragment.getFirstChild();
317         assertEquals("address", address1.getNodeName());
318         assertEquals(5, address1.getAttributes().getLength());
319         assertEquals("home", address1.getAttribute("type"));
320         assertEquals("Home Street", address1.getAttribute("street"));
321         assertEquals("Home City", address1.getAttribute("city"));
322         assertEquals("HS", address1.getAttribute("state"));
323         assertEquals("HmZip", address1.getAttribute("zipCode"));
324
325         assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType());
326         Element JavaDoc address2 = (Element JavaDoc)fragment.getLastChild();
327         assertEquals("address", address2.getNodeName());
328         assertEquals(5, address2.getAttributes().getLength());
329         assertEquals("office", address2.getAttribute("type"));
330         assertEquals("Office Street", address2.getAttribute("street"));
331         assertEquals("Office City", address2.getAttribute("city"));
332         assertEquals("OS", address2.getAttribute("state"));
333         assertEquals("OfZip", address2.getAttribute("zipCode"));
334
335     }
336
337
338     /**
339      * Tests whether namespaces are handled correctly, using the example from
340      * the file Test3 XML file.
341      */

342     public void testNamespaces()
343         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
344
345         digester.setNamespaceAware(true);
346         digester.setRuleNamespaceURI(null);
347         digester.addRule("employee",
348                          new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
349         Object JavaDoc result = digester.parse(getInputStream("Test3.xml"));
350
351         assertNotNull(result);
352         assertTrue(result instanceof DocumentFragment JavaDoc);
353         DocumentFragment JavaDoc fragment = (DocumentFragment JavaDoc)result;
354         assertEquals(2, fragment.getChildNodes().getLength());
355
356         assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType());
357         Element JavaDoc address1 = (Element JavaDoc)fragment.getFirstChild();
358         assertEquals("address", address1.getNodeName());
359         assertEquals("http://jakarta.apache.org/digester/Bar",
360                      address1.getNamespaceURI());
361         assertEquals("address", address1.getLocalName());
362         assertEquals(5, address1.getAttributes().getLength());
363         assertEquals("home", address1.getAttribute("type"));
364         assertEquals("Home Street", address1.getAttribute("street"));
365         assertEquals("Home City", address1.getAttribute("city"));
366         assertEquals("HS", address1.getAttribute("state"));
367         assertEquals("HmZip", address1.getAttribute("zipCode"));
368
369         assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType());
370         Element JavaDoc address2 = (Element JavaDoc)fragment.getLastChild();
371         assertEquals("address", address2.getNodeName());
372         assertEquals("http://jakarta.apache.org/digester/Bar",
373                      address2.getNamespaceURI());
374         assertEquals("address", address2.getLocalName());
375         assertEquals(5, address2.getAttributes().getLength());
376         assertEquals("office", address2.getAttribute("type"));
377         assertEquals("Office Street", address2.getAttribute("street"));
378         assertEquals("Office City", address2.getAttribute("city"));
379         assertEquals("OS", address2.getAttribute("state"));
380         assertEquals("OfZip", address2.getAttribute("zipCode"));
381
382     }
383
384
385     /**
386      * Tests whether the created fragment can be imported into an existing
387      * document.
388      */

389     public void testImport()
390         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
391
392         digester.addRule("root",
393                          new NodeCreateRule(Node.DOCUMENT_FRAGMENT_NODE));
394         Object JavaDoc result = digester.parse(new StringReader JavaDoc(TEST_XML));
395         DocumentFragment JavaDoc fragment = (DocumentFragment JavaDoc)result;
396
397         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
398         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
399         Document JavaDoc doc = builder.newDocument();
400         Node JavaDoc importedFragment = doc.importNode(fragment, true);
401         doc.appendChild(doc.createElement("root"));
402         doc.getFirstChild().appendChild(importedFragment);
403
404     }
405
406
407     // ------------------------------------------------ Utility Support Methods
408

409
410     /**
411      * Return an appropriate InputStream for the specified test file (which
412      * must be inside our current package.
413      *
414      * @param name Name of the test file we want
415      *
416      * @exception IOException if an input/output error occurs
417      */

418     protected InputStream JavaDoc getInputStream(String JavaDoc name) throws IOException JavaDoc {
419
420         return (this.getClass().getResourceAsStream
421                 ("/org/apache/commons/digester/" + name));
422
423     }
424
425
426 }
427
Popular Tags