    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.List;
    13
    14  import org.dom4j.io.XMLWriter;
    15
    16  /***
    17   * A test harness to test the backed list feature of DOM4J
    18   *
    19   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    20   * @version $Revision: 1.3 $
    21   */
    22  public class BackedListTest extends AbstractTestCase {
    23      public static void main(String[] args) {
    24          TestRunner.run(BackedListTest.class);
    25      }
    26
    27      // Test case(s)
    28      // -------------------------------------------------------------------------
    29      public void testXPaths() throws Exception {
    30          Element element = (Element) document.selectSingleNode("/root");
    31          mutate(element);
    32          element = (Element) document.selectSingleNode("//author");
    33          mutate(element);
    34      }
    35
    36      public void testAddRemove() throws Exception {
    37          Element parentElement = (Element) document.selectSingleNode("/root");
    38          List children = parentElement.elements();
    39          int lastPos = children.size() - 1;
    40          Element child = (Element) children.get(lastPos);
    41
    42          try {
    43              // should throw an exception cause we cannot add same child twice
    44              children.add(0, child);
    45              fail();
    46          } catch (IllegalAddException e) {
    47          }
    48      }
    49
    50      public void testAddWithIndex() throws Exception {
    51          DocumentFactory factory = DocumentFactory.getInstance();
    52
    53          Element root = (Element) document.selectSingleNode("/root");
    54          List children = root.elements(); // return a list of 2 author
    55          // elements
    56
    57          assertEquals(2, children.size());
    58
    59          children.add(1, factory.createElement("dummy1"));
    60          children = root.elements();
    61
    62          assertEquals(3, children.size());
    63
    64          children = root.elements("author");
    65
    66          assertEquals(2, children.size());
    67
    68          children.add(1, factory.createElement("dummy2"));
    69
    70          children = root.elements();
    71
    72          assertEquals(4, children.size());
    73          assertEquals("dummy1", ((Node) children.get(1)).getName());
    74          assertEquals("dummy2", ((Node) children.get(2)).getName());
    75
    76          /*
    77           * Some tests for issue reported at http://tinyurl.com/4jxrc
    78           */
    79          children.add(children.size(), factory.createElement("dummy3"));
    80          children = root.elements("author");
    81          children.add(children.size(), factory.createElement("dummy4"));
    82      }
    83
    84      // Implementation methods
    85      // -------------------------------------------------------------------------
    86      protected void mutate(Element element) throws Exception {
    87          DocumentFactory factory = DocumentFactory.getInstance();
    88
    89          List list = element.elements();
    90          list.add(factory.createElement("last"));
    91          list.add(0, factory.createElement("first"));
    92
    93          List list2 = element.elements();
    94
    95          assertTrue("Both lists should contain same number of elements", list
    96                  .size() == list2.size());
    97
    98          XMLWriter writer = new XMLWriter(System.out);
    99
    100         log("Element content is now: " + element.content());
    101         writer.write(element);
    102     }
    103 }
    104
    105 /*
    106  * Redistribution and use of this software and associated documentation
    107  * ("Software"), with or without modification, are permitted provided that the
    108  * following conditions are met:
    109  *
    110  * 1. Redistributions of source code must retain copyright statements and
    111  * notices. Redistributions must also contain a copy of this document.
    112  *
    113  * 2. Redistributions in binary form must reproduce the above copyright notice,
    114  * this list of conditions and the following disclaimer in the documentation
    115  * and/or other materials provided with the distribution.
    116  *
    117  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    118  * from this Software without prior written permission of MetaStuff, Ltd. For
    119  * written permission, please contact dom4j-info@metastuff.com.
    120  *
    121  * 4. Products derived from this Software may not be called "DOM4J" nor may
    122  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    123  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    124  *
    125  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    126  *
    127  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    128  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    129  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    130  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    131  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    132  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    133  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    134  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    135  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    136  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    137  * POSSIBILITY OF SUCH DAMAGE.
    138  *
    139  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    140  */