    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.rule;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.util.ArrayList;
    13  import java.util.Collections;
    14
    15  import org.dom4j.AbstractTestCase;
    16  import org.dom4j.CDATA;
    17  import org.dom4j.Document;
    18  import org.dom4j.DocumentFactory;
    19
    20  /***
    21   * Tests the ordering of Rules
    22   *
    23   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    24   * @version $Revision: 1.3 $
    25   */
    26  public class RuleTest extends AbstractTestCase {
    27      protected DocumentFactory factory = new DocumentFactory();
    28
    29      public static void main(String[] args) {
    30          TestRunner.run(RuleTest.class);
    31      }
    32
    33      public void testOrder() throws Exception {
    34          testGreater("foo", "*");
    35      }
    36
    37      protected void testGreater(String expr1, String expr2) throws Exception {
    38          System.out.println("parsing: " + expr1 + " and " + expr2);
    39
    40          Rule r1 = createRule(expr1);
    41          Rule r2 = createRule(expr2);
    42
    43          System.out.println("rule1: " + r1 + " rule2: " + r2);
    44
    45          int value = r1.compareTo(r2);
    46
    47          System.out.println("Comparison: " + value);
    48
    49          assertTrue("r1 > r2", value > 0);
    50
    51          ArrayList list = new ArrayList();
    52          list.add(r1);
    53          list.add(r2);
    54
    55          Collections.sort(list);
    56
    57          assertTrue("r2 should be first", list.get(0) == r2);
    58          assertTrue("r1 should be next", list.get(1) == r1);
    59
    60          list = new ArrayList();
    61          list.add(r2);
    62          list.add(r1);
    63
    64          Collections.sort(list);
    65
    66          assertTrue("r2 should be first", list.get(0) == r2);
    67          assertTrue("r1 should be next", list.get(1) == r1);
    68
    69          /*
    70           * TreeSet set = new TreeSet(); set.add( r1 ); set.add( r2 );
    71           *
    72           * assertTrue( "r2 should be first", set.first() == r2 ); assertTrue(
    73           * "r1 should be next", set.last() == r1 );
    74           *
    75           * Object[] array = set.toArray();
    76           *
    77           * assertTrue( "r2 should be first", array[0] == r2 ); assertTrue( "r1
    78           * should be next", array[1] == r1 );
    79           *
    80           * set = new TreeSet(); set.add( r2 ); set.add( r1 );
    81           *
    82           * assertTrue( "r2 should be first", set.first() == r2 ); assertTrue(
    83           * "r1 should be next", set.last() == r1 );
    84           *
    85           * array = set.toArray();
    86           *
    87           * assertTrue( "r2 should be first", array[0] == r2 ); assertTrue( "r1
    88           * should be next", array[1] == r1 );
    89           */
    90      }
    91
    92      public void testDocument() {
    93          Rule rule = createRule("/");
    94          Document document = factory.createDocument();
    95          document.addElement("foo");
    96
    97          assertTrue("/ matches document", rule.matches(document));
    98          assertTrue("/ does not match root element", !rule.matches(document
    99                  .getRootElement()));
    100     }
    101
    102     public void testTextMatchesCDATA() {
    103         CDATA cdata = factory.createCDATA("<>&");
    104         Rule rule = createRule("text()");
    105
    106         assertTrue("text() matches CDATA", rule.matches(cdata));
    107     }
    108
    109     protected Rule createRule(String expr) {
    110         Pattern pattern = factory.createPattern(expr);
    111
    112         return new Rule(pattern);
    113     }
    114 }
    115
    116 /*
    117  * Redistribution and use of this software and associated documentation
    118  * ("Software"), with or without modification, are permitted provided that the
    119  * following conditions are met:
    120  *
    121  * 1. Redistributions of source code must retain copyright statements and
    122  * notices. Redistributions must also contain a copy of this document.
    123  *
    124  * 2. Redistributions in binary form must reproduce the above copyright notice,
    125  * this list of conditions and the following disclaimer in the documentation
    126  * and/or other materials provided with the distribution.
    127  *
    128  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    129  * from this Software without prior written permission of MetaStuff, Ltd. For
    130  * written permission, please contact dom4j-info@metastuff.com.
    131  *
    132  * 4. Products derived from this Software may not be called "DOM4J" nor may
    133  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    134  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    135  *
    136  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    137  *
    138  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    139  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    140  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    141  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    142  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    143  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    144  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    145  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    146  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    147  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    148  * POSSIBILITY OF SUCH DAMAGE.
    149  *
    150  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    151  */