KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 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 java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.test.HiveMindTestCase;
25
26 /**
27  * Tests for {@link org.apache.tapestry.services.impl.InfrastructureImpl}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 4.0
31  */

32 public class TestInfrastructure extends HiveMindTestCase
33 {
34     private static class DeferredObjectFixture implements DeferredObject
35     {
36         private Object JavaDoc _object;
37
38         private Location _location;
39
40         public DeferredObjectFixture(Object JavaDoc object, Location location)
41         {
42             _object = object;
43             _location = location;
44         }
45
46         public Location getLocation()
47         {
48             return _location;
49         }
50
51         public Object JavaDoc getObject()
52         {
53             return _object;
54         }
55     }
56
57     private InfrastructureContribution newContribution(String JavaDoc propertyName, String JavaDoc mode,
58             Object JavaDoc object)
59     {
60         return newContribution(propertyName, mode, object, null);
61     }
62
63     private InfrastructureContribution newContribution(String JavaDoc propertyName, String JavaDoc mode,
64             Object JavaDoc object, Location location)
65     {
66         DeferredObject deferred = new DeferredObjectFixture(object, location);
67
68         InfrastructureContribution ic = new InfrastructureContribution();
69         ic.setDeferredObject(deferred);
70         ic.setProperty(propertyName);
71         ic.setMode(mode);
72         ic.setLocation(location);
73
74         return ic;
75     }
76
77     public void testGetPropertyUninitialized()
78     {
79         InfrastructureImpl infra = new InfrastructureImpl();
80
81         try
82         {
83             infra.getProperty("foo");
84             unreachable();
85         }
86         catch (IllegalStateException JavaDoc ex)
87         {
88             assertEquals(ImplMessages.infrastructureNotInitialized(), ex.getMessage());
89         }
90     }
91
92     public void testGetNullProperty()
93     {
94         InfrastructureImpl infra = new InfrastructureImpl();
95
96         infra.setNormalContributions(Collections.EMPTY_LIST);
97         infra.setOverrideContributions(Collections.EMPTY_LIST);
98
99         infra.initialize("test");
100
101         try
102         {
103             infra.getProperty("fred");
104             unreachable();
105         }
106         catch (ApplicationRuntimeException ex)
107         {
108             assertEquals(ImplMessages.missingInfrastructureProperty("fred"), ex.getMessage());
109         }
110     }
111
112     public void testReinitalize()
113     {
114         InfrastructureImpl infra = new InfrastructureImpl();
115
116         infra.setNormalContributions(Collections.EMPTY_LIST);
117         infra.setOverrideContributions(Collections.EMPTY_LIST);
118
119         infra.initialize("ONE");
120
121         try
122         {
123             infra.initialize("TWO");
124             unreachable();
125         }
126         catch (IllegalStateException JavaDoc ex)
127         {
128             assertEquals(ImplMessages.infrastructureAlreadyInitialized("TWO", "ONE"), ex
129                     .getMessage());
130         }
131     }
132
133     /**
134      * Test that a contribution for a mode quietly overrides a contribution for the same property
135      * that does not specify a mode.
136      */

137
138     public void testModeOverridesNonMode()
139     {
140         Object JavaDoc fredModal = new Object JavaDoc();
141         Object JavaDoc plainFred = new Object JavaDoc();
142
143         InfrastructureImpl infra = new InfrastructureImpl();
144
145         List JavaDoc l = new ArrayList JavaDoc();
146         l.add(newContribution("fred", "bedrock", fredModal));
147         l.add(newContribution("fred", null, plainFred));
148
149         infra.setNormalContributions(l);
150         infra.setOverrideContributions(Collections.EMPTY_LIST);
151
152         infra.initialize("bedrock");
153
154         assertSame(fredModal, infra.getProperty("fred"));
155     }
156     
157     public void testWrongModeIgnored()
158     {
159         Object JavaDoc fredModal = new Object JavaDoc();
160         Object JavaDoc wrongFred = new Object JavaDoc();
161
162         InfrastructureImpl infra = new InfrastructureImpl();
163
164         List JavaDoc l = new ArrayList JavaDoc();
165         l.add(newContribution("fred", "bedrock", fredModal));
166         l.add(newContribution("fred", "shale", wrongFred));
167
168         infra.setNormalContributions(l);
169         infra.setOverrideContributions(Collections.EMPTY_LIST);
170
171         infra.initialize("bedrock");
172
173         assertSame(fredModal, infra.getProperty("fred"));
174     }
175
176     /**
177      * Test that override contributions trump contributions from the normal path.
178      */

179
180     public void testOverrides()
181     {
182         Object JavaDoc normalFred = new Object JavaDoc();
183         Object JavaDoc overrideFred = new Object JavaDoc();
184
185         InfrastructureImpl infra = new InfrastructureImpl();
186
187         infra.setNormalContributions(Collections.singletonList(newContribution(
188                 "fred",
189                 null,
190                 normalFred)));
191         infra.setOverrideContributions(Collections.singletonList(newContribution(
192                 "fred",
193                 null,
194                 overrideFred)));
195
196         infra.initialize("bedrock");
197
198         assertSame(overrideFred, infra.getProperty("fred"));
199     }
200
201     public void testDuplicate()
202     {
203         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
204
205         Location l1 = fabricateLocation(99);
206         Location l2 = fabricateLocation(132);
207
208         Object JavaDoc fredModal = new Object JavaDoc();
209         Object JavaDoc duplicateFred = new Object JavaDoc();
210
211         List JavaDoc l = new ArrayList JavaDoc();
212         l.add(newContribution("fred", "bedrock", fredModal, l1));
213         InfrastructureContribution conflict = newContribution("fred", "bedrock", duplicateFred, l2);
214         l.add(conflict);
215
216         log.error(ImplMessages.duplicateInfrastructureContribution(conflict, l1), l2, null);
217
218         replayControls();
219
220         InfrastructureImpl infra = new InfrastructureImpl();
221         infra.setNormalContributions(l);
222         infra.setOverrideContributions(Collections.EMPTY_LIST);
223         infra.setErrorLog(log);
224
225         infra.initialize("bedrock");
226
227         assertSame(fredModal, infra.getProperty("fred"));
228
229         verifyControls();
230     }
231 }
Popular Tags