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
50
51
52
53
54
55
56
57
58
59
60
61
62 package org.jaxen.function;
63
64 import java.io.IOException;
65 import java.util.List;
66
67 import javax.xml.parsers.DocumentBuilder;
68 import javax.xml.parsers.DocumentBuilderFactory;
69 import javax.xml.parsers.ParserConfigurationException;
70
71 import junit.framework.TestCase;
72
73 import org.jaxen.FunctionCallException;
74 import org.jaxen.JaxenException;
75 import org.jaxen.XPath;
76 import org.jaxen.dom.DOMXPath;
77 import org.w3c.dom.Attr;
78 import org.w3c.dom.Comment;
79 import org.w3c.dom.Document;
80 import org.w3c.dom.ProcessingInstruction;
81 import org.w3c.dom.Text;
82 import org.xml.sax.SAXException;
83
84 /***
85 * @author Elliotte Rusty Harold
86 *
87 */
88 public class LocalNameTest extends TestCase {
89
90 private Document doc;
91
92 public void setUp() throws ParserConfigurationException, SAXException, IOException
93 {
94 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
95 factory.setNamespaceAware(true);
96 DocumentBuilder builder = factory.newDocumentBuilder();
97 doc = builder.parse( "xml/basic.xml" );
98 }
99
100
101 public LocalNameTest(String name) {
102 super(name);
103 }
104
105 public void testLocalNameOfNumber() throws JaxenException
106 {
107 try
108 {
109 XPath xpath = new DOMXPath( "local-name(3)" );
110 xpath.selectNodes( doc );
111 fail("local-name of non-node-set");
112 }
113 catch (FunctionCallException e)
114 {
115 assertEquals("The argument to the local-name function must be a node-set", e.getMessage());
116 }
117 }
118
119 public void testLocalNameWithTwoArguments() throws JaxenException
120 {
121 try
122 {
123 XPath xpath = new DOMXPath( "local-name(/*, //*)" );
124 xpath.selectNodes( doc );
125 fail("local-name with two arguments");
126 }
127 catch (FunctionCallException e)
128 {
129 assertEquals("local-name() requires zero or one argument.", e.getMessage());
130 }
131 }
132
133 public void testLocalNameAllowsNoArguments() throws JaxenException
134 {
135 XPath xpath = new DOMXPath( "local-name()" );
136 String result = (String) xpath.evaluate( doc.getDocumentElement() );
137 assertEquals("foo", result);
138 }
139
140 public void testLocalNameOfCommentIsEmptyString() throws JaxenException
141 {
142 XPath xpath = new DOMXPath( "local-name(/comment())" );
143 Comment c = doc.createComment("test");
144 doc.appendChild(c);
145 String result = (String) xpath.evaluate(doc);
146 assertEquals("", result);
147 }
148
149 public void testLocalNameOfEmptyNodeSetIsEmptyString() throws JaxenException
150 {
151 XPath xpath = new DOMXPath( "local-name(/aaa)" );
152 String result = (String) xpath.evaluate(doc);
153 assertEquals("", result);
154 }
155
156 public void testLocalNameOfProcessingInstructionIsTarget() throws JaxenException
157 {
158 XPath xpath = new DOMXPath( "local-name(/processing-instruction())" );
159 ProcessingInstruction pi = doc.createProcessingInstruction("target", "value");
160 doc.appendChild(pi);
161 String result = (String) xpath.evaluate(doc);
162 assertEquals("target", result);
163 }
164
165 public void testLocalNameOfAttribute() throws JaxenException
166 {
167 XPath xpath = new DOMXPath( "local-name(/*/@*)" );
168 Attr a = doc.createAttribute("name");
169 doc.getDocumentElement().setAttributeNode(a);
170 String result = (String) xpath.evaluate(doc);
171 assertEquals("name", result);
172 }
173
174 public void testLocalNameOfTextIsEmptyString() throws JaxenException
175 {
176 XPath xpath = new DOMXPath( "local-name(/*/text())" );
177 Text c = doc.createTextNode("test");
178 doc.getDocumentElement().appendChild(c);
179 String result = (String) xpath.evaluate(doc);
180 assertEquals("", result);
181 }
182
183 public void testLocalNameOfNamespaceIsPrefix() throws JaxenException
184 {
185 XPath xpath = new DOMXPath( "local-name(/*/namespace::node())" );
186 String result = (String) xpath.evaluate(doc);
187 assertEquals("xml", result);
188 }
189
190 public void testLocalNameNoArguments()
191 {
192 try
193 {
194 XPath xpath = new DOMXPath( "local-name()" );
195 List results = xpath.selectNodes( doc );
196 assertEquals("", results.get(0));
197 }
198 catch (Exception e)
199 {
200 e.printStackTrace();
201 fail( e.getMessage() );
202 }
203 }
204
205 }