1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jaxen.test;
50
51 import junit.framework.TestCase;
52
53 import java.io.ByteArrayOutputStream;
54 import java.io.IOException;
55 import java.io.ObjectOutputStream;
56 import java.util.ArrayList;
57 import java.util.List;
58
59 import javax.xml.parsers.DocumentBuilder;
60 import javax.xml.parsers.DocumentBuilderFactory;
61 import javax.xml.parsers.ParserConfigurationException;
62
63 import org.jaxen.Context;
64 import org.jaxen.ContextSupport;
65 import org.jaxen.JaxenException;
66 import org.jaxen.XPath;
67 import org.jaxen.dom.DOMXPath;
68 import org.w3c.dom.Document;
69 import org.w3c.dom.Element;
70 import org.xml.sax.SAXException;
71
72 public class ContextTest extends TestCase
73 {
74 private List nodeSet;
75 private ContextSupport support;
76
77 public ContextTest(String name)
78 {
79 super( name );
80 }
81
82 public void setUp()
83 {
84 this.nodeSet = new ArrayList();
85
86 this.nodeSet.add( "one" );
87 this.nodeSet.add( "two" );
88 this.nodeSet.add( "three" );
89 this.nodeSet.add( "four" );
90
91 this.support = new ContextSupport();
92 }
93
94 public void tearDown()
95 {
96 this.nodeSet = null;
97 }
98
99 public void testSetNodeSet()
100 {
101 Context original = new Context( this.support );
102 assertEquals(0, original.getNodeSet().size() );
103 original.setNodeSet( this.nodeSet );
104 assertEquals(4, original.getNodeSet().size() );
105 }
106
107 public void testShrinkNodeSet()
108 {
109
110 Context original = new Context( this.support );
111 original.setNodeSet( this.nodeSet );
112 original.setPosition(3);
113 ArrayList list = new ArrayList();
114 list.add("1");
115 list.add("2");
116 list.add("3");
117 original.setNodeSet(list);
118 assertEquals(0, original.getPosition());
119
120 }
121
122 public void testDuplicate()
123 {
124 Context original = new Context( this.support );
125
126 original.setNodeSet( this.nodeSet );
127
128 original.setSize( 4 );
129 original.setPosition( 2 );
130
131 Context dupe = original.duplicate();
132
133 assertEquals(2, dupe.getPosition());
134 assertEquals(4, dupe.getSize());
135
136 assertTrue( original != dupe );
137
138 List dupeNodeSet = dupe.getNodeSet();
139
140 assertTrue( original.getNodeSet() != dupe.getNodeSet() );
141
142 dupeNodeSet.clear();
143
144 assertSame( dupeNodeSet,
145 dupe.getNodeSet() );
146
147 assertEquals( 0,
148 dupe.getNodeSet().size() );
149
150
151 assertEquals( 4,
152 original.getNodeSet().size() );
153
154 dupe.setSize( 0 );
155 dupe.setPosition( 0 );
156
157 assertEquals( 0,
158 dupe.getSize() );
159
160 assertEquals( 0,
161 dupe.getPosition() );
162
163 assertEquals( 4,
164 original.getSize() );
165
166 assertEquals( 2,
167 original.getPosition() );
168 }
169
170
171 public void testXMLPrefixIsAlwaysBound()
172 throws ParserConfigurationException, SAXException, IOException, JaxenException
173 {
174
175 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
176 factory.setNamespaceAware(true);
177 DocumentBuilder builder = factory.newDocumentBuilder();
178 Document doc = builder.parse( "xml/basic.xml" );
179 Element root = doc.getDocumentElement();
180 root.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
181 XPath xpath = new DOMXPath( "/*/@xml:lang" );
182 List result = xpath.selectNodes( doc );
183 assertEquals(1, result.size());
184
185 }
186
187
188 public void testIsSerializable() throws IOException {
189
190 ByteArrayOutputStream out = new ByteArrayOutputStream();
191 ObjectOutputStream oos = new ObjectOutputStream(out);
192 oos.writeObject(support);
193 oos.close();
194 assertTrue(out.toByteArray().length > 0);
195
196 }
197
198
199 }
200