ContentTest xref

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.util.Iterator;
    13  import java.util.List;
    14
    15  /***
    16   * A test harness to test the content API in DOM4J
    17   *
    18   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    19   * @version $Revision: 1.3 $
    20   */
    21  public class ContentTest extends AbstractTestCase {
    22      protected DocumentFactory factory = new DocumentFactory();
    23
    24      public static void main(String[] args) {
    25          TestRunner.run(ContentTest.class);
    26      }
    27
    28      // Test case(s)
    29      // -------------------------------------------------------------------------
    30      public void testRoot() throws Exception {
    31          Element root = document.getRootElement();
    32          assertTrue("Has root element", root != null);
    33
    34          List authors = root.elements("author");
    35          assertTrue("Root has children", (authors != null)
    36                  && (authors.size() == 2));
    37
    38          Element author1 = (Element) authors.get(0);
    39          Element author2 = (Element) authors.get(1);
    40
    41          assertTrue("Author1 is James", author1.attributeValue("name").equals(
    42                  "James"));
    43          assertTrue("Author2 is Bob", author2.attributeValue("name").equals(
    44                  "Bob"));
    45
    46          testGetAttributes(author1);
    47          testGetAttributes(author2);
    48      }
    49
    50      public void testContent() throws Exception {
    51          Element root = document.getRootElement();
    52          assertTrue("Has root element", root != null);
    53
    54          List content = root.content();
    55          assertTrue("Root has content", (content != null)
    56                  && (content.size() >= 2));
    57
    58          boolean iterated = false;
    59
    60          for (Iterator iter = content.iterator(); iter.hasNext();) {
    61              Object object = iter.next();
    62              assertTrue("Content object is a node", object instanceof Node);
    63              iterated = true;
    64          }
    65
    66          assertTrue("Iteration completed", iterated);
    67      }
    68
    69      public void testGetNode() throws Exception {
    70          Element root = document.getRootElement();
    71          assertTrue("Has root element", root != null);
    72
    73          int count = root.nodeCount();
    74          assertTrue("Root has correct node count", count == 2);
    75
    76          boolean iterated = false;
    77
    78          for (int i = 0; i < count; i++) {
    79              Node node = root.node(i);
    80              assertTrue("Valid node returned from node()", node != null);
    81              iterated = true;
    82          }
    83
    84          assertTrue("Iteration completed", iterated);
    85      }
    86
    87      public void testGetXPathNode() throws Exception {
    88          Element root = document.getRootElement();
    89          assertTrue("Has root element", root != null);
    90
    91          int count = root.nodeCount();
    92          assertTrue("Root has correct node count", count == 2);
    93
    94          boolean iterated = false;
    95
    96          for (int i = 0; i < count; i++) {
    97              Node node = root.getXPathResult(i);
    98              assertTrue("Valid node returned from node()", node != null);
    99              assertTrue("Node supports the parent relationship", node
    100                     .supportsParent());
    101             iterated = true;
    102         }
    103
    104         assertTrue("Iteration completed", iterated);
    105     }
    106
    107     public void testOrderOfPI() throws Exception {
    108         Document document = factory.createDocument();
    109         document.addProcessingInstruction("xml-stylesheet",
    110                 "type=\"text/xsl\" href=\"...\"");
    111         document.addElement("root");
    112
    113         List list = document.content();
    114
    115         assertNotNull(list);
    116         assertEquals(2, list.size());
    117
    118         Object pi = list.get(0);
    119         Object root = list.get(1);
    120
    121         assertTrue("First element is not a PI",
    122                 pi instanceof ProcessingInstruction);
    123         assertTrue("Second element is an element", root instanceof Element);
    124
    125         String xml = "<?xml version=\"1.0\" ?>\n"
    126                 + "<?xml-stylesheet type=\"text/xsl\" href=\"foo\" ?>\n"
    127                 + "<root/>";
    128         document = DocumentHelper.parseText(xml);
    129
    130         list = document.content();
    131
    132         assertNotNull(list);
    133         assertEquals(2, list.size());
    134         pi = list.get(0);
    135         root = list.get(1);
    136
    137         assertTrue("First element is not a PI",
    138                 pi instanceof ProcessingInstruction);
    139         assertTrue("Second element is an element", root instanceof Element);
    140     }
    141
    142     public void testAddingInTheMiddle() throws Exception {
    143         Document doc = factory.createDocument();
    144         Element root = doc.addElement("html");
    145         Element header = root.addElement("header");
    146         Element footer = root.addElement("footer");
    147
    148         // now lets add <foo> in between header & footer
    149         List list = root.content();
    150         Element foo = factory.createElement("foo");
    151         list.add(1, foo);
    152
    153         // assertions
    154         assertTrue(list.size() == 3);
    155         assertTrue(list.get(0) == header);
    156         assertTrue(list.get(1) == foo);
    157         assertTrue(list.get(2) == footer);
    158     }
    159
    160     public void testAddAtIndex() throws Exception {
    161         Document doc = factory.createDocument();
    162         Element root = doc.addElement("html");
    163         Element header = root.addElement("header");
    164         Element body = root.addElement("body");
    165
    166         Element foo = factory.createElement("foo");
    167         Element bar = factory.createElement("bar");
    168
    169         List content = header.content();
    170         content.add(0, foo);
    171         content.add(0, bar);
    172
    173         assertEquals("foo", header.node(1).getName());
    174         assertEquals("bar", header.node(0).getName());
    175
    176         foo = factory.createElement("foo");
    177         bar = factory.createElement("bar");
    178
    179         content = body.content();
    180         content.add(0, foo);
    181         content.add(1, bar);
    182
    183         assertEquals("foo", body.node(0).getName());
    184         assertEquals("bar", body.node(1).getName());
    185     }
    186
    187     public void testAddAtIndex2() throws Exception {
    188         Document doc = factory.createDocument();
    189         Element parent = doc.addElement("parent");
    190         Element child = parent.addElement("child");
    191         Element anotherChild = factory.createElement("child2");
    192
    193         List elements = parent.elements();
    194         int index = elements.indexOf(child);
    195
    196         assertEquals(0, index);
    197
    198         elements.add(1, anotherChild);
    199         elements = parent.elements();
    200         assertEquals(child, elements.get(0));
    201         assertEquals(anotherChild, elements.get(1));
    202     }
    203
    204     // Implementation methods
    205     // -------------------------------------------------------------------------
    206     protected void testGetAttributes(Element author) throws Exception {
    207         String definedName = "name";
    208         String undefinedName = "undefined-attribute-name";
    209         String defaultValue = "** Default Value **";
    210
    211         String value = author.attributeValue(definedName, defaultValue);
    212         assertTrue("Defined value doesn't return specified default value",
    213                 value != defaultValue);
    214
    215         value = author.attributeValue(undefinedName, defaultValue);
    216         assertTrue("Undefined value returns specified default value",
    217                 value == defaultValue);
    218     }
    219 }
    220
    221 /*
    222  * Redistribution and use of this software and associated documentation
    223  * ("Software"), with or without modification, are permitted provided that the
    224  * following conditions are met:
    225  *
    226  * 1. Redistributions of source code must retain copyright statements and
    227  * notices. Redistributions must also contain a copy of this document.
    228  *
    229  * 2. Redistributions in binary form must reproduce the above copyright notice,
    230  * this list of conditions and the following disclaimer in the documentation
    231  * and/or other materials provided with the distribution.
    232  *
    233  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    234  * from this Software without prior written permission of MetaStuff, Ltd. For
    235  * written permission, please contact dom4j-info@metastuff.com.
    236  *
    237  * 4. Products derived from this Software may not be called "DOM4J" nor may
    238  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    239  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    240  *
    241  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    242  *
    243  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    244  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    245  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    246  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    247  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    248  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    249  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    250  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    251  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    252  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    253  * POSSIBILITY OF SUCH DAMAGE.
    254  *
    255  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    256  */