01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import static org.apache.tapestry.services.TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE;
18: import static org.apache.tapestry.services.TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE;
19:
20: import java.lang.reflect.Modifier;
21:
22: import org.apache.tapestry.internal.test.InternalBaseTestCase;
23: import org.apache.tapestry.model.MutableComponentModel;
24: import org.apache.tapestry.services.ClassTransformation;
25: import org.testng.annotations.Test;
26:
27: public class UnclaimedFieldWorkerTest extends InternalBaseTestCase {
28: @Test
29: public void no_fields() {
30: ClassTransformation ct = mockClassTransformation();
31: MutableComponentModel model = mockMutableComponentModel();
32:
33: train_findUnclaimedFields(ct);
34:
35: replay();
36:
37: new UnclaimedFieldWorker().transform(ct, model);
38:
39: verify();
40: }
41:
42: @Test
43: public void normal() {
44: ClassTransformation ct = mockClassTransformation();
45: MutableComponentModel model = mockMutableComponentModel();
46:
47: train_findUnclaimedFields(ct, "_fred");
48:
49: train_getFieldModifiers(ct, "_fred", Modifier.PRIVATE);
50:
51: train_getFieldType(ct, "_fred", "foo.Bar");
52:
53: expect(
54: ct.addField(Modifier.PRIVATE, "foo.Bar",
55: "_fred_default")).andReturn("_$fred_default");
56:
57: ct.extendMethod(CONTAINING_PAGE_DID_LOAD_SIGNATURE,
58: "_$fred_default = _fred;");
59: ct.extendMethod(CONTAINING_PAGE_DID_DETACH_SIGNATURE,
60: "_fred = _$fred_default;");
61:
62: replay();
63:
64: new UnclaimedFieldWorker().transform(ct, model);
65:
66: verify();
67: }
68:
69: @Test
70: public void final_fields_are_skipped() {
71: ClassTransformation ct = mockClassTransformation();
72: MutableComponentModel model = mockMutableComponentModel();
73:
74: train_findUnclaimedFields(ct, "_fred");
75:
76: train_getFieldModifiers(ct, "_fred", Modifier.PRIVATE
77: | Modifier.FINAL);
78:
79: replay();
80:
81: new UnclaimedFieldWorker().transform(ct, model);
82:
83: verify();
84: }
85:
86: protected final void train_getFieldModifiers(
87: ClassTransformation transformation, String fieldName,
88: int modifiers) {
89: expect(transformation.getFieldModifiers(fieldName)).andReturn(
90: modifiers).atLeastOnce();
91: }
92: }
|