Clover coverage report - angkor - 0.4
Coverage timestamp: ti okt 15 2002 22:32:48 CEST
file stats: LOC: 342   Methods: 26
NCLOC: 239   Classes: 3
Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
ProcessTest.java 42,9% 84,9% 73,1% 78,8%
 1   
 /*
 2   
  * Angkor Web Framework
 3   
  *
 4   
  * Distributable under LGPL license.
 5   
  * See terms of license at gnu.org.
 6   
  */
 7   
 
 8   
 package com.tirsen.angkor.test.unit;
 9   
 
 10   
 import com.tirsen.angkor.*;
 11   
 import com.tirsen.angkor.test.unit.mock.MockRenderContext;
 12   
 import com.tirsen.angkor.process.*;
 13   
 import com.tirsen.angkor.event.ChangeEvent;
 14   
 import com.tirsen.angkor.event.ChangeListener;
 15   
 import com.tirsen.angkor.widget.*;
 16   
 
 17   
 import javax.servlet.ServletException;
 18   
 import java.io.IOException;
 19   
 
 20   
 /**
 21   
  * TODO document ProcessTest
 22   
  *
 23   
  * <!-- $Id: ProcessTest.java,v 1.2 2002/10/13 13:37:26 tirsen Exp $ -->
 24   
  * <!-- $Author: tirsen $ -->
 25   
  *
 26   
  * @author Jon Tirs&acute;n (tirsen@users.sourceforge.net)
 27   
  * @version $Revision: 1.2 $
 28   
  */
 29   
 public class ProcessTest extends AbstractPipelineTest
 30   
 {
 31   
     private static final String firstModelValue = "firstvalue";
 32   
     private static final String nextModelValue = "newvalue";
 33   
 
 34   
     private TestController testController;
 35   
     private Application application;
 36   
     private MockRenderContext context;
 37   
     private MockPipeline parsePipeline;
 38   
     private MockPipeline eventPipeline;
 39   
     private TestView testView;
 40   
 
 41   
     public class TestController extends Controller implements ChangeListener
 42   
     {
 43   
         private ValueModel model = new BasicValueModel(firstModelValue);
 44   
         private boolean eventReceived = false;
 45   
         private boolean fatalError = false;
 46   
         private boolean failDuringEventProcessing = false;
 47   
 
 48  3
         public TestController(Application application)
 49   
         {
 50  3
             super(application);
 51   
         }
 52   
 
 53  3
         public View createView()
 54   
         {
 55  3
             TestView view = new TestView();
 56   
 
 57  3
             assertNotNull("model not created at this time", model);
 58   
 
 59  3
             view.setModel(model);
 60   
 
 61  3
             view.addChangeListener(this);
 62   
 
 63  3
             return view;
 64   
         }
 65   
 
 66  6
         public void stateChanged(ChangeEvent evt)
 67   
         {
 68  6
             eventReceived = true;
 69  6
             assertTrue("model value not changed, value: " + model.getValue(), nextModelValue.equals(model.getValue()));
 70  6
             try
 71   
             {
 72  0
                 if(failDuringEventProcessing) throwError("event processing");
 73   
             }
 74   
             catch(Exception e)
 75   
             {
 76  0
                 getApplication().handleError(e);
 77   
             }
 78   
         }
 79   
 
 80  8
         public boolean isEventReceived()
 81   
         {
 82  8
             return eventReceived;
 83   
         }
 84   
 
 85  6
         public void setEventReceived(boolean eventReceived)
 86   
         {
 87  6
             this.eventReceived = eventReceived;
 88   
         }
 89   
 
 90  0
         public void setFailDuringEventProcessing(boolean failDuringEventProcessing)
 91   
         {
 92  0
             this.failDuringEventProcessing = failDuringEventProcessing;
 93   
         }
 94   
 
 95  0
         public void setFatalError(boolean fatalError)
 96   
         {
 97  0
             this.fatalError = fatalError;
 98   
         }
 99   
 
 100  0
         protected void throwError(String phase) throws Exception
 101   
         {
 102  0
             if(fatalError)
 103   
             {
 104  0
                 throw new Exception("Fatal error during " + phase + "!");
 105   
             }
 106   
             else
 107   
             {
 108  0
                 throw new NumberFormatException("Warning during " + phase + "!");
 109   
             }
 110   
         }
 111   
     }
 112   
 
 113   
     public class TestView extends TextInput
 114   
     {
 115   
         private boolean parsed = false;
 116   
         private boolean rendered = false;
 117   
 
 118   
         private boolean failDuringParse = false;
 119   
         private boolean failDuringRender = false;
 120   
         private boolean fatalError = false;
 121   
 
 122  0
         protected void throwError(String phase)
 123   
         {
 124  0
             if(fatalError)
 125   
             {
 126  0
                 throw new SecurityException("Fatal error during " + phase + "!");
 127   
             }
 128   
             else
 129   
             {
 130  0
                 throw new NumberFormatException("Warning during " + phase + "!");
 131   
             }
 132   
         }
 133   
 
 134  0
         public void setFailDuringParse(boolean failDuringParse)
 135   
         {
 136  0
             this.failDuringParse = failDuringParse;
 137   
         }
 138   
 
 139  0
         public void setFailDuringRender(boolean failDuringRender)
 140   
         {
 141  0
             this.failDuringRender = failDuringRender;
 142   
         }
 143   
 
 144  0
         public void setFatalError(boolean fatalError)
 145   
         {
 146  0
             this.fatalError = fatalError;
 147   
         }
 148   
 
 149  6
         public void parse(RenderContext context)
 150   
         {
 151  0
             if(failDuringParse) throwError("parse");
 152   
 
 153  6
             super.parse(context);
 154  6
             parsed = true;
 155   
         }
 156   
 
 157  2
         public boolean isParsed()
 158   
         {
 159  2
             return parsed;
 160   
         }
 161   
 
 162  7
         public void render(RenderContext context) throws IOException
 163   
         {
 164  0
             if(failDuringRender) throwError("render");
 165   
 
 166  7
             super.render(context);
 167  7
             rendered = true;
 168   
         }
 169   
 
 170  6
         public boolean isRendered()
 171   
         {
 172  6
             return rendered;
 173   
         }
 174   
 
 175  6
         public void setRendered(boolean rendered)
 176   
         {
 177  6
             this.rendered = rendered;
 178   
         }
 179   
     }
 180   
 
 181  3
     public void setUp() throws Exception, ServletException
 182   
     {
 183  3
         super.setUp();
 184  3
         parsePipeline = new MockPipeline();
 185  3
         parsePipeline.addValve(new ErrorValve());
 186  3
         parsePipeline.addValve(new ParseValve());
 187   
 
 188  3
         eventPipeline = new MockPipeline();
 189  3
         eventPipeline.addValve(new ErrorValve());
 190  3
         eventPipeline.addValve(new EventValve());
 191   
 
 192   
         //Pipeline renderPipeline = createRenderPipeline(testController);
 193   
 
 194  3
         context = new MockRenderContext();
 195  3
         context.ensureApplicationInit(MockApplication.class);
 196  3
         application = context.getApplication();
 197   
 
 198  3
         testController = new TestController(application);
 199  3
         application.registerComponent("current", testController);
 200  3
         testView = (TestView) testController.getView();
 201  3
         testView.getId();
 202   
 
 203  3
         application.init();
 204   
     }
 205   
 
 206  1
     private Pipeline createRenderPipeline(ViewFactory component)
 207   
     {
 208  1
         Pipeline renderPipeline = new DefaultPipeline();
 209  1
         renderPipeline.addValve(new ErrorValve());
 210  1
         renderPipeline.addValve(new SingleViewFactoryRenderValve(component));
 211  1
         return renderPipeline;
 212   
     }
 213   
 
 214   
     /**
 215   
      * Tests the whole default processing. First tests each valve separately:
 216   
      * <ol> {@link ParseValve}, checks parsing does <em>not</em> occur on first request.
 217   
      * <ol> {@link EventValve}, checks events does <em>not</em> occur on first request.
 218   
      * <ol> {@link RenderValve}, checks view rendered.
 219   
      * <ol> {@link ParseValve}, checks parsing occured and model updated before events.
 220   
      * <ol> {@link EventValve}, checks events was sent.
 221   
      * After separate testing runs the processing as created by {@link Application}
 222   
      * a couple of times checking the above things.
 223   
      */
 224  1
     public void testProcessing() throws Exception
 225   
     {
 226  1
         application.beforeRequest(context);
 227   
 
 228  1
         ValueModel model = testView.getModel();
 229   
 
 230   
         // test parse valve
 231  1
         parsePipeline.execute(context);
 232   
 
 233  1
         assertTrue("test view was parsed before it was actually rendered", !testView.isParsed());
 234   
 
 235   
         // send events
 236  1
         eventPipeline.execute(context);
 237   
 
 238  1
         assertTrue("change event was received before any change actually occured", !testController.isEventReceived());
 239   
 
 240   
         // render valve (as a transition also prepares)
 241  1
         Pipeline renderPipeline = createRenderPipeline(testController);
 242  1
         renderPipeline.execute(context);
 243   
 
 244  1
         assertTrue("testView was not rendered", testView.isRendered());
 245  1
         testView.setRendered(false);
 246   
 
 247   
         // this should reset all the controllers prepare-state
 248  1
         context.afterRequest();
 249   
 
 250  1
         assertTrue("model doesn't have correct value", firstModelValue.equals(model.getValue()));
 251   
 
 252   
         // simulate we changed the value of the testView
 253  1
         prepareContextForChange(testView, false);
 254   
 
 255   
         // parse again, this time components should have been registered and things will start happening
 256  1
         parsePipeline.execute(context);
 257   
 
 258  1
         assertTrue("test view was not parsed", testView.isParsed());
 259   
 
 260  1
         TestView view = testView;
 261  1
         ValueModel valueModel = view.getModel();
 262  1
         assertTrue("model have not been updated", nextModelValue.equals(valueModel.getValue()));
 263  1
         assertTrue("events was sent before models were updated (or at least before they were ordered to be sent)",
 264   
                !testController.isEventReceived());
 265   
 
 266   
         // 4b. send events
 267  1
         eventPipeline.execute(context);
 268   
 
 269  1
         assertTrue("change event was not received", testController.isEventReceived());
 270  1
         testController.setEventReceived(false);
 271   
 
 272  1
         valueModel.setValue(firstModelValue);
 273  1
         context.afterRequest();
 274   
 
 275   
         // do the whole process in one pipeline
 276   
 
 277  1
         process();
 278   
 
 279  1
         for(int i = 0; i < 5; i++)
 280   
         {
 281   
             //Pipeline process = application.createRenderPipeline(testController);
 282   
 
 283  5
             prepareContextForChange(testView, false);
 284   
 
 285  5
             process();
 286   
 
 287  5
             assertTrue("testView was not rendered", testView.isRendered());
 288  5
             testView.setRendered(false);
 289  5
             assertTrue("change event was not sent", testController.isEventReceived());
 290  5
             testController.setEventReceived(false);
 291   
 
 292   
             // clean up for next test method
 293  5
             context.afterRequest();
 294   
         }
 295   
     }
 296   
 
 297  1
     public void testAccessCurrent() throws Exception
 298   
     {
 299  1
         Pipeline pipeline = new DefaultPipeline();
 300  1
         pipeline.addValve(new Valve() {
 301  1
             public void execute(ExecuteContext exec) throws Exception
 302   
             {
 303  1
                 assertSame("could not access application", application, Application.getApplication());
 304  1
                 assertSame("could not access render context", context, RenderContext.getRenderContext());
 305   
             }
 306   
         });
 307  1
         pipeline.execute(context);
 308   
     }
 309   
 
 310  1
     public void testError() throws Exception
 311   
     {
 312  1
         final Exception exceptionToThrow = new Exception();
 313  1
         pipeline.addValve(new ErrorValve());
 314  1
         pipeline.addValve(new Valve()
 315   
         {
 316  1
             public void execute(ExecuteContext exec) throws Exception
 317   
             {
 318  1
                 throw exceptionToThrow;
 319   
             }
 320   
         });
 321  1
         ExecuteContext executeContext = pipeline.createExecuteContext(context);
 322  1
         pipeline.execute(executeContext);
 323  1
         assertSame(exceptionToThrow, application.getError());
 324   
     }
 325   
 
 326  6
     private void process() throws Exception
 327   
     {
 328  6
         Pipeline process = application.createRenderPipeline(testController);
 329  6
         process.execute(context);
 330   
     }
 331   
 
 332  6
     private void prepareContextForChange(View view, boolean register) throws Exception
 333   
     {
 334  6
         parsePipeline.start(); // really just a QD to get uniqueId working...
 335  6
         context.setRequestParameter(view.uniqueId(context), nextModelValue);
 336  6
         if(register)
 337   
         {
 338  0
             context.registerParsingComponent(view);
 339   
         }
 340   
     }
 341   
 }
 342