1 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 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 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 152 | Popular Tags |