MakeElementTest 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  /***
    13   * A test harness to test the DocumentHelper.makeElement() methodt
    14   *
    15   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    16   * @version $Revision: 1.3 $
    17   */
    18  public class MakeElementTest extends AbstractTestCase {
    19      public static void main(String[] args) {
    20          TestRunner.run(MakeElementTest.class);
    21      }
    22
    23      // Test case(s)
    24      // -------------------------------------------------------------------------
    25      public void testMakeElement() throws Exception {
    26          Document doc = DocumentHelper.createDocument();
    27
    28          Element c = DocumentHelper.makeElement(doc, "a/b/c");
    29          assertTrue("Should return a valid element", c != null);
    30
    31          Element c2 = DocumentHelper.makeElement(doc, "a/b/c");
    32
    33          assertTrue("Found same element again", c == c2);
    34
    35          c.addAttribute("x", "123");
    36
    37          Node found = doc.selectSingleNode("/a/b/c[@x='123']");
    38
    39          assertEquals("Found same node via XPath", c, found);
    40
    41          Element b = c.getParent();
    42
    43          Element e = DocumentHelper.makeElement(b, "c/d/e");
    44
    45          assertTrue("Should return a valid element", e != null);
    46
    47          Element e2 = DocumentHelper.makeElement(b, "c/d/e");
    48
    49          assertTrue("Found same element again", e == e2);
    50
    51          e.addAttribute("y", "456");
    52
    53          found = b.selectSingleNode("c/d/e[@y='456']");
    54
    55          assertEquals("Found same node via XPath", e, found);
    56      }
    57
    58      public void testMakeQualifiedElement() throws Exception {
    59          Document doc = DocumentHelper.createDocument();
    60          Element root = doc.addElement("root");
    61          root.addNamespace("", "defaultURI");
    62          root.addNamespace("foo", "fooURI");
    63          root.addNamespace("bar", "barURI");
    64
    65          Element c = DocumentHelper.makeElement(doc, "root/foo:b/bar:c");
    66          assertTrue("Should return a valid element", c != null);
    67
    68          assertEquals("c has a valid namespace", "barURI", c.getNamespaceURI());
    69
    70          Element b = c.getParent();
    71
    72          assertEquals("b has a valid namespace", "fooURI", b.getNamespaceURI());
    73
    74          log("Created: " + c);
    75
    76          Element c2 = DocumentHelper.makeElement(doc, "root/foo:b/bar:c");
    77          assertTrue("Found same element again", c == c2);
    78      }
    79  }
    80
    81  /*
    82   * Redistribution and use of this software and associated documentation
    83   * ("Software"), with or without modification, are permitted provided that the
    84   * following conditions are met:
    85   *
    86   * 1. Redistributions of source code must retain copyright statements and
    87   * notices. Redistributions must also contain a copy of this document.
    88   *
    89   * 2. Redistributions in binary form must reproduce the above copyright notice,
    90   * this list of conditions and the following disclaimer in the documentation
    91   * and/or other materials provided with the distribution.
    92   *
    93   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    94   * from this Software without prior written permission of MetaStuff, Ltd. For
    95   * written permission, please contact dom4j-info@metastuff.com.
    96   *
    97   * 4. Products derived from this Software may not be called "DOM4J" nor may
    98   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    99   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    100  *
    101  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    102  *
    103  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    104  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    105  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    106  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    107  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    108  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    109  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    110  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    111  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    112  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    113  * POSSIBILITY OF SUCH DAMAGE.
    114  *
    115  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    116  */