You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLFragmentTest.java 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.util;
  18. import org.apache.tools.ant.BuildFileTest;
  19. import org.w3c.dom.Element;
  20. import org.w3c.dom.Node;
  21. import org.w3c.dom.NodeList;
  22. public class XMLFragmentTest extends BuildFileTest {
  23. public XMLFragmentTest(String name) {
  24. super(name);
  25. }
  26. public void setUp() {
  27. configureProject("src/etc/testcases/types/xmlfragment.xml");
  28. }
  29. public void testNestedText() {
  30. XMLFragment x = (XMLFragment) getProject().getReference("nested-text");
  31. assertNotNull(x);
  32. Node n = x.getFragment();
  33. assertTrue("No attributes", !n.hasAttributes());
  34. NodeList nl = n.getChildNodes();
  35. assertEquals(1, nl.getLength());
  36. assertEquals(Node.TEXT_NODE, nl.item(0).getNodeType());
  37. assertEquals("foo", nl.item(0).getNodeValue());
  38. }
  39. public void testNestedChildren() {
  40. XMLFragment x =
  41. (XMLFragment) getProject().getReference("with-children");
  42. assertNotNull(x);
  43. Node n = x.getFragment();
  44. assertTrue("No attributes", !n.hasAttributes());
  45. NodeList nl = n.getChildNodes();
  46. assertEquals(3, nl.getLength());
  47. assertEquals(Node.ELEMENT_NODE, nl.item(0).getNodeType());
  48. Element child1 = (Element) nl.item(0);
  49. assertEquals("child1", child1.getTagName());
  50. assertTrue(!child1.hasAttributes());
  51. NodeList nl2 = child1.getChildNodes();
  52. assertEquals(1, nl2.getLength());
  53. assertEquals(Node.TEXT_NODE, nl2.item(0).getNodeType());
  54. assertEquals("foo", nl2.item(0).getNodeValue());
  55. assertEquals(Node.ELEMENT_NODE, nl.item(1).getNodeType());
  56. Element child2 = (Element) nl.item(1);
  57. assertEquals("child2", child2.getTagName());
  58. assertTrue(child2.hasAttributes());
  59. nl2 = child2.getChildNodes();
  60. assertEquals(0, nl2.getLength());
  61. assertEquals("bar", child2.getAttribute("foo"));
  62. assertEquals(Node.ELEMENT_NODE, nl.item(2).getNodeType());
  63. Element child3 = (Element) nl.item(2);
  64. assertEquals("child3", child3.getTagName());
  65. assertTrue(!child3.hasAttributes());
  66. nl2 = child3.getChildNodes();
  67. assertEquals(1, nl2.getLength());
  68. assertEquals(Node.ELEMENT_NODE, nl2.item(0).getNodeType());
  69. assertEquals("child4", ((Element) nl2.item(0)).getTagName());
  70. }
  71. }