KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.event.PageDetachListener;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests {@link org.apache.tapestry.enhance.AbstractPropertyWorker}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestAbstractPropertyWorker extends HiveMindTestCase
38 {
39     public void testSuccess()
40     {
41         MockControl opc = newControl(EnhancementOperation.class);
42         EnhancementOperation op = (EnhancementOperation) opc.getMock();
43
44         op.findUnclaimedAbstractProperties();
45         opc.setReturnValue(Collections.singletonList("fred"));
46
47         op.getPropertyType("fred");
48         opc.setReturnValue(String JavaDoc.class);
49
50         op.addField("_$fred", String JavaDoc.class);
51         op.addField("_$fred$defaultValue", String JavaDoc.class);
52
53         op.getAccessorMethodName("fred");
54         opc.setReturnValue("getFred");
55
56         op.addMethod(
57                 Modifier.PUBLIC,
58                 new MethodSignature(String JavaDoc.class, "getFred", null, null),
59                 "return _$fred;");
60
61         op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class JavaDoc[]
62         { String JavaDoc.class }, null), "_$fred = $1;");
63
64         op.extendMethodImplementation(
65                 IComponent.class,
66                 EnhanceUtils.FINISH_LOAD_SIGNATURE,
67                 "_$fred$defaultValue = _$fred;");
68
69         op.extendMethodImplementation(
70                 PageDetachListener.class,
71                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
72                 "_$fred = _$fred$defaultValue;");
73
74         op.claimProperty("fred");
75
76         replayControls();
77
78         new AbstractPropertyWorker().performEnhancement(op, null);
79
80         verifyControls();
81     }
82
83     public void testFailure()
84     {
85         MockControl opc = newControl(EnhancementOperation.class);
86         EnhancementOperation op = (EnhancementOperation) opc.getMock();
87
88         MockControl specc = newControl(IComponentSpecification.class);
89         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
90
91         Location l = fabricateLocation(21);
92
93         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
94
95         op.findUnclaimedAbstractProperties();
96         opc.setReturnValue(Collections.singletonList("fred"));
97
98         Throwable JavaDoc ex = new ApplicationRuntimeException("Arbitrary error.");
99
100         op.getPropertyType("fred");
101         opc.setThrowable(ex);
102
103         op.getBaseClass();
104         opc.setReturnValue(BaseComponent.class);
105
106         spec.getLocation();
107         specc.setReturnValue(l);
108
109         log.error(EnhanceMessages.errorAddingProperty("fred", BaseComponent.class, ex), l, ex);
110
111         replayControls();
112
113         AbstractPropertyWorker w = new AbstractPropertyWorker();
114         w.setErrorLog(log);
115
116         w.performEnhancement(op, spec);
117
118         verifyControls();
119     }
120 }
Popular Tags