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 java.util.Collections;
018: import java.util.Locale;
019: import java.util.Map;
020:
021: import org.apache.tapestry.Asset;
022: import org.apache.tapestry.internal.test.InternalBaseTestCase;
023: import org.apache.tapestry.ioc.Resource;
024: import org.apache.tapestry.ioc.internal.util.ClasspathResource;
025: import org.apache.tapestry.ioc.services.ThreadLocale;
026: import org.apache.tapestry.services.AssetFactory;
027: import org.apache.tapestry.services.AssetSource;
028: import org.testng.annotations.Test;
029:
030: public class AssetSourceImplTest extends InternalBaseTestCase {
031: private final Resource _baseResource = new ClasspathResource(
032: "org/apache/tapestry/internal/services/SimpleComponent.class");
033:
034: private final Resource _rootResource = new ClasspathResource("/");
035:
036: @Test
037: public void relative_asset() {
038: AssetFactory factory = mockAssetFactory();
039: ThreadLocale threadLocale = mockThreadLocale();
040: Asset asset = mockAsset();
041:
042: Resource expectedResource = _baseResource
043: .forFile("SimpleComponent_en_GB.properties");
044:
045: train_getRootResource(factory, _rootResource);
046:
047: train_createAsset(factory, expectedResource, asset);
048:
049: Map<String, AssetFactory> configuration = Collections
050: .singletonMap("classpath", factory);
051:
052: replay();
053:
054: AssetSource source = new AssetSourceImpl(threadLocale,
055: configuration);
056:
057: // First try creates it:
058:
059: assertSame(source.findAsset(_baseResource,
060: "SimpleComponent.properties", Locale.UK), asset);
061:
062: // Second try shows that it is cached
063:
064: assertSame(source.findAsset(_baseResource,
065: "SimpleComponent.properties", Locale.UK), asset);
066:
067: verify();
068: }
069:
070: @Test
071: public void get_classpath_asset() {
072: AssetFactory factory = mockAssetFactory();
073: ThreadLocale threadLocale = mockThreadLocale();
074: Asset asset = mockAsset();
075:
076: Resource expectedResource = _baseResource
077: .forFile("SimpleComponent_en_GB.properties");
078:
079: train_getRootResource(factory, _rootResource);
080:
081: train_createAsset(factory, expectedResource, asset);
082:
083: Map<String, AssetFactory> configuration = Collections
084: .singletonMap("classpath", factory);
085:
086: replay();
087:
088: AssetSource source = new AssetSourceImpl(threadLocale,
089: configuration);
090:
091: // First try creates it:
092:
093: assertSame(
094: source
095: .getClasspathAsset(
096: "org/apache/tapestry/internal/services/SimpleComponent.properties",
097: Locale.UK), asset);
098:
099: verify();
100: }
101:
102: @Test
103: public void get_classpath_asset_for_unspecified_locale() {
104: AssetFactory factory = mockAssetFactory();
105: ThreadLocale threadLocale = mockThreadLocale();
106: Asset asset = mockAsset();
107: Locale locale = Locale.UK;
108:
109: Resource expectedResource = _baseResource
110: .forFile("SimpleComponent_en_GB.properties");
111:
112: train_getRootResource(factory, _rootResource);
113:
114: train_createAsset(factory, expectedResource, asset);
115:
116: Map<String, AssetFactory> configuration = Collections
117: .singletonMap("classpath", factory);
118:
119: train_getLocale(threadLocale, locale);
120:
121: replay();
122:
123: AssetSource source = new AssetSourceImpl(threadLocale,
124: configuration);
125:
126: assertSame(
127: source
128: .getClasspathAsset("org/apache/tapestry/internal/services/SimpleComponent.properties"),
129: asset);
130:
131: verify();
132: }
133:
134: @Test
135: public void absolute_asset_with_known_prefix() {
136: AssetFactory factory = mockAssetFactory();
137: ThreadLocale threadLocale = mockThreadLocale();
138: Asset asset = mockAsset();
139:
140: Resource expectedResource = _rootResource
141: .forFile("org/apache/tapestry/internal/services/SimpleComponent_en_GB.properties");
142:
143: train_getRootResource(factory, _rootResource);
144:
145: train_createAsset(factory, expectedResource, asset);
146:
147: Map<String, AssetFactory> configuration = Collections
148: .singletonMap("classpath", factory);
149:
150: replay();
151:
152: AssetSource source = new AssetSourceImpl(threadLocale,
153: configuration);
154:
155: assertSame(
156: source
157: .findAsset(
158: _baseResource,
159: "classpath:org/apache/tapestry/internal/services/SimpleComponent.properties",
160: Locale.UK), asset);
161:
162: // Check that a leading slash is not a problem:
163:
164: assertSame(
165: source
166: .findAsset(
167: _baseResource,
168: "classpath:/org/apache/tapestry/internal/services/SimpleComponent.properties",
169: Locale.UK), asset);
170:
171: verify();
172: }
173:
174: @Test
175: public void unknown_asset_prefix() {
176: ThreadLocale threadLocale = mockThreadLocale();
177:
178: Map<String, AssetFactory> configuration = Collections
179: .emptyMap();
180:
181: replay();
182:
183: AssetSource source = new AssetSourceImpl(threadLocale,
184: configuration);
185:
186: try {
187: source
188: .findAsset(
189: _baseResource,
190: "classpath:org/apache/tapestry/internal/services/SimpleComponent.properties",
191: Locale.UK);
192: unreachable();
193: } catch (IllegalArgumentException ex) {
194: assertEquals(
195: ex.getMessage(),
196: "Unknown prefix for asset path 'classpath:org/apache/tapestry/internal/services/SimpleComponent.properties'.");
197: }
198:
199: verify();
200: }
201:
202: @Test
203: public void missing_resource() {
204: ThreadLocale threadLocale = mockThreadLocale();
205:
206: Map<String, AssetFactory> configuration = Collections
207: .emptyMap();
208:
209: replay();
210:
211: AssetSource source = new AssetSourceImpl(threadLocale,
212: configuration);
213:
214: try {
215: source.findAsset(_baseResource, "DoesNotExist.properties",
216: Locale.UK);
217: unreachable();
218: } catch (RuntimeException ex) {
219: assertEquals(
220: ex.getMessage(),
221: "Unable to locate asset 'classpath:org/apache/tapestry/internal/services/DoesNotExist.properties' (the file does not exist).");
222: }
223:
224: verify();
225: }
226:
227: }
|