01: // Copyright 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.Path;
18: import org.apache.tapestry.internal.test.InternalBaseTestCase;
19: import org.apache.tapestry.ioc.ObjectLocator;
20: import org.apache.tapestry.ioc.services.SymbolSource;
21: import org.apache.tapestry.model.MutableComponentModel;
22: import org.apache.tapestry.services.AssetSource;
23: import org.apache.tapestry.services.ClassTransformation;
24: import org.apache.tapestry.services.InjectionProvider;
25: import org.testng.annotations.Test;
26:
27: public class AssetInjectionProviderTest extends InternalBaseTestCase {
28: @Test
29: public void no_path_annotation() {
30: SymbolSource symbolSource = mockSymbolSource();
31: AssetSource assetSource = mockAssetSource();
32: ObjectLocator locator = mockObjectLocator();
33: ClassTransformation ct = mockClassTransformation();
34: MutableComponentModel model = mockMutableComponentModel();
35:
36: String fieldName = "myField";
37: String fieldType = "java.lang.String";
38:
39: train_getFieldAnnotation(ct, fieldName, Path.class, null);
40:
41: replay();
42:
43: InjectionProvider provider = new AssetInjectionProvider(
44: symbolSource, assetSource);
45:
46: assertFalse(provider.provideInjection(fieldName, fieldType,
47: locator, ct, model));
48:
49: verify();
50: }
51:
52: @Test
53: public void path_annotation_present() {
54: SymbolSource symbolSource = mockSymbolSource();
55: AssetSource assetSource = mockAssetSource();
56: ObjectLocator locator = mockObjectLocator();
57: ClassTransformation ct = mockClassTransformation();
58: MutableComponentModel model = mockMutableComponentModel();
59: Path annotation = mockPath();
60:
61: String fieldName = "myField";
62: String fieldType = "java.lang.Object";
63: String value = "${foo}";
64: String expanded = "foo.gif";
65:
66: train_getFieldAnnotation(ct, fieldName, Path.class, annotation);
67:
68: train_value(annotation, value);
69: train_expandSymbols(symbolSource, value, expanded);
70:
71: train_addInjectedField(ct, AssetSource.class, "assetSource",
72: assetSource, "as");
73: train_getResourcesFieldName(ct, "rez");
74:
75: // This only tests that the code is generated as expected (which is a bit brittle), it
76: // doesn't prove that the generated code actually works, but we have lots of integration
77: // tests for that.
78:
79: ct
80: .extendConstructor("myField = (java.lang.Object) as.findAsset(rez.getBaseResource(), \"foo.gif\", rez.getLocale());");
81:
82: ct.makeReadOnly(fieldName);
83:
84: replay();
85:
86: InjectionProvider provider = new AssetInjectionProvider(
87: symbolSource, assetSource);
88:
89: assertTrue(provider.provideInjection(fieldName, fieldType,
90: locator, ct, model));
91:
92: verify();
93: }
94: }
|