01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.tools.ant.util;
20:
21: import org.apache.tools.ant.BuildFileTest;
22: import org.w3c.dom.Element;
23: import org.w3c.dom.Node;
24: import org.w3c.dom.NodeList;
25:
26: public class XMLFragmentTest extends BuildFileTest {
27:
28: public XMLFragmentTest(String name) {
29: super (name);
30: }
31:
32: public void setUp() {
33: configureProject("src/etc/testcases/types/xmlfragment.xml");
34: }
35:
36: public void testNestedText() {
37: XMLFragment x = (XMLFragment) getProject().getReference(
38: "nested-text");
39: assertNotNull(x);
40: Node n = x.getFragment();
41: assertTrue("No attributes", !n.hasAttributes());
42: NodeList nl = n.getChildNodes();
43: assertEquals(1, nl.getLength());
44: assertEquals(Node.TEXT_NODE, nl.item(0).getNodeType());
45: assertEquals("foo", nl.item(0).getNodeValue());
46: }
47:
48: public void testNestedChildren() {
49: XMLFragment x = (XMLFragment) getProject().getReference(
50: "with-children");
51: assertNotNull(x);
52: Node n = x.getFragment();
53: assertTrue("No attributes", !n.hasAttributes());
54: NodeList nl = n.getChildNodes();
55: assertEquals(3, nl.getLength());
56:
57: assertEquals(Node.ELEMENT_NODE, nl.item(0).getNodeType());
58: Element child1 = (Element) nl.item(0);
59: assertEquals("child1", child1.getTagName());
60: assertTrue(!child1.hasAttributes());
61: NodeList nl2 = child1.getChildNodes();
62: assertEquals(1, nl2.getLength());
63: assertEquals(Node.TEXT_NODE, nl2.item(0).getNodeType());
64: assertEquals("foo", nl2.item(0).getNodeValue());
65:
66: assertEquals(Node.ELEMENT_NODE, nl.item(1).getNodeType());
67: Element child2 = (Element) nl.item(1);
68: assertEquals("child2", child2.getTagName());
69: assertTrue(child2.hasAttributes());
70: nl2 = child2.getChildNodes();
71: assertEquals(0, nl2.getLength());
72: assertEquals("bar", child2.getAttribute("foo"));
73:
74: assertEquals(Node.ELEMENT_NODE, nl.item(2).getNodeType());
75: Element child3 = (Element) nl.item(2);
76: assertEquals("child3", child3.getTagName());
77: assertTrue(!child3.hasAttributes());
78: nl2 = child3.getChildNodes();
79: assertEquals(1, nl2.getLength());
80: assertEquals(Node.ELEMENT_NODE, nl2.item(0).getNodeType());
81: assertEquals("child4", ((Element) nl2.item(0)).getTagName());
82: }
83: }
|