AbstractTestCase 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.framework.TestCase;
    11
    12  import java.io.File;
    13
    14  import org.apache.xalan.processor.TransformerFactoryImpl;
    15  import org.apache.xerces.jaxp.SAXParserFactoryImpl;
    16
    17  import org.dom4j.io.SAXReader;
    18  import org.dom4j.util.NodeComparator;
    19
    20  /***
    21   * An abstract base class for some DOM4J test cases
    22   *
    23   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    24   * @version $Revision: 1.24 $
    25   */
    26  public class AbstractTestCase extends TestCase {
    27      protected Document document;
    28
    29      protected AbstractTestCase() {
    30          super();
    31      }
    32
    33      protected AbstractTestCase(String name) {
    34          super(name);
    35      }
    36
    37      protected void log(String text) {
    38          System.out.println(text);
    39      }
    40
    41      protected Document getDocument() {
    42          return document;
    43      }
    44
    45      protected Document getDocument(String path) throws Exception {
    46          return getDocument(path, new SAXReader());
    47      }
    48
    49      protected Document getDocument(String path, SAXReader reader)
    50              throws Exception {
    51          return reader.read(getFile(path));
    52      }
    53
    54      protected File getFile(String path) {
    55          return new File(System.getProperty("user.dir"), path);
    56      }
    57
    58      public void assertDocumentsEqual(Document doc1, Document doc2)
    59              throws Exception {
    60          try {
    61              assertTrue("Doc1 not null", doc1 != null);
    62              assertTrue("Doc2 not null", doc2 != null);
    63
    64              doc1.normalize();
    65              doc2.normalize();
    66
    67              assertNodesEqual(doc1, doc2);
    68
    69              NodeComparator comparator = new NodeComparator();
    70              assertTrue("Documents are equal",
    71                      comparator.compare(doc1, doc2) == 0);
    72          } catch (Exception e) {
    73              log("Failed during comparison of: " + doc1 + " and: " + doc2);
    74              throw e;
    75          }
    76      }
    77
    78      public void assertNodesEqual(Document n1, Document n2) {
    79          // assertEquals( "Document names", n1.getName(), n2.getName() );
    80          assertNodesEqual(n1.getDocType(), n2.getDocType());
    81          assertNodesEqualContent(n1, n2);
    82      }
    83
    84      public void assertNodesEqual(Element n1, Element n2) {
    85          assertNodesEqual(n1.getQName(), n2.getQName());
    86
    87          int c1 = n1.attributeCount();
    88          int c2 = n2.attributeCount();
    89
    90          assertEquals("Elements have same number of attributes (" + c1 + ", "
    91                  + c2 + " for: " + n1 + " and " + n2, c1, c2);
    92
    93          for (int i = 0; i < c1; i++) {
    94              Attribute a1 = n1.attribute(i);
    95              Attribute a2 = n2.attribute(a1.getQName());
    96              assertNodesEqual(a1, a2);
    97          }
    98
    99          assertNodesEqualContent(n1, n2);
    100     }
    101
    102     public void assertNodesEqual(Attribute n1, Attribute n2) {
    103         assertNodesEqual(n1.getQName(), n2.getQName());
    104
    105         assertEquals("Attribute values for: " + n1 + " and " + n2, n1
    106                 .getValue(), n2.getValue());
    107     }
    108
    109     public void assertNodesEqual(QName n1, QName n2) {
    110         assertEquals("URIs equal for: " + n1.getQualifiedName() + " and "
    111                 + n2.getQualifiedName(), n1.getNamespaceURI(), n2
    112                 .getNamespaceURI());
    113         assertEquals("qualified names equal", n1.getQualifiedName(), n2
    114                 .getQualifiedName());
    115     }
    116
    117     public void assertNodesEqual(CharacterData t1, CharacterData t2) {
    118         assertEquals("Text equal for: " + t1 + " and " + t2, t1.getText(), t2
    119                 .getText());
    120     }
    121
    122     public void assertNodesEqual(DocumentType o1, DocumentType o2) {
    123         if (o1 != o2) {
    124             if (o1 == null) {
    125                 assertTrue("Missing DocType: " + o2, false);
    126             } else if (o2 == null) {
    127                 assertTrue("Missing DocType: " + o1, false);
    128             } else {
    129                 assertEquals("DocType name equal", o1.getName(), o2.getName());
    130                 assertEquals("DocType publicID equal", o1.getPublicID(), o2
    131                         .getPublicID());
    132                 assertEquals("DocType systemID equal", o1.getSystemID(), o2
    133                         .getSystemID());
    134             }
    135         }
    136     }
    137
    138     public void assertNodesEqual(Entity o1, Entity o2) {
    139         assertEquals("Entity names equal", o1.getName(), o2.getName());
    140         assertEquals("Entity values equal", o1.getText(), o2.getText());
    141     }
    142
    143     public void assertNodesEqual(ProcessingInstruction n1,
    144             ProcessingInstruction n2) {
    145         assertEquals("PI targets equal", n1.getTarget(), n2.getTarget());
    146         assertEquals("PI text equal", n1.getText(), n2.getText());
    147     }
    148
    149     public void assertNodesEqual(Namespace n1, Namespace n2) {
    150         assertEquals("Namespace prefixes not equal", n1.getPrefix(), n2
    151                 .getPrefix());
    152         assertEquals("Namespace URIs not equal", n1.getURI(), n2.getURI());
    153     }
    154
    155     public void assertNodesEqualContent(Branch b1, Branch b2) {
    156         int c1 = b1.nodeCount();
    157         int c2 = b2.nodeCount();
    158
    159         if (c1 != c2) {
    160             log("Content of: " + b1);
    161             log("is: " + b1.content());
    162             log("Content of: " + b2);
    163             log("is: " + b2.content());
    164         }
    165
    166         assertEquals("Branches have same number of children (" + c1 + ", " + c2
    167                 + " for: " + b1 + " and " + b2, c1, c2);
    168
    169         for (int i = 0; i < c1; i++) {
    170             Node n1 = b1.node(i);
    171             Node n2 = b2.node(i);
    172             assertNodesEqual(n1, n2);
    173         }
    174     }
    175
    176     public void assertNodesEqual(Node n1, Node n2) {
    177         int nodeType1 = n1.getNodeType();
    178         int nodeType2 = n2.getNodeType();
    179         assertTrue("Nodes are of same type: ", nodeType1 == nodeType2);
    180
    181         switch (nodeType1) {
    182             case Node.ELEMENT_NODE:
    183                 assertNodesEqual((Element) n1, (Element) n2);
    184
    185                 break;
    186
    187             case Node.DOCUMENT_NODE:
    188                 assertNodesEqual((Document) n1, (Document) n2);
    189
    190                 break;
    191
    192             case Node.ATTRIBUTE_NODE:
    193                 assertNodesEqual((Attribute) n1, (Attribute) n2);
    194
    195                 break;
    196
    197             case Node.TEXT_NODE:
    198                 assertNodesEqual((Text) n1, (Text) n2);
    199
    200                 break;
    201
    202             case Node.CDATA_SECTION_NODE:
    203                 assertNodesEqual((CDATA) n1, (CDATA) n2);
    204
    205                 break;
    206
    207             case Node.ENTITY_REFERENCE_NODE:
    208                 assertNodesEqual((Entity) n1, (Entity) n2);
    209
    210                 break;
    211
    212             case Node.PROCESSING_INSTRUCTION_NODE:
    213                 assertNodesEqual((ProcessingInstruction) n1,
    214                         (ProcessingInstruction) n2);
    215
    216                 break;
    217
    218             case Node.COMMENT_NODE:
    219                 assertNodesEqual((Comment) n1, (Comment) n2);
    220
    221                 break;
    222
    223             case Node.DOCUMENT_TYPE_NODE:
    224                 assertNodesEqual((DocumentType) n1, (DocumentType) n2);
    225
    226                 break;
    227
    228             case Node.NAMESPACE_NODE:
    229                 assertNodesEqual((Namespace) n1, (Namespace) n2);
    230
    231                 break;
    232
    233             default:
    234                 assertTrue("Invalid node types. node1: " + n1 + " and node2: "
    235                         + n2, false);
    236         }
    237     }
    238
    239     // Implementation methods
    240     // -------------------------------------------------------------------------
    241     protected void setUp() throws Exception {
    242         System.setProperty("javax.xml.parsers.SAXParserFactory",
    243                 SAXParserFactoryImpl.class.getName());
    244         System.setProperty("javax.xml.transform.TransformerFactory",
    245                 TransformerFactoryImpl.class.getName());
    246         document = DocumentHelper.createDocument();
    247
    248         Element root = document.addElement("root");
    249
    250         Element author1 = root.addElement("author").addAttribute("name",
    251                 "James").addAttribute("location", "UK").addText(
    252                 "James Strachan");
    253
    254         Element url1 = author1.addElement("url");
    255         url1.addText("http://sourceforge.net/users/jstrachan/");
    256
    257         Element author2 = root.addElement("author").addAttribute("name", "Bob")
    258                 .addAttribute("location", "Canada").addText("Bob McWhirter");
    259
    260         Element url2 = author2.addElement("url");
    261         url2.addText("http://sourceforge.net/users/werken/");
    262     }
    263
    264     /***
    265      * DOCUMENT ME!
    266      *
    267      * @return the root element of the document
    268      */
    269     protected Element getRootElement() {
    270         Element root = document.getRootElement();
    271         assertTrue("Document has root element", root != null);
    272
    273         return root;
    274     }
    275 }
    276
    277 /*
    278  * Redistribution and use of this software and associated documentation
    279  * ("Software"), with or without modification, are permitted provided that the
    280  * following conditions are met:
    281  *
    282  * 1. Redistributions of source code must retain copyright statements and
    283  * notices. Redistributions must also contain a copy of this document.
    284  *
    285  * 2. Redistributions in binary form must reproduce the above copyright notice,
    286  * this list of conditions and the following disclaimer in the documentation
    287  * and/or other materials provided with the distribution.
    288  *
    289  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    290  * from this Software without prior written permission of MetaStuff, Ltd. For
    291  * written permission, please contact dom4j-info@metastuff.com.
    292  *
    293  * 4. Products derived from this Software may not be called "DOM4J" nor may
    294  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    295  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    296  *
    297  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    298  *
    299  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    300  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    301  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    302  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    303  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    304  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    305  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    306  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    307  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    308  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    309  * POSSIBILITY OF SUCH DAMAGE.
    310  *
    311  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    312  */