01: // Copyright 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.internal.services.ComponentInvocationMap;
18: import org.apache.tapestry.internal.services.CookieSink;
19: import org.apache.tapestry.internal.services.CookieSource;
20: import org.apache.tapestry.ioc.Configuration;
21: import org.apache.tapestry.ioc.ObjectLocator;
22: import org.apache.tapestry.ioc.ServiceBinder;
23: import org.apache.tapestry.services.AliasContribution;
24: import org.apache.tapestry.services.MarkupWriterFactory;
25: import org.apache.tapestry.services.Request;
26: import org.apache.tapestry.services.Response;
27: import org.apache.tapestry.test.PageTester;
28:
29: /**
30: * Used in conjuction with {@link PageTester} to mock up and/or stub out portions of Tapestry that
31: * need to be handled differently when testing.
32: */
33: public class PageTesterModule {
34: public static final String TEST_MODE = "test";
35:
36: public static void bind(ServiceBinder binder) {
37: binder.bind(TestableRequest.class, TestableRequestImpl.class);
38: binder.bind(TestableMarkupWriterFactory.class,
39: TestableMarkupWriterFactoryImpl.class);
40: }
41:
42: public static void contributeAlias(
43: Configuration<AliasContribution> configuration,
44: ObjectLocator locator) {
45: add(configuration, ComponentInvocationMap.class,
46: new PageTesterComponentInvocationMap());
47: add(configuration, Response.class, new TestableResponseImpl());
48:
49: add(configuration, locator, Request.class, "TestableRequest");
50: add(configuration, locator, MarkupWriterFactory.class,
51: "TestableMarkupWriterFactory");
52:
53: TestableCookieSinkSource cookies = new TestableCookieSinkSource();
54:
55: add(configuration, CookieSink.class, cookies);
56: add(configuration, CookieSource.class, cookies);
57: }
58:
59: private static <T> void add(
60: Configuration<AliasContribution> configuration,
61: ObjectLocator locator, Class<T> serviceClass,
62: String serviceId) {
63: T service = locator.getService(serviceId, serviceClass);
64:
65: add(configuration, serviceClass, service);
66: }
67:
68: private static <T> void add(
69: Configuration<AliasContribution> configuration,
70: Class<T> serviceClass, T service) {
71: AliasContribution<T> contribution = AliasContribution.create(
72: serviceClass, TEST_MODE, service);
73:
74: configuration.add(contribution);
75: }
76: }
|