KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
18 import java.util.Collections JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ErrorLog;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.service.MethodSignature;
24 import org.apache.hivemind.test.HiveMindTestCase;
25 import org.apache.tapestry.BaseComponent;
26 import org.apache.tapestry.IComponent;
27 import org.apache.tapestry.spec.ContainedComponent;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.apache.tapestry.spec.IContainedComponent;
30 import org.easymock.MockControl;
31
32 /**
33  * Tests for {@link org.apache.tapestry.enhance.InjectComponentWorker}.
34  *
35  * @author Howard M. Lewis Ship
36  * @since 4.0
37  */

38 public class TestInjectComponentWorker extends HiveMindTestCase
39 {
40     private IComponentSpecification newSpec(String JavaDoc componentId, String JavaDoc propertyName,
41             Location location)
42     {
43         IContainedComponent cc = new ContainedComponent();
44         cc.setPropertyName(propertyName);
45         cc.setLocation(location);
46
47         MockControl control = newControl(IComponentSpecification.class);
48         IComponentSpecification spec = (IComponentSpecification) control.getMock();
49
50         spec.getComponentIds();
51         control.setReturnValue(Collections.singletonList(componentId));
52
53         spec.getComponent(componentId);
54         control.setReturnValue(cc);
55
56         return spec;
57     }
58
59     public void testNoWork()
60     {
61         IComponentSpecification spec = newSpec("fred", null, null);
62         EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class);
63
64         replayControls();
65
66         new InjectComponentWorker().performEnhancement(op, spec);
67
68         verifyControls();
69     }
70
71     public void testSuccess()
72     {
73         IComponentSpecification spec = newSpec("fred", "barney", null);
74         MockControl control = newControl(EnhancementOperation.class);
75         EnhancementOperation op = (EnhancementOperation) control.getMock();
76
77         op.getPropertyType("barney");
78         control.setReturnValue(IComponent.class);
79
80         op.claimProperty("barney");
81
82         op.addField("_$barney", IComponent.class);
83
84         op.getAccessorMethodName("barney");
85         control.setReturnValue("getBarney");
86
87         op.addMethod(
88                 Modifier.PUBLIC,
89                 new MethodSignature(IComponent.class, "getBarney", null, null),
90                 "return _$barney;");
91
92         op.extendMethodImplementation(
93                 IComponent.class,
94                 EnhanceUtils.FINISH_LOAD_SIGNATURE,
95                 "_$barney = (org.apache.tapestry.IComponent) getComponent(\"fred\");");
96
97         replayControls();
98
99         new InjectComponentWorker().performEnhancement(op, spec);
100
101         verifyControls();
102     }
103
104     public void testFailure()
105     {
106         Location l = fabricateLocation(99);
107         Throwable JavaDoc ex = new ApplicationRuntimeException(EnhanceMessages.claimedProperty("barney"));
108
109         MockControl control = newControl(EnhancementOperation.class);
110         EnhancementOperation op = (EnhancementOperation) control.getMock();
111
112         IComponentSpecification spec = newSpec("fred", "barney", l);
113
114         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
115
116         op.getPropertyType("barney");
117         control.setReturnValue(IComponent.class);
118
119         op.claimProperty("barney");
120         control.setThrowable(ex);
121
122         op.getBaseClass();
123         control.setReturnValue(BaseComponent.class);
124
125         log.error(EnhanceMessages.errorAddingProperty("barney", BaseComponent.class, ex), l, ex);
126
127         replayControls();
128
129         InjectComponentWorker w = new InjectComponentWorker();
130
131         w.setErrorLog(log);
132
133         w.performEnhancement(op, spec);
134
135         verifyControls();
136     }
137 }
Popular Tags