Source Code Cross Referenced for TestWebXml.java in  » Testing » jakarta-cactus » org » apache » cactus » integration » ant » deployment » webapp » 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 » Testing » jakarta cactus » org.apache.cactus.integration.ant.deployment.webapp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* 
0002:         * ========================================================================
0003:         * 
0004:         * Copyright 2003-2004 The Apache Software Foundation.
0005:         *
0006:         * Licensed under the Apache License, Version 2.0 (the "License");
0007:         * you may not use this file except in compliance with the License.
0008:         * You may obtain a copy of the License at
0009:         * 
0010:         *   http://www.apache.org/licenses/LICENSE-2.0
0011:         * 
0012:         * Unless required by applicable law or agreed to in writing, software
0013:         * distributed under the License is distributed on an "AS IS" BASIS,
0014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015:         * See the License for the specific language governing permissions and
0016:         * limitations under the License.
0017:         * 
0018:         * ========================================================================
0019:         */
0020:        package org.apache.cactus.integration.ant.deployment.webapp;
0021:
0022:        import java.io.ByteArrayInputStream;
0023:        import java.io.StringReader;
0024:        import java.util.ArrayList;
0025:        import java.util.Collections;
0026:        import java.util.Iterator;
0027:        import java.util.List;
0028:
0029:        import javax.xml.parsers.DocumentBuilder;
0030:        import javax.xml.parsers.DocumentBuilderFactory;
0031:        import javax.xml.parsers.ParserConfigurationException;
0032:
0033:        import junit.framework.TestCase;
0034:
0035:        import org.w3c.dom.Document;
0036:        import org.w3c.dom.Element;
0037:        import org.w3c.dom.NodeList;
0038:        import org.xml.sax.EntityResolver;
0039:        import org.xml.sax.InputSource;
0040:        import org.xml.sax.SAXException;
0041:
0042:        /**
0043:         * Unit tests for {@link WebXml}.
0044:         *
0045:         * @version $Id: TestWebXml.java 239138 2005-02-11 09:17:14Z vmassol $
0046:         */
0047:        public final class TestWebXml extends TestCase {
0048:            /**
0049:             * The document builder factory.
0050:             */
0051:            private DocumentBuilderFactory factory;
0052:
0053:            /**
0054:             * The JAXP document builder.
0055:             */
0056:            private DocumentBuilder builder;
0057:
0058:            /**
0059:             * @see TestCase#setUp
0060:             */
0061:            public void setUp() throws ParserConfigurationException {
0062:                factory = DocumentBuilderFactory.newInstance();
0063:                factory.setValidating(false);
0064:                factory.setNamespaceAware(false);
0065:
0066:                builder = factory.newDocumentBuilder();
0067:                builder.setEntityResolver(new EntityResolver() {
0068:                    public InputSource resolveEntity(String thePublicId,
0069:                            String theSystemId) throws SAXException {
0070:                        return new InputSource(new StringReader(""));
0071:                    }
0072:                });
0073:            }
0074:
0075:            /**
0076:             * Tests whether the construction of a WebXml object with a
0077:             * <code>null</code> parameter for the DOM document throws a
0078:             * <code>NullPointerException</code>.
0079:             * 
0080:             * @throws Exception If an unexpected error occurs
0081:             */
0082:            public void testConstructionWithNullDocument() throws Exception {
0083:                try {
0084:                    new WebXml(null);
0085:                    fail("Expected NullPointerException");
0086:                } catch (NullPointerException npe) {
0087:                    // expected
0088:                }
0089:
0090:            }
0091:
0092:            /**
0093:             * Tests whether a servlet API version 2.2 descriptor is correctly detected.
0094:             * 
0095:             * @throws Exception If an unexpected error occurs
0096:             */
0097:            public void testGetVersion22() throws Exception {
0098:                String xml = "<!DOCTYPE web-app "
0099:                        + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN' "
0100:                        + "'http://java.sun.com/j2ee/dtds/web-app_2.2.dtd'>"
0101:                        + "<web-app></web-app>";
0102:                Document doc = builder.parse(new ByteArrayInputStream(xml
0103:                        .getBytes()));
0104:                WebXml webXml = new WebXml(doc);
0105:                assertEquals(WebXmlVersion.V2_2, webXml.getVersion());
0106:            }
0107:
0108:            /**
0109:             * Tests whether a servlet API version 2.3 descriptor is correctly detected.
0110:             * 
0111:             * @throws Exception If an unexpected error occurs
0112:             */
0113:            public void testGetVersion23() throws Exception {
0114:                String xml = "<!DOCTYPE web-app "
0115:                        + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' "
0116:                        + "'http://java.sun.com/dtd/web-app_2_3.dtd'>"
0117:                        + "<web-app></web-app>";
0118:                Document doc = builder.parse(new ByteArrayInputStream(xml
0119:                        .getBytes()));
0120:                WebXml webXml = new WebXml(doc);
0121:                assertEquals(WebXmlVersion.V2_3, webXml.getVersion());
0122:            }
0123:
0124:            /**
0125:             * Tests whether WebXml#getVersion returns <code>null</code> when the public
0126:             * ID of the <code>DOCTYPE</code> is not recognized.
0127:             * 
0128:             * @throws Exception If an unexpected error occurs
0129:             */
0130:            public void testGetVersionUnknown() throws Exception {
0131:                String xml = "<!DOCTYPE web-app "
0132:                        + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 1.9//EN' "
0133:                        + "'http://java.sun.com/dtd/web-app_1_9.dtd'>"
0134:                        + "<web-app></web-app>";
0135:                Document doc = builder.parse(new ByteArrayInputStream(xml
0136:                        .getBytes()));
0137:                WebXml webXml = new WebXml(doc);
0138:                assertNull(webXml.getVersion());
0139:            }
0140:
0141:            /**
0142:             * Tests whether WebXml#getVersion returns <code>null</code> when the
0143:             * <code>DOCTYPE</code> is missing.
0144:             * 
0145:             * @throws Exception If an unexpected error occurs
0146:             */
0147:            public void testGetVersionWithoutDoctype() throws Exception {
0148:                String xml = "<web-app></web-app>";
0149:                Document doc = builder.parse(new ByteArrayInputStream(xml
0150:                        .getBytes()));
0151:                WebXml webXml = new WebXml(doc);
0152:                assertNull(webXml.getVersion());
0153:            }
0154:
0155:            /**
0156:             * Tests whether calling {@link WebXml.hasFilter} with <code>null</code> as
0157:             * filter name parameter results in a <code>NullPointerException</code> 
0158:             * being thrown. 
0159:             * 
0160:             * @throws Exception If an unexpected error occurs
0161:             */
0162:            public void testHasFilterWithNullName() throws Exception {
0163:                String xml = "<web-app></web-app>";
0164:                Document doc = builder.parse(new ByteArrayInputStream(xml
0165:                        .getBytes()));
0166:                WebXml webXml = new WebXml(doc);
0167:                try {
0168:                    webXml.hasFilter(null);
0169:                    fail("Expected NullPointerException");
0170:                } catch (NullPointerException npe) {
0171:                    // expected
0172:                }
0173:
0174:            }
0175:
0176:            /**
0177:             * Tests whether {@link WebXml.hasFilter} returns the correct value for
0178:             * a descriptor containing one filter definition.
0179:             * 
0180:             * @throws Exception If an unexpected error occurs
0181:             */
0182:            public void testHasFilterWithOneFilter() throws Exception {
0183:                String xml = "<web-app>" + "  <filter>"
0184:                        + "    <filter-name>f1</filter-name>"
0185:                        + "    <filter-class>fclass1</filter-class>"
0186:                        + "  </filter>" + "</web-app>";
0187:                Document doc = builder.parse(new ByteArrayInputStream(xml
0188:                        .getBytes()));
0189:                WebXml webXml = new WebXml(doc);
0190:                assertTrue(webXml.hasFilter("f1"));
0191:                assertTrue(!webXml.hasFilter("f2"));
0192:            }
0193:
0194:            /**
0195:             * Tests whether {@link WebXml.hasFilter} returns the correct values for
0196:             * a descriptor containing multiple filter definitions.
0197:             * 
0198:             * @throws Exception If an unexpected error occurs
0199:             */
0200:            public void testHasFilterWithMultipleFilters() throws Exception {
0201:                String xml = "<web-app>" + "  <filter>"
0202:                        + "    <filter-name>f1</filter-name>"
0203:                        + "    <filter-class>fclass1</filter-class>"
0204:                        + "  </filter>" + "  <filter>"
0205:                        + "    <filter-name>f2</filter-name>"
0206:                        + "    <filter-class>fclass2</filter-class>"
0207:                        + "  </filter>" + "  <filter>"
0208:                        + "    <filter-name>f3</filter-name>"
0209:                        + "    <filter-class>fclass3</filter-class>"
0210:                        + "  </filter>" + "</web-app>";
0211:                Document doc = builder.parse(new ByteArrayInputStream(xml
0212:                        .getBytes()));
0213:                WebXml webXml = new WebXml(doc);
0214:                assertTrue(webXml.hasFilter("f1"));
0215:                assertTrue(webXml.hasFilter("f2"));
0216:                assertTrue(webXml.hasFilter("f3"));
0217:                assertTrue(!webXml.hasFilter("f4"));
0218:            }
0219:
0220:            /**
0221:             * Tests whether a DOM element representing a single filter definition can
0222:             * be correctly retrieved from a descriptor containing only that filter.
0223:             * 
0224:             * @throws Exception If an unexpected error occurs
0225:             */
0226:            public void testGetFilterElementWithOneFilter() throws Exception {
0227:                String xml = "<web-app>" + "  <filter>".trim()
0228:                        + "    <filter-name>f1</filter-name>".trim()
0229:                        + "    <filter-class>fclass1</filter-class>".trim()
0230:                        + "  </filter>".trim() + "</web-app>";
0231:                Document doc = builder.parse(new ByteArrayInputStream(xml
0232:                        .getBytes()));
0233:                WebXml webXml = new WebXml(doc);
0234:                Element servletElement = webXml.getFilter("f1");
0235:                assertNotNull(servletElement);
0236:                assertEquals("filter", servletElement.getNodeName());
0237:                assertEquals("filter-name", servletElement.getFirstChild()
0238:                        .getNodeName());
0239:                assertEquals("f1", servletElement.getFirstChild()
0240:                        .getFirstChild().getNodeValue());
0241:                assertEquals("filter-class", servletElement.getLastChild()
0242:                        .getNodeName());
0243:                assertEquals("fclass1", servletElement.getLastChild()
0244:                        .getFirstChild().getNodeValue());
0245:            }
0246:
0247:            /**
0248:             * Tests whether the filter names are retrieved in the expected order.
0249:             * 
0250:             * @throws Exception If an unexpected error occurs
0251:             */
0252:            public void testGetFilterNames() throws Exception {
0253:                String xml = "<web-app>" + "  <filter>"
0254:                        + "    <filter-name>f1</filter-name>"
0255:                        + "    <filter-class>fclass1</filter-class>"
0256:                        + "  </filter>" + "  <filter>"
0257:                        + "    <filter-name>f2</filter-name>"
0258:                        + "    <filter-class>fclass2</filter-class>"
0259:                        + "  </filter>" + "  <filter>"
0260:                        + "    <filter-name>f3</filter-name>"
0261:                        + "    <filter-class>fclass3</filter-class>"
0262:                        + "  </filter>" + "</web-app>";
0263:                Document doc = builder.parse(new ByteArrayInputStream(xml
0264:                        .getBytes()));
0265:                WebXml webXml = new WebXml(doc);
0266:                Iterator filterNames = webXml.getFilterNames();
0267:                assertEquals("f1", filterNames.next());
0268:                assertEquals("f2", filterNames.next());
0269:                assertEquals("f3", filterNames.next());
0270:                assertTrue(!filterNames.hasNext());
0271:            }
0272:
0273:            /**
0274:             * Tests whether a retrieving a filter name by the name of the class
0275:             * implementing the filter works correctly for a descriptor with a single
0276:             * filter definition.
0277:             * 
0278:             * @throws Exception If an unexpected error occurs
0279:             */
0280:            public void testGetFilterNamesForClassWithSingleFilter()
0281:                    throws Exception {
0282:                String xml = "<web-app>" + "  <filter>"
0283:                        + "    <filter-name>f1</filter-name>"
0284:                        + "    <filter-class>f1class</filter-class>"
0285:                        + "  </filter>" + "</web-app>";
0286:                Document doc = builder.parse(new ByteArrayInputStream(xml
0287:                        .getBytes()));
0288:                WebXml webXml = new WebXml(doc);
0289:                Iterator filterNames = webXml.getFilterNamesForClass("f1class");
0290:                assertEquals("f1", filterNames.next());
0291:                assertTrue(!filterNames.hasNext());
0292:            }
0293:
0294:            /**
0295:             * Tests whether a retrieving the filter names by the name of the class
0296:             * implementing the filter works correctly for a descriptor with multiple
0297:             * filter definitions.
0298:             * 
0299:             * @throws Exception If an unexpected error occurs
0300:             */
0301:            public void testGetFilterNamesForClassWithMultipleFilters()
0302:                    throws Exception {
0303:                String xml = "<web-app>" + "  <filter>"
0304:                        + "    <filter-name>f1</filter-name>"
0305:                        + "    <filter-class>f1class</filter-class>"
0306:                        + "  </filter>" + "  <filter>"
0307:                        + "    <filter-name>f2</filter-name>"
0308:                        + "    <filter-class>f2class</filter-class>"
0309:                        + "  </filter>" + "  <filter>"
0310:                        + "    <filter-name>f3</filter-name>"
0311:                        + "    <filter-class>f1class</filter-class>"
0312:                        + "  </filter>" + "</web-app>";
0313:                Document doc = builder.parse(new ByteArrayInputStream(xml
0314:                        .getBytes()));
0315:                WebXml webXml = new WebXml(doc);
0316:                Iterator filterNames = webXml.getFilterNamesForClass("f1class");
0317:                assertEquals("f1", filterNames.next());
0318:                assertEquals("f3", filterNames.next());
0319:                assertTrue(!filterNames.hasNext());
0320:            }
0321:
0322:            /**
0323:             * Tests whether a filter-mapping is correctly retrieved from a descriptor.
0324:             * 
0325:             * @throws Exception If an unexpected error occurs
0326:             */
0327:            public void testGetFilterMappingsWithOneMapping() throws Exception {
0328:                String xml = "<web-app>" + "  <filter-mapping>"
0329:                        + "    <filter-name>f1</filter-name>"
0330:                        + "    <url-pattern>/f1mapping</url-pattern>"
0331:                        + "  </filter-mapping>" + "</web-app>";
0332:                Document doc = builder.parse(new ByteArrayInputStream(xml
0333:                        .getBytes()));
0334:                WebXml webXml = new WebXml(doc);
0335:                Iterator filterMappings = webXml.getFilterMappings("f1");
0336:                assertEquals("/f1mapping", filterMappings.next());
0337:                assertTrue(!filterMappings.hasNext());
0338:            }
0339:
0340:            /**
0341:             * Tests whether multiple filter-mappings are correctly retrieved from a 
0342:             * descriptor.
0343:             * 
0344:             * @throws Exception If an unexpected error occurs
0345:             */
0346:            public void testGetFilterMappingsWithMultipleMappings()
0347:                    throws Exception {
0348:                String xml = "<web-app>" + "  <filter-mapping>"
0349:                        + "    <filter-name>f1</filter-name>"
0350:                        + "    <url-pattern>/f1mapping1</url-pattern>"
0351:                        + "  </filter-mapping>" + "  <filter-mapping>"
0352:                        + "    <filter-name>f1</filter-name>"
0353:                        + "    <url-pattern>/f1mapping2</url-pattern>"
0354:                        + "  </filter-mapping>" + "  <filter-mapping>"
0355:                        + "    <filter-name>f1</filter-name>"
0356:                        + "    <url-pattern>/f1mapping3</url-pattern>"
0357:                        + "  </filter-mapping>" + "</web-app>";
0358:                Document doc = builder.parse(new ByteArrayInputStream(xml
0359:                        .getBytes()));
0360:                WebXml webXml = new WebXml(doc);
0361:                Iterator filterMappings = webXml.getFilterMappings("f1");
0362:                assertEquals("/f1mapping1", filterMappings.next());
0363:                assertEquals("/f1mapping2", filterMappings.next());
0364:                assertEquals("/f1mapping3", filterMappings.next());
0365:                assertTrue(!filterMappings.hasNext());
0366:            }
0367:
0368:            /**
0369:             * Tests whether a filter-mapping is correctly retrieved from a descriptor.
0370:             * 
0371:             * @throws Exception If an unexpected error occurs
0372:             */
0373:            public void testGetFilterMappingsWithFilter() throws Exception {
0374:                String xml = "<web-app>" + "  <filter>"
0375:                        + "    <filter-name>f1</filter-name>"
0376:                        + "    <filter-class>f1class</filter-class>"
0377:                        + "  </filter>" + "  <filter-mapping>"
0378:                        + "    <filter-name>f1</filter-name>"
0379:                        + "    <url-pattern>/f1mapping</url-pattern>"
0380:                        + "  </filter-mapping>" + "</web-app>";
0381:                Document doc = builder.parse(new ByteArrayInputStream(xml
0382:                        .getBytes()));
0383:                WebXml webXml = new WebXml(doc);
0384:                Iterator filterMappings = webXml.getFilterMappings("f1");
0385:                assertEquals("/f1mapping", filterMappings.next());
0386:                assertTrue(!filterMappings.hasNext());
0387:            }
0388:
0389:            /**
0390:             * Tests whether a single context-param is correctly inserted into an empty
0391:             * descriptor.
0392:             * 
0393:             * @throws Exception If an unexpected error occurs
0394:             */
0395:            public void testAddContextParamToEmptyDocument() throws Exception {
0396:                String xml = "<web-app></web-app>";
0397:                Document doc = builder.parse(new ByteArrayInputStream(xml
0398:                        .getBytes()));
0399:                WebXml webXml = new WebXml(doc);
0400:                Element contextParamElement = createContextParamElement(doc,
0401:                        "param", "value");
0402:                webXml.addContextParam(contextParamElement);
0403:                assertTrue(webXml.hasContextParam("param"));
0404:            }
0405:
0406:            /**
0407:             * Tests whether a single filter is correctly inserted into an empty
0408:             * descriptor.
0409:             * 
0410:             * @throws Exception If an unexpected error occurs
0411:             */
0412:            public void testAddFilterToEmptyDocument() throws Exception {
0413:                String xml = "<web-app></web-app>";
0414:                Document doc = builder.parse(new ByteArrayInputStream(xml
0415:                        .getBytes()));
0416:                WebXml webXml = new WebXml(doc);
0417:                Element filterElement = createFilterElement(doc, "f1",
0418:                        "f1class");
0419:                webXml.addFilter(filterElement);
0420:                assertTrue(webXml.hasFilter("f1"));
0421:            }
0422:
0423:            /**
0424:             * Tests whether a single context param is correctly inserted into a 
0425:             * descriptor that already contains an other context param definition.
0426:             * 
0427:             * @throws Exception If an unexpected error occurs
0428:             */
0429:            public void testAddContextParamToDocumentWithAnotherContextParam()
0430:                    throws Exception {
0431:                String xml = "<web-app>" + "  <context-param>"
0432:                        + "    <param-name>param1</param-name>"
0433:                        + "    <param-value>value1</param-value>"
0434:                        + "  </context-param>" + "</web-app>";
0435:                Document doc = builder.parse(new ByteArrayInputStream(xml
0436:                        .getBytes()));
0437:                WebXml webXml = new WebXml(doc);
0438:                Element contextParamElement = createContextParamElement(doc,
0439:                        "param2", "value2");
0440:                webXml.addContextParam(contextParamElement);
0441:                assertTrue(webXml.hasContextParam("param1"));
0442:                assertTrue(webXml.hasContextParam("param2"));
0443:            }
0444:
0445:            /**
0446:             * Tests whether a single filter is correctly inserted into a descriptor 
0447:             * that already contains an other filter definition.
0448:             * 
0449:             * @throws Exception If an unexpected error occurs
0450:             */
0451:            public void testAddFilterToDocumentWithAnotherFilter()
0452:                    throws Exception {
0453:                String xml = "<web-app>" + "  <filter>"
0454:                        + "    <filter-name>f1</filter-name>"
0455:                        + "    <filter-class>fclass1</filter-class>"
0456:                        + "  </filter>" + "</web-app>";
0457:                Document doc = builder.parse(new ByteArrayInputStream(xml
0458:                        .getBytes()));
0459:                WebXml webXml = new WebXml(doc);
0460:                Element filterElement = createFilterElement(doc, "f2",
0461:                        "f2class");
0462:                webXml.addFilter(filterElement);
0463:                assertTrue(webXml.hasFilter("f1"));
0464:                assertTrue(webXml.hasFilter("f2"));
0465:            }
0466:
0467:            /**
0468:             * Tests whether trying to add a context param to a descriptor that already
0469:             * contains a context param definition with the same name results in an
0470:             * exception.
0471:             * 
0472:             * @throws Exception If an unexpected error occurs
0473:             */
0474:            public void testAddContextParamToDocumentWithTheSameContextParam()
0475:                    throws Exception {
0476:                String xml = "<web-app>" + "  <context-param>"
0477:                        + "    <param-name>param</param-name>"
0478:                        + "    <param-value>value</param-value>"
0479:                        + "  </context-param>" + "</web-app>";
0480:                Document doc = builder.parse(new ByteArrayInputStream(xml
0481:                        .getBytes()));
0482:                WebXml webXml = new WebXml(doc);
0483:                Element contextParamElement = createContextParamElement(doc,
0484:                        "param", "value");
0485:                try {
0486:                    webXml.addContextParam(contextParamElement);
0487:                    fail("Expected IllegalStateException");
0488:                } catch (IllegalStateException ise) {
0489:                    // expected
0490:                }
0491:            }
0492:
0493:            /**
0494:             * Tests whether trying to add a filter to a descriptor that already
0495:             * contains a filter definition with the same name results in a exception.
0496:             * 
0497:             * @throws Exception If an unexpected error occurs
0498:             */
0499:            public void testAddFilterToDocumentWithTheSameFilter()
0500:                    throws Exception {
0501:                String xml = "<web-app>" + "  <filter>"
0502:                        + "    <filter-name>f1</filter-name>"
0503:                        + "    <filter-class>fclass1</filter-class>"
0504:                        + "  </filter>" + "</web-app>";
0505:                Document doc = builder.parse(new ByteArrayInputStream(xml
0506:                        .getBytes()));
0507:                WebXml webXml = new WebXml(doc);
0508:                Element filterElement = createFilterElement(doc, "f1",
0509:                        "f1class");
0510:                try {
0511:                    webXml.addFilter(filterElement);
0512:                    fail("Expected IllegalStateException");
0513:                } catch (IllegalStateException ise) {
0514:                    // expected
0515:                }
0516:            }
0517:
0518:            /**
0519:             * Tests whether a single initialization parameter can be added to a filter
0520:             * definition.
0521:             * 
0522:             * @throws Exception If an unexpected error occurs
0523:             */
0524:            public void testAddOneFilterInitParam() throws Exception {
0525:                String xml = "<web-app>" + "  <filter>"
0526:                        + "    <filter-name>f1</filter-name>"
0527:                        + "    <filter-class>fclass1</filter-class>"
0528:                        + "  </filter>" + "</web-app>";
0529:                Document doc = builder.parse(new ByteArrayInputStream(xml
0530:                        .getBytes()));
0531:                WebXml webXml = new WebXml(doc);
0532:                webXml.addFilterInitParam("f1", "f1param1", "f1param1value");
0533:                Iterator initParams = webXml.getFilterInitParamNames("f1");
0534:                assertEquals("f1param1", initParams.next());
0535:                assertTrue(!initParams.hasNext());
0536:            }
0537:
0538:            /**
0539:             * Tests whether multiple initialization parameter can be added to a filter
0540:             * definition.
0541:             * 
0542:             * @throws Exception If an unexpected error occurs
0543:             */
0544:            public void testAddMultipleFilterInitParams() throws Exception {
0545:                String xml = "<web-app>" + "  <filter>"
0546:                        + "    <filter-name>f1</filter-name>"
0547:                        + "    <filter-class>fclass1</filter-class>"
0548:                        + "  </filter>" + "</web-app>";
0549:                Document doc = builder.parse(new ByteArrayInputStream(xml
0550:                        .getBytes()));
0551:                WebXml webXml = new WebXml(doc);
0552:                webXml.addFilterInitParam("f1", "f1param1", "f1param1value");
0553:                webXml.addFilterInitParam("f1", "f1param2", "f1param2value");
0554:                webXml.addFilterInitParam("f1", "f1param3", "f1param3value");
0555:                Iterator initParams = webXml.getFilterInitParamNames("f1");
0556:                assertEquals("f1param1", initParams.next());
0557:                assertEquals("f1param2", initParams.next());
0558:                assertEquals("f1param3", initParams.next());
0559:                assertTrue(!initParams.hasNext());
0560:            }
0561:
0562:            /**
0563:             * Tests whether a single filter can be added using the method that takes 
0564:             * a string for the filter name and a string for the filter class.
0565:             * 
0566:             * @throws Exception If an unexpected error occurs
0567:             */
0568:            public void testAddFilterWithNameAndClass() throws Exception {
0569:                String xml = "<web-app>" + "</web-app>";
0570:                Document doc = builder.parse(new ByteArrayInputStream(xml
0571:                        .getBytes()));
0572:                WebXml webXml = new WebXml(doc);
0573:                webXml.addServlet("f1", "f1class");
0574:                assertTrue(webXml.hasServlet("f1"));
0575:            }
0576:
0577:            /**
0578:             * Tests whether calling {@link WebXml#hasServlet} with a <code>null</code> 
0579:             * parameter as servlet name throws a <code>NullPointerException</code>.  
0580:             * 
0581:             * @throws Exception If an unexpected error occurs
0582:             */
0583:            public void testHasServletWithNullName() throws Exception {
0584:                String xml = "<web-app></web-app>";
0585:                Document doc = builder.parse(new ByteArrayInputStream(xml
0586:                        .getBytes()));
0587:                WebXml webXml = new WebXml(doc);
0588:                try {
0589:                    webXml.hasServlet(null);
0590:                    fail("Expected NullPointerException");
0591:                } catch (NullPointerException npe) {
0592:                    // expected
0593:                }
0594:
0595:            }
0596:
0597:            /**
0598:             * Tests whether {@link WebXml#hasServlet} reports the correct values for a
0599:             * descriptor containing a single servlet definition.
0600:             * 
0601:             * @throws Exception If an unexpected error occurs
0602:             */
0603:            public void testHasServletWithOneServlet() throws Exception {
0604:                String xml = "<web-app>" + "  <servlet>"
0605:                        + "    <servlet-name>s1</servlet-name>"
0606:                        + "    <servlet-class>sclass1</servlet-class>"
0607:                        + "  </servlet>" + "</web-app>";
0608:                Document doc = builder.parse(new ByteArrayInputStream(xml
0609:                        .getBytes()));
0610:                WebXml webXml = new WebXml(doc);
0611:                assertTrue(webXml.hasServlet("s1"));
0612:                assertTrue(!webXml.hasServlet("s2"));
0613:            }
0614:
0615:            /**
0616:             * Tests whether {@link WebXml#hasServlet} reports the correct values for a
0617:             * descriptor containing multiple servlet definitions.
0618:             * 
0619:             * @throws Exception If an unexpected error occurs
0620:             */
0621:            public void testHasServletWithMultipleServlets() throws Exception {
0622:                String xml = "<web-app>" + "  <servlet>"
0623:                        + "    <servlet-name>s1</servlet-name>"
0624:                        + "    <servlet-class>sclass1</servlet-class>"
0625:                        + "  </servlet>" + "  <servlet>"
0626:                        + "    <servlet-name>s2</servlet-name>"
0627:                        + "    <servlet-class>sclass2</servlet-class>"
0628:                        + "  </servlet>" + "  <servlet>"
0629:                        + "    <servlet-name>s3</servlet-name>"
0630:                        + "    <servlet-class>sclass3</servlet-class>"
0631:                        + "  </servlet>" + "</web-app>";
0632:                Document doc = builder.parse(new ByteArrayInputStream(xml
0633:                        .getBytes()));
0634:                WebXml webXml = new WebXml(doc);
0635:                assertTrue(webXml.hasServlet("s1"));
0636:                assertTrue(webXml.hasServlet("s2"));
0637:                assertTrue(webXml.hasServlet("s3"));
0638:                assertTrue(!webXml.hasServlet("s4"));
0639:            }
0640:
0641:            /**
0642:             * Tests whether a servlet element is correctly retrieved from a descriptor
0643:             * containing only one servlet definition.
0644:             * 
0645:             * @throws Exception If an unexpected error occurs
0646:             */
0647:            public void testGetServletElementWithOneServlet() throws Exception {
0648:                String xml = "<web-app>" + "  <servlet>".trim()
0649:                        + "    <servlet-name>s1</servlet-name>".trim()
0650:                        + "    <servlet-class>sclass1</servlet-class>".trim()
0651:                        + "  </servlet>".trim() + "</web-app>";
0652:                Document doc = builder.parse(new ByteArrayInputStream(xml
0653:                        .getBytes()));
0654:                WebXml webXml = new WebXml(doc);
0655:                Element servletElement = webXml.getServlet("s1");
0656:                assertNotNull(servletElement);
0657:                assertEquals("servlet", servletElement.getNodeName());
0658:                assertEquals("servlet-name", servletElement.getFirstChild()
0659:                        .getNodeName());
0660:                assertEquals("s1", servletElement.getFirstChild()
0661:                        .getFirstChild().getNodeValue());
0662:                assertEquals("servlet-class", servletElement.getLastChild()
0663:                        .getNodeName());
0664:                assertEquals("sclass1", servletElement.getLastChild()
0665:                        .getFirstChild().getNodeValue());
0666:            }
0667:
0668:            /**
0669:             * Tests whether the names of the servlets defined in a descriptor are
0670:             * correctly returned in the expected order.
0671:             * 
0672:             * @throws Exception If an unexpected error occurs
0673:             */
0674:            public void testGetServletNames() throws Exception {
0675:                String xml = "<web-app>" + "  <servlet>"
0676:                        + "    <servlet-name>s1</servlet-name>"
0677:                        + "    <servlet-class>sclass1</servlet-class>"
0678:                        + "  </servlet>" + "  <servlet>"
0679:                        + "    <servlet-name>s2</servlet-name>"
0680:                        + "    <servlet-class>sclass2</servlet-class>"
0681:                        + "  </servlet>" + "  <servlet>"
0682:                        + "    <servlet-name>s3</servlet-name>"
0683:                        + "    <servlet-class>sclass3</servlet-class>"
0684:                        + "  </servlet>" + "</web-app>";
0685:                Document doc = builder.parse(new ByteArrayInputStream(xml
0686:                        .getBytes()));
0687:                WebXml webXml = new WebXml(doc);
0688:                Iterator servletNames = webXml.getServletNames();
0689:                assertEquals("s1", servletNames.next());
0690:                assertEquals("s2", servletNames.next());
0691:                assertEquals("s3", servletNames.next());
0692:                assertTrue(!servletNames.hasNext());
0693:            }
0694:
0695:            /**
0696:             * Tests whether a retrieving a servlet name by the name of the class
0697:             * implementing the servlet works correctly for a descriptor with a single
0698:             * servlet definition.
0699:             * 
0700:             * @throws Exception If an unexpected error occurs
0701:             */
0702:            public void testGetServletNamesForClassWithSingleServlet()
0703:                    throws Exception {
0704:                String xml = "<web-app>" + "  <servlet>"
0705:                        + "    <servlet-name>s1</servlet-name>"
0706:                        + "    <servlet-class>s1class</servlet-class>"
0707:                        + "  </servlet>" + "</web-app>";
0708:                Document doc = builder.parse(new ByteArrayInputStream(xml
0709:                        .getBytes()));
0710:                WebXml webXml = new WebXml(doc);
0711:                Iterator servletNames = webXml
0712:                        .getServletNamesForClass("s1class");
0713:                assertEquals("s1", servletNames.next());
0714:                assertTrue(!servletNames.hasNext());
0715:            }
0716:
0717:            /**
0718:             * Tests whether a retrieving the servlet names by the name of the class
0719:             * implementing the servlet works correctly for a descriptor with multiple
0720:             * servlet definitions.
0721:             * 
0722:             * @throws Exception If an unexpected error occurs
0723:             */
0724:            public void testGetServletNamesForClassWithMultipleServlets()
0725:                    throws Exception {
0726:                String xml = "<web-app>" + "  <servlet>"
0727:                        + "    <servlet-name>s1</servlet-name>"
0728:                        + "    <servlet-class>sclass1</servlet-class>"
0729:                        + "  </servlet>" + "  <servlet>"
0730:                        + "    <servlet-name>s2</servlet-name>"
0731:                        + "    <servlet-class>sclass2</servlet-class>"
0732:                        + "  </servlet>" + "  <servlet>"
0733:                        + "    <servlet-name>s3</servlet-name>"
0734:                        + "    <servlet-class>sclass1</servlet-class>"
0735:                        + "  </servlet>" + "</web-app>";
0736:                Document doc = builder.parse(new ByteArrayInputStream(xml
0737:                        .getBytes()));
0738:                WebXml webXml = new WebXml(doc);
0739:                Iterator servletNames = webXml
0740:                        .getServletNamesForClass("sclass1");
0741:                assertEquals("s1", servletNames.next());
0742:                assertEquals("s3", servletNames.next());
0743:                assertTrue(!servletNames.hasNext());
0744:            }
0745:
0746:            /**
0747:             * Tests whether a retrieving a servlet name by the path of the JSP file
0748:             * implementing the servlet works correctly for a descriptor with a single
0749:             * servlet definition.
0750:             * 
0751:             * @throws Exception If an unexpected error occurs
0752:             */
0753:            public void testGetServletNamesForJspFileWithSingleServlet()
0754:                    throws Exception {
0755:                String xml = "<web-app>" + "  <servlet>"
0756:                        + "    <servlet-name>s1</servlet-name>"
0757:                        + "    <jsp-file>/s1.jsp</jsp-file>" + "  </servlet>"
0758:                        + "</web-app>";
0759:                Document doc = builder.parse(new ByteArrayInputStream(xml
0760:                        .getBytes()));
0761:                WebXml webXml = new WebXml(doc);
0762:                Iterator servletNames = webXml
0763:                        .getServletNamesForJspFile("/s1.jsp");
0764:                assertEquals("s1", servletNames.next());
0765:                assertTrue(!servletNames.hasNext());
0766:            }
0767:
0768:            /**
0769:             * Tests whether a retrieving the servlet names by the path of the JSP file
0770:             * implementing the servlet works correctly for a descriptor with multiple
0771:             * servlet definitions.
0772:             * 
0773:             * @throws Exception If an unexpected error occurs
0774:             */
0775:            public void testGetServletNamesForJspFileWithMultipleServlets()
0776:                    throws Exception {
0777:                String xml = "<web-app>" + "  <servlet>"
0778:                        + "    <servlet-name>s1</servlet-name>"
0779:                        + "    <jsp-file>/s1.jsp</jsp-file>" + "  </servlet>"
0780:                        + "  <servlet>" + "    <servlet-name>s2</servlet-name>"
0781:                        + "    <servlet-class>sclass2</servlet-class>"
0782:                        + "  </servlet>" + "  <servlet>"
0783:                        + "    <servlet-name>s3</servlet-name>"
0784:                        + "    <jsp-file>/s3.jsp</jsp-file>" + "  </servlet>"
0785:                        + "</web-app>";
0786:                Document doc = builder.parse(new ByteArrayInputStream(xml
0787:                        .getBytes()));
0788:                WebXml webXml = new WebXml(doc);
0789:                Iterator servletNames = webXml
0790:                        .getServletNamesForJspFile("/s3.jsp");
0791:                assertEquals("s3", servletNames.next());
0792:                assertTrue(!servletNames.hasNext());
0793:            }
0794:
0795:            /**
0796:             * Tests whether a single serrvlet-mapping is correctly retrieved from a
0797:             * descriptor.
0798:             * 
0799:             * @throws Exception If an unexpected error occurs
0800:             */
0801:            public void testGetServletMappingsWithOneMapping() throws Exception {
0802:                String xml = "<web-app>" + "  <servlet-mapping>"
0803:                        + "    <servlet-name>s1</servlet-name>"
0804:                        + "    <url-pattern>/s1mapping</url-pattern>"
0805:                        + "  </servlet-mapping>" + "</web-app>";
0806:                Document doc = builder.parse(new ByteArrayInputStream(xml
0807:                        .getBytes()));
0808:                WebXml webXml = new WebXml(doc);
0809:                Iterator servletMappings = webXml.getServletMappings("s1");
0810:                assertEquals("/s1mapping", servletMappings.next());
0811:                assertTrue(!servletMappings.hasNext());
0812:            }
0813:
0814:            /**
0815:             * Tests whether multiple servlet mappings are correctly retrieved from a
0816:             * descriptor.
0817:             * 
0818:             * @throws Exception If an unexpected error occurs
0819:             */
0820:            public void testGetServletMappingsWithMultipleMappings()
0821:                    throws Exception {
0822:                String xml = "<web-app>" + "  <servlet-mapping>"
0823:                        + "    <servlet-name>s1</servlet-name>"
0824:                        + "    <url-pattern>/s1mapping1</url-pattern>"
0825:                        + "  </servlet-mapping>" + "  <servlet-mapping>"
0826:                        + "    <servlet-name>s1</servlet-name>"
0827:                        + "    <url-pattern>/s1mapping2</url-pattern>"
0828:                        + "  </servlet-mapping>" + "  <servlet-mapping>"
0829:                        + "    <servlet-name>s1</servlet-name>"
0830:                        + "    <url-pattern>/s1mapping3</url-pattern>"
0831:                        + "  </servlet-mapping>" + "</web-app>";
0832:                Document doc = builder.parse(new ByteArrayInputStream(xml
0833:                        .getBytes()));
0834:                WebXml webXml = new WebXml(doc);
0835:                Iterator servletMappings = webXml.getServletMappings("s1");
0836:                assertEquals("/s1mapping1", servletMappings.next());
0837:                assertEquals("/s1mapping2", servletMappings.next());
0838:                assertEquals("/s1mapping3", servletMappings.next());
0839:                assertTrue(!servletMappings.hasNext());
0840:            }
0841:
0842:            /**
0843:             * Tests whether a single servlet can be added to an empty descriptor.
0844:             * 
0845:             * @throws Exception If an unexpected error occurs
0846:             */
0847:            public void testAddServletToEmptyDocument() throws Exception {
0848:                String xml = "<web-app></web-app>";
0849:                Document doc = builder.parse(new ByteArrayInputStream(xml
0850:                        .getBytes()));
0851:                WebXml webXml = new WebXml(doc);
0852:                webXml.addServlet(createServletElement(doc, "s1", "s1class"));
0853:                assertTrue(webXml.hasServlet("s1"));
0854:            }
0855:
0856:            /**
0857:             * Tests whether a single servlet can be added to a descriptor already 
0858:             * containing an other servlet.
0859:             * 
0860:             * @throws Exception If an unexpected error occurs
0861:             */
0862:            public void testAddServletToDocumentWithAnotherServlet()
0863:                    throws Exception {
0864:                String xml = "<web-app>" + "  <servlet>"
0865:                        + "    <servlet-name>s1</servlet-name>"
0866:                        + "    <servlet-class>sclass1</servlet-class>"
0867:                        + "  </servlet>" + "</web-app>";
0868:                Document doc = builder.parse(new ByteArrayInputStream(xml
0869:                        .getBytes()));
0870:                WebXml webXml = new WebXml(doc);
0871:                webXml.addServlet(createServletElement(doc, "s2", "s2class"));
0872:                assertTrue(webXml.hasServlet("s1"));
0873:                assertTrue(webXml.hasServlet("s2"));
0874:            }
0875:
0876:            /**
0877:             * Tests whether trying to add a servlet to a descriptor that already
0878:             * contains a servlet with the same name results in an exception.
0879:             * 
0880:             * @throws Exception If an unexpected error occurs
0881:             */
0882:            public void testAddServletToDocumentWithTheSameServlet()
0883:                    throws Exception {
0884:                String xml = "<web-app>" + "  <servlet>"
0885:                        + "    <servlet-name>s1</servlet-name>"
0886:                        + "    <servlet-class>sclass1</servlet-class>"
0887:                        + "  </servlet>" + "</web-app>";
0888:                Document doc = builder.parse(new ByteArrayInputStream(xml
0889:                        .getBytes()));
0890:                WebXml webXml = new WebXml(doc);
0891:                try {
0892:                    webXml
0893:                            .addServlet(createServletElement(doc, "s1",
0894:                                    "s1class"));
0895:                    fail("Expected IllegalStateException");
0896:                } catch (IllegalStateException ise) {
0897:                    // expected
0898:                }
0899:            }
0900:
0901:            /**
0902:             * Tests whether a single initialization parameter is correctly added to an
0903:             * existing servlet definition.
0904:             * 
0905:             * @throws Exception If an unexpected error occurs
0906:             */
0907:            public void testAddOneServletInitParam() throws Exception {
0908:                String xml = "<web-app>" + "  <servlet>"
0909:                        + "    <servlet-name>s1</servlet-name>"
0910:                        + "    <servlet-class>sclass1</servlet-class>"
0911:                        + "  </servlet>" + "</web-app>";
0912:                Document doc = builder.parse(new ByteArrayInputStream(xml
0913:                        .getBytes()));
0914:                WebXml webXml = new WebXml(doc);
0915:                webXml.addServletInitParam("s1", "s1param1", "s1param1value");
0916:                Iterator initParams = webXml.getServletInitParamNames("s1");
0917:                assertEquals("s1param1", initParams.next());
0918:                assertTrue(!initParams.hasNext());
0919:            }
0920:
0921:            /**
0922:             * Tests whether multiple initialization parameters are correctly added to
0923:             * an existing servlet definition.
0924:             * 
0925:             * @throws Exception If an unexpected error occurs
0926:             */
0927:            public void testAddMultipleServletInitParams() throws Exception {
0928:                String xml = "<web-app>" + "  <servlet>"
0929:                        + "    <servlet-name>s1</servlet-name>"
0930:                        + "    <servlet-class>sclass1</servlet-class>"
0931:                        + "  </servlet>" + "</web-app>";
0932:                Document doc = builder.parse(new ByteArrayInputStream(xml
0933:                        .getBytes()));
0934:                WebXml webXml = new WebXml(doc);
0935:                webXml.addServletInitParam("s1", "s1param1", "s1param1value");
0936:                webXml.addServletInitParam("s1", "s1param2", "s1param2value");
0937:                webXml.addServletInitParam("s1", "s1param3", "s1param3value");
0938:                Iterator initParams = webXml.getServletInitParamNames("s1");
0939:                assertEquals("s1param1", initParams.next());
0940:                assertEquals("s1param2", initParams.next());
0941:                assertEquals("s1param3", initParams.next());
0942:                assertTrue(!initParams.hasNext());
0943:            }
0944:
0945:            /**
0946:             * Tests whether a single servlet can be added using the method that takes 
0947:             * a string for the servlet name and a string for the servlet class.
0948:             * 
0949:             * @throws Exception If an unexpected error occurs
0950:             */
0951:            public void testAddServletWithNameAndClass() throws Exception {
0952:                String xml = "<web-app>" + "</web-app>";
0953:                Document doc = builder.parse(new ByteArrayInputStream(xml
0954:                        .getBytes()));
0955:                WebXml webXml = new WebXml(doc);
0956:                webXml.addServlet("s1", "s1class");
0957:                assertTrue(webXml.hasServlet("s1"));
0958:            }
0959:
0960:            /**
0961:             * Tests whether a single servlet can be added using the method that takes 
0962:             * a string for the servlet name and a string for the JSP file.
0963:             * 
0964:             * @throws Exception If an unexpected error occurs
0965:             */
0966:            public void testAddServletWithNameAndJspFile() throws Exception {
0967:                String xml = "<web-app>" + "</web-app>";
0968:                Document doc = builder.parse(new ByteArrayInputStream(xml
0969:                        .getBytes()));
0970:                WebXml webXml = new WebXml(doc);
0971:                webXml.addJspFile("s1", "s1.jsp");
0972:                assertTrue(webXml.hasServlet("s1"));
0973:            }
0974:
0975:            /**
0976:             * Tests whether a security-constraint with no roles is successfully added
0977:             * to an empty descriptor.
0978:             * 
0979:             * @throws Exception If an unexpected error occurs
0980:             */
0981:            public void testAddSecurityConstraint() throws Exception {
0982:                String xml = "<web-app></web-app>";
0983:                Document doc = builder.parse(new ByteArrayInputStream(xml
0984:                        .getBytes()));
0985:                WebXml webXml = new WebXml(doc);
0986:                webXml.addSecurityConstraint("wrn", "/url",
0987:                        Collections.EMPTY_LIST);
0988:                assertTrue(webXml.hasSecurityConstraint("/url"));
0989:            }
0990:
0991:            /**
0992:             * Tests whether a security-constraint with two roles is successfully added
0993:             * to an empty descriptor.
0994:             * 
0995:             * @throws Exception If an unexpected error occurs
0996:             */
0997:            public void testAddSecurityConstraintWithRoles() throws Exception {
0998:                String xml = "<web-app></web-app>";
0999:                Document doc = builder.parse(new ByteArrayInputStream(xml
1000:                        .getBytes()));
1001:                WebXml webXml = new WebXml(doc);
1002:                List roles = new ArrayList();
1003:                roles.add("role1");
1004:                roles.add("role2");
1005:                webXml.addSecurityConstraint("wrn", "/url", roles);
1006:                assertTrue(webXml.hasSecurityConstraint("/url"));
1007:                Element securityConstraintElement = webXml
1008:                        .getSecurityConstraint("/url");
1009:                assertNotNull(securityConstraintElement);
1010:                Element authConstraintElement = (Element) securityConstraintElement
1011:                        .getElementsByTagName("auth-constraint").item(0);
1012:                assertNotNull(authConstraintElement);
1013:                NodeList roleNameElements = authConstraintElement
1014:                        .getElementsByTagName("role-name");
1015:                assertEquals(2, roleNameElements.getLength());
1016:                assertEquals("role1", roleNameElements.item(0).getChildNodes()
1017:                        .item(0).getNodeValue());
1018:                assertEquals("role2", roleNameElements.item(1).getChildNodes()
1019:                        .item(0).getNodeValue());
1020:            }
1021:
1022:            /**
1023:             * Tests whether checking an empty descriptor for a login configuration
1024:             * results in <code>false</code>.
1025:             * 
1026:             * @throws Exception If an unexpected error occurs
1027:             */
1028:            public void testHasLoginConfigEmpty() throws Exception {
1029:                String xml = "<web-app></web-app>";
1030:                Document doc = builder.parse(new ByteArrayInputStream(xml
1031:                        .getBytes()));
1032:                WebXml webXml = new WebXml(doc);
1033:                assertTrue(!webXml.hasLoginConfig());
1034:            }
1035:
1036:            /**
1037:             * Tests whether checking a descriptor with a login configuration for a
1038:             * login configuration results in <code>true</code>.
1039:             * 
1040:             * @throws Exception If an unexpected error occurs
1041:             */
1042:            public void testHasLoginConfig() throws Exception {
1043:                String xml = "<web-app>" + "  <login-config>"
1044:                        + "    <auth-method>BASIC</auth-method>"
1045:                        + "  </login-config>" + "</web-app>";
1046:                Document doc = builder.parse(new ByteArrayInputStream(xml
1047:                        .getBytes()));
1048:                WebXml webXml = new WebXml(doc);
1049:                assertTrue(webXml.hasLoginConfig());
1050:            }
1051:
1052:            /**
1053:             * Tests retrieving the authentication method from a descriptor.
1054:             * 
1055:             * @throws Exception If an unexpected error occurs
1056:             */
1057:            public void testGetLoginConfigAuthMethod() throws Exception {
1058:                String xml = "<web-app>" + "  <login-config>"
1059:                        + "    <auth-method>BASIC</auth-method>"
1060:                        + "  </login-config>" + "</web-app>";
1061:                Document doc = builder.parse(new ByteArrayInputStream(xml
1062:                        .getBytes()));
1063:                WebXml webXml = new WebXml(doc);
1064:                assertEquals("BASIC", webXml.getLoginConfigAuthMethod());
1065:            }
1066:
1067:            /**
1068:             * Tests retrieving the authentication method from a descriptor.
1069:             * 
1070:             * @throws Exception If an unexpected error occurs
1071:             */
1072:            public void testSetLoginConfigAdding() throws Exception {
1073:                String xml = "<web-app></web-app>";
1074:                Document doc = builder.parse(new ByteArrayInputStream(xml
1075:                        .getBytes()));
1076:                WebXml webXml = new WebXml(doc);
1077:                webXml.setLoginConfig("BASIC", "Test Realm");
1078:                assertTrue(webXml.hasLoginConfig());
1079:                assertEquals("BASIC", webXml.getLoginConfigAuthMethod());
1080:            }
1081:
1082:            /**
1083:             * Tests retrieving the authentication method from a descriptor.
1084:             * 
1085:             * @throws Exception If an unexpected error occurs
1086:             */
1087:            public void testSetLoginConfigReplacing() throws Exception {
1088:                String xml = "<web-app>" + "  <login-config>"
1089:                        + "    <auth-method>DIGEST</auth-method>"
1090:                        + "  </login-config>" + "</web-app>";
1091:                Document doc = builder.parse(new ByteArrayInputStream(xml
1092:                        .getBytes()));
1093:                WebXml webXml = new WebXml(doc);
1094:                webXml.setLoginConfig("BASIC", "Test Realm");
1095:                assertTrue(webXml.hasLoginConfig());
1096:                assertEquals("BASIC", webXml.getLoginConfigAuthMethod());
1097:            }
1098:
1099:            /**
1100:             * Tests whether checking an empty descriptor for some security constraint
1101:             * results in <code>false</code>.
1102:             * 
1103:             * @throws Exception If an unexpected error occurs
1104:             */
1105:            public void testHasSecurityConstraintEmpty() throws Exception {
1106:                String xml = "<web-app></web-app>";
1107:                Document doc = builder.parse(new ByteArrayInputStream(xml
1108:                        .getBytes()));
1109:                WebXml webXml = new WebXml(doc);
1110:                assertTrue(!webXml.hasSecurityConstraint("/TestUrl"));
1111:            }
1112:
1113:            /**
1114:             * Tests whether a single security-constraint element in the descriptor is
1115:             * correctly retrieved.
1116:             * 
1117:             * @throws Exception If an unexpected error occurs
1118:             */
1119:            public void testGetSingleSecurityConstraint() throws Exception {
1120:                String xml = "<web-app>" + "  <security-constraint>"
1121:                        + "    <web-resource-collection>"
1122:                        + "      <web-resource-name>wr1</web-resource-name>"
1123:                        + "      <url-pattern>/url1</url-pattern>"
1124:                        + "    </web-resource-collection>"
1125:                        + "  </security-constraint>" + "</web-app>";
1126:                Document doc = builder.parse(new ByteArrayInputStream(xml
1127:                        .getBytes()));
1128:                WebXml webXml = new WebXml(doc);
1129:                assertTrue(webXml.hasSecurityConstraint("/url1"));
1130:                Element securityConstraintElement = webXml
1131:                        .getSecurityConstraint("/url1");
1132:                assertNotNull(securityConstraintElement);
1133:            }
1134:
1135:            /**
1136:             * Tests whether multiple security-constraint elements are returned in 
1137:             * the expected order.
1138:             * 
1139:             * @throws Exception If an unexpected error occurs
1140:             */
1141:            public void testGetMutlipleSecurityConstraints() throws Exception {
1142:                String xml = "<web-app>" + "  <security-constraint>"
1143:                        + "    <web-resource-collection>"
1144:                        + "      <web-resource-name>wr1</web-resource-name>"
1145:                        + "      <url-pattern>/url1</url-pattern>"
1146:                        + "    </web-resource-collection>"
1147:                        + "  </security-constraint>"
1148:                        + "  <security-constraint>"
1149:                        + "    <web-resource-collection>"
1150:                        + "      <web-resource-name>wr2</web-resource-name>"
1151:                        + "      <url-pattern>/url2</url-pattern>"
1152:                        + "    </web-resource-collection>"
1153:                        + "  </security-constraint>"
1154:                        + "  <security-constraint>"
1155:                        + "    <web-resource-collection>"
1156:                        + "      <web-resource-name>wr3</web-resource-name>"
1157:                        + "      <url-pattern>/url3</url-pattern>"
1158:                        + "    </web-resource-collection>"
1159:                        + "  </security-constraint>" + "</web-app>";
1160:                Document doc = builder.parse(new ByteArrayInputStream(xml
1161:                        .getBytes()));
1162:                WebXml webXml = new WebXml(doc);
1163:                assertTrue(webXml.hasSecurityConstraint("/url1"));
1164:                assertTrue(webXml.hasSecurityConstraint("/url2"));
1165:                assertTrue(webXml.hasSecurityConstraint("/url3"));
1166:                Iterator securityConstraints = webXml
1167:                        .getElements(WebXmlTag.SECURITY_CONSTRAINT);
1168:                assertNotNull(securityConstraints.next());
1169:                assertNotNull(securityConstraints.next());
1170:                assertNotNull(securityConstraints.next());
1171:                assertTrue(!securityConstraints.hasNext());
1172:            }
1173:
1174:            /**
1175:             * Tests whether retrieving the login-config from an empty descriptor 
1176:             * returns <code>null</code>.
1177:             * 
1178:             * @throws Exception If an unexpected error occurs
1179:             */
1180:            public void testGetLoginConfigEmpty() throws Exception {
1181:                String xml = "<web-app></web-app>";
1182:                Document doc = builder.parse(new ByteArrayInputStream(xml
1183:                        .getBytes()));
1184:                WebXml webXml = new WebXml(doc);
1185:                assertTrue(!webXml.getElements(WebXmlTag.LOGIN_CONFIG)
1186:                        .hasNext());
1187:            }
1188:
1189:            /**
1190:             * Tests whether the login-config element can be correctly retrieved.
1191:             * 
1192:             * @throws Exception If an unexpected error occurs
1193:             */
1194:            public void testGetLoginConfig() throws Exception {
1195:                String xml = "<web-app><login-config/></web-app>";
1196:                Document doc = builder.parse(new ByteArrayInputStream(xml
1197:                        .getBytes()));
1198:                WebXml webXml = new WebXml(doc);
1199:                assertTrue(webXml.getElements(WebXmlTag.LOGIN_CONFIG).hasNext());
1200:            }
1201:
1202:            /**
1203:             * Tests whether checking an empty descriptor for some security roles
1204:             * results in <code>false</code>.
1205:             * 
1206:             * @throws Exception If an unexpected error occurs
1207:             */
1208:            public void testHasSecurityRoleEmpty() throws Exception {
1209:                String xml = "<web-app></web-app>";
1210:                Document doc = builder.parse(new ByteArrayInputStream(xml
1211:                        .getBytes()));
1212:                WebXml webXml = new WebXml(doc);
1213:                assertTrue(!webXml.hasSecurityRole("someRole"));
1214:                assertTrue(!webXml.getSecurityRoleNames().hasNext());
1215:            }
1216:
1217:            /**
1218:             * Tests whether a single security-role element is correctly retrieved.
1219:             * 
1220:             * @throws Exception If an unexpected error occurs
1221:             */
1222:            public void testGetSingleSecurityRole() throws Exception {
1223:                String xml = "<web-app>" + "  <security-role>".trim()
1224:                        + "    <role-name>r1</role-name>".trim()
1225:                        + "  </security-role>".trim() + "</web-app>";
1226:                Document doc = builder.parse(new ByteArrayInputStream(xml
1227:                        .getBytes()));
1228:                WebXml webXml = new WebXml(doc);
1229:                assertTrue(webXml.hasSecurityRole("r1"));
1230:                Element securityRoleElement = webXml.getSecurityRole("r1");
1231:                assertNotNull(securityRoleElement);
1232:                assertEquals("security-role", securityRoleElement.getNodeName());
1233:                assertEquals("role-name", securityRoleElement.getFirstChild()
1234:                        .getNodeName());
1235:                assertEquals("r1", securityRoleElement.getFirstChild()
1236:                        .getFirstChild().getNodeValue());
1237:                Iterator securityRoleNames = webXml.getSecurityRoleNames();
1238:                assertTrue(securityRoleNames.hasNext());
1239:                assertEquals("r1", securityRoleNames.next());
1240:                assertTrue(!securityRoleNames.hasNext());
1241:            }
1242:
1243:            /**
1244:             * Tests whether multiple security-role elements are correctly retrieved
1245:             * in the expected order.
1246:             * 
1247:             * @throws Exception If an unexpected error occurs
1248:             */
1249:            public void testGetMutlipleSecurityRoles() throws Exception {
1250:                String xml = "<web-app>" + "  <security-role>".trim()
1251:                        + "    <role-name>r1</role-name>".trim()
1252:                        + "  </security-role>".trim()
1253:                        + "  <security-role>".trim()
1254:                        + "    <role-name>r2</role-name>".trim()
1255:                        + "  </security-role>".trim()
1256:                        + "  <security-role>".trim()
1257:                        + "    <role-name>r3</role-name>".trim()
1258:                        + "  </security-role>".trim() + "</web-app>";
1259:                Document doc = builder.parse(new ByteArrayInputStream(xml
1260:                        .getBytes()));
1261:                WebXml webXml = new WebXml(doc);
1262:                assertTrue(webXml.hasSecurityRole("r1"));
1263:                Element securityRoleElement1 = webXml.getSecurityRole("r1");
1264:                assertNotNull(securityRoleElement1);
1265:                assertEquals("security-role", securityRoleElement1
1266:                        .getNodeName());
1267:                assertEquals("role-name", securityRoleElement1.getFirstChild()
1268:                        .getNodeName());
1269:                assertEquals("r1", securityRoleElement1.getFirstChild()
1270:                        .getFirstChild().getNodeValue());
1271:                assertTrue(webXml.hasSecurityRole("r2"));
1272:                Element securityRoleElement2 = webXml.getSecurityRole("r2");
1273:                assertNotNull(securityRoleElement2);
1274:                assertEquals("security-role", securityRoleElement2
1275:                        .getNodeName());
1276:                assertEquals("role-name", securityRoleElement2.getFirstChild()
1277:                        .getNodeName());
1278:                assertEquals("r2", securityRoleElement2.getFirstChild()
1279:                        .getFirstChild().getNodeValue());
1280:                assertTrue(webXml.hasSecurityRole("r3"));
1281:                Element securityRoleElement3 = webXml.getSecurityRole("r3");
1282:                assertNotNull(securityRoleElement3);
1283:                assertEquals("security-role", securityRoleElement3
1284:                        .getNodeName());
1285:                assertEquals("role-name", securityRoleElement3.getFirstChild()
1286:                        .getNodeName());
1287:                assertEquals("r3", securityRoleElement3.getFirstChild()
1288:                        .getFirstChild().getNodeValue());
1289:                Iterator securityRoleNames = webXml.getSecurityRoleNames();
1290:                assertTrue(securityRoleNames.hasNext());
1291:                assertEquals("r1", securityRoleNames.next());
1292:                assertTrue(securityRoleNames.hasNext());
1293:                assertEquals("r2", securityRoleNames.next());
1294:                assertTrue(securityRoleNames.hasNext());
1295:                assertEquals("r3", securityRoleNames.next());
1296:                assertTrue(!securityRoleNames.hasNext());
1297:            }
1298:
1299:            /**
1300:             * Tests whether a filter is inserted before a servlet element.
1301:             * 
1302:             * @throws Exception If an unexpected error occurs
1303:             */
1304:            public void testElementOrderFilterBeforeServlet() throws Exception {
1305:                String xml = "<web-app>" + "  <servlet>".trim()
1306:                        + "    <servlet-name>s1</servlet-name>".trim()
1307:                        + "    <servlet-class>s1class</servlet-class>".trim()
1308:                        + "  </servlet>".trim() + "</web-app>";
1309:                Document doc = builder.parse(new ByteArrayInputStream(xml
1310:                        .getBytes()));
1311:                WebXml webXml = new WebXml(doc);
1312:                webXml.addFilter(createFilterElement(doc, "f1", "f1class"));
1313:                NodeList order = doc.getDocumentElement().getChildNodes();
1314:                assertEquals("filter", order.item(0).getNodeName());
1315:                assertEquals("servlet", order.item(1).getNodeName());
1316:            }
1317:
1318:            /**
1319:             * Tests whether a filter is inserted before the comment node preceding a 
1320:             * servlet definition.
1321:             * 
1322:             * @throws Exception If an unexpected error occurs
1323:             */
1324:            public void testElementOrderFilterBeforeServletWithComment()
1325:                    throws Exception {
1326:                String xml = "<web-app>" + "  <!-- My servlets -->".trim()
1327:                        + "  <servlet>".trim()
1328:                        + "    <servlet-name>s1</servlet-name>".trim()
1329:                        + "    <servlet-class>s1class</servlet-class>".trim()
1330:                        + "  </servlet>".trim() + "</web-app>";
1331:                Document doc = builder.parse(new ByteArrayInputStream(xml
1332:                        .getBytes()));
1333:                WebXml webXml = new WebXml(doc);
1334:                webXml.addFilter(createFilterElement(doc, "f1", "f1class"));
1335:                NodeList order = doc.getDocumentElement().getChildNodes();
1336:                assertEquals("filter", order.item(0).getNodeName());
1337:                assertEquals("#comment", order.item(1).getNodeName());
1338:                assertEquals("servlet", order.item(2).getNodeName());
1339:            }
1340:
1341:            /**
1342:             * Tests whether a servlet is inserted after a filter.
1343:             * 
1344:             * @throws Exception If an unexpected error occurs
1345:             */
1346:            public void testElementOrderServletAfterFilter() throws Exception {
1347:                String xml = "<web-app>" + "  <filter>".trim()
1348:                        + "    <filter-name>f1</filter-name>".trim()
1349:                        + "    <filter-class>f1class</filter-class>".trim()
1350:                        + "  </filter>".trim() + "</web-app>";
1351:                Document doc = builder.parse(new ByteArrayInputStream(xml
1352:                        .getBytes()));
1353:                WebXml webXml = new WebXml(doc);
1354:                webXml.addServlet(createServletElement(doc, "s1", "s1class"));
1355:                NodeList order = doc.getDocumentElement().getChildNodes();
1356:                assertEquals("filter", order.item(0).getNodeName());
1357:                assertEquals("servlet", order.item(1).getNodeName());
1358:            }
1359:
1360:            /**
1361:             * Tests whether a servlet is inserted after a filter that is preceded by
1362:             * a comment node.
1363:             * 
1364:             * @throws Exception If an unexpected error occurs
1365:             */
1366:            public void testElementOrderServletAfterFilterWithComment()
1367:                    throws Exception {
1368:                String xml = "<web-app>" + "  <!-- My filters -->".trim()
1369:                        + "  <filter>".trim()
1370:                        + "    <filter-name>f1</filter-name>".trim()
1371:                        + "    <filter-class>f1class</filter-class>".trim()
1372:                        + "  </filter>".trim() + "</web-app>";
1373:                Document doc = builder.parse(new ByteArrayInputStream(xml
1374:                        .getBytes()));
1375:                WebXml webXml = new WebXml(doc);
1376:                webXml.addServlet(createServletElement(doc, "s1", "s1class"));
1377:                NodeList order = doc.getDocumentElement().getChildNodes();
1378:                assertEquals("#comment", order.item(0).getNodeName());
1379:                assertEquals("filter", order.item(1).getNodeName());
1380:                assertEquals("servlet", order.item(2).getNodeName());
1381:            }
1382:
1383:            /**
1384:             * Tests that the a servlets run-as role-name can be extracted
1385:             * 
1386:             * @throws Exception If an unexpected error occurs
1387:             */
1388:            public void testGetServletRunAsRole() throws Exception {
1389:                String xml = "<web-app>" + "  <servlet>"
1390:                        + "    <servlet-name>s1</servlet-name>"
1391:                        + "    <servlet-class>sclass1</servlet-class>"
1392:                        + "    <run-as>" + "      <role-name>r1</role-name>"
1393:                        + "    </run-as>" + "  </servlet>" + "</web-app>";
1394:                Document doc = builder.parse(new ByteArrayInputStream(xml
1395:                        .getBytes()));
1396:                WebXml webXml = new WebXml(doc);
1397:                String roleName = webXml.getServletRunAsRoleName("s1");
1398:                assertEquals("r1", roleName);
1399:            }
1400:
1401:            /**
1402:             * Tests that a run-as role-name can be added to a servlet
1403:             * 
1404:             * @throws Exception If an unexpected error occurs
1405:             */
1406:            public void testAddServletRunAsRole() throws Exception {
1407:                String xml = "<web-app>" + "  <servlet>"
1408:                        + "    <servlet-name>s1</servlet-name>"
1409:                        + "    <servlet-class>sclass1</servlet-class>"
1410:                        + "  </servlet>" + "</web-app>";
1411:                Document doc = builder.parse(new ByteArrayInputStream(xml
1412:                        .getBytes()));
1413:                WebXml webXml = new WebXml(doc);
1414:                webXml.addServletRunAsRoleName("s1", "r1");
1415:                String roleName = webXml.getServletRunAsRoleName("s1");
1416:                assertEquals("r1", roleName);
1417:            }
1418:
1419:            // Private Methods ---------------------------------------------------------
1420:
1421:            /**
1422:             * Create a <code>context-param</code> element containing the specified 
1423:             * text in the child elements.
1424:             * 
1425:             * @param theDocument The DOM document
1426:             * @param theParamName The parameter name
1427:             * @param theParamValue The parameter value
1428:             * @return The created element
1429:             */
1430:            public Element createContextParamElement(Document theDocument,
1431:                    String theParamName, String theParamValue) {
1432:                Element contextParamElement = theDocument
1433:                        .createElement("context-param");
1434:                Element paramNameElement = theDocument
1435:                        .createElement("param-name");
1436:                paramNameElement.appendChild(theDocument
1437:                        .createTextNode(theParamName));
1438:                contextParamElement.appendChild(paramNameElement);
1439:                Element paramValueElement = theDocument
1440:                        .createElement("param-value");
1441:                paramValueElement.appendChild(theDocument
1442:                        .createTextNode(theParamValue));
1443:                contextParamElement.appendChild(paramValueElement);
1444:                return contextParamElement;
1445:            }
1446:
1447:            /**
1448:             * Create a <code>filter</code> element containing the specified text in 
1449:             * the child elements.
1450:             * 
1451:             * @param theDocument The DOM document
1452:             * @param theFilterName The name of the filter
1453:             * @param theFilterClass The name of the filter implementation class
1454:             * @return The created element
1455:             */
1456:            public Element createFilterElement(Document theDocument,
1457:                    String theFilterName, String theFilterClass) {
1458:                Element filterElement = theDocument.createElement("filter");
1459:                Element filterNameElement = theDocument
1460:                        .createElement("filter-name");
1461:                filterNameElement.appendChild(theDocument
1462:                        .createTextNode(theFilterName));
1463:                filterElement.appendChild(filterNameElement);
1464:                Element filterClassElement = theDocument
1465:                        .createElement("filter-class");
1466:                filterClassElement.appendChild(theDocument
1467:                        .createTextNode(theFilterClass));
1468:                filterElement.appendChild(filterClassElement);
1469:                return filterElement;
1470:            }
1471:
1472:            /**
1473:             * Create a <code>servlet</code> element containing the specified text in 
1474:             * the child elements.
1475:             * 
1476:             * @param theDocument The DOM document
1477:             * @param theServletName The name of the servlet
1478:             * @param theServletClass The name of the servlet implementation class
1479:             * @return The created element
1480:             */
1481:            public Element createServletElement(Document theDocument,
1482:                    String theServletName, String theServletClass) {
1483:                Element filterElement = theDocument.createElement("servlet");
1484:                Element filterNameElement = theDocument
1485:                        .createElement("servlet-name");
1486:                filterNameElement.appendChild(theDocument
1487:                        .createTextNode(theServletName));
1488:                filterElement.appendChild(filterNameElement);
1489:                Element filterClassElement = theDocument
1490:                        .createElement("servlet-class");
1491:                filterClassElement.appendChild(theDocument
1492:                        .createTextNode(theServletClass));
1493:                filterElement.appendChild(filterClassElement);
1494:                return filterElement;
1495:            }
1496:
1497:        }
w___w___w___._j___av___a2___s__.___c__o___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.