1 7 package com.inversoft.verge.mvc.controller.form.config.test; 8 9 10 import org.jdom.Element; 11 12 import com.inversoft.config.ConfigurationException; 13 import com.inversoft.junit.WebTestCase; 14 import com.inversoft.verge.mvc.controller.form.config.Constants; 15 import com.inversoft.verge.mvc.controller.form.config.MappingConfig; 16 import com.inversoft.verge.mvc.controller.form.config.MappingConfigBuilder; 17 import com.inversoft.verge.util.url.test.URLGeneratorTest; 18 19 20 27 public class MappingBuilderTest extends WebTestCase { 28 29 32 public MappingBuilderTest(String name) { 33 super(name); 34 setLocal(true); 35 } 36 37 38 41 public void testBuild() { 42 Element element = new Element("mapping"); 43 element.setAttribute(Constants.NAME_ATTRIBUTE, "test"); 44 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping.jsp"); 45 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "true"); 46 47 MappingConfigBuilder builder = new MappingConfigBuilder(); 48 try { 49 MappingConfig config = (MappingConfig) builder.build(element); 50 assertTrue("Name should be test", config.getName().equals("test")); 51 assertTrue("url should be testMapping.jsp", 52 config.getURL().equals("testMapping.jsp")); 53 assertTrue("forward should be true", config.isForward()); 54 assertEquals("testMapping.jsp", config.getGeneratedURL(null)); 55 } catch (Exception e) { 56 fail(e.toString()); 57 } 58 59 element = new Element("mapping"); 60 element.setAttribute(Constants.NAME_ATTRIBUTE, "test2"); 61 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping2.jsp"); 62 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "false"); 63 64 try { 65 MappingConfig config = (MappingConfig) builder.build(element); 66 assertTrue("Name should be test", config.getName().equals("test2")); 67 assertTrue("url should be testMapping2.jsp", 68 config.getURL().equals("testMapping2.jsp")); 69 assertTrue("forward should be false", !config.isForward()); 70 } catch (Exception e) { 71 fail(e.toString()); 72 } 73 74 element = new Element("mapping"); 75 element.setAttribute(Constants.NAME_ATTRIBUTE, "test3"); 76 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping3.jsp"); 77 78 try { 79 MappingConfig config = (MappingConfig) builder.build(element); 80 assertTrue("forward should be true", config.isForward()); 81 } catch (Exception e) { 82 fail(e.toString()); 83 } 84 } 85 86 public void testCategories() { 87 Element element = new Element("mapping"); 88 element.setAttribute(Constants.NAME_ATTRIBUTE, "test4"); 89 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping3.jsp"); 90 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "false"); 91 element.setAttribute(Constants.CATEGORY_ATTRIBUTE, "cat"); 92 93 MappingConfigBuilder builder = new MappingConfigBuilder(); 94 try { 95 MappingConfig config = (MappingConfig) builder.build(element); 96 assertFalse("forward should be false", config.isForward()); 97 assertEquals("cat", config.getCategory()); 98 } catch (Exception e) { 99 fail(e.toString()); 100 } 101 102 new URLGeneratorTest(""); 104 105 element = new Element("mapping"); 106 element.setAttribute(Constants.NAME_ATTRIBUTE, "test4"); 107 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping3.jsp"); 108 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "false"); 109 element.setAttribute(Constants.CATEGORY_ATTRIBUTE, "testCat1"); 110 111 try { 112 MappingConfig config = (MappingConfig) builder.build(element); 113 assertFalse("forward should be false", config.isForward()); 114 assertEquals("testCat1", config.getCategory()); 115 116 String expected = "https://www.inversoft.com:8000/MyWebApp/testMapping3.jsp"; 117 assertEquals(expected, config.getGeneratedURL(request)); 118 } catch (Exception e) { 119 fail(e.toString()); 120 } 121 } 122 123 126 public void testFailure() { 127 Element element = new Element("mapping"); 128 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping.jsp"); 130 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "true"); 131 132 MappingConfigBuilder builder = new MappingConfigBuilder(); 133 try { 134 builder.build(element); 135 fail("Should have failed because no name"); 136 } catch (Exception e) { 137 assertTrue("Should be a ConfigurationException", 138 e instanceof ConfigurationException); 139 assertTrue("Should have errorlist", 140 ((ConfigurationException) e).getErrors() != null && 141 !((ConfigurationException) e).getErrors().isEmpty()); 142 } 143 144 element = new Element("mapping"); 145 element.setAttribute(Constants.NAME_ATTRIBUTE, "test"); 146 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "true"); 148 149 try { 150 builder.build(element); 151 fail("Should have failed because no url"); 152 } catch (Exception e) { 153 assertTrue("Should be a ConfigurationException", 154 e instanceof ConfigurationException); 155 assertTrue("Should have errorlist", 156 ((ConfigurationException) e).getErrors() != null && 157 !((ConfigurationException) e).getErrors().isEmpty()); 158 } 159 160 element = new Element("mapping"); 161 element.setAttribute(Constants.NAME_ATTRIBUTE, "test"); 162 element.setAttribute(Constants.URL_ATTRIBUTE, "testMapping.jsp"); 163 element.setAttribute(Constants.FORWARD_ATTRIBUTE, "notABoolean"); 164 165 try { 166 builder.build(element); 167 fail("Should have failed because invalid forward"); 168 } catch (Exception e) { 169 assertTrue("Should be a ConfigurationException", 170 e instanceof ConfigurationException); 171 assertTrue("Should have errorlist", 172 ((ConfigurationException) e).getErrors() != null && 173 !((ConfigurationException) e).getErrors().isEmpty()); 174 } 175 } 176 } 177 178 | Popular Tags |