KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestBasicInfrastructure


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.services.impl;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.Registry;
24 import org.apache.hivemind.impl.DefaultClassResolver;
25 import org.apache.hivemind.impl.RegistryBuilder;
26 import org.apache.hivemind.test.AggregateArgumentsMatcher;
27 import org.apache.hivemind.test.ArgumentMatcher;
28 import org.apache.hivemind.test.HiveMindTestCase;
29 import org.apache.hivemind.test.TypeMatcher;
30 import org.apache.hivemind.util.ClasspathResource;
31 import org.apache.tapestry.engine.IPropertySource;
32 import org.apache.tapestry.services.ClasspathResourceFactory;
33 import org.apache.tapestry.services.Infrastructure;
34 import org.apache.tapestry.services.RequestGlobals;
35 import org.apache.tapestry.services.ServletRequestServicer;
36 import org.apache.tapestry.services.ResetEventCoordinator;
37 import org.apache.tapestry.web.ServletWebRequest;
38 import org.apache.tapestry.web.ServletWebResponse;
39 import org.apache.tapestry.web.WebRequest;
40 import org.apache.tapestry.web.WebResponse;
41 import org.easymock.MockControl;
42
43 /**
44  * Tests for:
45  * <ul>
46  * <li>{@link org.apache.tapestry.services.impl.ServletInfoImpl}
47  * <li>{@link org.apache.tapestry.services.impl.StoreServletInfoFilter}
48  * <li>{@link org.apache.tapestry.services.impl.ApplicationGlobalsImpl}
49  * <li>{@link org.apache.tapestry.services.impl.InfrastructureObjectProvider}
50  * </ul>
51  *
52  * @author Howard Lewis Ship
53  * @since 4.0
54  */

55 public class TestBasicInfrastructure extends HiveMindTestCase
56 {
57     public void testRequestGlobals()
58     {
59         RequestGlobalsImpl si = new RequestGlobalsImpl();
60
61         HttpServletRequest JavaDoc r = (HttpServletRequest JavaDoc) newMock(HttpServletRequest JavaDoc.class);
62         HttpServletResponse JavaDoc p = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
63
64         replayControls();
65
66         si.store(r, p);
67
68         assertSame(r, si.getRequest());
69         assertSame(p, si.getResponse());
70
71         verifyControls();
72     }
73
74     public void testClasspathResourceFactory()
75     {
76         ClassResolver cr = new DefaultClassResolver();
77         ClasspathResourceFactoryImpl f = new ClasspathResourceFactoryImpl();
78         f.setClassResolver(cr);
79
80         String JavaDoc path = "/foo/bar";
81
82         ClasspathResource expected = new ClasspathResource(cr, path);
83
84         assertEquals(expected, f.newResource(path));
85     }
86
87     /**
88      * Validate that the factory is declared properly in the module deployment descriptor.
89      */

90     public void testClasspathResourceFactoryIntegrated()
91     {
92         ClassResolver cr = new DefaultClassResolver();
93
94         Registry registry = RegistryBuilder.constructDefaultRegistry();
95
96         ClasspathResourceFactory f = (ClasspathResourceFactory) registry.getService(
97                 "tapestry.ClasspathResourceFactory",
98                 ClasspathResourceFactory.class);
99
100         String JavaDoc path = "/foo/bar";
101
102         ClasspathResource expected = new ClasspathResource(cr, path);
103
104         assertEquals(expected, f.newResource(path));
105     }
106
107     public void testGlobalPropertyObjectProviderSuccess()
108     {
109         MockControl sourceControl = newControl(IPropertySource.class);
110         IPropertySource source = (IPropertySource) sourceControl.getMock();
111
112         // Training
113

114         source.getPropertyValue("foo");
115         sourceControl.setReturnValue("bar");
116
117         replayControls();
118
119         PropertyObjectProvider p = new PropertyObjectProvider();
120         p.setSource(source);
121
122         assertEquals("bar", p.provideObject(null, null, "foo", null));
123
124         verifyControls();
125     }
126
127     public void testGlobalPropertyObjectProviderFailure()
128     {
129         Location l = fabricateLocation(223);
130
131         MockControl sourceControl = newControl(IPropertySource.class);
132         IPropertySource source = (IPropertySource) sourceControl.getMock();
133
134         // Training
135

136         source.getPropertyValue("foo");
137         sourceControl.setThrowable(new ApplicationRuntimeException("failure"));
138
139         replayControls();
140
141         PropertyObjectProvider p = new PropertyObjectProvider();
142         p.setSource(source);
143
144         try
145         {
146             p.provideObject(null, null, "foo", l);
147         }
148         catch (ApplicationRuntimeException ex)
149         {
150             assertEquals("failure", ex.getMessage());
151             assertEquals(l, ex.getLocation());
152         }
153
154         verifyControls();
155     }
156
157     public void testSuccessfulInfrastructureLookup()
158     {
159         MockControl ifrControl = newControl(Infrastructure.class);
160         Infrastructure ifr = (Infrastructure) ifrControl.getMock();
161
162         ResetEventCoordinator coord = (ResetEventCoordinator) newMock(ResetEventCoordinator.class);
163
164         ifr.getResetEventCoordinator();
165         ifrControl.setReturnValue(coord);
166
167         replayControls();
168
169         InfrastructureObjectProvider p = new InfrastructureObjectProvider();
170
171         p.setInfrastructure(ifr);
172
173         Object JavaDoc actual = p.provideObject(null, null, "resetEventCoordinator", null);
174
175         assertSame(coord, actual);
176
177         verifyControls();
178     }
179 }
Popular Tags