001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import static org.easymock.EasyMock.aryEq;
018: import static org.easymock.EasyMock.eq;
019: import static org.easymock.EasyMock.isA;
020:
021: import org.apache.tapestry.ComponentEventHandler;
022: import org.apache.tapestry.TapestryConstants;
023: import org.apache.tapestry.internal.structure.ComponentPageElement;
024: import org.apache.tapestry.internal.structure.Page;
025: import org.apache.tapestry.internal.test.InternalBaseTestCase;
026: import org.apache.tapestry.services.ComponentClassResolver;
027: import org.apache.tapestry.services.ComponentEventResultProcessor;
028: import org.apache.tapestry.services.Dispatcher;
029: import org.apache.tapestry.services.PageRenderRequestHandler;
030: import org.apache.tapestry.services.Request;
031: import org.apache.tapestry.services.Response;
032: import org.testng.annotations.Test;
033:
034: public class PageRenderDispatcherTest extends InternalBaseTestCase {
035: @Test
036: public void not_a_page_request() throws Exception {
037: ComponentClassResolver resolver = mockComponentClassResolver();
038: PageResponseRenderer renderer = mockPageResponseRenderer();
039: RequestPageCache cache = mockRequestPageCache();
040: PageRenderRequestHandler handler = new PageRenderRequestHandlerImpl(
041: cache, null, null, null);
042: Request request = mockRequest();
043: Response response = mockResponse();
044:
045: stub_isPageName(resolver, false);
046:
047: train_getPath(request, "/foo/Bar.baz");
048:
049: replay();
050:
051: Dispatcher d = new PageRenderDispatcher(resolver, handler);
052:
053: assertFalse(d.dispatch(request, response));
054:
055: verify();
056: }
057:
058: // TAPESTRY-1343
059: @Test
060: public void empty_path() throws Exception {
061: ComponentClassResolver resolver = mockComponentClassResolver();
062: PageResponseRenderer renderer = mockPageResponseRenderer();
063: PageRenderRequestHandler handler = newMock(PageRenderRequestHandler.class);
064: Request request = mockRequest();
065: Response response = mockResponse();
066:
067: train_getPath(request, "");
068:
069: replay();
070:
071: Dispatcher d = new PageRenderDispatcher(resolver, handler);
072:
073: assertFalse(d.dispatch(request, response));
074:
075: verify();
076: }
077:
078: @Test
079: public void no_extra_context_without_final_slash() throws Exception {
080: no_extra_context(false);
081: }
082:
083: @Test
084: public void no_extra_context_with_final_slash() throws Exception {
085: no_extra_context(true);
086: }
087:
088: private void no_extra_context(boolean finalSlash) throws Exception {
089: ComponentClassResolver resolver = mockComponentClassResolver();
090: PageResponseRenderer renderer = mockPageResponseRenderer();
091: RequestPageCache cache = mockRequestPageCache();
092: ComponentEventResultProcessor processor = newComponentEventResultProcessor();
093: Request request = mockRequest();
094: Response response = mockResponse();
095: Page page = mockPage();
096: ComponentPageElement rootElement = mockComponentPageElement();
097:
098: String path = "/foo/Bar" + (finalSlash ? "/" : "");
099: train_getPath(request, path);
100:
101: train_isPageName(resolver, "foo", false);
102: train_isPageName(resolver, "foo/Bar", true);
103:
104: train_get(cache, "foo/Bar", page);
105: train_getRootElement(page, rootElement);
106:
107: train_triggerEvent(rootElement,
108: TapestryConstants.ACTIVATE_EVENT, new Object[0], null,
109: false);
110:
111: renderer.renderPageResponse(page, response);
112:
113: replay();
114:
115: PageRenderRequestHandler handler = new PageRenderRequestHandlerImpl(
116: cache, processor, renderer, response);
117:
118: Dispatcher d = new PageRenderDispatcher(resolver, handler);
119:
120: assertTrue(d.dispatch(request, response));
121:
122: verify();
123: }
124:
125: @Test
126: public void context_passed_in_path_without_final_slash()
127: throws Exception {
128: context_passed_in_path(false);
129: }
130:
131: @Test
132: public void context_passed_in_path_with_final_slash()
133: throws Exception {
134: context_passed_in_path(true);
135: }
136:
137: private void context_passed_in_path(boolean finalSlash)
138: throws Exception {
139: ComponentEventResultProcessor processor = newComponentEventResultProcessor();
140: ComponentClassResolver resolver = mockComponentClassResolver();
141: PageResponseRenderer renderer = mockPageResponseRenderer();
142: RequestPageCache cache = mockRequestPageCache();
143: Request request = mockRequest();
144: Response response = mockResponse();
145: Page page = mockPage();
146: ComponentPageElement rootElement = mockComponentPageElement();
147:
148: String path = "/foo/Bar/zip/zoom" + (finalSlash ? "/" : "");
149: train_getPath(request, path);
150:
151: train_isPageName(resolver, "foo", false);
152: train_isPageName(resolver, "foo/Bar", true);
153:
154: train_get(cache, "foo/Bar", page);
155: train_getRootElement(page, rootElement);
156:
157: train_triggerEvent(rootElement,
158: TapestryConstants.ACTIVATE_EVENT, new Object[] { "zip",
159: "zoom" }, null, false);
160:
161: renderer.renderPageResponse(page, response);
162:
163: replay();
164:
165: PageRenderRequestHandler handler = new PageRenderRequestHandlerImpl(
166: cache, processor, renderer, response);
167:
168: Dispatcher d = new PageRenderDispatcher(resolver, handler);
169:
170: assertTrue(d.dispatch(request, response));
171:
172: verify();
173: }
174:
175: protected ComponentEventResultProcessor newComponentEventResultProcessor() {
176: return newMock(ComponentEventResultProcessor.class);
177: }
178:
179: private void train_triggerEvent(ComponentPageElement element,
180: String eventType, Object[] context,
181: ComponentEventHandler handler, boolean handled) {
182: expect(
183: element.triggerEvent(eq(eventType), aryEq(context),
184: isA(ComponentEventHandler.class))).andReturn(
185: handled);
186: }
187: }
|