001: /* Copyright 2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021: package nu.xom.tests;
022:
023: import nu.xom.Element;
024: import nu.xom.IllegalNameException;
025: import nu.xom.MalformedURIException;
026: import nu.xom.Namespace;
027: import nu.xom.NamespaceConflictException;
028: import nu.xom.NoSuchChildException;
029: import nu.xom.Nodes;
030:
031: /**
032: * <p>
033: * Tests for namespace nodes used in XPath
034: * </p>
035: *
036: * @author Elliotte Rusty Harold
037: * @version 1.1a2
038: *
039: */
040: public class NamespaceNodeTest extends XOMTestCase {
041:
042: public NamespaceNodeTest(String name) {
043: super (name);
044: }
045:
046: public void testGetters() {
047:
048: Element root = new Element("pre:root",
049: "http://www.example.org/");
050: Nodes result = root.query("namespace::pre");
051: assertEquals(1, result.size());
052: Namespace namespace = (Namespace) result.get(0);
053: assertEquals("pre", namespace.getPrefix());
054: assertEquals("http://www.example.org/", namespace.getValue());
055: assertEquals(root, namespace.getParent());
056:
057: }
058:
059: public void testCopy() {
060:
061: Element root = new Element("pre:root",
062: "http://www.example.org/");
063: Nodes result = root.query("namespace::pre");
064: assertEquals(1, result.size());
065: Namespace namespace = (Namespace) result.get(0);
066:
067: Namespace copy = (Namespace) namespace.copy();
068: assertEquals(namespace, copy);
069: assertEquals("pre", copy.getPrefix());
070: assertEquals("http://www.example.org/", copy.getValue());
071: assertEquals(null, copy.getParent());
072:
073: }
074:
075: public void testToXML() {
076:
077: Element root = new Element("pre:root",
078: "http://www.example.org/");
079: Nodes result = root.query("namespace::pre");
080: Namespace namespace = (Namespace) result.get(0);
081: assertEquals("xmlns:pre=\"http://www.example.org/\"", namespace
082: .toXML());
083:
084: }
085:
086: public void testGetChildCount() {
087:
088: Element root = new Element("pre:root",
089: "http://www.example.org/");
090: Nodes result = root.query("namespace::pre");
091: Namespace namespace = (Namespace) result.get(0);
092: assertEquals(0, namespace.getChildCount());
093:
094: }
095:
096: public void testGetChild() {
097:
098: Element root = new Element("pre:root",
099: "http://www.example.org/");
100: Nodes result = root.query("namespace::pre");
101: Namespace namespace = (Namespace) result.get(0);
102: try {
103: namespace.getChild(0);
104: fail("Got namespace child");
105: } catch (IndexOutOfBoundsException success) {
106: assertEquals("Namespaces do not have children", success
107: .getMessage());
108: }
109:
110: }
111:
112: public void testToXMLOnDefaultNamespace() {
113:
114: Element root = new Element("root", "http://www.example.org/");
115: Nodes result = root.query("namespace::*[name() != 'xml']");
116: Namespace namespace = (Namespace) result.get(0);
117: assertEquals("xmlns=\"http://www.example.org/\"", namespace
118: .toXML());
119:
120: }
121:
122: public void testDetachNamespaceNode() {
123:
124: Element root = new Element("pre:root",
125: "http://www.example.org/");
126: Nodes result = root.query("namespace::pre");
127: Namespace namespace = (Namespace) result.get(0);
128: namespace.detach();
129: assertNull(namespace.getParent());
130:
131: }
132:
133: public void testRemoveNamespaceNode() {
134:
135: Element root = new Element("pre:root",
136: "http://www.example.org/");
137: Nodes result = root.query("namespace::pre");
138: Namespace namespace = (Namespace) result.get(0);
139: try {
140: root.removeChild(namespace);
141: fail("Namespaces are not children");
142: } catch (NoSuchChildException success) {
143: assertNotNull(success.getMessage());
144: }
145:
146: }
147:
148: public void testGetParent() {
149:
150: Element root = new Element("pre:root",
151: "http://www.example.org/");
152: Nodes result = root.query("namespace::pre");
153: Namespace namespace = (Namespace) result.get(0);
154: assertEquals(root, namespace.getParent());
155:
156: }
157:
158: public void testToString() {
159:
160: Element root = new Element("pre:root",
161: "http://www.example.org/");
162: Nodes result = root.query("namespace::pre");
163: Namespace namespace = (Namespace) result.get(0);
164: assertEquals(
165: "[Namespace: xmlns:pre=\"http://www.example.org/\"]",
166: namespace.toString());
167:
168: }
169:
170: public void testIllegalPrefix() {
171:
172: try {
173: new Namespace("white space", "http://www.example.org", null);
174: fail("Allowed prefix containing white space");
175: } catch (IllegalNameException success) {
176: assertNotNull(success.getMessage());
177: }
178:
179: }
180:
181: public void testEmptyStringPrefix() {
182: Namespace ns = new Namespace("", "http://www.example.org", null);
183: assertEquals("", ns.getPrefix());
184: }
185:
186: public void testNullPrefix() {
187: Namespace ns = new Namespace(null, "http://www.example.org",
188: null);
189: assertEquals("", ns.getPrefix());
190: }
191:
192: public void testIllegalURI() {
193:
194: try {
195: new Namespace("pre", "http:// www.example.org", null);
196: fail("Allowed URI containing white space");
197: } catch (MalformedURIException success) {
198: assertNotNull(success.getMessage());
199: }
200:
201: }
202:
203: public void testNullURI() {
204: Namespace ns = new Namespace("", null, null);
205: assertEquals("", ns.getValue());
206: }
207:
208: public void testCantBindPrefixToEmptyURI() {
209:
210: try {
211: new Namespace("pre", "", null);
212: fail("Bound prefix to no namespace");
213: } catch (NamespaceConflictException success) {
214: assertNotNull(success.getMessage());
215: }
216:
217: }
218:
219: public void testCantBindXMLNS() {
220:
221: try {
222: new Namespace("xmlns", "", null);
223: fail("Bound xmlns prefix to no namespace");
224: } catch (IllegalNameException success) {
225: assertNotNull(success.getMessage());
226: }
227:
228: }
229:
230: public void testCantBindXMLNSToDOMURI() {
231:
232: try {
233: new Namespace("xmlns", "http://www.w3.org/2000/xmlns/",
234: null);
235: fail("Bound xmlns prefix to DOM namespace");
236: } catch (IllegalNameException success) {
237: assertNotNull(success.getMessage());
238: }
239:
240: }
241:
242: public void testCantBindXMLPrefixToWrongURI() {
243:
244: try {
245: new Namespace("xml", "http://www.w3.org/2000/xmlns/", null);
246: fail("Bound xml prefix to DOM namespace");
247: } catch (NamespaceConflictException success) {
248: assertNotNull(success.getMessage());
249: }
250:
251: }
252:
253: public void testCanBindXMLPrefixToCorrectURI() {
254:
255: Namespace ns = new Namespace("xml", Namespace.XML_NAMESPACE,
256: null);
257: assertEquals(Namespace.XML_NAMESPACE, ns.getValue());
258:
259: }
260:
261: public void testCanBindNonXMLPrefixToXMLURI() {
262:
263: try {
264: new Namespace("pre", Namespace.XML_NAMESPACE, null);
265: fail("Bound non-xml prefix to XML namespace");
266: } catch (NamespaceConflictException success) {
267: assertNotNull(success.getMessage());
268: }
269:
270: }
271:
272: }
|