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 org.apache.tapestry.Asset;
018: import org.apache.tapestry.internal.test.InternalBaseTestCase;
019: import org.apache.tapestry.ioc.Resource;
020: import org.apache.tapestry.ioc.internal.util.ClasspathResource;
021: import org.apache.tapestry.services.AssetFactory;
022: import org.apache.tapestry.services.ClasspathAssetAliasManager;
023: import org.testng.annotations.Test;
024:
025: public class ClasspathAssetFactoryTest extends InternalBaseTestCase {
026: @Test
027: public void asset_client_URL_is_cached() {
028: ResourceCache cache = mockResourceCache();
029:
030: Resource r = new ClasspathResource("foo/Bar.txt");
031:
032: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
033:
034: train_requiresDigest(cache, r, false);
035:
036: String expectedClientURL = "/context/asset/foo/Bar.txt";
037:
038: train_toClientURL(aliasManager, "foo/Bar.txt",
039: expectedClientURL);
040:
041: replay();
042:
043: ClasspathAssetFactory factory = new ClasspathAssetFactory(
044: cache, aliasManager);
045:
046: Asset asset = factory.createAsset(r);
047:
048: assertEquals(asset.toClientURL(), expectedClientURL);
049:
050: // Now, to check the cache:
051:
052: assertEquals(asset.toClientURL(), expectedClientURL);
053:
054: verify();
055:
056: // Now, to test cache clearing:
057: train_requiresDigest(cache, r, false);
058:
059: train_toClientURL(aliasManager, "foo/Bar.txt",
060: expectedClientURL);
061:
062: replay();
063:
064: factory.objectWasInvalidated();
065:
066: assertEquals(asset.toClientURL(), expectedClientURL);
067:
068: verify();
069: }
070:
071: @Test
072: public void simple_asset_client_URL() {
073: ResourceCache cache = mockResourceCache();
074: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
075:
076: Resource r = new ClasspathResource("foo/Bar.txt");
077:
078: train_requiresDigest(cache, r, false);
079:
080: String expectedClientURL = "/context/asset/foo/Bar.txt";
081:
082: train_toClientURL(aliasManager, "foo/Bar.txt",
083: expectedClientURL);
084:
085: replay();
086:
087: AssetFactory factory = new ClasspathAssetFactory(cache,
088: aliasManager);
089:
090: Asset asset = factory.createAsset(r);
091:
092: assertSame(asset.getResource(), r);
093: assertEquals(asset.toClientURL(), expectedClientURL);
094: assertEquals(asset.toString(), asset.toClientURL());
095:
096: verify();
097: }
098:
099: @Test
100: public void protected_asset_client_URL() {
101: ResourceCache cache = mockResourceCache();
102: ClasspathAssetAliasManager aliasManager = mockClasspathAssetAliasManager();
103:
104: Resource r = new ClasspathResource("foo/Bar.txt");
105:
106: train_requiresDigest(cache, r, true);
107:
108: expect(cache.getDigest(r)).andReturn("ABC123");
109:
110: String expectedClientURL = "/context/asset/foo/Bar.ABC123.txt";
111:
112: train_toClientURL(aliasManager, "foo/Bar.ABC123.txt",
113: expectedClientURL);
114:
115: replay();
116:
117: AssetFactory factory = new ClasspathAssetFactory(cache,
118: aliasManager);
119:
120: Asset asset = factory.createAsset(r);
121:
122: assertSame(asset.getResource(), r);
123: assertEquals(asset.toClientURL(), expectedClientURL);
124: assertEquals(asset.toString(), asset.toClientURL());
125:
126: verify();
127: }
128:
129: }
|