KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > TestInjectObjectWorker


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.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.service.MethodSignature;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.engine.HomeService;
24 import org.apache.tapestry.engine.IEngineService;
25 import org.apache.tapestry.services.InjectedValueProvider;
26 import org.apache.tapestry.spec.InjectSpecification;
27 import org.apache.tapestry.spec.InjectSpecificationImpl;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.enhance.InjectObjectWorker}.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class TestInjectObjectWorker extends HiveMindTestCase
37 {
38     private InjectSpecification newSpec(String JavaDoc name, String JavaDoc locator, Location location)
39     {
40         InjectSpecification is = new InjectSpecificationImpl();
41
42         is.setProperty(name);
43         is.setObject(locator);
44         is.setLocation(location);
45
46         return is;
47     }
48
49     public void testNoExistingProperty()
50     {
51         Location l = newLocation();
52         Object JavaDoc injectedValue = new Object JavaDoc();
53
54         InjectSpecification spec = newSpec("fred", "service:barney", l);
55
56         MockControl opc = newControl(EnhancementOperation.class);
57         EnhancementOperation op = (EnhancementOperation) opc.getMock();
58
59         MockControl pc = newControl(InjectedValueProvider.class);
60         InjectedValueProvider p = (InjectedValueProvider) pc.getMock();
61
62         op.getPropertyType("fred");
63         opc.setReturnValue(null);
64
65         op.claimProperty("fred");
66
67         p.obtainValue("service:barney", l);
68
69         pc.setReturnValue(injectedValue);
70
71         // When the same bean is injected multiple times, the field name
72
// returned won't match the field name suggested; make sure the code
73
// generation used the correct field name.
74

75         op.addInjectedField("_$fred", Object JavaDoc.class, injectedValue);
76         opc.setReturnValue("_$gnarly");
77
78         op.addMethod(
79                 Modifier.PUBLIC,
80                 new MethodSignature(Object JavaDoc.class, "getFred", null, null),
81                 "return _$gnarly;");
82
83         replayControls();
84
85         InjectObjectWorker w = new InjectObjectWorker();
86         w.setProvider(p);
87
88         w.performEnhancement(op, spec);
89
90         verifyControls();
91     }
92
93     public void testWithExistingProperty()
94     {
95         Location l = newLocation();
96         Object JavaDoc injectedValue = new HomeService();
97
98         InjectSpecification spec = newSpec("wilma", "service:betty", l);
99
100         MockControl opc = newControl(EnhancementOperation.class);
101         EnhancementOperation op = (EnhancementOperation) opc.getMock();
102
103         MockControl pc = newControl(InjectedValueProvider.class);
104         InjectedValueProvider p = (InjectedValueProvider) pc.getMock();
105
106         op.getPropertyType("wilma");
107         opc.setReturnValue(IEngineService.class);
108
109         op.claimProperty("wilma");
110
111         p.obtainValue("service:betty", l);
112         pc.setReturnValue(injectedValue);
113
114         op.addInjectedField("_$wilma", IEngineService.class, injectedValue);
115         opc.setReturnValue("_$wilma");
116
117         op.addMethod(Modifier.PUBLIC, new MethodSignature(IEngineService.class, "getWilma", null,
118                 null), "return _$wilma;");
119
120         replayControls();
121
122         InjectObjectWorker w = new InjectObjectWorker();
123         w.setProvider(p);
124
125         w.performEnhancement(op, spec);
126
127         verifyControls();
128     }
129
130     /**
131      * Test what happens when the value to be injected is null (usually indicating an error in the
132      * locator and/or a configuration error inside the HiveMind descriptors).
133      */

134
135     public void testInjectNull()
136     {
137         Location l = newLocation();
138
139         InjectSpecification spec = newSpec("fred", "service:barney", l);
140
141         MockControl opc = newControl(EnhancementOperation.class);
142         EnhancementOperation op = (EnhancementOperation) opc.getMock();
143
144         MockControl pc = newControl(InjectedValueProvider.class);
145         InjectedValueProvider p = (InjectedValueProvider) pc.getMock();
146
147         op.getPropertyType("fred");
148         opc.setReturnValue(null);
149
150         op.claimProperty("fred");
151
152         p.obtainValue("service:barney", l);
153         pc.setReturnValue(null);
154
155         replayControls();
156
157         InjectObjectWorker w = new InjectObjectWorker();
158         w.setProvider(p);
159
160         try
161         {
162             w.performEnhancement(op, spec);
163             unreachable();
164         }
165         catch (ApplicationRuntimeException ex)
166         {
167             assertEquals("Value obtained using locator 'service:barney' is null.", ex.getMessage());
168             assertSame(l, ex.getLocation());
169         }
170
171         verifyControls();
172     }
173
174     public void testInjectedValueWrongType()
175     {
176         Location l = newLocation();
177
178         InjectSpecification spec = newSpec("fred", "service:barney", l);
179
180         MockControl opc = newControl(EnhancementOperation.class);
181         EnhancementOperation op = (EnhancementOperation) opc.getMock();
182
183         MockControl pc = newControl(InjectedValueProvider.class);
184         InjectedValueProvider p = (InjectedValueProvider) pc.getMock();
185
186         op.getPropertyType("fred");
187         opc.setReturnValue(IEngineService.class);
188
189         op.claimProperty("fred");
190
191         p.obtainValue("service:barney", l);
192         pc.setReturnValue("INJECTED-VALUE");
193
194         replayControls();
195
196         InjectObjectWorker w = new InjectObjectWorker();
197         w.setProvider(p);
198
199         try
200         {
201             w.performEnhancement(op, spec);
202             unreachable();
203
204         }
205         catch (ApplicationRuntimeException ex)
206         {
207             assertEquals(
208                     "The value obtained using locator 'service:barney' (INJECTED-VALUE) is not compatible with the existing property (of type org.apache.tapestry.engine.IEngineService).",
209                     ex.getMessage());
210             assertSame(l, ex.getLocation());
211         }
212
213         verifyControls();
214     }
215 }
Popular Tags