Source Code Cross Referenced for NamespacesTest.java in  » XML » xom » nu » xom » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xom » nu.xom.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2002-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:
022:        package nu.xom.tests;
023:
024:        import nu.xom.Element;
025:        import nu.xom.Attribute;
026:        import nu.xom.MalformedURIException;
027:        import nu.xom.NamespaceConflictException;
028:
029:        /**
030:         * <p>
031:         * Tests that namespace well-formedness is maintained.
032:         * </p>
033:         * 
034:         * @author Elliotte Rusty Harold
035:         * @version 1.1d6
036:         *
037:         */
038:        public class NamespacesTest extends XOMTestCase {
039:
040:            public NamespacesTest() {
041:                super ("Namespaces tests");
042:            }
043:
044:            private Element someNamespaces;
045:            private Element noNamespaces;
046:            private Element severalNamespaces;
047:
048:            protected void setUp() {
049:                noNamespaces = new Element("test");
050:
051:                someNamespaces = new Element("test");
052:                someNamespaces.addNamespaceDeclaration("xlink",
053:                        "http://www.w3.org/2001/xlink");
054:                someNamespaces.addNamespaceDeclaration("xsl",
055:                        "http://www.w3.org/1999/xslt");
056:
057:                severalNamespaces = new Element("test",
058:                        "http://www.example.com/");
059:                severalNamespaces.addAttribute(new Attribute("xlink:type",
060:                        "http://www.w3.org/2001/xlink", "simple"));
061:                severalNamespaces.addNamespaceDeclaration("xsl",
062:                        "http://www.w3.org/1999/xslt");
063:                severalNamespaces.addNamespaceDeclaration("",
064:                        "http://www.example.com/");
065:                severalNamespaces.addNamespaceDeclaration("xlink",
066:                        "http://www.w3.org/2001/xlink");
067:
068:            }
069:
070:            public void testSetNamespacePrefixInConflictWithAdditionalNamespaceDeclaration() {
071:
072:                someNamespaces.setNamespaceURI("http://www.example.net");
073:                try {
074:                    someNamespaces.setNamespacePrefix("xsl");
075:                    fail("changed prefix to conflict with additional namespace declaration");
076:                } catch (NamespaceConflictException success) {
077:                    assertNotNull(success.getMessage());
078:                }
079:
080:            }
081:
082:            public void testSetNamespaceURIInConflictWithAdditionalNamespaceDeclaration() {
083:
084:                someNamespaces.setNamespaceURI("http://www.w3.org/2001/xlink");
085:                someNamespaces.setNamespacePrefix("xlink");
086:                try {
087:                    someNamespaces.setNamespaceURI("http://www.example.net");
088:                    fail("changed namespace URI to conflict with additional namespace declaration");
089:                } catch (NamespaceConflictException ex) {
090:                    assertNotNull(ex.getMessage());
091:                }
092:
093:            }
094:
095:            public void testAdditionalNamespaceDuplicatesElementNamespace() {
096:
097:                Element element = new Element("pre:element",
098:                        "http://www.example.org");
099:                element
100:                        .addNamespaceDeclaration("pre",
101:                                "http://www.example.org");
102:                element.setNamespacePrefix("foo");
103:                assertEquals("http://www.example.org", element
104:                        .getNamespaceURI("pre"));
105:
106:            }
107:
108:            public void testXMLNamespace() {
109:
110:                assertEquals("http://www.w3.org/XML/1998/namespace",
111:                        noNamespaces.getNamespaceURI("xml"));
112:                assertEquals("http://www.w3.org/XML/1998/namespace",
113:                        severalNamespaces.getNamespaceURI("xml"));
114:
115:            }
116:
117:            public void testWrongPrefixNotAllowedWithXMLURI() {
118:
119:                try {
120:                    noNamespaces.addNamespaceDeclaration("pre",
121:                            "http://www.w3.org/XML/1998/namespace");
122:                    fail("Allowed XML namespace to be associated with non-xml prefix");
123:                } catch (NamespaceConflictException success) {
124:                    assertNotNull(success.getMessage());
125:                }
126:
127:            }
128:
129:            public void testUnmappingPrefix() {
130:
131:                try {
132:                    noNamespaces.addNamespaceDeclaration("pre", "");
133:                } catch (MalformedURIException success) {
134:                    assertNotNull(success.getMessage());
135:                }
136:
137:            }
138:
139:            public void testAllowCapitalSchemes() {
140:                noNamespaces.addNamespaceDeclaration("pre",
141:                        "HTTP://WWW.EXAMPLE.COM/");
142:                assertEquals(noNamespaces.getNamespaceURI("pre"),
143:                        "HTTP://WWW.EXAMPLE.COM/");
144:            }
145:
146:            public void testBadSchemes() {
147:
148:                try {
149:                    noNamespaces.addNamespaceDeclaration("pre",
150:                            "uri!urn:somedata");
151:                    fail("Allowed illegal characters in scheme");
152:                } catch (MalformedURIException success) {
153:                    assertNotNull(success.getMessage());
154:                }
155:            }
156:
157:            public void testXMLPrefixNotAllowedWithWrongURI() {
158:
159:                try {
160:                    noNamespaces.addNamespaceDeclaration("xml",
161:                            "http://www.example.org/");
162:                    fail("Allowed xml prefix to be associated with wrong URI");
163:                } catch (NamespaceConflictException success) {
164:                    assertNotNull(success.getMessage());
165:                }
166:
167:            }
168:
169:            public void testXMLNSNamespace() {
170:                assertEquals("", noNamespaces.getNamespaceURI("xmlns"));
171:                assertEquals("", severalNamespaces.getNamespaceURI("xmlns"));
172:            }
173:
174:            public void testCantUseXMLNSPrefix() {
175:
176:                try {
177:                    noNamespaces.addNamespaceDeclaration("xmlns",
178:                            "http://www.w3.org/2000/xmlns/");
179:                    fail("added xmlns prefix");
180:                } catch (NamespaceConflictException success) {
181:                    assertNotNull(success.getMessage());
182:                }
183:
184:                try {
185:                    noNamespaces.addNamespaceDeclaration("xmlns",
186:                            "http://www.example.com");
187:                    fail("added xmlns prefix");
188:                } catch (NamespaceConflictException success) {
189:                    assertNotNull(success.getMessage());
190:                }
191:
192:            }
193:
194:            public void testCantUseXMLPrefix() {
195:
196:                try {
197:                    noNamespaces.addNamespaceDeclaration("xml",
198:                            "http://www.example.com");
199:                    fail("added xmlns prefix");
200:                } catch (NamespaceConflictException success) {
201:                    assertNotNull(success.getMessage());
202:                }
203:
204:            }
205:
206:            public void testCanUseXMLPrefix() {
207:                noNamespaces.addNamespaceDeclaration("xml",
208:                        "http://www.w3.org/XML/1998/namespace");
209:                assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
210:            }
211:
212:            public void testIndexedAccess() {
213:
214:                assertEquals("", noNamespaces.getNamespacePrefix(0));
215:
216:                assertNotNull(someNamespaces.getNamespacePrefix(0));
217:                assertNotNull(someNamespaces.getNamespacePrefix(1));
218:                assertNotNull(someNamespaces.getNamespacePrefix(2));
219:
220:                assertNotNull(severalNamespaces.getNamespacePrefix(0));
221:                assertNotNull(severalNamespaces.getNamespacePrefix(1));
222:                assertNotNull(severalNamespaces.getNamespacePrefix(2));
223:                try {
224:                    severalNamespaces.getNamespacePrefix(3);
225:                    fail("Got a namespace 3");
226:                } catch (IndexOutOfBoundsException ex) {
227:                    // success;   
228:                }
229:
230:            }
231:
232:            public void testSize() {
233:                assertEquals(1, noNamespaces.getNamespaceDeclarationCount());
234:                assertEquals(3, someNamespaces.getNamespaceDeclarationCount());
235:                assertEquals(3, severalNamespaces
236:                        .getNamespaceDeclarationCount());
237:            }
238:
239:            public void testDefaultNamespace() {
240:                Element html = new Element("html",
241:                        "http://www.w3.org/1999/xhtml");
242:                assertEquals(1, html.getNamespaceDeclarationCount());
243:                assertEquals("", html.getNamespacePrefix(0));
244:                assertEquals("http://www.w3.org/1999/xhtml", html
245:                        .getNamespaceURI(""));
246:            }
247:
248:            public void testGetByPrefix() {
249:
250:                assertEquals("http://www.w3.org/2001/xlink", someNamespaces
251:                        .getNamespaceURI("xlink"));
252:                assertEquals("http://www.w3.org/1999/xslt", someNamespaces
253:                        .getNamespaceURI("xsl"));
254:                assertNull(someNamespaces.getNamespaceURI("fo"));
255:                assertNull(noNamespaces.getNamespaceURI("xsl"));
256:                assertEquals("", someNamespaces.getNamespaceURI(""));
257:                assertEquals("", noNamespaces.getNamespaceURI(""));
258:
259:            }
260:
261:            public void testGetNamespaceDeclarationCount() {
262:                Element test = new Element("test");
263:                assertEquals(1, test.getNamespaceDeclarationCount());
264:                test.setNamespaceURI("http://www.example.com");
265:                assertEquals(1, test.getNamespaceDeclarationCount());
266:                test.addAttribute(new Attribute("test", "test"));
267:                assertEquals(1, test.getNamespaceDeclarationCount());
268:                test.addAttribute(new Attribute("xlink:type",
269:                        "http://www.w3.org/2001/xlink", "value"));
270:                assertEquals(2, test.getNamespaceDeclarationCount());
271:                test.addAttribute(new Attribute("xlink:href",
272:                        "http://www.w3.org/2001/xlink", "value"));
273:                assertEquals(2, test.getNamespaceDeclarationCount());
274:                test.addNamespaceDeclaration("xlink",
275:                        "http://www.w3.org/2001/xlink");
276:                assertEquals(2, test.getNamespaceDeclarationCount());
277:                test.addNamespaceDeclaration("xsi",
278:                        "http://www.w3.org/2001/xmlschema-instance");
279:                assertEquals(3, test.getNamespaceDeclarationCount());
280:
281:            }
282:
283:            public void testRemoving() {
284:
285:                assertEquals("http://www.w3.org/2001/xlink", severalNamespaces
286:                        .getNamespaceURI("xlink"));
287:                assertEquals("http://www.w3.org/1999/xslt", severalNamespaces
288:                        .getNamespaceURI("xsl"));
289:                assertEquals("http://www.example.com/", severalNamespaces
290:                        .getNamespaceURI(""));
291:
292:                severalNamespaces.removeNamespaceDeclaration("xlink");
293:                severalNamespaces.removeNamespaceDeclaration("xsl");
294:                severalNamespaces.removeNamespaceDeclaration("");
295:                severalNamespaces
296:                        .removeNamespaceDeclaration("nosuchdeclaration");
297:
298:                assertEquals("http://www.w3.org/2001/xlink", severalNamespaces
299:                        .getNamespaceURI("xlink"));
300:                assertNull(severalNamespaces.getNamespaceURI("xsl"));
301:                assertEquals("http://www.example.com/", severalNamespaces
302:                        .getNamespaceURI(""));
303:
304:            }
305:
306:            public void testAddSameNamespaceDeclaration() {
307:
308:                Element e = new Element("test", "http://www.example.com");
309:
310:                try {
311:                    e.addNamespaceDeclaration("", "http://www.red.com");
312:                    fail("added conflicting default namespace");
313:                } catch (NamespaceConflictException success) {
314:                    assertNotNull(success.getMessage());
315:                }
316:
317:                e.addNamespaceDeclaration("", "http://www.example.com");
318:                assertEquals("http://www.example.com", e.getNamespaceURI(""));
319:                assertEquals(1, e.getNamespaceDeclarationCount());
320:
321:            }
322:
323:            public void testAddEmptyNamespaceDeclaration() {
324:
325:                Element e = new Element("test");
326:
327:                try {
328:                    e.addNamespaceDeclaration("", "http://www.example.com");
329:                    fail("added conflicting default namespace");
330:                } catch (NamespaceConflictException success) {
331:                    assertNotNull(success.getMessage());
332:                }
333:
334:                e.setNamespaceURI("http://www.example.com");
335:                e.setNamespacePrefix("pre");
336:                e.addNamespaceDeclaration("", "http://www.example.net");
337:
338:                assertEquals("http://www.example.net", e.getNamespaceURI(""));
339:
340:            }
341:
342:            public void testAddNullPrefix() {
343:
344:                Element e = new Element("test");
345:
346:                try {
347:                    e.addNamespaceDeclaration(null, "http://www.example.com");
348:                    fail("added conflicting empty prefix to element in no namespace");
349:                } catch (NamespaceConflictException success) {
350:                    assertNotNull(success.getMessage());
351:                }
352:
353:                e.setNamespaceURI("http://www.example.com");
354:                e.setNamespacePrefix("pre");
355:                e.addNamespaceDeclaration(null, "http://www.example.net");
356:
357:                assertEquals("http://www.example.net", e.getNamespaceURI(""));
358:
359:            }
360:
361:            public void testAddNullURI() {
362:                Element parent = new Element("parent",
363:                        "http://www.example.org/");
364:                Element e = new Element("pre:test", "http://www.example.com/");
365:                parent.appendChild(e);
366:                e.addNamespaceDeclaration("", null);
367:                assertEquals("", e.getNamespaceURI(""));
368:            }
369:
370:            public void testRemoveNullPrefix() {
371:                Element e = new Element("pre:test", "http://www.example.com/");
372:                e.addNamespaceDeclaration("", "http://www.example.net");
373:                e.removeNamespaceDeclaration(null);
374:                assertEquals("", e.getNamespaceURI(""));
375:            }
376:
377:            public void testBindXMLNSPrefix() {
378:
379:                Element e = new Element("pre:test", "http://www.example.com/");
380:                try {
381:                    e
382:                            .addNamespaceDeclaration("xmlns",
383:                                    "http://www.example.net");
384:                    fail("Bound xmlns prefix to http://www.example.net");
385:                } catch (NamespaceConflictException success) {
386:                    assertNotNull(success.getMessage());
387:                }
388:
389:            }
390:
391:            public void testBindXMLNSPrefixToEmptyString() {
392:
393:                Element e = new Element("pre:test", "http://www.example.com/");
394:                assertEquals("", e.getNamespaceURI("xmlns"));
395:                e.addNamespaceDeclaration("xmlns", "");
396:                assertEquals("", e.getNamespaceURI("xmlns"));
397:
398:            }
399:
400:            public void testUndeclareDefaultNamespace() {
401:                Element parent = new Element("parent",
402:                        "http://www.example.org/");
403:                Element e2 = new Element("pre:test", "http://www.example.net");
404:                parent.appendChild(e2);
405:                e2.addNamespaceDeclaration("", "");
406:                assertEquals("", e2.getNamespaceURI(""));
407:            }
408:
409:            public void testForConflictWithDefaultNamespace() {
410:                Element e = new Element("test", "http://www.example.net");
411:                try {
412:                    e.addNamespaceDeclaration("", "http://www.example.com");
413:                    fail("Conflicting default namespace");
414:                } catch (NamespaceConflictException success) {
415:                    assertNotNull(success.getMessage());
416:                }
417:            }
418:
419:            public void testConflictingUndeclarationOfDefaultNamespace() {
420:                Element e = new Element("test", "http://www.example.net");
421:                try {
422:                    e.addNamespaceDeclaration("", "");
423:                    fail("Conflicting undeclaration of default namespace");
424:                } catch (NamespaceConflictException success) {
425:                    assertNotNull(success.getMessage());
426:                }
427:            }
428:
429:            public void testAdding() {
430:
431:                try {
432:                    noNamespaces.addNamespaceDeclaration("",
433:                            "http://www.example.com/");
434:                    fail("added conflicting default namespace");
435:                } catch (NamespaceConflictException success) {
436:                    assertNotNull(success.getMessage());
437:                }
438:
439:                try {
440:                    severalNamespaces.addNamespaceDeclaration("xlink",
441:                            "http://www.example.com/");
442:                    fail("added conflicting attribute prefix namespace");
443:                } catch (NamespaceConflictException success) {
444:                    assertNotNull(success.getMessage());
445:                }
446:
447:                try {
448:                    someNamespaces.addNamespaceDeclaration("xsl",
449:                            "http://www.example.com/");
450:                    fail("added conflicting additional prefix namespace");
451:                } catch (NamespaceConflictException success) {
452:                    assertNotNull(success.getMessage());
453:                }
454:
455:                someNamespaces.addNamespaceDeclaration("foo",
456:                        "http://www.example.com/");
457:                assertEquals("http://www.example.com/", someNamespaces
458:                        .getNamespaceURI("foo"));
459:
460:            }
461:
462:            public void testReplacingNamespaceDeclaration() {
463:
464:                assertEquals("http://www.w3.org/2001/xlink", someNamespaces
465:                        .getNamespaceURI("xlink"));
466:                try {
467:                    someNamespaces.addNamespaceDeclaration("xlink",
468:                            "http://www.example.com/");
469:                    fail("Redeclared without removal");
470:                } catch (NamespaceConflictException success) {
471:                    assertNotNull(success.getMessage());
472:                }
473:                someNamespaces.removeNamespaceDeclaration("xlink");
474:                assertNull(someNamespaces.getNamespaceURI("xlink"));
475:                someNamespaces.addNamespaceDeclaration("xlink",
476:                        "http://www.example.com/");
477:                assertEquals("http://www.example.com/", someNamespaces
478:                        .getNamespaceURI("xlink"));
479:
480:            }
481:
482:            public void testSetPrefix() {
483:
484:                try {
485:                    Attribute a = severalNamespaces.getAttribute(0);
486:                    a.setNamespace("xsl", "http://www.example.com/");
487:                    fail("added conflicting attribute prefix");
488:                } catch (NamespaceConflictException success) {
489:                    assertNotNull(success.getMessage());
490:                }
491:
492:            }
493:
494:            public void testElementConflict() {
495:
496:                Element element = new Element("pre:test",
497:                        "http://www.example.com/");
498:                element.addNamespaceDeclaration("pre",
499:                        "http://www.example.com/");
500:                try {
501:                    element.setNamespaceURI("http://www.yahoo.com");
502:                    fail("changed to conflicting element namespace");
503:                } catch (NamespaceConflictException success) {
504:                    assertNotNull(success.getMessage());
505:                }
506:
507:            }
508:
509:            public void testAttributeConflict() {
510:
511:                Element element = new Element("test");
512:                element.addNamespaceDeclaration("pre",
513:                        "http://www.example.com/");
514:                Attribute a = new Attribute("pre:test",
515:                        "http://www.example.com/", "value");
516:                element.addAttribute(a);
517:                try {
518:                    a.setNamespace("pre", "http://www.yahoo.com/");
519:                    fail("changed to conflicting attribute namespace");
520:                } catch (NamespaceConflictException success) {
521:                    assertNotNull(success.getMessage());
522:                }
523:
524:            }
525:
526:            public void testSetAttributePrefix() {
527:
528:                try {
529:                    severalNamespaces.setNamespacePrefix("xlink");
530:                    fail("added conflicting element prefix");
531:                } catch (NamespaceConflictException success) {
532:                    assertNotNull(success.getMessage());
533:                }
534:
535:                try {
536:                    severalNamespaces.setNamespacePrefix("xsl");
537:                    fail("added conflicting element prefix");
538:                } catch (NamespaceConflictException success) {
539:                    assertNotNull(success.getMessage());
540:                }
541:
542:            }
543:
544:            /*
545:             * Test document submitted by Laurent Bihanic
546:             <b xmlns="urn:x-xom:b" id="b">
547:               <c:c xmlns:c="urn:x-xom:c" id="c" />
548:             </b>
549:             */
550:            public void testLaurent() {
551:
552:                Element b = new Element("b", "urn:x-xom:b");
553:                b.addAttribute(new Attribute("id", "b"));
554:
555:                Element c = new Element("c:c", "urn:x-xom:c");
556:                c.addAttribute(new Attribute("id", "c"));
557:                b.appendChild(c);
558:
559:                assertEquals("urn:x-xom:b", b.getNamespaceURI());
560:                assertEquals("urn:x-xom:c", c.getNamespaceURI());
561:                assertEquals("urn:x-xom:b", b.getNamespaceURI(""));
562:                assertEquals("urn:x-xom:b", c.getNamespaceURI(""));
563:                assertEquals("urn:x-xom:c", c.getNamespaceURI("c"));
564:
565:            }
566:
567:            public void testCountNamespaces() {
568:
569:                Element html = new Element("html");
570:                assertEquals(1, html.getNamespaceDeclarationCount());
571:                html.setNamespaceURI("http://www.w3.org/1999/xhtml");
572:                assertEquals(1, html.getNamespaceDeclarationCount());
573:                html.addAttribute(new Attribute("pre:test",
574:                        "http://www.examnple.org/", "value"));
575:                assertEquals(2, html.getNamespaceDeclarationCount());
576:                html.addNamespaceDeclaration("rddl", "http://www.rddl.org");
577:                assertEquals(3, html.getNamespaceDeclarationCount());
578:                html.addNamespaceDeclaration("xlink",
579:                        "http://www.w3.org/2001/xlink");
580:                assertEquals(4, html.getNamespaceDeclarationCount());
581:                html.addAttribute(new Attribute("xml:space",
582:                        "http://www.w3.org/XML/1998/namespace", "default"));
583:                assertEquals(4, html.getNamespaceDeclarationCount());
584:
585:            }
586:
587:            public void testGetNamespaceURIByPrefix() {
588:
589:                Element a = new Element("a");
590:                a.addNamespaceDeclaration("foo", "urn:foo");
591:                Element b = new Element("b");
592:                Element c = new Element("c");
593:                Element d = new Element("foo:d", "urn:foo");
594:                a.appendChild(b);
595:                b.appendChild(c);
596:                c.appendChild(d);
597:
598:                assertEquals("urn:foo", a.getNamespaceURI("foo"));
599:                assertEquals("urn:foo", b.getNamespaceURI("foo"));
600:                assertEquals("urn:foo", c.getNamespaceURI("foo"));
601:                assertEquals("urn:foo", d.getNamespaceURI("foo"));
602:            }
603:
604:            public void testNumbersAllowedInSchemes() {
605:
606:                String namespace = "u0123456789:schemespecificdata";
607:                Element e = new Element("test", namespace);
608:                assertEquals(namespace, e.getNamespaceURI());
609:
610:            }
611:
612:            public void testPunctuationMarksAllowedInSchemes() {
613:                String namespace = "u+-.:schemespecificdata";
614:                Element e = new Element("test", namespace);
615:                assertEquals(namespace, e.getNamespaceURI());
616:            }
617:
618:            public void testPunctuationMarksCantStartSchemes() {
619:                String namespace = "+:schemespecificdata";
620:                try {
621:                    new Element("test", namespace);
622:                    fail("Allowed scheme name to start with +");
623:                } catch (MalformedURIException success) {
624:                    assertNotNull(success.getMessage());
625:                }
626:            }
627:
628:            public void testNumbersCantStartSchemes() {
629:
630:                String namespace = "8uri:schemespecificdata";
631:                try {
632:                    new Element("test", namespace);
633:                    fail("Allowed scheme name to start with digit");
634:                } catch (MalformedURIException success) {
635:                    assertNotNull(success.getMessage());
636:                }
637:
638:            }
639:
640:            /** This is a very funny test case. RFC 1738 allows URIs like
641:             *  <code>prospero://host.dom//pros/name</code> but RFC 2396 and
642:             *  RFC2396bis forbid them.
643:             */
644:            public void testPathCantStartWithDoubleSlash() {
645:
646:                String namespace = "prospero://host.dom//pros/name";
647:                try {
648:                    new Element("test", namespace);
649:                    fail("Allowed URI with path containing double slash");
650:                } catch (MalformedURIException success) {
651:                    assertNotNull(success.getMessage());
652:                }
653:
654:            }
655:
656:            public void testURIReferenceCantHaveTwoFragmentIDs() {
657:                String namespace = "uri:schemespecificdata#test#id";
658:                try {
659:                    new Element("test", namespace);
660:                    fail("Allowed URI reference to contain multiple fragment IDs");
661:                } catch (MalformedURIException success) {
662:                    assertNotNull(success.getMessage());
663:                }
664:            }
665:
666:            public void testHalfPercentEscape() {
667:                String namespace = "http://www.example.org/%ce%a";
668:                try {
669:                    new Element("test", namespace);
670:                    fail("Allowed path to contain only half a percent escape");
671:                } catch (MalformedURIException success) {
672:                    assertNotNull(success.getMessage());
673:                }
674:
675:            }
676:
677:            public void testUnescapedPercentSign() {
678:
679:                String namespace = "http://www.example.org/%ce%";
680:                try {
681:                    new Element("test", namespace);
682:                    fail("Allowed path to contain only percent");
683:                } catch (MalformedURIException success) {
684:                    assertNotNull(success.getMessage());
685:                }
686:
687:            }
688:
689:            public void testPercentSignFollowedByNonHexDigits() {
690:
691:                for (char c = 'G'; c <= '`'; c++) {
692:                    String namespace = "http://www.example.org/%1" + c
693:                            + "test/";
694:                    try {
695:                        new Element("test", namespace);
696:                        fail("Allowed malformed namespace URI " + namespace);
697:                    } catch (MalformedURIException success) {
698:                        assertNotNull(success.getMessage());
699:                    }
700:                }
701:
702:                for (char c = ':'; c <= '@'; c++) {
703:                    String namespace = "http://www.example.org/%1" + c
704:                            + "test/";
705:                    try {
706:                        new Element("test", namespace);
707:                        fail("Allowed malformed namespace URI " + namespace);
708:                    } catch (MalformedURIException success) {
709:                        assertNotNull(success.getMessage());
710:                    }
711:                }
712:
713:            }
714:
715:            public void testVariousSchemes() {
716:
717:                String[] urls = {
718:                        "ftp://example.com",
719:                        "FTP://example.com/pub/",
720:                        "MAILTO:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
721:                        "mailto:elharo@metalab.unc.edu?Subject=XOM%20Namespace",
722:                        "telnet:namespaces.ibiblio.org",
723:                        "TELNET:namespaces.ibiblio.org",
724:                        "gopher://gopher.uminn.edu/",
725:                        "GOPHER://gopher.uminn.edu/",
726:                        "uri:urn:nwalsh:namespaces",
727:                        "URI:urn:nwalsh:namespaces",
728:                        "news:comp.lang.xml",
729:                        "NEWS:comp.lang.xml",
730:                        "wais://wais.example.com:78/database",
731:                        "WAIS://wais.example.com:78/database",
732:                        "file://vms.host.edu/disk$user/my/notes/note12345.txt",
733:                        "FILE://vms.host.edu/disk$user/my/notes/note12345.txt",
734:                        "z39.50s://melvyl.ucop.edu/cat",
735:                        "Z39.50S://melvyl.ucop.edu/cat",
736:                        "z39.50r://melvyl.ucop.edu/mags?elecworld.v30.n19",
737:                        "Z39.50R://melvyl.ucop.edu/mags?elecworld.v30.n19",
738:                        "z39.50r://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
739:                        "Z39.50R://cnidr.org:2100/tmf?bkirch_rules__a1;esn=f;rs=marc",
740:                        "mid:960830.1639@XIson.com/partA.960830.1639@XIson.com",
741:                        "MID:960830.1639@XIson.com/partA.960830.1639@XIson.com",
742:                        "cid:foo4*foo1@bar.net",
743:                        "CID:foo4*foo1@bar.net",
744:                        "vemmi://zeus.mctel.fr/demo",
745:                        "VEMMI://zeus.mctel.fr/demo",
746:                        "vemmi://mctel.fr/demo;$USERDATA=smith;account=1234",
747:                        "opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
748:                        "OPAQUELOCKTOKEN:f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
749:                        "fax:+358.555.1234567",
750:                        "FAX:+358.555.1234567",
751:                        "modem:+3585551234567;type=v32b?7e1;type=v110",
752:                        "tel:0w003585551234567;phone-context=+3585551234",
753:                        "tel:+1234567890;phone-context=+1234;vnd.company.option=foo",
754:                        "xmlrpc.beeps://stateserver.example.com/NumberToName",
755:                        "XMLRPC.BEEPS://stateserver.example.com/NumberToName",
756:                        "h323:user@h323.example.com",
757:                        "H323:user@h323.example.com",
758:                        "tn3270://login.example.com" };
759:                for (int i = 0; i < urls.length; i++) {
760:                    Element e = new Element("pre:test", urls[i]);
761:                    assertEquals(e.getNamespaceURI("pre"), urls[i]);
762:                }
763:
764:            }
765:
766:            public void testPercentEscapes() {
767:
768:                // Namespace URIs are compared for direct string equality;
769:                // no de-escaping is performed
770:                for (char c = ' '; c <= '~'; c++) {
771:                    String url = "http://www.example.com/%"
772:                            + Integer.toHexString(c) + "test/";
773:                    Element e = new Element("pre:test", url);
774:                    assertEquals(url, e.getNamespaceURI());
775:                    assertEquals(url, e.getNamespaceURI("pre"));
776:                }
777:                // repeat in upper case
778:                for (char c = ' '; c <= '~'; c++) {
779:                    String url = "http://www.example.com/%"
780:                            + Integer.toHexString(c).toUpperCase() + "test/";
781:                    Element e = new Element("pre:test", url);
782:                    assertEquals(url, e.getNamespaceURI());
783:                    assertEquals(url, e.getNamespaceURI("pre"));
784:                }
785:
786:            }
787:
788:            public void testDelims() {
789:
790:                String[] delims = { "<", ">", "\"" };
791:                for (int i = 0; i < delims.length; i++) {
792:                    String url = "http://www.example.com/" + delims[i] + "/";
793:                    try {
794:                        new Element("test", url);
795:                        fail("Allowed " + url + " as namespace URI");
796:                    } catch (MalformedURIException success) {
797:                        assertNotNull(success.getMessage());
798:                    }
799:                }
800:
801:            }
802:
803:            public void testUnwise() {
804:
805:                char[] unwise = { '{', '}', '|', '\\', '^', '`' };
806:                for (int i = 0; i < unwise.length; i++) {
807:                    String url = "http://www.example.com/" + unwise[i] + "/";
808:                    try {
809:                        new Element("test", url);
810:                        fail("Allowed " + url + " as namespace URI");
811:                    } catch (MalformedURIException success) {
812:                        assertNotNull(success.getMessage());
813:                    }
814:                }
815:
816:            }
817:
818:            public void testSetPrefixOnElementInNoNamespace() {
819:
820:                Element e = new Element("Seq");
821:                try {
822:                    e.setNamespacePrefix("rdf");
823:                    fail("Set prefix on element in no namespace");
824:                } catch (NamespaceConflictException success) {
825:                    assertNotNull(success.getMessage());
826:                }
827:
828:            }
829:
830:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.