001    // Copyright 2005 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.asset;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.IAsset;
021    import static org.easymock.EasyMock.expect;
022    import org.testng.annotations.Test;
023    
024    import java.util.Collections;
025    import java.util.List;
026    import java.util.Locale;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.asset.AssetSourceImpl}.
030     *
031     */
032    @Test
033    public class TestAssetSource extends BaseComponentTestCase
034    {
035        private AssetFactoryContribution newContribution(String prefix, AssetFactory factory)
036        {
037            AssetFactoryContribution c = new AssetFactoryContribution();
038            c.setPrefix(prefix);
039            c.setFactory(factory);
040    
041            return c;
042        }
043    
044        private List newContributions(String prefix, AssetFactory factory)
045        {
046            return Collections.singletonList(newContribution(prefix, factory));
047        }
048    
049        private AssetFactory newAssetFactory(Resource base, String path, Locale locale, Location location, IAsset asset)
050        {
051            AssetFactory f = newMock(AssetFactory.class);
052    
053            expect(f.createAsset(base, null, path, locale, location)).andReturn(asset);
054    
055            return f;
056        }
057    
058        public void test_Known_Prefix()
059        {
060            Location l = newLocation();
061    
062            Resource r = newResource();
063            IAsset asset = newAsset();
064    
065            List contributions = newContributions("known", newAssetFactory(
066              r,
067              "path/to/asset",
068              Locale.ENGLISH,
069              l,
070              asset));
071    
072            replay();
073    
074            AssetSourceImpl as = new AssetSourceImpl();
075            as.setContributions(contributions);
076    
077            as.initializeService();
078    
079            IAsset actual = as.findAsset(r, "known:path/to/asset", Locale.ENGLISH, l);
080    
081            assertSame(actual, asset);
082    
083            verify();
084        }
085    
086        public void test_Unknown_Prefix()
087        {
088            Location l = fabricateLocation(17);
089    
090            Resource r = newResource();
091            IAsset asset = newAsset();
092    
093            AssetFactory f = newAssetFactory(r, "unknown:path/to/asset", Locale.ENGLISH, l, asset);
094    
095            replay();
096    
097            AssetSourceImpl as = new AssetSourceImpl();
098            as.setDefaultAssetFactory(f);
099    
100            IAsset actual = as.findAsset(r, "unknown:path/to/asset", Locale.ENGLISH, l);
101    
102            assertSame(actual, asset);
103    
104            verify();
105        }
106    
107        public void test_No_Prefix()
108        {
109            Location l = fabricateLocation(17);
110    
111            Resource r = newResource();
112            IAsset asset = newAsset();
113    
114            AssetFactory classFactory = newAssetFactory(r, "path/to/asset", Locale.ENGLISH, l, asset);
115    
116            expect(classFactory.assetExists(null, r, "path/to/asset", Locale.ENGLISH)).andReturn(true);
117    
118            replay();
119    
120            AssetSourceImpl as = new AssetSourceImpl();
121            as.setClasspathAssetFactory(classFactory);
122    
123            IAsset actual = as.findAsset(r, "path/to/asset", Locale.ENGLISH, l);
124    
125            assertSame(actual, asset);
126    
127            verify();
128        }
129    
130        public void test_Known_Prefix_Null_Base()
131        {
132            Location l = newLocation();
133            IAsset asset = newAsset();
134    
135            List contributions = newContributions("known", newAssetFactory(
136              null,
137              "path/to/asset",
138              Locale.ENGLISH,
139              l,
140              asset));
141    
142            replay();
143    
144            AssetSourceImpl as = new AssetSourceImpl();
145            as.setContributions(contributions);
146    
147            as.initializeService();
148    
149            IAsset actual = as.findAsset(null, "known:path/to/asset", Locale.ENGLISH, l);
150    
151            assertSame(actual, asset);
152    
153            verify();
154        }
155    }