1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
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
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 }