001: // Copyright 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:
020: import java.io.IOException;
021:
022: import org.apache.tapestry.TapestryConstants;
023: import org.apache.tapestry.internal.InternalConstants;
024: import org.apache.tapestry.internal.test.InternalBaseTestCase;
025: import org.apache.tapestry.services.ActionResponseGenerator;
026: import org.apache.tapestry.services.ComponentActionRequestHandler;
027: import org.apache.tapestry.services.Dispatcher;
028: import org.apache.tapestry.services.Request;
029: import org.apache.tapestry.services.Response;
030: import org.testng.annotations.Test;
031:
032: public class ComponentActionDispatcherTest extends InternalBaseTestCase {
033: @Test
034: public void no_dot_or_colon_in_path() throws Exception {
035: ComponentActionRequestHandler handler = newComponentActionRequestHandler();
036: Request request = mockRequest();
037: Response response = mockResponse();
038:
039: train_getPath(request, "/foo/bar/baz");
040:
041: replay();
042:
043: Dispatcher dispatcher = new ComponentActionDispatcher(handler);
044:
045: assertFalse(dispatcher.dispatch(request, response));
046:
047: verify();
048: }
049:
050: protected final ComponentActionRequestHandler newComponentActionRequestHandler() {
051: return newMock(ComponentActionRequestHandler.class);
052: }
053:
054: @Test
055: public void event_on_page() throws Exception {
056: test("/foo/MyPage:anevent", "foo/MyPage", "", "anevent");
057: }
058:
059: @Test
060: public void event_on_component_within_page() throws Exception {
061: test("/foo/MyPage.fred:anevent", "foo/MyPage", "fred",
062: "anevent");
063: }
064:
065: @Test
066: public void default_event_with_nested_id() throws Exception {
067: test("/foo/MyPage.fred", "foo/MyPage", "fred",
068: TapestryConstants.ACTION_EVENT);
069: }
070:
071: @Test
072: public void default_event_with_nested_id_and_context()
073: throws Exception {
074: test("/foo/MyPage.fred/fee/fie/foe/fum", "foo/MyPage", "fred",
075: TapestryConstants.ACTION_EVENT, "fee", "fie", "foe",
076: "fum");
077: }
078:
079: @Test
080: public void default_event_with_context_that_includes_a_colon()
081: throws Exception {
082: test("/foo/MyPage.underdog/a:b:c/d", "foo/MyPage", "underdog",
083: TapestryConstants.ACTION_EVENT, "a:b:c", "d");
084: }
085:
086: @Test
087: public void event_on_nested_component_within_page()
088: throws Exception {
089: test("/foo/MyPage.barney.fred:anevent", "foo/MyPage",
090: "barney.fred", "anevent");
091: }
092:
093: @Test
094: public void page_event_with_context() throws Exception {
095: test("/foo/MyPage:trigger/foo", "foo/MyPage", "", "trigger",
096: "foo");
097: }
098:
099: @Test
100: public void nested_component_event_with_context() throws Exception {
101: test("/foo/MyPage.nested:trigger/foo/bar/baz", "foo/MyPage",
102: "nested", "trigger", "foo", "bar", "baz");
103: }
104:
105: @Test
106: public void page_activation_context_in_request() throws Exception {
107: ComponentActionRequestHandler handler = newComponentActionRequestHandler();
108: Request request = mockRequest();
109: Response response = mockResponse();
110: ActionResponseGenerator generator = newMock(ActionResponseGenerator.class);
111:
112: train_getPath(request, "/mypage:eventname");
113:
114: train_getParameter(request,
115: InternalConstants.PAGE_CONTEXT_NAME, "alpha/beta");
116:
117: expect(
118: handler.handle(eq("mypage"), eq(""), eq("eventname"),
119: aryEq(new String[0]), aryEq(new String[] {
120: "alpha", "beta" }))).andReturn(
121: generator);
122:
123: generator.sendClientResponse(response);
124:
125: replay();
126:
127: Dispatcher dispatcher = new ComponentActionDispatcher(handler);
128:
129: assertTrue(dispatcher.dispatch(request, response));
130:
131: verify();
132: }
133:
134: private void test(String requestPath, String logicalPageName,
135: String nestedComponentId, String eventType,
136: String... context) throws IOException {
137: ComponentActionRequestHandler handler = newComponentActionRequestHandler();
138: Request request = mockRequest();
139: Response response = mockResponse();
140: ActionResponseGenerator generator = newMock(ActionResponseGenerator.class);
141:
142: train_getPath(request, requestPath);
143:
144: train_getParameter(request,
145: InternalConstants.PAGE_CONTEXT_NAME, null);
146:
147: expect(
148: handler.handle(eq(logicalPageName),
149: eq(nestedComponentId), eq(eventType),
150: aryEq(context), aryEq(new String[0])))
151: .andReturn(generator);
152:
153: generator.sendClientResponse(response);
154:
155: replay();
156:
157: Dispatcher dispatcher = new ComponentActionDispatcher(handler);
158:
159: assertTrue(dispatcher.dispatch(request, response));
160:
161: verify();
162: }
163: }
|