KickJava   Java API By Example, From Geeks To Geeks.

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


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.enhance;
16
17 import java.util.Collections JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.html.BasePage;
24 import org.apache.tapestry.spec.IComponentSpecification;
25 import org.apache.tapestry.spec.InjectSpecification;
26 import org.apache.tapestry.spec.InjectSpecificationImpl;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.enhance.DispatchToInjectWorker}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestDispatchToInjectWorker extends HiveMindTestCase
36 {
37     private InjectSpecification newInjectSpecification(String JavaDoc propertyName, String JavaDoc type,
38             String JavaDoc object)
39     {
40         return newInjectSpecification(propertyName, type, object, null);
41     }
42
43     private InjectSpecification newInjectSpecification(String JavaDoc propertyName, String JavaDoc type,
44             String JavaDoc object, Location location)
45     {
46         InjectSpecification result = new InjectSpecificationImpl();
47         result.setProperty(propertyName);
48         result.setType(type);
49         result.setObject(object);
50         result.setLocation(location);
51
52         return result;
53     }
54
55     private IComponentSpecification newSpec(InjectSpecification injectSpec)
56     {
57         MockControl control = newControl(IComponentSpecification.class);
58         IComponentSpecification spec = (IComponentSpecification) control.getMock();
59
60         spec.getInjectSpecifications();
61         control.setReturnValue(Collections.singletonList(injectSpec));
62
63         return spec;
64     }
65
66     private Map JavaDoc newMap(String JavaDoc key, Object JavaDoc value)
67     {
68         return Collections.singletonMap(key, value);
69     }
70
71     public void testSuccess()
72     {
73         EnhancementOperation op = newOp();
74         InjectSpecification is = newInjectSpecification("property", "object", "service:Foo");
75         InjectEnhancementWorker worker = newWorker();
76         Map JavaDoc map = newMap("object", worker);
77         IComponentSpecification spec = newSpec(is);
78
79         worker.performEnhancement(op, is);
80
81         replayControls();
82
83         DispatchToInjectWorker d = new DispatchToInjectWorker();
84         d.setInjectWorkers(map);
85
86         d.performEnhancement(op, spec);
87
88         verifyControls();
89     }
90
91     public void testUnknownType()
92     {
93         Location l = newLocation();
94         EnhancementOperation op = newOp();
95         InjectSpecification is = newInjectSpecification(
96                 "injectedProperty",
97                 "object",
98                 "service:Foo",
99                 l);
100         IComponentSpecification spec = newSpec(is);
101         ErrorLog log = newLog();
102
103         log.error(EnhanceMessages.unknownInjectType("injectedProperty", "object"), l, null);
104
105         replayControls();
106
107         DispatchToInjectWorker d = new DispatchToInjectWorker();
108         d.setInjectWorkers(Collections.EMPTY_MAP);
109         d.setErrorLog(log);
110
111         d.performEnhancement(op, spec);
112
113         verifyControls();
114     }
115
116     public void testFailure()
117     {
118         Location l = newLocation();
119         MockControl opc = newControl(EnhancementOperation.class);
120         EnhancementOperation op = (EnhancementOperation) opc.getMock();
121         InjectSpecification is = newInjectSpecification("myProperty", "object", "service:Foo", l);
122         MockControl workerc = newControl(InjectEnhancementWorker.class);
123         InjectEnhancementWorker worker = (InjectEnhancementWorker) workerc.getMock();
124         Map JavaDoc map = newMap("object", worker);
125         IComponentSpecification spec = newSpec(is);
126         Throwable JavaDoc t = new RuntimeException JavaDoc("Simulated failure.");
127         ErrorLog log = newLog();
128
129         worker.performEnhancement(op, is);
130         workerc.setThrowable(t);
131
132         op.getBaseClass();
133         opc.setReturnValue(BasePage.class);
134
135         log
136                 .error(
137                         "Error adding property myProperty to class org.apache.tapestry.html.BasePage: Simulated failure.",
138                         l,
139                         t);
140
141         replayControls();
142
143         DispatchToInjectWorker d = new DispatchToInjectWorker();
144         d.setInjectWorkers(map);
145         d.setErrorLog(log);
146
147         d.performEnhancement(op, spec);
148
149         verifyControls();
150     }
151
152     private InjectEnhancementWorker newWorker()
153     {
154         return (InjectEnhancementWorker) newMock(InjectEnhancementWorker.class);
155     }
156
157     private ErrorLog newLog()
158     {
159         return (ErrorLog) newMock(ErrorLog.class);
160     }
161
162     private EnhancementOperation newOp()
163     {
164         return (EnhancementOperation) newMock(EnhancementOperation.class);
165     }
166 }
167
Popular Tags