AttributeDetachTest 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 detach() method on attributes
    17   *
    18   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    19   * @version $Revision: 1.3 $
    20   */
    21  public class AttributeDetachTest extends AbstractTestCase {
    22      public static void main(String[] args) {
    23          TestRunner.run(AttributeDetachTest.class);
    24      }
    25
    26      // Test case(s)
    27      // -------------------------------------------------------------------------
    28      public void testDetachAttribute() throws Exception {
    29          List attributes = document.selectNodes("//@name");
    30
    31          assertTrue("Found more than one attribute: ", attributes.size() > 0);
    32
    33          for (Iterator iter = attributes.iterator(); iter.hasNext();) {
    34              Attribute attribute = (Attribute) iter.next();
    35              Element element = attribute.getParent();
    36
    37              assertTrue("Attribute: " + attribute + " has parent: " + element,
    38                      attribute.getParent() == element);
    39
    40              QName qname = attribute.getQName();
    41
    42              Attribute attribute2 = element.attribute(qname);
    43
    44              String value = attribute.getValue();
    45              String value2 = element.attributeValue(qname);
    46
    47              assertEquals("Attribute and Element have same attrbute value",
    48                      value, value2);
    49
    50              attribute.detach();
    51
    52              attribute2 = element.attribute(qname);
    53              value2 = element.attributeValue(qname);
    54
    55              assertTrue("Element now has no value: " + value2, value2 == null);
    56              assertTrue("Element now has no attribute: " + attribute2,
    57                      attribute2 == null);
    58          }
    59      }
    60  }
    61
    62  /*
    63   * Redistribution and use of this software and associated documentation
    64   * ("Software"), with or without modification, are permitted provided that the
    65   * following conditions are met:
    66   *
    67   * 1. Redistributions of source code must retain copyright statements and
    68   * notices. Redistributions must also contain a copy of this document.
    69   *
    70   * 2. Redistributions in binary form must reproduce the above copyright notice,
    71   * this list of conditions and the following disclaimer in the documentation
    72   * and/or other materials provided with the distribution.
    73   *
    74   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    75   * from this Software without prior written permission of MetaStuff, Ltd. For
    76   * written permission, please contact dom4j-info@metastuff.com.
    77   *
    78   * 4. Products derived from this Software may not be called "DOM4J" nor may
    79   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    80   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    81   *
    82   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    83   *
    84   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    85   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    86   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    87   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    88   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    89   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    90   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    91   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    92   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    93   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    94   * POSSIBILITY OF SUCH DAMAGE.
    95   *
    96   * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    97   */