001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import org.apache.tapestry.annotations.Mixin;
018: import org.apache.tapestry.internal.test.InternalBaseTestCase;
019: import org.apache.tapestry.model.MutableComponentModel;
020: import org.apache.tapestry.services.ClassTransformation;
021: import org.apache.tapestry.services.ComponentClassResolver;
022: import org.apache.tapestry.services.TransformConstants;
023: import org.testng.annotations.Test;
024:
025: public class MixinWorkerTest extends InternalBaseTestCase {
026: @Test
027: public void no_fields_with_mixin_annotation() {
028: ComponentClassResolver resolver = mockComponentClassResolver();
029: ClassTransformation transformation = mockClassTransformation();
030: MutableComponentModel model = mockMutableComponentModel();
031:
032: train_findFieldsWithAnnotation(transformation, Mixin.class);
033:
034: replay();
035:
036: new MixinWorker(resolver).transform(transformation, model);
037:
038: verify();
039: }
040:
041: @Test
042: public void field_with_explicit_type() {
043: ComponentClassResolver resolver = mockComponentClassResolver();
044: ClassTransformation transformation = mockClassTransformation();
045: MutableComponentModel model = mockMutableComponentModel();
046: Mixin annotation = newMixin("Bar");
047:
048: train_findFieldsWithAnnotation(transformation, Mixin.class,
049: "fred");
050: train_getFieldAnnotation(transformation, "fred", Mixin.class,
051: annotation);
052: train_getFieldType(transformation, "fred", "foo.bar.Baz");
053:
054: train_resolveMixinTypeToClassName(resolver, "Bar",
055: "foo.bar.BazMixin");
056:
057: model.addMixinClassName("foo.bar.BazMixin");
058:
059: transformation.makeReadOnly("fred");
060:
061: train_getResourcesFieldName(transformation, "rez");
062:
063: train_extendMethod(transformation,
064: TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE,
065: "fred = (foo.bar.Baz) rez.getMixinByClassName(\"foo.bar.BazMixin\");");
066:
067: transformation.claimField("fred", annotation);
068:
069: replay();
070:
071: new MixinWorker(resolver).transform(transformation, model);
072:
073: verify();
074: }
075:
076: @Test
077: public void field_with_no_specific_mixin_type() {
078: ComponentClassResolver resolver = mockComponentClassResolver();
079: ClassTransformation transformation = mockClassTransformation();
080: MutableComponentModel model = mockMutableComponentModel();
081: Mixin annotation = newMixin("");
082:
083: train_findFieldsWithAnnotation(transformation, Mixin.class,
084: "fred");
085: train_getFieldAnnotation(transformation, "fred", Mixin.class,
086: annotation);
087: train_getFieldType(transformation, "fred", "foo.bar.Baz");
088:
089: model.addMixinClassName("foo.bar.Baz");
090:
091: transformation.makeReadOnly("fred");
092:
093: train_getResourcesFieldName(transformation, "rez");
094:
095: train_extendMethod(transformation,
096: TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE,
097: "fred = (foo.bar.Baz) rez.getMixinByClassName(\"foo.bar.Baz\");");
098:
099: transformation.claimField("fred", annotation);
100:
101: replay();
102:
103: new MixinWorker(resolver).transform(transformation, model);
104:
105: verify();
106:
107: }
108:
109: protected final void train_resolveMixinTypeToClassName(
110: ComponentClassResolver resolver, String mixinType,
111: String mixinClassName) {
112: expect(resolver.resolveMixinTypeToClassName(mixinType))
113: .andReturn(mixinClassName);
114: }
115:
116: private Mixin newMixin(String value) {
117: Mixin annotation = newMock(Mixin.class);
118:
119: expect(annotation.value()).andReturn(value);
120:
121: return annotation;
122: }
123: }
|