1 7 package com.inversoft.verge.mvc.test; 8 9 10 import com.inversoft.junit.Request; 11 import com.inversoft.junit.WebTestCase; 12 import com.inversoft.junit.internal.http.MockServletContext; 13 import com.inversoft.verge.mvc.DefaultMVCMediator; 14 import com.inversoft.verge.mvc.MVCConstants; 15 import com.inversoft.verge.mvc.MVCException; 16 import com.inversoft.verge.mvc.MVCRegistry; 17 18 19 25 public class DefaultMVCMediatorTest extends WebTestCase { 26 27 30 public DefaultMVCMediatorTest(String name) { 31 super(name); 32 setLocal(true); 33 } 34 35 36 public void beginRequest(Request request) { 37 request.addParameter(MVCConstants.CONTROLLER_REQUEST_PARAM, "test"); 38 request.addParameter(MVCConstants.MODEL_REQUEST_PARAM, "test"); 39 request.addParameter(MVCConstants.VALIDATOR_REQUEST_PARAM, "test"); 40 } 41 42 45 public void testRequest() { 46 47 TestControllerParser cp = new TestControllerParser(); 48 TestModelParser mp = new TestModelParser(); 49 TestValidatorParser vp = new TestValidatorParser(); 50 MVCRegistry.register("test", cp); 51 MVCRegistry.register("test", mp); 52 MVCRegistry.register("test", vp); 53 DefaultMVCMediator mediator = new DefaultMVCMediator(); 54 55 try { 56 mediator.mediate(request, response); 57 assertTrue(cp.called > 0); 58 assertTrue(mp.called > 0); 59 assertTrue(vp.called > 0); 60 } catch (MVCException mvce) { 61 fail(mvce.toString()); 62 } 63 } 64 65 68 public void testContext() { 69 if (isLocal()) { 70 MockServletContext context = getContext(); 71 context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test"); 72 context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test"); 73 context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test"); 74 75 TestControllerParser cp = new TestControllerParser(); 76 TestModelParser mp = new TestModelParser(); 77 TestValidatorParser vp = new TestValidatorParser(); 78 MVCRegistry.register("test", cp); 79 MVCRegistry.register("test", mp); 80 MVCRegistry.register("test", vp); 81 DefaultMVCMediator mediator = new DefaultMVCMediator(); 82 83 try { 84 mediator.mediate(request, response); 85 assertTrue(cp.called > 0); 86 assertTrue(mp.called > 0); 87 assertTrue(vp.called > 0); 88 } catch (MVCException mvce) { 89 fail(mvce.toString()); 90 } 91 } 92 } 93 94 97 public void testControllerDefault() { 98 if (isLocal()) { 99 MockServletContext context = getContext(); 100 context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test"); 101 context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test"); 102 103 TestControllerParser cp = new TestControllerParser(); 104 TestModelParser mp = new TestModelParser(); 105 TestValidatorParser vp = new TestValidatorParser(); 106 MVCRegistry.register(MVCConstants.DEFAULT_NAME, cp); 107 MVCRegistry.register("test", mp); 108 MVCRegistry.register("test", vp); 109 DefaultMVCMediator mediator = new DefaultMVCMediator(); 110 111 try { 112 mediator.mediate(request, response); 113 assertTrue(cp.called > 0); 114 assertTrue(mp.called > 0); 115 assertTrue(vp.called > 0); 116 } catch (MVCException mvce) { 117 fail("Should have used the controller default [" + mvce + "]"); 118 } 119 } 120 } 121 122 125 public void testModelDefault() { 126 if (isLocal()) { 127 MockServletContext context = getContext(); 128 context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test"); 129 context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test"); 130 } 131 132 TestControllerParser cp = new TestControllerParser(); 133 TestModelParser mp = new TestModelParser(); 134 TestValidatorParser vp = new TestValidatorParser(); 135 MVCRegistry.register("test", cp); 136 MVCRegistry.register(MVCConstants.DEFAULT_NAME, mp); 137 MVCRegistry.register("test", vp); 138 DefaultMVCMediator mediator = new DefaultMVCMediator(); 139 140 try { 141 mediator.mediate(request, response); 142 assertTrue(cp.called > 0); 143 assertTrue(mp.called > 0); 144 assertTrue(vp.called > 0); 145 } catch (MVCException mvce) { 146 fail("Should have used the model default"); 147 } 148 } 149 150 153 public void testValidatorDefault() { 154 if (isLocal()) { 155 MockServletContext context = getContext(); 156 context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test"); 157 context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test"); 158 159 TestControllerParser cp = new TestControllerParser(); 160 TestModelParser mp = new TestModelParser(); 161 TestValidatorParser vp = new TestValidatorParser(); 162 MVCRegistry.register("test", cp); 163 MVCRegistry.register("test", mp); 164 MVCRegistry.register(MVCConstants.DEFAULT_NAME, vp); 165 DefaultMVCMediator mediator = new DefaultMVCMediator(); 166 167 try { 168 mediator.mediate(request, response); 169 assertTrue(cp.called > 0); 170 assertTrue(mp.called > 0); 171 assertTrue(vp.called > 0); 172 } catch (MVCException mvce) { 173 fail("Should have used the validator default"); 174 } 175 } 176 } 177 178 181 public void testOrder() { 182 if (isLocal()) { 183 MockServletContext context = getContext(); 184 context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test"); 185 context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test"); 186 context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test"); 187 } 188 189 TestControllerParser cp = new TestControllerParser(); 190 TestValidatorParser vp = new TestValidatorParser(); 191 TestModelParser mp = new TestModelParser(); 192 193 MVCRegistry.register("test", cp); 194 MVCRegistry.register("test", mp); 195 MVCRegistry.register("test", vp); 196 DefaultMVCMediator mediator = new DefaultMVCMediator(); 197 198 try { 199 mediator.mediate(request, response); 200 assertTrue("Should have called controller parser", cp.getCalled() > 0); 201 assertTrue("Should have called model parser", mp.getCalled() > 0); 202 assertTrue("Should have called validator parser", vp.getCalled() > 0); 203 assertTrue("Should have called validator parser second", vp.getCalled() > mp.getCalled()); 204 assertTrue("Should have called controller parser last ", cp.getCalled() > mp.getCalled() && 205 cp.getCalled() > vp.getCalled()); 206 } catch (MVCException mvce) { 207 fail(mvce.toString()); 208 } 209 } 210 } | Popular Tags |