1 7 package com.inversoft.verge.util.url.config.test; 8 9 10 import org.jdom.Document; 11 import org.jdom.Element; 12 13 import com.inversoft.config.ConfigurationException; 14 import com.inversoft.junit.WebTestCase; 15 import com.inversoft.verge.util.url.config.CategoryConfig; 16 import com.inversoft.verge.util.url.config.URLConfigBuilder; 17 import com.inversoft.verge.util.url.config.URLConfigRegistry; 18 19 20 29 public class URLConfigBuilderTest extends WebTestCase { 30 31 36 public URLConfigBuilderTest(String name) { 37 super(name); 38 setLocal(true); 39 } 40 41 42 public void testSuccess() { 43 URLConfigBuilder builder = new URLConfigBuilder(); 44 Document doc = createGoodDocument(); 45 46 try { 47 builder.build(doc, URLConfigRegistry.getInstance(request)); 48 } catch (ConfigurationException ce) { 49 fail(ce.toString()); 50 } 51 52 CategoryConfig cc = URLConfigRegistry.getInstance(request).lookupCategory("cat1"); 53 assertEquals("Should have protocol https", cc.getProtocol(), "https"); 54 assertTrue("Should have protocol https", cc.isHttps()); 55 assertEquals("Should have host www.inversoft.com", cc.getHost(), "www.inversoft.com"); 56 assertEquals("Should have port 8000", cc.getPort(), 8000); 57 assertEquals("Should have context MyWebApp", cc.getContext(), "MyWebApp"); 58 59 cc = URLConfigRegistry.getInstance(request).lookupCategory("cat2"); 60 assertNull("Should have no protocol", cc.getProtocol()); 61 assertEquals("Should have host www.inversoft.com", cc.getHost(), "www.inversoft.com"); 62 assertEquals("Should have port 8000", cc.getPort(), 8000); 63 assertEquals("Should have context MyWebApp", cc.getContext(), "MyWebApp"); 64 65 cc = URLConfigRegistry.getInstance(request).lookupCategory("cat3"); 66 assertEquals("Should have protocol https", cc.getProtocol(), "https"); 67 assertTrue("Should have protocol https", cc.isHttps()); 68 assertNull("Should have no host", cc.getHost()); 69 assertEquals("Should have port 8000", cc.getPort(), 8000); 70 assertEquals("Should have context MyWebApp", cc.getContext(), "MyWebApp"); 71 72 cc = URLConfigRegistry.getInstance(request).lookupCategory("cat4"); 73 assertEquals("Should have protocol https", cc.getProtocol(), "https"); 74 assertTrue("Should have protocol https", cc.isHttps()); 75 assertEquals("Should have host www.inversoft.com", cc.getHost(), "www.inversoft.com"); 76 assertEquals("Should have port -1", cc.getPort(), -1); 77 assertEquals("Should have context MyWebApp", cc.getContext(), "MyWebApp"); 78 79 cc = URLConfigRegistry.getInstance(request).lookupCategory("cat5"); 80 assertEquals("Should have protocol http", cc.getProtocol(), "http"); 81 assertFalse("Should have protocol http", cc.isHttps()); 82 assertEquals("Should have host www.inversoft.com", cc.getHost(), "www.inversoft.com"); 83 assertEquals("Should have port 8000", cc.getPort(), 8000); 84 assertNull("Should have no context", cc.getContext()); 85 } 86 87 public void testFailure() { 88 URLConfigBuilder builder = new URLConfigBuilder(); 89 Document doc = createBadDocument(); 90 91 try { 92 builder.build(doc, URLConfigRegistry.getInstance(request)); 93 fail("Should have failed"); 94 } catch (ConfigurationException ce) { 95 System.out.println(ce.toString()); 96 } 97 } 98 99 public Document createGoodDocument() { 100 Element root = new Element("url"); 101 Document doc = new Document(root); 102 103 Element cat = new Element("category"); 104 cat.setAttribute("name", "cat1"); 105 Element protocol = new Element("protocol"); 106 protocol.addContent("https"); 107 Element host = new Element("host"); 108 host.addContent("www.inversoft.com"); 109 Element port = new Element("port"); 110 port.addContent("8000"); 111 Element context = new Element("context"); 112 context.addContent("MyWebApp"); 113 cat.addContent(protocol); 114 cat.addContent(host); 115 cat.addContent(port); 116 cat.addContent(context); 117 root.addContent(cat); 118 119 cat = new Element("category"); 120 cat.setAttribute("name", "cat2"); 121 host = new Element("host"); 124 host.addContent("www.inversoft.com"); 125 port = new Element("port"); 126 port.addContent("8000"); 127 context = new Element("context"); 128 context.addContent("MyWebApp"); 129 cat.addContent(host); 131 cat.addContent(port); 132 cat.addContent(context); 133 root.addContent(cat); 134 135 cat = new Element("category"); 136 cat.setAttribute("name", "cat3"); 137 protocol = new Element("protocol"); 138 protocol.addContent("https"); 139 port = new Element("port"); 142 port.addContent("8000"); 143 context = new Element("context"); 144 context.addContent("MyWebApp"); 145 cat.addContent(protocol); 146 cat.addContent(port); 148 cat.addContent(context); 149 root.addContent(cat); 150 151 cat = new Element("category"); 152 cat.setAttribute("name", "cat4"); 153 protocol = new Element("protocol"); 154 protocol.addContent("https"); 155 host = new Element("host"); 156 host.addContent("www.inversoft.com"); 157 context = new Element("context"); 160 context.addContent("MyWebApp"); 161 cat.addContent(protocol); 162 cat.addContent(host); 163 cat.addContent(context); 165 root.addContent(cat); 166 167 cat = new Element("category"); 168 cat.setAttribute("name", "cat5"); 169 protocol = new Element("protocol"); 170 protocol.addContent("http"); 171 host = new Element("host"); 172 host.addContent("www.inversoft.com"); 173 port = new Element("port"); 174 port.addContent("8000"); 175 cat.addContent(protocol); 178 cat.addContent(host); 179 cat.addContent(port); 180 root.addContent(cat); 182 183 return doc; 184 } 185 186 public Document createBadDocument() { 187 Element root = new Element("url"); 188 Document doc = new Document(root); 189 190 Element cat = new Element("category"); 191 cat.setAttribute("name", "badCat1"); 192 Element protocol = new Element("protocol"); 193 protocol.addContent("bad"); 194 Element host = new Element("host"); 195 host.addContent("www.inversoft.com"); 196 Element port = new Element("port"); 197 port.addContent("8000"); 198 Element context = new Element("context"); 199 context.addContent("MyWebApp"); 200 cat.addContent(protocol); 201 cat.addContent(host); 202 cat.addContent(port); 203 cat.addContent(context); 204 root.addContent(cat); 205 206 cat = new Element("category"); 207 cat.setAttribute("name", "badCat2"); 208 protocol = new Element("protocol"); 209 protocol.addContent("https"); 210 host = new Element("host"); 211 host.addContent("www.inversoft.com"); 212 port = new Element("port"); 213 port.addContent("-1"); 214 context = new Element("context"); 215 context.addContent("MyWebApp"); 216 cat.addContent(protocol); 217 cat.addContent(host); 218 cat.addContent(port); 219 cat.addContent(context); 220 root.addContent(cat); 221 222 cat = new Element("category"); 223 cat.setAttribute("name", "badCat3"); 224 protocol = new Element("protocol"); 225 protocol.addContent("https"); 226 host = new Element("host"); 227 host.addContent("www.inversoft.com"); 228 port = new Element("port"); 229 port.addContent("a"); 230 context = new Element("context"); 231 context.addContent("MyWebApp"); 232 cat.addContent(protocol); 233 cat.addContent(host); 234 cat.addContent(port); 235 cat.addContent(context); 236 root.addContent(cat); 237 238 cat = new Element("category"); 239 protocol = new Element("protocol"); 241 protocol.addContent("https"); 242 host = new Element("host"); 243 host.addContent("www.inversoft.com"); 244 port = new Element("port"); 245 port.addContent("8000"); 246 context = new Element("context"); 247 context.addContent("MyWebApp"); 248 cat.addContent(protocol); 249 cat.addContent(host); 250 cat.addContent(port); 251 cat.addContent(context); 252 root.addContent(cat); 253 254 cat = new Element("category"); 255 cat.setAttribute("name", "badCat1"); 256 protocol = new Element("protocol"); 257 protocol.addContent("https"); 258 host = new Element("host"); 259 host.addContent("www.inversoft.com"); 260 port = new Element("port"); 261 port.addContent("8000"); 262 context = new Element("context"); 263 context.addContent("MyWebApp"); 264 cat.addContent(protocol); 265 cat.addContent(host); 266 cat.addContent(port); 267 cat.addContent(context); 268 root.addContent(cat); 269 270 cat = new Element("category"); 271 cat.setAttribute("name", "badCat1"); 272 protocol = new Element("protocol"); 273 protocol.addContent("https"); 274 host = new Element("host"); 275 host.addContent("www.inversoft.com"); 276 port = new Element("port"); 277 port.addContent("8000"); 278 context = new Element("context"); 279 context.addContent("MyWebApp"); 280 cat.addContent(protocol); 281 cat.addContent(host); 282 cat.addContent(port); 283 cat.addContent(context); 284 root.addContent(cat); 285 286 return doc; 287 } 288 } | Popular Tags |