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