1 7 package com.inversoft.verge.mvc.controller.actionflow.config.test; 8 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 import org.jdom.Document; 15 import org.jdom.Element; 16 17 import com.inversoft.config.ConfigurationException; 18 import com.inversoft.junit.WebTestCase; 19 import com.inversoft.util.ReflectionException; 20 import com.inversoft.verge.config.VergeConfigConstants; 21 import com.inversoft.verge.mvc.config.BaseFormConfig; 22 import com.inversoft.verge.mvc.config.BaseValidatorConfig; 23 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigBuilder; 24 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigFactory; 25 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigRegistry; 26 import com.inversoft.verge.mvc.controller.actionflow.config.ActionHandlerNode; 27 import com.inversoft.verge.mvc.controller.actionflow.config.ActionLink; 28 import com.inversoft.verge.mvc.controller.actionflow.config.Constants; 29 import com.inversoft.verge.mvc.controller.actionflow.config.Link; 30 import com.inversoft.verge.mvc.controller.actionflow.config.Namespace; 31 import com.inversoft.verge.mvc.controller.actionflow.config.Node; 32 import com.inversoft.verge.mvc.controller.actionflow.config.PresentationNode; 33 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 34 35 36 49 public class ActionFlowConfigBuilderTest extends WebTestCase { 50 51 54 public static final String TEST_FILE = 55 "src/com/inversoft/verge/controller/actionflow/config/test/good-config.xml"; 56 57 58 63 public ActionFlowConfigBuilderTest(String name) { 64 super(name); 65 setLocal(true); 66 } 67 68 69 73 public void testConfigBuilderSimple() { 74 75 Document document = makeGoodDocument(); 76 77 try { 78 ActionFlowConfigBuilder builder = new ActionFlowConfigBuilder(); 79 ActionFlowConfigFactory factory = new ActionFlowConfigFactory(); 80 ActionFlowConfigRegistry registry = 81 (ActionFlowConfigRegistry) factory.createRegistry(); 82 builder.build(document, registry); 83 Map registries = new HashMap (); 84 registries.put(VergeConfigConstants.REPOSITORY_KEY, 85 RepositoryConfigRegistry.getInstance(request)); 86 builder.validate(registry, registries); 87 builder.commit(registry, registries); 88 } catch (ConfigurationException ce) { 89 fail(ce.toString()); 90 } 91 92 Namespace namespace = ActionFlowConfigRegistry.getInstance(request).lookup("namespace"); 93 assertTrue("Should have found namespace", namespace != null); 94 assertTrue("Should have two nodes", countNodes(namespace) == 2); 95 96 Node index = namespace.getNode("/index.jsp"); 97 assertTrue("Should have found the /index.jsp node", index != null); 98 assertTrue("Should be a presentation node", index instanceof PresentationNode); 99 assertTrue("Should be a presentation type", index.getType().equals("presentation")); 100 assertTrue("Should have a path of /", ((PresentationNode) index).getPath().equals("/")); 101 assertTrue("Should have a filename of index.jsp", 102 ((PresentationNode) index).getFileName().equals("index.jsp")); 103 104 Node handler = namespace.getNode("login"); 105 assertTrue("Should have found the login node", handler != null); 106 assertTrue("Should be an action handler node", handler instanceof ActionHandlerNode); 107 assertTrue("Should be a actionHandler type", handler.getType().equals("actionHandler")); 108 109 Link login = index.findLink("login"); 110 assertTrue("index.jsp should have a link called login", login != null); 111 assertTrue("link should be an ActionLink", login instanceof ActionLink); 112 assertTrue("login link should point to login handler", login.getDestination() == handler); 113 114 Link success = handler.findLink("success"); 115 assertTrue("login should have a link called success", success != null); 116 assertTrue("link should be an ActionLink", success instanceof ActionLink); 117 assertTrue("success link should point to index.jsp", success.getDestination() == index); 118 119 BaseFormConfig form = namespace.lookupForm("loginForm"); 120 assertNotNull("Should have found the form", form); 121 assertEquals("Should have 3 validators", 3, form.getValidators().size()); 122 123 try { 124 assertEquals("Validator 1 should be LoginValidator", LoginValidator.class, 125 ((BaseValidatorConfig) form.getValidators().get(0)).newValidator().getClass()); 126 assertEquals("Validator 3 should be LoginValidator2", LoginValidator2.class, 127 ((BaseValidatorConfig) form.getValidators().get(2)).newValidator().getClass()); 128 } catch (ReflectionException re) { 129 fail(re.toString()); 130 } 131 132 assertEquals("Validator 3 should have failureDef", "failure", 133 ((BaseValidatorConfig) form.getValidators().get(2)).getFailureDefinition()); 134 } 135 136 139 public static int countNodes(Namespace namespace) { 140 Iterator iter = namespace.nodeIterator(); 141 int count = 0; 142 while (iter.hasNext()) { 143 iter.next(); 144 count++; 145 } 146 147 return count; 148 } 149 150 153 protected Document makeGoodDocument() { 154 155 Element root = new Element("namespace"); 156 Document doc = new Document(root); 157 root.setAttribute(Constants.NAME_ATTRIBUTE, "namespace"); 158 159 Element index = new Element("node"); 160 index.setAttribute(Constants.NAME_ATTRIBUTE, "/index.jsp"); 161 index.setAttribute(Constants.TYPE_ATTRIBUTE, "presentation"); 162 index.setAttribute(Constants.EXIT_ATTRIBUTE, "true"); 163 164 Element indexLink = new Element("actionLink"); 165 indexLink.setAttribute(Constants.ACTION_ATTRIBUTE, "login"); 166 indexLink.setAttribute(Constants.NODE_ATTRIBUTE, "login"); 167 index.addContent(indexLink); 168 Element indexLink2 = new Element("actionLink"); 169 indexLink2.setAttribute(Constants.ACTION_ATTRIBUTE, "failure"); 170 indexLink2.setAttribute(Constants.NODE_ATTRIBUTE, "/index.jsp"); 171 index.addContent(indexLink2); 172 173 Element handler = new Element("node"); 174 handler.setAttribute(Constants.NAME_ATTRIBUTE, "login"); 175 handler.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 176 handler.setAttribute(Constants.CLASS_ATTRIBUTE, 177 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 178 179 Element handlerLink = new Element("actionLink"); 180 handlerLink.setAttribute(Constants.ACTION_ATTRIBUTE, "success"); 181 handlerLink.setAttribute(Constants.NODE_ATTRIBUTE, "/index.jsp"); 182 handler.addContent(handlerLink); 183 184 root.addContent(index); 185 root.addContent(handler); 186 187 Element form = new Element("form"); 188 form.setAttribute(Constants.NAME_ATTRIBUTE, "loginForm"); 189 190 Element fb = new Element("formBean"); 191 fb.setAttribute("name", "loginBean"); 192 fb.setAttribute("class", "com.inversoft.verge.mvc.controller.actionflow.config.test.LoginFormBean"); 193 fb.setAttribute("scope", "session"); 194 form.addContent(fb); 195 196 Element fb2 = new Element("formBean"); 197 fb2.setAttribute("name", "loginBean2"); 198 fb2.setAttribute("class", "com.inversoft.verge.mvc.controller.actionflow.config.test.LoginFormBean"); 199 fb2.setAttribute("scope", "request"); 200 form.addContent(fb2); 201 202 Element v1 = new Element("validator"); 203 v1.setAttribute("class", "com.inversoft.verge.mvc.controller.actionflow.config.test.LoginValidator"); 204 form.addContent(v1); 205 206 Element v2 = new Element("validator"); 207 Element prop = new Element("property"); 208 prop.setAttribute("name", "loginBean.number"); 209 prop.setAttribute("type", "number"); 210 prop.setAttribute("min", "0"); 211 prop.setAttribute("max", "10"); 212 prop.setAttribute("errorMsg", "Bad number"); 213 v2.addContent(prop); 214 form.addContent(v2); 215 216 Element v3 = new Element("validator"); 217 v3.setAttribute("class", "com.inversoft.verge.mvc.controller.actionflow.config.test.LoginValidator2"); 218 v3.setAttribute("failureLink", "failure"); 219 form.addContent(v3); 220 221 root.addContent(form); 222 223 return doc; 224 } 225 } 226 | Popular Tags |