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 org.apache.tapestry.annotations.Inject;
18: import org.apache.tapestry.internal.test.InternalBaseTestCase;
19: import org.apache.tapestry.ioc.ObjectLocator;
20: import org.apache.tapestry.model.MutableComponentModel;
21: import org.apache.tapestry.services.ClassTransformation;
22: import org.apache.tapestry.services.ComponentClassTransformWorker;
23: import org.apache.tapestry.services.InjectionProvider;
24: import org.apache.tapestry.services.Request;
25: import org.testng.annotations.Test;
26:
27: public class InjectResourcesWorkerTest extends InternalBaseTestCase {
28: private static final String WEBREQUEST_CLASS_NAME = Request.class
29: .getName();
30:
31: @Test
32: public void anonymous_injection() {
33: ObjectLocator locator = mockObjectLocator();
34: InjectionProvider ip = newMock(InjectionProvider.class);
35: Inject annotation = newMock(Inject.class);
36: ClassTransformation ct = mockClassTransformation();
37: MutableComponentModel model = mockMutableComponentModel();
38:
39: train_findFieldsWithAnnotation(ct, Inject.class, "myfield");
40: train_getFieldAnnotation(ct, "myfield", Inject.class,
41: annotation);
42:
43: train_getFieldType(ct, "myfield", WEBREQUEST_CLASS_NAME);
44:
45: train_provideInjection(ip, "myfield", WEBREQUEST_CLASS_NAME,
46: locator, ct, model, true);
47:
48: ct.claimField("myfield", annotation);
49:
50: replay();
51:
52: ComponentClassTransformWorker worker = new InjectResourcesWorker(
53: locator, ip);
54:
55: worker.transform(ct, model);
56:
57: verify();
58: }
59:
60: @Test
61: public void anonymous_injection_not_provided() {
62: ObjectLocator locator = mockObjectLocator();
63: InjectionProvider ip = newMock(InjectionProvider.class);
64: Inject annotation = newMock(Inject.class);
65: ClassTransformation ct = mockClassTransformation();
66: MutableComponentModel model = mockMutableComponentModel();
67:
68: train_findFieldsWithAnnotation(ct, Inject.class, "myfield");
69: train_getFieldAnnotation(ct, "myfield", Inject.class,
70: annotation);
71:
72: train_getFieldType(ct, "myfield", WEBREQUEST_CLASS_NAME);
73:
74: train_provideInjection(ip, "myfield", WEBREQUEST_CLASS_NAME,
75: locator, ct, model, false);
76:
77: replay();
78:
79: ComponentClassTransformWorker worker = new InjectResourcesWorker(
80: locator, ip);
81:
82: // Does the work but doesn't claim the field, since there was no match.
83:
84: worker.transform(ct, model);
85:
86: verify();
87: }
88: }
|