Source Code Cross Referenced for TestXmlUtil.java in  » ESB » open-esb » com » sun » jbi » framework » 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 » ESB » open esb » com.sun.jbi.framework 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BEGIN_HEADER - DO NOT EDIT
003:         *
004:         * The contents of this file are subject to the terms
005:         * of the Common Development and Distribution License
006:         * (the "License").  You may not use this file except
007:         * in compliance with the License.
008:         *
009:         * You can obtain a copy of the license at
010:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011:         * See the License for the specific language governing
012:         * permissions and limitations under the License.
013:         *
014:         * When distributing Covered Code, include this CDDL
015:         * HEADER in each file and include the License file at
016:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017:         * If applicable add the following below this CDDL HEADER,
018:         * with the fields enclosed by brackets "[]" replaced with
019:         * your own identifying information: Portions Copyright
020:         * [year] [name of copyright owner]
021:         */
022:
023:        /*
024:         * @(#)TestXmlUtil.java
025:         * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026:         *
027:         * END_HEADER - DO NOT EDIT
028:         */
029:
030:        package com.sun.jbi.framework;
031:
032:        import java.io.ByteArrayInputStream;
033:
034:        import java.util.List;
035:        import java.util.Properties;
036:
037:        import javax.xml.parsers.DocumentBuilder;
038:        import javax.xml.parsers.DocumentBuilderFactory;
039:
040:        import org.w3c.dom.Document;
041:        import org.w3c.dom.Node;
042:        import org.w3c.dom.NodeList;
043:
044:        /**
045:         * Tests for the Component class.
046:         *
047:         * @author Sun Microsystems, Inc.
048:         */
049:        public class TestXmlUtil extends junit.framework.TestCase {
050:            /**
051:             * Current test name.
052:             */
053:            private String mTestName;
054:
055:            /**
056:             * EnvironmentContext
057:             */
058:            private EnvironmentContext mEnvironmentContext;
059:
060:            /**
061:             * The constructor for this testcase, forwards the test name to
062:             * the jUnit TestCase base class.
063:             * @param aTestName String with the name of this test.
064:             */
065:            public TestXmlUtil(String aTestName) {
066:                super (aTestName);
067:                mTestName = aTestName;
068:            }
069:
070:            /**
071:             * Setup for the test.
072:             * @throws Exception when set up fails for any reason.
073:             */
074:            public void setUp() throws Exception {
075:                super .setUp();
076:                System.err.println("***** START of test " + mTestName);
077:                mEnvironmentContext = new EnvironmentContext(
078:                        new ScaffoldPlatformContext(), new JBIFramework(),
079:                        new Properties());
080:            }
081:
082:            /**
083:             * Cleanup for the test.
084:             * @throws Exception when tearDown fails for any reason.
085:             */
086:            public void tearDown() throws Exception {
087:                System.err.println("***** END of test " + mTestName);
088:                super .tearDown();
089:            }
090:
091:            // =============================  test methods ================================
092:
093:            //
094:            // First test the getElements() method, as it is called by other methods
095:            // in the class.
096:            //
097:
098:            /**
099:             * Test the getElements() method for a required tag that is present.
100:             * @throws Exception if an unexpected error occurs.
101:             */
102:            public void testGetElementsRequiredPresent() throws Exception {
103:                // Create an XML document for the test.
104:
105:                String xmlString = "<topnode>" + "<element0>value0</element0>"
106:                        + "<element1>value1</element1>" + "</topnode>";
107:                Node top = createDocument(xmlString);
108:                NodeList nodes = XmlUtil.getElements(top, "element1", true);
109:
110:                // Verify that the correct Node was returned.
111:
112:                assertEquals("Incorrect number of nodes returned: ", 1, nodes
113:                        .getLength());
114:                assertEquals("Incorrect node returned: ", "element1", nodes
115:                        .item(0).getNodeName());
116:            }
117:
118:            /**
119:             * Test the getElements() method for a required tag that is repeated.
120:             * @throws Exception if an unexpected error occurs.
121:             */
122:            public void testGetElementsRequiredRepeated() throws Exception {
123:                // Create an XML document for the test.
124:
125:                String xmlString = "<topnode>" + "<element0>value0</element0>"
126:                        + "<element1>value1</element1>"
127:                        + "<element2>value2</element2>"
128:                        + "<element1>value1</element1>" + "</topnode>";
129:                Node top = createDocument(xmlString);
130:                NodeList nodes = XmlUtil.getElements(top, "element1", true);
131:
132:                // Verify that the correct Nodes were returned.
133:
134:                assertEquals("Incorrect number of nodes returned: ", 2, nodes
135:                        .getLength());
136:                assertEquals("Incorrect node returned: ", "element1", nodes
137:                        .item(0).getNodeName());
138:                assertEquals("Incorrect node returned: ", "element1", nodes
139:                        .item(1).getNodeName());
140:            }
141:
142:            /**
143:             * Test the getElements() method for a required tag that is missing.
144:             * @throws Exception if an unexpected error occurs.
145:             */
146:            public void testGetElementsRequiredMissing() throws Exception {
147:                // Create an XML document for the test.
148:
149:                String xmlString = "<topnode>" + "<element0>value0</element0>"
150:                        + "</topnode>";
151:                Node top = createDocument(xmlString);
152:
153:                try {
154:                    NodeList nodes = XmlUtil.getElements(top, "element1", true);
155:                    fail("Expected exception not received");
156:                } catch (javax.jbi.JBIException ex) {
157:                    // Verification
158:                    assertTrue(
159:                            "Incorrect exception received: " + ex.toString(),
160:                            (-1 < ex.getMessage().indexOf("JBIFW2600")));
161:                }
162:            }
163:
164:            /**
165:             * Test the getElements() method for an optional tag that is present.
166:             * @throws Exception if an unexpected error occurs.
167:             */
168:            public void testGetElementsOptionalPresent() throws Exception {
169:                // Create an XML document for the test.
170:
171:                String xmlString = "<topnode>" + "<element0>value0</element0>"
172:                        + "<element1>value1</element1>" + "</topnode>";
173:                Node top = createDocument(xmlString);
174:                NodeList nodes = XmlUtil.getElements(top, "element1", false);
175:
176:                // Verify that the correct Node was returned.
177:
178:                assertEquals("Incorrect number of nodes returned: ", 1, nodes
179:                        .getLength());
180:                assertEquals("Incorrect node returned: ", "element1", nodes
181:                        .item(0).getNodeName());
182:            }
183:
184:            /**
185:             * Test the getElements() method for an optional tag that is missing.
186:             * @throws Exception if an unexpected error occurs.
187:             */
188:            public void testGetElementsOptionalMissing() throws Exception {
189:                // Create an XML document for the test.
190:
191:                String xmlString = "<topnode>" + "<element0>value0</element0>"
192:                        + "<element1>value1</element1>" + "</topnode>";
193:                Node top = createDocument(xmlString);
194:                NodeList nodes = XmlUtil.getElements(top, "element2", false);
195:
196:                // Verify that an empty NodeList was returned.
197:                assertEquals("Nonempty NodeList returned: ", 0, nodes
198:                        .getLength());
199:            }
200:
201:            //
202:            // Tests for the getElement() method, which calls getElements()
203:            //
204:
205:            /**
206:             * Test the getElement() method for a required tag that is present.
207:             * @throws Exception if an unexpected error occurs.
208:             */
209:            public void testGetElementRequiredPresent() throws Exception {
210:                // Create an XML document for the test.
211:
212:                String xmlString = "<topnode>" + "<element0>value0</element0>"
213:                        + "<element1>value1</element1>" + "</topnode>";
214:                Node top = createDocument(xmlString);
215:                Node node = XmlUtil.getElement(top, "element1", true);
216:
217:                // Verify that the correct Node was returned.
218:
219:                assertEquals("Incorrect node returned: ", "element1", node
220:                        .getNodeName());
221:            }
222:
223:            /**
224:             * Test the getElement() method for a required tag that is repeated.
225:             * @throws Exception if an unexpected error occurs.
226:             */
227:            public void testGetElementRequiredRepeated() throws Exception {
228:                // Create an XML document for the test.
229:
230:                String xmlString = "<topnode>" + "<element0>value0</element0>"
231:                        + "<element1>value1</element1>"
232:                        + "<element2>value2</element2>"
233:                        + "<element1>value1</element1>" + "</topnode>";
234:                Node top = createDocument(xmlString);
235:
236:                try {
237:                    Node node = XmlUtil.getElement(top, "element1", true);
238:                    fail("Expected exception not received");
239:                } catch (javax.jbi.JBIException ex) {
240:                    // Verification
241:                    assertTrue(
242:                            "Incorrect exception received: " + ex.toString(),
243:                            (-1 < ex.getMessage().indexOf("JBIFW2601")));
244:                }
245:            }
246:
247:            /**
248:             * Test the getElement() method for a required tag that is missing.
249:             * @throws Exception if an unexpected error occurs.
250:             */
251:            public void testGetElementRequiredMissing() throws Exception {
252:                // Create an XML document for the test.
253:
254:                String xmlString = "<topnode>" + "<element0>value0</element0>"
255:                        + "</topnode>";
256:                Node top = createDocument(xmlString);
257:
258:                try {
259:                    Node node = XmlUtil.getElement(top, "element1", true);
260:                    fail("Expected exception not received");
261:                } catch (javax.jbi.JBIException ex) {
262:                    // Verification
263:                    assertTrue(
264:                            "Incorrect exception received: " + ex.toString(),
265:                            (-1 < ex.getMessage().indexOf("JBIFW2600")));
266:                }
267:            }
268:
269:            /**
270:             * Test the getElement() method for an optional tag that is present.
271:             * @throws Exception if an unexpected error occurs.
272:             */
273:            public void testGetElementOptionalPresent() throws Exception {
274:                // Create an XML document for the test.
275:
276:                String xmlString = "<topnode>" + "<element0>value0</element0>"
277:                        + "<element1>value1</element1>" + "</topnode>";
278:                Node top = createDocument(xmlString);
279:                Node node = XmlUtil.getElement(top, "element1", false);
280:
281:                // Verify that the correct Node was returned.
282:
283:                assertEquals("Incorrect node returned: ", "element1", node
284:                        .getNodeName());
285:            }
286:
287:            /**
288:             * Test the getElement() method for an optional tag that is missing.
289:             * @throws Exception if an unexpected error occurs.
290:             */
291:            public void testGetElementOptionalMissing() throws Exception {
292:                // Create an XML document for the test.
293:
294:                String xmlString = "<topnode>" + "<element0>value0</element0>"
295:                        + "<element1>value1</element1>" + "</topnode>";
296:                Node top = createDocument(xmlString);
297:                Node node = XmlUtil.getElement(top, "element2", false);
298:
299:                // Verify that a null value was returned.
300:
301:                assertNull(
302:                        "Missing optional element returned a non-null value when "
303:                                + "a null was expected.", node);
304:            }
305:
306:            //
307:            // Tests for the getAttribute() method
308:            //
309:
310:            /**
311:             * Test the getAttribute() method with a null node parameter.
312:             * @throws Exception if an unexpected error occurs.
313:             */
314:            public void testGetAttributeBadNullNode() throws Exception {
315:                try {
316:                    String value = XmlUtil.getAttribute((Node) null,
317:                            "attribute0");
318:                    fail("Expected exception not received");
319:                } catch (java.lang.IllegalArgumentException ex) {
320:                    // Verification
321:                    assertTrue(
322:                            "Incorrect exception received: " + ex.toString(),
323:                            (-1 < ex.getMessage().indexOf("JBIFW0063")));
324:                }
325:            }
326:
327:            /**
328:             * Test the getAttribute() method with a null attrName parameter.
329:             * @throws Exception if an unexpected error occurs.
330:             */
331:            public void testGetAttributeBadNullAttrName() throws Exception {
332:                // Create an XML document for the test.
333:
334:                String xmlString = "<topnode attr0=\"value0\">"
335:                        + "<element0>value0</element0>"
336:                        + "<element1>value1</element1>" + "</topnode>";
337:                Node top = createDocument(xmlString);
338:
339:                try {
340:                    String value = XmlUtil.getAttribute(top, null);
341:                    fail("Expected exception not received");
342:                } catch (java.lang.IllegalArgumentException ex) {
343:                    // Verification
344:                    assertTrue(
345:                            "Incorrect exception received: " + ex.toString(),
346:                            (-1 < ex.getMessage().indexOf("JBIFW0063")));
347:                }
348:            }
349:
350:            /**
351:             * Test the getAttribute() method with a missing attribute.
352:             * @throws Exception if an unexpected error occurs.
353:             */
354:            public void testGetAttributeGoodNotFound() throws Exception {
355:                // Create an XML document for the test.
356:
357:                String xmlString = "<topnode>" + "<element0>value0</element0>"
358:                        + "<element1>value1</element1>" + "</topnode>";
359:                Node doc = createDocument(xmlString);
360:
361:                String value = XmlUtil.getAttribute(doc, "attribute0");
362:
363:                // Verification
364:                assertEquals("Unexpected value received: " + value, 0, value
365:                        .length());
366:            }
367:
368:            /**
369:             * Test the getAttribute() method with a specified attribute.
370:             * @throws Exception if an unexpected error occurs.
371:             */
372:            public void testGetAttributeGoodFound() throws Exception {
373:                // Create an XML document for the test.
374:
375:                String xmlString = "<topnode attribute0=\"attr0\">"
376:                        + "<element0>value0</element0>"
377:                        + "<element1>value1</element1>" + "</topnode>";
378:                Node doc = createDocument(xmlString);
379:
380:                String value = XmlUtil.getAttribute(doc, "attribute0");
381:
382:                // Verification
383:                assertEquals("Expected value not received: " + value, "attr0",
384:                        value);
385:            }
386:
387:            /**
388:             * Test the getAttribute() method with a specified attribute.
389:             * @throws Exception if an unexpected error occurs.
390:             */
391:            public void testGetAttributeGoodFoundChild() throws Exception {
392:                // Create an XML document for the test.
393:
394:                String xmlString = "<topnode>"
395:                        + "<element0 attribute0=\"attr0\">value0</element0>"
396:                        + "<element1>value1</element1>" + "</topnode>";
397:                Node doc = createDocument(xmlString);
398:                Node elem = XmlUtil.getElement(doc, "element0", true);
399:
400:                String value = XmlUtil.getAttribute(elem, "attribute0");
401:
402:                // Verification
403:                assertEquals("Expected value not received: " + value, "attr0",
404:                        value);
405:            }
406:
407:            //
408:            // Tests for the getBooleanValue() method
409:            //
410:
411:            /**
412:             * Test the getBooleanValue() method for a required tag that is present.
413:             * @throws Exception if an unexpected error occurs.
414:             */
415:            public void testGetBooleanValueRequiredPresent() throws Exception {
416:                // Create an XML document for the test.
417:
418:                String xmlString = "<topnode>" + "<element0>value0</element0>"
419:                        + "<element1>true</element1>"
420:                        + "<element2>false</element2>"
421:                        + "<element3>nerbunge</element3>" + "</topnode>";
422:                Node top = createDocument(xmlString);
423:                Boolean value1 = XmlUtil.getBooleanValue(top, "element1", true);
424:                Boolean value2 = XmlUtil.getBooleanValue(top, "element2", true);
425:                Boolean value3 = XmlUtil.getBooleanValue(top, "element3", true);
426:
427:                // Verify that the correct values were returned.
428:
429:                assertTrue("Incorrect value returned for element1: ", value1
430:                        .booleanValue());
431:                assertFalse("Incorrect value returned for element2: ", value2
432:                        .booleanValue());
433:                assertFalse("Incorrect value returned for element3: ", value3
434:                        .booleanValue());
435:            }
436:
437:            /**
438:             * Test the getBooleanValue() method for a required tag that is missing.
439:             * @throws Exception if an unexpected error occurs.
440:             */
441:            public void testGetBooleanValueRequiredMissing() throws Exception {
442:                // Create an XML document for the test.
443:
444:                String xmlString = "<topnode>" + "<element0>value0</element0>"
445:                        + "</topnode>";
446:                Node top = createDocument(xmlString);
447:
448:                try {
449:                    Boolean value = XmlUtil.getBooleanValue(top, "element1",
450:                            true);
451:                    fail("Expected exception not received");
452:                } catch (javax.jbi.JBIException ex) {
453:                    // Verification
454:                    assertTrue(
455:                            "Incorrect exception received: " + ex.toString(),
456:                            (-1 < ex.getMessage().indexOf("JBIFW2600")));
457:                }
458:            }
459:
460:            /**
461:             * Test the getBooleanValue() method for an optional tag that is present.
462:             * @throws Exception if an unexpected error occurs.
463:             */
464:            public void testGetBooleanValueOptionalPresent() throws Exception {
465:                // Create an XML document for the test.
466:
467:                String xmlString = "<topnode>" + "<element0>value0</element0>"
468:                        + "<element1>true</element1>"
469:                        + "<element2>false</element2>"
470:                        + "<element3>nerbunge</element3>" + "</topnode>";
471:                Node top = createDocument(xmlString);
472:                Boolean value1 = XmlUtil
473:                        .getBooleanValue(top, "element1", false);
474:                Boolean value2 = XmlUtil
475:                        .getBooleanValue(top, "element2", false);
476:                Boolean value3 = XmlUtil
477:                        .getBooleanValue(top, "element3", false);
478:
479:                // Verify that the correct values were returned.
480:
481:                assertTrue("Incorrect value returned for element1: ", value1
482:                        .booleanValue());
483:                assertFalse("Incorrect value returned for element2: ", value2
484:                        .booleanValue());
485:                assertFalse("Incorrect value returned for element3: ", value3
486:                        .booleanValue());
487:            }
488:
489:            /**
490:             * Test the getBooleanValue() method for an optional tag that is missing.
491:             * @throws Exception if an unexpected error occurs.
492:             */
493:            public void testGetBooleanValueOptionalMissing() throws Exception {
494:                // Create an XML document for the test.
495:
496:                String xmlString = "<topnode>" + "<element0>value0</element0>"
497:                        + "</topnode>";
498:                Node top = createDocument(xmlString);
499:
500:                Boolean value1 = XmlUtil
501:                        .getBooleanValue(top, "element1", false);
502:
503:                // Verification
504:                assertNull("Incorrect value returned for element1: ", value1);
505:            }
506:
507:            //
508:            // Tests for the getStringValue(Node) method
509:            //
510:
511:            /**
512:             * Test the getStringValue(Node) method with a null input parameter.
513:             * @throws Exception if an unexpected error occurs.
514:             */
515:            public void testGetStringValueNodeBadNullInput() throws Exception {
516:                try {
517:                    String value = XmlUtil.getStringValue((Node) null);
518:                    fail("Expected exception not received");
519:                } catch (java.lang.IllegalArgumentException ex) {
520:                    // Verification
521:                    assertTrue(
522:                            "Incorrect exception received: " + ex.toString(),
523:                            (-1 < ex.getMessage().indexOf("JBIFW0063")));
524:                }
525:            }
526:
527:            /**
528:             * Test the getStringValue(Node) method with an empty input parameter.
529:             * @throws Exception if an unexpected error occurs.
530:             */
531:            public void testGetStringValueNodeBadEmptyInput() throws Exception {
532:                // Create an XML document for the test.
533:
534:                String xmlString = "<topnode></topnode>";
535:                Node doc = createDocument(xmlString);
536:                try {
537:                    String value = XmlUtil.getStringValue(doc);
538:                    fail("Expected exception not received");
539:                } catch (javax.jbi.JBIException ex) {
540:                    // Verification
541:                    assertTrue(
542:                            "Incorrect exception received: " + ex.toString(),
543:                            (-1 < ex.getMessage().indexOf("JBIFW2602")));
544:                }
545:            }
546:
547:            /**
548:             * Test the getStringValue(Node) method with a missing text node.
549:             * @throws Exception if an unexpected error occurs.
550:             */
551:            public void testGetStringValueNodeBadMissingTextNode()
552:                    throws Exception {
553:                // Create an XML document for the test.
554:
555:                String xmlString = "<topnode>" + "<element0></element0>"
556:                        + "</topnode>";
557:                Node top = createDocument(xmlString);
558:                Node node = XmlUtil.getElement(top, "element0", true);
559:                try {
560:                    String value = XmlUtil.getStringValue(node);
561:                    fail("Expected exception not received");
562:                } catch (javax.jbi.JBIException ex) {
563:                    // Verification
564:                    assertTrue(
565:                            "Incorrect exception received: " + ex.toString(),
566:                            (-1 < ex.getMessage().indexOf("JBIFW2602")));
567:                }
568:            }
569:
570:            /**
571:             * Test the getStringValue(Node) method with a text node present.
572:             * @throws Exception if an unexpected error occurs.
573:             */
574:            public void testGetStringValueNodeGood() throws Exception {
575:                // Create an XML document for the test.
576:
577:                String xmlString = "<topnode>" + "<element0>value0</element0>"
578:                        + "<element1>value1</element1>" + "</topnode>";
579:                Node top = createDocument(xmlString);
580:
581:                // Execute test
582:
583:                Node node = XmlUtil.getElement(top, "element0", true);
584:                String value = XmlUtil.getStringValue(node);
585:
586:                // Verify that the correct value was returned.
587:
588:                assertEquals("Incorrect value returned for element0: ",
589:                        "value0", value);
590:
591:                // Execute test
592:
593:                node = XmlUtil.getElement(top, "element1", true);
594:                value = XmlUtil.getStringValue(node);
595:
596:                // Verify that the correct value was returned.
597:
598:                assertEquals("Incorrect value returned for element1: ",
599:                        "value1", value);
600:            }
601:
602:            //
603:            // Tests for the getStringValue(NodeList) methods
604:            //
605:
606:            /**
607:             * Test the getStringValue() method with a null input parameter.
608:             * @throws Exception if an unexpected error occurs.
609:             */
610:            public void testGetStringValueBadNullInput() throws Exception {
611:                try {
612:                    String value = XmlUtil.getStringValue((NodeList) null);
613:                    fail("Expected exception not received");
614:                } catch (java.lang.IllegalArgumentException ex) {
615:                    // Verification
616:                    assertTrue(
617:                            "Incorrect exception received: " + ex.toString(),
618:                            (-1 < ex.getMessage().indexOf("JBIFW0063")));
619:                }
620:            }
621:
622:            /**
623:             * Test the getStringValue() method with an empty input parameter.
624:             * @throws Exception if an unexpected error occurs.
625:             */
626:            public void testGetStringValueBadEmptyInput() throws Exception {
627:                // Create an XML document for the test.
628:
629:                String xmlString = "<topnode>" + "</topnode>";
630:                Node top = createDocument(xmlString);
631:                NodeList nodes = XmlUtil.getElements(top, "element0", false);
632:                try {
633:                    String value = XmlUtil.getStringValue(nodes);
634:                    fail("Expected exception not received");
635:                } catch (java.lang.IllegalArgumentException ex) {
636:                    // Verification
637:                    assertTrue(
638:                            "Incorrect exception received: " + ex.toString(),
639:                            (-1 < ex.getMessage().indexOf("JBIFW0060")));
640:                }
641:            }
642:
643:            /**
644:             * Test the getStringValue() method with a missing text node.
645:             * @throws Exception if an unexpected error occurs.
646:             */
647:            public void testGetStringValueBadMissingTextNode() throws Exception {
648:                // Create an XML document for the test.
649:
650:                String xmlString = "<topnode>" + "<element0></element0>"
651:                        + "</topnode>";
652:                Node top = createDocument(xmlString);
653:                NodeList nodes = XmlUtil.getElements(top, "element0", false);
654:                try {
655:                    String value = XmlUtil.getStringValue(nodes);
656:                    fail("Expected exception not received");
657:                } catch (javax.jbi.JBIException ex) {
658:                    // Verification
659:                    assertTrue(
660:                            "Incorrect exception received: " + ex.toString(),
661:                            (-1 < ex.getMessage().indexOf("JBIFW2602")));
662:                }
663:            }
664:
665:            /**
666:             * Test the getStringValue() method with an incorrect node.
667:             * @throws Exception if an unexpected error occurs.
668:             */
669:            public void testGetStringValueBadWrongType() throws Exception {
670:                // Create an XML document for the test.
671:
672:                String xmlString = "<topnode>" + "<element0>" + "<value0/>"
673:                        + "</element0>" + "</topnode>";
674:                Node top = createDocument(xmlString);
675:                NodeList nodes = XmlUtil.getElements(top, "element0", true);
676:                try {
677:                    String value = XmlUtil.getStringValue(nodes);
678:                    fail("Expected exception not received");
679:                } catch (javax.jbi.JBIException ex) {
680:                    // Verification
681:                    assertTrue(
682:                            "Incorrect exception received: " + ex.toString(),
683:                            (-1 < ex.getMessage().indexOf("JBIFW2603")));
684:                }
685:            }
686:
687:            /**
688:             * Test the getStringValue() method with a tag that is repeated.
689:             * @throws Exception if an unexpected error occurs.
690:             */
691:            public void testGetStringValueBadRepeated() throws Exception {
692:                // Create an XML document for the test.
693:
694:                String xmlString = "<topnode>" + "<element0>value0</element0>"
695:                        + "<element1>value1</element1>"
696:                        + "<element2>value2</element2>"
697:                        + "<element1>value1</element1>" + "</topnode>";
698:                Node top = createDocument(xmlString);
699:                NodeList nodes = XmlUtil.getElements(top, "element1", true);
700:                try {
701:                    String value = XmlUtil.getStringValue(nodes);
702:                    fail("Expected exception not received");
703:                } catch (javax.jbi.JBIException ex) {
704:                    // Verification
705:                    assertTrue(
706:                            "Incorrect exception received: " + ex.toString(),
707:                            (-1 < ex.getMessage().indexOf("JBIFW2601")));
708:                }
709:            }
710:
711:            /**
712:             * Test the getStringValue() method for a required tag that is present.
713:             * @throws Exception if an unexpected error occurs.
714:             */
715:            public void testGetStringValueRequiredPresent() throws Exception {
716:                // Create an XML document for the test.
717:
718:                String xmlString = "<topnode>" + "<element0>value0</element0>"
719:                        + "<element1>value1</element1>" + "</topnode>";
720:                Node top = createDocument(xmlString);
721:                String value1 = XmlUtil.getStringValue(top, "element1", true);
722:
723:                // Verify that the correct value was returned.
724:
725:                assertEquals("Incorrect value returned for element1: ",
726:                        "value1", value1);
727:            }
728:
729:            /**
730:             * Test the getStringValue() method for a required tag that is missing.
731:             * @throws Exception if an unexpected error occurs.
732:             */
733:            public void testGetStringValueRequiredMissing() throws Exception {
734:                // Create an XML document for the test.
735:
736:                String xmlString = "<topnode>" + "<element0>value0</element0>"
737:                        + "</topnode>";
738:                Node top = createDocument(xmlString);
739:
740:                try {
741:                    String value = XmlUtil
742:                            .getStringValue(top, "element1", true);
743:                    fail("Expected exception not received");
744:                } catch (javax.jbi.JBIException ex) {
745:                    // Verification
746:                    assertTrue(
747:                            "Incorrect exception received: " + ex.toString(),
748:                            (-1 < ex.getMessage().indexOf("JBIFW2600")));
749:                }
750:            }
751:
752:            /**
753:             * Test the getStringValue() method for an optional tag that is present.
754:             * @throws Exception if an unexpected error occurs.
755:             */
756:            public void testGetStringValueOptionalPresent() throws Exception {
757:                // Create an XML document for the test.
758:
759:                String xmlString = "<topnode>" + "<element0>value0</element0>"
760:                        + "<element1>value1</element1>" + "</topnode>";
761:                Node top = createDocument(xmlString);
762:                String value1 = XmlUtil.getStringValue(top, "element1", false);
763:
764:                // Verify that the correct value was returned.
765:
766:                assertEquals("Incorrect value returned for element1: ",
767:                        "value1", value1);
768:            }
769:
770:            /**
771:             * Test the getStringValue() method for an optional tag that is missing.
772:             * @throws Exception if an unexpected error occurs.
773:             */
774:            public void testGetStringValueOptionalMissing() throws Exception {
775:                // Create an XML document for the test.
776:
777:                String xmlString = "<topnode>" + "<element0>value0</element0>"
778:                        + "</topnode>";
779:                Node top = createDocument(xmlString);
780:
781:                String value1 = XmlUtil.getStringValue(top, "element1", false);
782:
783:                // Verification
784:                assertNull("Incorrect value returned for element1: ", value1);
785:            }
786:
787:            /**
788:             * Test the getStringValueArray() method for a required tag that is present.
789:             * @throws Exception if an unexpected error occurs.
790:             */
791:            public void testGetStringValueArrayRequiredPresent()
792:                    throws Exception {
793:                // Create an XML document for the test.
794:
795:                String xmlString = "<topnode>" + "<element0>value0</element0>"
796:                        + "<element1>value1_1</element1>"
797:                        + "<element2>value2</element2>"
798:                        + "<element1>value1_2</element1>" + "</topnode>";
799:                Node top = createDocument(xmlString);
800:                List<String> values = XmlUtil.getStringValueArray(top,
801:                        "element1", true);
802:
803:                // Verify that the correct values were returned.
804:
805:                assertEquals("Incorrect number of values for element1: ", 2,
806:                        values.size());
807:                assertEquals("Incorrect value returned for element1: ",
808:                        "value1_1", values.get(0));
809:                assertEquals("Incorrect value returned for element1: ",
810:                        "value1_2", values.get(1));
811:            }
812:
813:            /**
814:             * Test the getStringValueArray() method for a required tag that is missing.
815:             * @throws Exception if an unexpected error occurs.
816:             */
817:            public void testGetStringValueArrayRequiredMissing()
818:                    throws Exception {
819:                // Create an XML document for the test.
820:
821:                String xmlString = "<topnode>" + "<element0>value0</element0>"
822:                        + "</topnode>";
823:                Node top = createDocument(xmlString);
824:
825:                try {
826:                    List<String> values = XmlUtil.getStringValueArray(top,
827:                            "element1", true);
828:                    fail("Expected exception not received");
829:                } catch (javax.jbi.JBIException ex) {
830:                    // Verification
831:                    assertTrue(
832:                            "Incorrect exception received: " + ex.toString(),
833:                            (-1 < ex.getMessage().indexOf("JBIFW2600")));
834:                }
835:            }
836:
837:            /**
838:             * Test the getStringValueArray() method for an optional tag that is present.
839:             * @throws Exception if an unexpected error occurs.
840:             */
841:            public void testGetStringValueArrayOptionalPresent()
842:                    throws Exception {
843:                // Create an XML document for the test.
844:
845:                String xmlString = "<topnode>" + "<element0>value0</element0>"
846:                        + "<element1>value1_1</element1>"
847:                        + "<element2>value2</element2>"
848:                        + "<element1>value1_2</element1>" + "</topnode>";
849:                Node top = createDocument(xmlString);
850:                List<String> values = XmlUtil.getStringValueArray(top,
851:                        "element1", false);
852:
853:                // Verify that the correct values were returned.
854:
855:                assertEquals("Incorrect number of values for element1: ", 2,
856:                        values.size());
857:                assertEquals("Incorrect value returned for element1: ",
858:                        "value1_1", values.get(0));
859:                assertEquals("Incorrect value returned for element1: ",
860:                        "value1_2", values.get(1));
861:            }
862:
863:            /**
864:             * Test the getStringValueArray() method for an optional tag that is missing.
865:             * @throws Exception if an unexpected error occurs.
866:             */
867:            public void testGetStringValueArrayOptionalMissing()
868:                    throws Exception {
869:                // Create an XML document for the test.
870:
871:                String xmlString = "<topnode>" + "<element0>value0</element0>"
872:                        + "</topnode>";
873:                Node top = createDocument(xmlString);
874:
875:                List<String> values = XmlUtil.getStringValueArray(top,
876:                        "element1", false);
877:
878:                // Verification
879:
880:                assertEquals(
881:                        "Incorrect value list size returned for element1: ", 0,
882:                        values.size());
883:            }
884:
885:            //
886:            // Private methods used for setting up test documents
887:            //
888:
889:            /**
890:             * Utility method to create a document from an XML string provided by
891:             * the caller, and then return the Node representing the top-level node.
892:             * @param xmlString an XML document in a String.
893:             * @return the top-level node as a DOM Node.
894:             * @throws Exception if an unexpected error occurs.
895:             */
896:            private Node createDocument(String xmlString) throws Exception {
897:                Document doc = null;
898:
899:                // Add a standard header to the XML string.
900:
901:                String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
902:                        + xmlString;
903:
904:                // Create a DOM Document from the XML string.
905:
906:                ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml
907:                        .getBytes());
908:                try {
909:                    DocumentBuilder docBuilder = DocumentBuilderFactory
910:                            .newInstance().newDocumentBuilder();
911:                    docBuilder.setErrorHandler(new SAXErrorHandler());
912:                    doc = docBuilder.parse(xmlStream);
913:                } catch (javax.xml.parsers.ParserConfigurationException pcEx) {
914:                    fail("Unexpected exception received: " + pcEx.toString());
915:                } catch (org.xml.sax.SAXParseException spEx) {
916:                    fail("Unexpected exception received: " + spEx.toString());
917:                } catch (org.xml.sax.SAXException saxEx) {
918:                    fail("Unexpected exception received: " + saxEx.toString());
919:                }
920:
921:                NodeList nl = doc.getChildNodes();
922:                return nl.item(0);
923:            }
924:
925:            /**
926:             * This class provides the error handler for the SAX parser.
927:             * @author Sun Microsystems, Inc.
928:             */
929:            class SAXErrorHandler implements  org.xml.sax.ErrorHandler {
930:                /**
931:                 * This is a callback from the XML parser used to handle warnings.
932:                 * @param aSaxException SAXParseException is the warning.
933:                 * @throws org.xml.sax.SAXException when finished logging.
934:                 */
935:                public void warning(org.xml.sax.SAXParseException aSaxException)
936:                        throws org.xml.sax.SAXException {
937:                    throw aSaxException;
938:                }
939:
940:                /**
941:                 * This is a callback from the XML parser used to handle errors.
942:                 * @param aSaxException SAXParseException is the error.
943:                 * @throws org.xml.sax.SAXException when finished logging.
944:                 */
945:                public void error(org.xml.sax.SAXParseException aSaxException)
946:                        throws org.xml.sax.SAXException {
947:                    throw aSaxException;
948:                }
949:
950:                /**
951:                 * This is a callback from the XML parser used to handle fatal errors.
952:                 * @param aSaxException SAXParseException is the error.
953:                 * @throws org.xml.sax.SAXException when finished logging.
954:                 */
955:                public void fatalError(
956:                        org.xml.sax.SAXParseException aSaxException)
957:                        throws org.xml.sax.SAXException {
958:                    throw aSaxException;
959:                }
960:            }
961:
962:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.