1 7 package com.inversoft.verge.mvc.controller.actionflow.config.test; 8 9 10 import java.util.Map ; 11 12 import org.jdom.Element; 13 14 import com.inversoft.config.ConfigurationException; 15 import com.inversoft.junit.WebTestCase; 16 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowAction; 17 import com.inversoft.verge.mvc.controller.actionflow.NodeExecutor; 18 import com.inversoft.verge.mvc.controller.actionflow.NodeExecutorRegistry; 19 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigRegistry; 20 import com.inversoft.verge.mvc.controller.actionflow.config.ActionHandlerNode; 21 import com.inversoft.verge.mvc.controller.actionflow.config.ActionHandlerNodeBuilder; 22 import com.inversoft.verge.mvc.controller.actionflow.config.BaseNamespace; 23 import com.inversoft.verge.mvc.controller.actionflow.config.Constants; 24 import com.inversoft.verge.mvc.controller.actionflow.config.DefaultTypes; 25 import com.inversoft.verge.mvc.controller.actionflow.config.Namespace; 26 import com.inversoft.verge.mvc.controller.actionflow.config.Node; 27 import com.inversoft.verge.mvc.controller.actionflow.config.NodeBuilder; 28 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 29 import com.inversoft.verge.repository.test.RepositoryTest; 30 31 32 39 public class ActionBuilderTest extends WebTestCase { 40 41 44 public ActionBuilderTest(String name) { 45 super(name); 46 setLocal(true); 47 } 48 49 52 public void testSimple() { 53 54 BaseNamespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 55 Element element = new Element("node"); 56 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 57 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 58 element.setAttribute(Constants.CLASS_ATTRIBUTE, 59 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 60 61 ActionHandlerNodeBuilder builder = new ActionHandlerNodeBuilder(); 62 try { 63 ActionHandlerNode config = 64 (ActionHandlerNode) builder.build(namespace, element); 65 assertTrue("Name should be testActionHandler", 66 config.getName().equals("testActionHandler")); 67 assertTrue("type should be actionHandler", 68 config.getType().equals("actionHandler")); 69 assertTrue("class should be actionHandler", 70 config.getClassName().equals("com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler")); 71 } catch (ConfigurationException ce) { 72 fail(ce.toString()); 73 } 74 } 75 76 79 public void testFailure() { 80 81 BaseNamespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 82 Element element = new Element("node"); 83 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 85 element.setAttribute(Constants.CLASS_ATTRIBUTE, 86 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 87 88 ActionHandlerNodeBuilder builder = new ActionHandlerNodeBuilder(); 89 try { 90 builder.build(namespace, element); 91 fail("Should have failed because no name"); 92 } catch (ConfigurationException ce) { 93 assertTrue("Should have errors", ce.getErrors() != null && 94 ce.getErrors().isEmpty() == false); 95 } 96 97 element = new Element("node"); 98 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 99 element.setAttribute(Constants.CLASS_ATTRIBUTE, 101 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 102 103 try { 104 builder.build(namespace, element); 105 fail("Should have failed because no type"); 106 } catch (ConfigurationException ce) { 107 assertTrue("Should have errors", ce.getErrors() != null && 108 ce.getErrors().isEmpty() == false); 109 } 110 111 element = new Element("node"); 112 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 113 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 114 117 try { 118 builder.build(namespace, element); 119 fail("Should have failed because no class"); 120 } catch (ConfigurationException ce) { 121 assertTrue("Should have errors", ce.getErrors() != null && 122 ce.getErrors().isEmpty() == false); 123 } 124 125 element = new Element("node"); 126 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 127 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 128 element.setAttribute(Constants.CLASS_ATTRIBUTE, 129 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 130 element.setAttribute(Constants.EXIT_ATTRIBUTE, "bob"); 131 132 try { 133 builder.build(namespace, element); 134 fail("Should have failed because exit is bob"); 135 } catch (ConfigurationException ce) { 136 assertTrue("Should have errors", ce.getErrors() != null && 137 ce.getErrors().isEmpty() == false); 138 } 139 140 element = new Element("node"); 141 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 142 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 143 element.setAttribute(Constants.CLASS_ATTRIBUTE, 144 "com.inversoft.verge.mvc.controller.actionflow.config.test.TestActionHandler"); 145 element.setAttribute(Constants.DEFAULT_ATTRIBUTE, "bob"); 146 147 try { 148 builder.build(namespace, element); 149 fail("Should have failed because default is bob"); 150 } catch (ConfigurationException ce) { 151 assertTrue("Should have errors", ce.getErrors() != null && 152 ce.getErrors().isEmpty() == false); 153 } 154 } 155 156 159 public void testRepositoryBuildPartial() { 160 161 BaseNamespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 162 Element element = new Element("node"); 163 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 164 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 165 element.setAttribute(Constants.REPOSITORY_ID_ATTRIBUTE, "test"); 166 167 ActionHandlerNodeBuilder builder = new ActionHandlerNodeBuilder(); 168 try { 169 ActionHandlerNode config = 170 (ActionHandlerNode) builder.build(namespace, element); 171 assertTrue("Name should be testActionHandler", 172 config.getName().equals("testActionHandler")); 173 assertTrue("type should be actionHandler", 174 config.getType().equals("actionHandler")); 175 assertTrue("rid should be test",config.getRepositoryId().equals("test")); 176 } catch (ConfigurationException ce) { 177 fail(ce.toString()); 178 } 179 } 180 181 184 public void testRepositoryBuild() { 185 186 RepositoryTest tester = new RepositoryTest("testInitialization"); 188 tester.setContext(context); 189 tester.testInitialization(); 190 191 BaseNamespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 192 Element element = new Element("node"); 193 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 194 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 195 element.setAttribute(Constants.REPOSITORY_ID_ATTRIBUTE, "item1"); 196 197 ActionHandlerNodeBuilder builder = new ActionHandlerNodeBuilder(); 198 try { 199 ActionHandlerNode config = 200 (ActionHandlerNode) builder.build(namespace, element); 201 assertTrue("Name should be testActionHandler", 202 config.getName().equals("testActionHandler")); 203 assertTrue("type should be actionHandler", 204 config.getType().equals("actionHandler")); 205 assertTrue("rid should be test", config.getRepositoryId().equals("item1")); 206 207 builder.postBuild(config, ActionFlowConfigRegistry.getInstance(request), 208 RepositoryConfigRegistry.getInstance(request)); 209 assertTrue("Name should be testActionHandler", 210 config.getName().equals("testActionHandler")); 211 assertTrue("type should be actionHandler", 212 config.getType().equals("actionHandler")); 213 assertTrue("rid should be test", config.getRepositoryId().equals("item1")); 214 } catch (ConfigurationException ce) { 215 fail(ce.toString()); 216 } 217 } 218 219 222 public void testRepositoryFailure() { 223 224 RepositoryTest tester = new RepositoryTest("testInitialization"); 226 tester.setContext(context); 227 tester.testInitialization(); 228 229 BaseNamespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 230 Element element = new Element("node"); 231 element.setAttribute(Constants.NAME_ATTRIBUTE, "testActionHandler"); 232 element.setAttribute(Constants.TYPE_ATTRIBUTE, "actionHandler"); 233 element.setAttribute(Constants.REPOSITORY_ID_ATTRIBUTE, "bob"); 234 235 ActionHandlerNodeBuilder builder = new ActionHandlerNodeBuilder(); 236 try { 237 ActionHandlerNode config = 238 (ActionHandlerNode) builder.build(namespace, element); 239 assertTrue("Name should be testActionHandler", 240 config.getName().equals("testActionHandler")); 241 assertTrue("type should be actionHandler", 242 config.getType().equals("actionHandler")); 243 assertTrue("rid should be test",config.getRepositoryId().equals("bob")); 244 245 builder.postBuild(config, ActionFlowConfigRegistry.getInstance(request), 246 RepositoryConfigRegistry.getInstance(request)); 247 fail("Should have failed because rid is bob"); 248 } catch (ConfigurationException ce) { 249 assertTrue("Should have errors", ce.getErrors() != null && 250 ce.getErrors().isEmpty() == false); 251 } 252 } 253 254 255 258 public void testComplete() { 259 NodeBuilder builder = new ActionHandlerNodeBuilder(); 260 Namespace namespace = new BaseNamespace("test", DefaultTypes.NAMESPACE); 261 262 Element element = new Element("node"); 263 element.setAttribute("name", "foo"); 264 element.setAttribute("type", "actionHandler"); 265 element.setAttribute("class", "com.foo.bar"); 266 element.setAttribute("isEntryPoint", "true"); 267 element.setAttribute("isDefaultEntry", "true"); 268 element.setAttribute("isExitPoint", "true"); 269 270 NodeExecutorRegistry.register("actionHandler", 271 new NodeExecutor() { 272 public Object execute(Namespace namespace, Node node, ActionFlowAction state, 273 Map map) { 274 return null; 275 } 276 }); 277 278 try { 279 Node node = builder.build(namespace, element); 280 assertTrue("Should be a action handler node", node instanceof ActionHandlerNode); 281 assertTrue("name should be foo", node.getName().equals("foo")); 282 assertTrue("type should be actionHandler", node.getType().equals("actionHandler")); 283 assertTrue("ClassName should be com.foo.bar", node.getClassName().equals("com.foo.bar")); 284 assertTrue("Is default entry point", node.isDefaultEntry()); 285 assertTrue("Is exit point", node.isExitPoint()); 286 } catch (ConfigurationException be) { 287 fail(be.toString()); 288 } 289 290 element.removeAttribute("name"); 292 element.removeAttribute("type"); 293 try { 294 builder.build(namespace, element); 295 fail("Should have failed because name and type are null"); 296 } catch (ConfigurationException be) { 297 assertTrue("Should have one error at least", !be.getErrors().isEmpty()); 298 System.out.println(be.toString()); 299 } 300 301 element.setAttribute("name", "foo"); 303 element.setAttribute("type", "actionHandler"); 304 element.setAttribute("repositoryId", "foo"); 305 try { 306 builder.build(namespace, element); 307 fail("Should have failed because classname and repositoryId were set"); 308 } catch (ConfigurationException be) { 309 assertTrue("Should have one error at least", !be.getErrors().isEmpty()); 310 System.out.println(be.toString()); 311 } 312 313 element.removeAttribute("class"); 314 element.removeAttribute("repositoryId"); 315 try { 316 builder.build(namespace, element); 317 fail("Should have failed because classname and repositoryId were set"); 318 } catch (ConfigurationException be) { 319 assertTrue("Should have one error at least", !be.getErrors().isEmpty()); 320 System.out.println(be.toString()); 321 } 322 } 323 } | Popular Tags |