001: // Copyright 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.Id;
018: import org.apache.tapestry.annotations.Inject;
019: import org.apache.tapestry.model.MutableComponentModel;
020: import org.apache.tapestry.services.ClassTransformation;
021: import org.apache.tapestry.services.TransformConstants;
022: import org.apache.tapestry.test.TapestryTestCase;
023: import org.testng.annotations.Test;
024:
025: /**
026: * Tests a couple of edge cases where there's nothing to inject.
027: */
028: public class InjectBlockWorkerTest extends TapestryTestCase {
029: @Test
030: public void no_fields_of_type_block() {
031: ClassTransformation ct = mockClassTransformation();
032: MutableComponentModel model = mockMutableComponentModel();
033:
034: train_findFieldsOfType(ct, InjectBlockWorker.BLOCK_TYPE_NAME);
035:
036: replay();
037:
038: new InjectBlockWorker().transform(ct, model);
039:
040: verify();
041: }
042:
043: @Test
044: public void field_missing_annotation() {
045: ClassTransformation ct = mockClassTransformation();
046: MutableComponentModel model = mockMutableComponentModel();
047:
048: train_findFieldsOfType(ct, InjectBlockWorker.BLOCK_TYPE_NAME,
049: "fred");
050:
051: train_getResourcesFieldName(ct, "rez");
052:
053: train_getFieldAnnotation(ct, "fred", Inject.class, null);
054:
055: replay();
056:
057: new InjectBlockWorker().transform(ct, model);
058:
059: verify();
060: }
061:
062: protected final Id newId() {
063: return newMock(Id.class);
064: }
065:
066: /**
067: * This doesn't prove anything; later there will be integration tests that prove that the
068: * generated code is valid and works.
069: */
070: @Test
071: public void fields_with_annotations() {
072: ClassTransformation ct = mockClassTransformation();
073: MutableComponentModel model = mockMutableComponentModel();
074: Inject fredAnnotation = mockInject();
075: Inject barneyAnnotation = mockInject();
076: Id barneyId = newId();
077:
078: String barneyFieldName = "_barneyBlock";
079: String fredFieldName = "fred";
080:
081: train_findFieldsOfType(ct, InjectBlockWorker.BLOCK_TYPE_NAME,
082: fredFieldName, barneyFieldName);
083:
084: train_getResourcesFieldName(ct, "rez");
085:
086: train_getFieldAnnotation(ct, fredFieldName, Inject.class,
087: fredAnnotation);
088:
089: train_getFieldAnnotation(ct, fredFieldName, Id.class, null);
090:
091: ct.makeReadOnly(fredFieldName);
092: ct.claimField(fredFieldName, fredAnnotation);
093:
094: train_getFieldAnnotation(ct, barneyFieldName, Inject.class,
095: barneyAnnotation);
096: train_getFieldAnnotation(ct, barneyFieldName, Id.class,
097: barneyId);
098:
099: train_value(barneyId, "barney");
100:
101: ct.makeReadOnly(barneyFieldName);
102: ct.claimField(barneyFieldName, barneyAnnotation);
103:
104: train_extendMethod(ct,
105: TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE,
106: "{", "fred = rez.getBlock(\"fred\");",
107: "_barneyBlock = rez.getBlock(\"barney\");", "}");
108:
109: replay();
110:
111: new InjectBlockWorker().transform(ct, model);
112:
113: verify();
114: }
115:
116: }
|