1 61 62 63 package org.jaxen; 64 65 import junit.framework.TestCase; 66 67 import java.io.IOException ; 68 import java.util.ArrayList ; 69 import java.util.List ; 70 71 import javax.xml.parsers.DocumentBuilder ; 72 import javax.xml.parsers.DocumentBuilderFactory ; 73 import javax.xml.parsers.ParserConfigurationException ; 74 75 import org.jaxen.dom.DOMXPath; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 import org.xml.sax.SAXException ; 79 80 public class ContextTest extends TestCase 81 { 82 private List nodeSet; 83 private ContextSupport support; 84 85 public ContextTest(String name) 86 { 87 super( name ); 88 } 89 90 public void setUp() 91 { 92 this.nodeSet = new ArrayList (); 93 94 this.nodeSet.add( "one" ); 95 this.nodeSet.add( "two" ); 96 this.nodeSet.add( "three" ); 97 this.nodeSet.add( "four" ); 98 99 this.support = new ContextSupport(); 100 } 101 102 public void tearDown() 103 { 104 this.nodeSet = null; 105 } 106 107 public void testDuplicate() 108 { 109 Context original = new Context( this.support ); 110 111 original.setNodeSet( this.nodeSet ); 112 113 original.setSize( 4 ); 114 original.setPosition( 2 ); 115 116 Context dupe = original.duplicate(); 117 118 assertTrue( original != dupe ); 119 120 List dupeNodeSet = dupe.getNodeSet(); 121 122 assertTrue( original.getNodeSet() != dupe.getNodeSet() ); 123 124 dupeNodeSet.clear(); 125 126 assertSame( dupeNodeSet, 127 dupe.getNodeSet() ); 128 129 assertEquals( 0, 130 dupe.getNodeSet().size() ); 131 132 133 assertEquals( 4, 134 original.getNodeSet().size() ); 135 136 dupe.setSize( 0 ); 137 dupe.setPosition( 0 ); 138 139 assertEquals( 0, 140 dupe.getSize() ); 141 142 assertEquals( 0, 143 dupe.getPosition() ); 144 145 assertEquals( 4, 146 original.getSize() ); 147 148 assertEquals( 2, 149 original.getPosition() ); 150 } 151 152 public void testXMLPrefixIsAlwaysBound() 153 throws ParserConfigurationException , SAXException , IOException , JaxenException 154 { 155 156 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 157 factory.setNamespaceAware(true); 158 DocumentBuilder builder = factory.newDocumentBuilder(); 159 Document doc = builder.parse( "xml/basic.xml" ); 160 Element root = doc.getDocumentElement(); 161 root.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en"); 162 XPath xpath = new DOMXPath( "/*/@xml:lang" ); 163 List result = xpath.selectNodes( doc ); 164 assertEquals(1, result.size()); 165 166 } 167 168 169 } 170 171 | Popular Tags |