1 20 package org.apache.cactus.integration.ant.deployment.application; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.StringReader ; 24 import java.util.Iterator ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 import junit.framework.TestCase; 31 32 import org.w3c.dom.Document ; 33 import org.xml.sax.EntityResolver ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 37 42 public final class TestApplicationXml extends TestCase 43 { 44 47 private DocumentBuilderFactory factory; 48 49 52 private DocumentBuilder builder; 53 54 57 public void setUp() throws ParserConfigurationException 58 { 59 factory = DocumentBuilderFactory.newInstance(); 60 factory.setValidating(false); 61 factory.setNamespaceAware(false); 62 63 builder = factory.newDocumentBuilder(); 64 builder.setEntityResolver(new EntityResolver () 65 { 66 public InputSource resolveEntity(String thePublicId, 67 String theSystemId) throws SAXException 68 { 69 return new InputSource (new StringReader ("")); 70 } 71 }); 72 } 73 74 81 public void testConstructionWithNullDocument() throws Exception 82 { 83 try 84 { 85 new DefaultApplicationXml(null); 86 fail("Expected NullPointerException"); 87 } 88 catch (NullPointerException npe) 89 { 90 } 92 93 } 94 95 101 public void testGetWebModuleUrisWithEmptyDocument() throws Exception 102 { 103 String xml = "<application>" 104 + " <module>" 105 + " <java>javaclient.jar</java>" 106 + " </module>" 107 + "</application>"; 108 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 109 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 110 Iterator webUris = applicationXml.getWebModuleUris(); 111 assertTrue("No web modules defined", !webUris.hasNext()); 112 } 113 114 121 public void testGetWebModuleUrisWithSingleWebModule() throws Exception 122 { 123 String xml = "<application>" 124 + " <module>" 125 + " <web>" 126 + " <web-uri>webmodule.jar</web-uri>" 127 + " <context-root>/webmodule</context-root>" 128 + " </web>" 129 + " </module>" 130 + "</application>"; 131 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 132 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 133 Iterator webUris = applicationXml.getWebModuleUris(); 134 assertEquals("webmodule.jar", webUris.next()); 135 assertTrue(!webUris.hasNext()); 136 } 137 138 145 public void testGetWebModuleUrisWithMultipleWebModules() throws Exception 146 { 147 String xml = "<application>" 148 + " <module>" 149 + " <web>" 150 + " <web-uri>webmodule1.jar</web-uri>" 151 + " <context-root>/webmodule1</context-root>" 152 + " </web>" 153 + " </module>" 154 + " <module>" 155 + " <web>" 156 + " <web-uri>webmodule2.jar</web-uri>" 157 + " <context-root>/webmodule2</context-root>" 158 + " </web>" 159 + " </module>" 160 + " <module>" 161 + " <web>" 162 + " <web-uri>webmodule3.jar</web-uri>" 163 + " <context-root>/webmodule3</context-root>" 164 + " </web>" 165 + " </module>" 166 + "</application>"; 167 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 168 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 169 Iterator webUris = applicationXml.getWebModuleUris(); 170 assertEquals("webmodule1.jar", webUris.next()); 171 assertEquals("webmodule2.jar", webUris.next()); 172 assertEquals("webmodule3.jar", webUris.next()); 173 assertTrue(!webUris.hasNext()); 174 } 175 176 183 public void testGetWebModuleContextRootUndefined() throws Exception 184 { 185 String xml = "<application>" 186 + " <module>" 187 + " <java>javaclient.jar</java>" 188 + " </module>" 189 + "</application>"; 190 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 191 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 192 try 193 { 194 applicationXml.getWebModuleContextRoot("webmodule.jar"); 195 fail("IllegalArgumentException expected"); 196 } 197 catch (IllegalArgumentException expected) 198 { 199 } 201 } 202 203 209 public void testGetWebModuleContextRootSingleWebModule() throws Exception 210 { 211 String xml = "<application>" 212 + " <module>" 213 + " <web>" 214 + " <web-uri>webmodule.jar</web-uri>" 215 + " <context-root>/webmodule</context-root>" 216 + " </web>" 217 + " </module>" 218 + "</application>"; 219 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 220 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 221 assertEquals("/webmodule", 222 applicationXml.getWebModuleContextRoot("webmodule.jar")); 223 } 224 225 231 public void testGetWebModuleContextRootMultipleWebModules() throws Exception 232 { 233 String xml = "<application>" 234 + " <module>" 235 + " <web>" 236 + " <web-uri>webmodule1.jar</web-uri>" 237 + " <context-root>/webmodule1</context-root>" 238 + " </web>" 239 + " </module>" 240 + " <module>" 241 + " <web>" 242 + " <web-uri>webmodule2.jar</web-uri>" 243 + " <context-root>/webmodule2</context-root>" 244 + " </web>" 245 + " </module>" 246 + " <module>" 247 + " <web>" 248 + " <web-uri>webmodule3.jar</web-uri>" 249 + " <context-root>/webmodule3</context-root>" 250 + " </web>" 251 + " </module>" 252 + "</application>"; 253 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 254 ApplicationXml applicationXml = new DefaultApplicationXml(doc); 255 assertEquals("/webmodule1", 256 applicationXml.getWebModuleContextRoot("webmodule1.jar")); 257 assertEquals("/webmodule2", 258 applicationXml.getWebModuleContextRoot("webmodule2.jar")); 259 assertEquals("/webmodule3", 260 applicationXml.getWebModuleContextRoot("webmodule3.jar")); 261 } 262 263 } 264 | Popular Tags |