1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.web.mvc;
30  
31  import java.util.Vector;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.jcontainer.dna.Configuration;
37  import org.jcontainer.dna.Logger;
38  import org.jmock.Mock;
39  import org.objectledge.authentication.UserUnknownException;
40  import org.objectledge.configuration.ConfigurationFactory;
41  import org.objectledge.context.Context;
42  import org.objectledge.filesystem.FileSystem;
43  import org.objectledge.logging.LoggerFactory;
44  import org.objectledge.logging.LoggingConfigurator;
45  import org.objectledge.parameters.RequestParametersLoaderValve;
46  import org.objectledge.pipeline.ErrorHandlingPipeline;
47  import org.objectledge.pipeline.ProcessingException;
48  import org.objectledge.templating.Templating;
49  import org.objectledge.templating.TemplatingContext;
50  import org.objectledge.templating.TemplatingContextLoaderValve;
51  import org.objectledge.templating.velocity.VelocityTemplating;
52  import org.objectledge.utils.LedgeTestCase;
53  import org.objectledge.web.HttpContext;
54  import org.objectledge.web.WebConfigurator;
55  import org.objectledge.xml.XMLGrammarCache;
56  import org.objectledge.xml.XMLValidator;
57  
58  /**
59   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
60   *
61   * To change the template for this generated type comment go to
62   * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
63   */
64  public class ExceptionRedirectorValveTest extends LedgeTestCase
65  {
66      private ExceptionRedirectorValve exceptionRedirectorValve;
67  
68      private Context context;
69  
70      private TemplatingContextLoaderValve templatingLoader;
71  
72  	private Mock mockHttpServletRequest;
73      private HttpServletRequest httpServletRequest;
74      private Mock mockHttpServletResponse;
75      private HttpServletResponse httpServletResponse;
76      
77      public void setUp()
78      {
79          try
80          {
81              context = new Context();
82              context.clearAttributes();
83              //prepare test
84              FileSystem fs = FileSystem.
85                  getStandardFileSystem("src/test/resources/exception-redirector");
86              XMLValidator validator = new XMLValidator(new XMLGrammarCache());
87              ConfigurationFactory configFactory = new ConfigurationFactory(fs, validator, ".");
88              Configuration config = configFactory.getConfig(WebConfigurator.class, 
89                                                             WebConfigurator.class);
90              WebConfigurator webConfigurator = new WebConfigurator(config);
91              
92              mockHttpServletRequest = mock(HttpServletRequest.class);
93              httpServletRequest = (HttpServletRequest)mockHttpServletRequest.proxy();
94              mockHttpServletRequest.stubs().method("getContentType").will(returnValue("text/html"));
95              mockHttpServletRequest.stubs().method("getParameterNames").
96                  will(returnValue((new Vector()).elements()));
97              mockHttpServletRequest.stubs().method("getQueryString").will(returnValue(""));
98              mockHttpServletRequest.stubs().method("getPathInfo").will(returnValue("view/Default"));
99              mockHttpServletRequest.stubs().method("getContextPath").will(returnValue("/test"));
100             mockHttpServletRequest.stubs().method("getServletPath").will(returnValue("ledge"));
101             mockHttpServletRequest.stubs().method("getRequestURI").will(returnValue(""));
102             mockHttpServletRequest.stubs().method("getServerName").
103                 will(returnValue("objectledge.org"));
104 
105             mockHttpServletResponse = mock(HttpServletResponse.class);
106             httpServletResponse = (HttpServletResponse)mockHttpServletResponse.proxy();
107 
108             HttpContext httpContext = new HttpContext(httpServletRequest, httpServletResponse);
109             context.setAttribute(HttpContext.class, httpContext);
110 
111             RequestParametersLoaderValve paramsLoader = new RequestParametersLoaderValve();
112             paramsLoader.process(context);
113             MVCInitializerValve mvcInitializer = new MVCInitializerValve(webConfigurator);
114             mvcInitializer.process(context);
115 
116             config = configFactory.getConfig(ExceptionRedirectorValve.class, 
117                                             ExceptionRedirectorValve.class);
118             LoggerFactory loggerFactory = new LoggerFactory(new LoggingConfigurator());
119             Logger logger = loggerFactory.getLogger(ExceptionRedirectorValve.class);
120             exceptionRedirectorValve = new ExceptionRedirectorValve(config, logger);
121             
122             config = configFactory.getConfig(Templating.class, VelocityTemplating.class);
123             logger = loggerFactory.getLogger(Templating.class);
124             Templating templating = new VelocityTemplating(config, logger, fs);
125             templatingLoader = new TemplatingContextLoaderValve(templating);        
126         }
127         catch (Exception e)
128         {
129             throw new Error(e);
130         }
131     }
132 
133     public void testExceptionRedirectorValveProcess() throws Exception
134     {
135         MVCContext mvcContext = MVCContext.getMVCContext(context);
136         mvcContext.setView("foo");
137         Throwable t = (Throwable)context.getAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION);
138         if(t != null)
139         {
140             context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, null);
141         }
142         exceptionRedirectorValve.process(context);
143         assertEquals(mvcContext.getView(), "foo");
144         context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, new Error("foo"));
145         exceptionRedirectorValve.process(context);
146         assertEquals(mvcContext.getView(), "DefaultError");
147         context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, new Exception("bar"));
148         exceptionRedirectorValve.process(context);
149         assertEquals(mvcContext.getView(), "Error");
150         context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, 
151                              new ProcessingException("foo"));
152         exceptionRedirectorValve.process(context);
153         assertEquals(mvcContext.getView(), "PrError");
154         context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, 
155                              new UnsupportedOperationException("foo"));
156         exceptionRedirectorValve.process(context);
157         assertEquals(mvcContext.getView(), "FooError");
158         //check the orginal view in context.
159         templatingLoader.process(context);
160         context.setAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION, 
161                              new UserUnknownException("foo"));
162         exceptionRedirectorValve.process(context);
163         assertEquals(mvcContext.getView(), "BarError");
164         TemplatingContext templatingContext = TemplatingContext.getTemplatingContext(context);
165         assertEquals("FooError", (String)templatingContext.get("originalView"));
166     }
167 }