01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.test;
16:
17: import org.apache.tapestry.Link;
18: import org.apache.tapestry.dom.Document;
19: import org.apache.tapestry.internal.services.ActionLinkTarget;
20: import org.apache.tapestry.internal.services.ComponentInvocation;
21: import org.apache.tapestry.internal.services.ComponentInvocationMap;
22: import org.apache.tapestry.internal.services.InvocationTarget;
23: import org.apache.tapestry.internal.services.LinkActionResponseGenerator;
24: import org.apache.tapestry.ioc.Registry;
25: import org.apache.tapestry.ioc.internal.util.Defense;
26: import org.apache.tapestry.services.ActionResponseGenerator;
27: import org.apache.tapestry.services.ComponentActionRequestHandler;
28:
29: /**
30: * Simulates a click on an action link.
31: */
32: public class ActionLinkInvoker implements ComponentInvoker {
33: private final Registry _registry;
34:
35: private final ComponentInvoker _followupInvoker;
36:
37: private final ComponentActionRequestHandler _componentActionRequestHandler;
38:
39: private final ComponentInvocationMap _componentInvocationMap;
40:
41: public ActionLinkInvoker(Registry registry,
42: ComponentInvoker followupInvoker,
43: ComponentInvocationMap componentInvocationMap) {
44: _registry = registry;
45: _followupInvoker = followupInvoker;
46: _componentActionRequestHandler = _registry.getService(
47: "ComponentActionRequestHandler",
48: ComponentActionRequestHandler.class);
49: _componentInvocationMap = componentInvocationMap;
50:
51: }
52:
53: /**
54: * Click on the action link and get another link in return. Then follow up the link with another
55: * {@link ComponentInvoker}.
56: *
57: * @param invocation
58: * The ComponentInvocation object corresponding to the action link.
59: * @return The DOM created. Typically you will assert against it.
60: */
61: public Document invoke(ComponentInvocation invocation) {
62: ActionResponseGenerator generator = click(invocation);
63:
64: if (generator instanceof LinkActionResponseGenerator) {
65: LinkActionResponseGenerator linkGenerator = (LinkActionResponseGenerator) generator;
66:
67: Link link = linkGenerator.getLink();
68:
69: ComponentInvocation followup = _componentInvocationMap
70: .get(link);
71:
72: return _followupInvoker.invoke(followup);
73: }
74:
75: String message = String
76: .format(
77: "ActionResponseGenerator %s is an instance of class %s, which is not compatible with PageTester.",
78: generator, generator.getClass());
79:
80: throw new RuntimeException(message);
81: }
82:
83: private ActionResponseGenerator click(ComponentInvocation invocation) {
84: try {
85: InvocationTarget target = invocation.getTarget();
86:
87: ActionLinkTarget actionLinkTarget = Defense.cast(target,
88: ActionLinkTarget.class, "target");
89:
90: return _componentActionRequestHandler.handle(
91: actionLinkTarget.getPageName(), actionLinkTarget
92: .getComponentNestedId(), actionLinkTarget
93: .getEventType(), invocation.getContext(),
94: invocation.getActivationContext());
95: } finally {
96: _registry.cleanupThread();
97: }
98: }
99: }
|