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 static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.isA;
019:
020: import org.apache.tapestry.annotations.Inject;
021: import org.apache.tapestry.internal.test.InternalBaseTestCase;
022: import org.apache.tapestry.ioc.AnnotationProvider;
023: import org.apache.tapestry.ioc.ObjectProvider;
024: import org.apache.tapestry.ioc.ObjectLocator;
025: import org.apache.tapestry.model.MutableComponentModel;
026: import org.apache.tapestry.services.ClassTransformation;
027: import org.apache.tapestry.services.Request;
028: import org.testng.annotations.Test;
029:
030: public class InjectWorkerTest extends InternalBaseTestCase {
031: private static final String WEBREQUEST_CLASS_NAME = Request.class
032: .getName();
033:
034: @Test
035: public void annotation_has_value() {
036: ObjectProvider provider = mockObjectProvider();
037: ObjectLocator locator = mockObjectLocator();
038: Inject annotation = newMock(Inject.class);
039: ClassTransformation ct = mockClassTransformation();
040: MutableComponentModel model = mockMutableComponentModel();
041: Request injected = mockRequest();
042:
043: train_findFieldsWithAnnotation(ct, Inject.class, "myfield");
044: train_getFieldAnnotation(ct, "myfield", Inject.class,
045: annotation);
046:
047: train_getFieldType(ct, "myfield", WEBREQUEST_CLASS_NAME);
048: train_toClass(ct, WEBREQUEST_CLASS_NAME, Request.class);
049:
050: expect(
051: provider.provide(eq(Request.class),
052: isA(AnnotationProvider.class), eq(locator)))
053: .andReturn(injected);
054:
055: ct.injectField("myfield", injected);
056:
057: ct.claimField("myfield", annotation);
058:
059: replay();
060:
061: InjectWorker worker = new InjectWorker(provider, locator);
062:
063: worker.transform(ct, model);
064:
065: verify();
066: }
067:
068: @Test
069: public void provide_object_fails() {
070: ObjectProvider provider = mockObjectProvider();
071: ObjectLocator locator = mockObjectLocator();
072: Inject annotation = newMock(Inject.class);
073: ClassTransformation ct = mockClassTransformation();
074: MutableComponentModel model = mockMutableComponentModel();
075: Throwable cause = new RuntimeException("Injection failed.");
076:
077: train_findFieldsWithAnnotation(ct, Inject.class, "myfield");
078: train_getFieldAnnotation(ct, "myfield", Inject.class,
079: annotation);
080:
081: train_getFieldType(ct, "myfield", WEBREQUEST_CLASS_NAME);
082: train_toClass(ct, WEBREQUEST_CLASS_NAME, Request.class);
083:
084: expect(
085: provider.provide(eq(Request.class),
086: isA(AnnotationProvider.class), eq(locator)))
087: .andThrow(cause);
088: train_getClassName(ct, "foo.pages.Bar");
089:
090: replay();
091:
092: InjectWorker worker = new InjectWorker(provider, locator);
093:
094: try {
095: worker.transform(ct, model);
096: unreachable();
097: } catch (RuntimeException ex) {
098: assertEquals(
099: ex.getMessage(),
100: "Error obtaining injected value for field foo.pages.Bar.myfield: Injection failed.");
101: assertSame(ex.getCause(), cause);
102: }
103:
104: verify();
105: }
106: }
|