KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.service.MethodSignature;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.BaseComponent;
25 import org.apache.tapestry.spec.IComponentSpecification;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.enhance.InjectSpecificationWorker}.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

34 public class TestInjectSpecificationWorker extends HiveMindTestCase
35 {
36     private IComponentSpecification newSpec()
37     {
38         return (IComponentSpecification) newMock(IComponentSpecification.class);
39     }
40
41     public void testSuccess() throws Exception JavaDoc
42     {
43         MockControl control = newControl(EnhancementOperation.class);
44         EnhancementOperation op = (EnhancementOperation) control.getMock();
45
46         IComponentSpecification spec = newSpec();
47
48         op.claimProperty("specification");
49
50         op.addInjectedField("_$specification", IComponentSpecification.class, spec);
51         control.setReturnValue("_$specification");
52
53         op.getAccessorMethodName("specification");
54         control.setReturnValue("getSpecification");
55
56         op.addMethod(Modifier.PUBLIC, new MethodSignature(IComponentSpecification.class,
57                 "getSpecification", null, null), "return _$specification;");
58
59         replayControls();
60
61         new InjectSpecificationWorker().performEnhancement(op, spec);
62
63         verifyControls();
64     }
65
66     public void testFailure()
67     {
68         Location l = fabricateLocation(11);
69
70         MockControl control = newControl(EnhancementOperation.class);
71         EnhancementOperation op = (EnhancementOperation) control.getMock();
72
73         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
74
75         Throwable JavaDoc ex = new ApplicationRuntimeException(EnhanceMessages
76                 .claimedProperty("specification"));
77
78         MockControl specc = newControl(IComponentSpecification.class);
79         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
80
81         op.claimProperty("specification");
82         control.setThrowable(ex);
83
84         op.getBaseClass();
85         control.setReturnValue(BaseComponent.class);
86
87         spec.getLocation();
88         specc.setReturnValue(l);
89
90         log.error(
91                 EnhanceMessages.errorAddingProperty("specification", BaseComponent.class, ex),
92                 l,
93                 ex);
94
95         replayControls();
96
97         InjectSpecificationWorker w = new InjectSpecificationWorker();
98         w.setErrorLog(log);
99
100         w.performEnhancement(op, spec);
101
102         verifyControls();
103     }
104 }
Popular Tags