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