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.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.Resource;
020    import org.apache.hivemind.impl.LocationImpl;
021    import org.apache.hivemind.util.ClasspathResource;
022    import org.apache.tapestry.BaseComponentTestCase;
023    import org.apache.tapestry.IAsset;
024    import org.apache.tapestry.engine.IEngineService;
025    import org.apache.tapestry.l10n.DefaultResourceLocalizer;
026    import org.apache.tapestry.spec.IComponentSpecification;
027    import org.testng.annotations.Test;
028    
029    import java.util.Locale;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.asset.ClasspathAssetFactory}.
033     * 
034     * @author Howard M. Lewis Ship
035     */
036    @Test
037    public class ClasspathAssetFactoryTest extends BaseComponentTestCase
038    {
039        public void test_Create_Asset()
040        {
041            IEngineService assetService = newService();
042            Location l = newLocation();
043            IComponentSpecification spec = newSpec();
044            
045            replay();
046    
047            ClasspathAssetFactory factory = new ClasspathAssetFactory();
048            factory.setClassResolver(getClassResolver());
049            factory.setAssetService(assetService);
050            factory.setLocalizer(new DefaultResourceLocalizer());
051    
052            Resource base = newBaseResource();
053    
054            IAsset asset = factory.createAsset(base, spec, "relative-resource.txt", Locale.FRENCH, l);
055    
056            assertTrue(asset instanceof PrivateAsset);
057            assertEquals("/org/apache/tapestry/asset/relative-resource_fr.txt", asset.getResourceLocation().getPath());
058            assertSame(l, asset.getLocation());
059    
060            verify();
061        }
062    
063        public void test_Absolute_Asset_Exists()
064        {
065            String path = "/org/apache/tapestry/html/Shell.jwc";
066    
067            IEngineService assetService = newService();
068            IComponentSpecification spec = newSpec();
069            
070            replay();
071    
072            ClasspathAssetFactory factory = new ClasspathAssetFactory();
073            factory.setClassResolver(getClassResolver());
074            factory.setAssetService(assetService);
075            factory.setLocalizer(new DefaultResourceLocalizer());
076    
077            Resource base = newBaseResource();
078    
079            assert factory.assetExists(spec, base, path, null);
080    
081            verify();
082        }
083    
084        public void test_Create_Asset_Missing()
085        {
086            IEngineService assetService = newService();
087            Location l = newLocation();
088            IComponentSpecification spec = newSpec();
089    
090            replay();
091    
092            ClasspathAssetFactory factory = new ClasspathAssetFactory();
093            factory.setClassResolver(getClassResolver());
094            factory.setAssetService(assetService);
095            factory.setLocalizer(new DefaultResourceLocalizer());
096    
097            Resource base = newBaseResource();
098    
099            try
100            {
101                factory.createAsset(base, spec, "does-not-exist.txt", Locale.ENGLISH, l);
102                unreachable();
103            }
104            catch (ApplicationRuntimeException ex)
105            {
106                assertEquals(
107                        "Unable to locate resource 'does-not-exist.txt' relative to classpath:/org/apache/tapestry/asset/base-resource.txt.",
108                        ex.getMessage());
109                assertSame(l, ex.getLocation());
110            }
111    
112            verify();
113        }
114    
115        public void test_Create_Absolute_Asset()
116        {
117            IEngineService assetService = newService();
118            Location l = newLocation();
119            replay();
120    
121            ClasspathAssetFactory factory = new ClasspathAssetFactory();
122            factory.setClassResolver(getClassResolver());
123            factory.setAssetService(assetService);
124            factory.setLocalizer(new DefaultResourceLocalizer());
125    
126            IAsset asset = factory.createAbsoluteAsset(
127                    "/org/apache/tapestry/asset/relative-resource.txt",
128                    Locale.FRENCH,
129                    l);
130    
131            assertTrue(asset instanceof PrivateAsset);
132            assertEquals("/org/apache/tapestry/asset/relative-resource_fr.txt", asset
133                    .getResourceLocation().getPath());
134            assertSame(l, asset.getLocation());
135    
136            verify();
137        }
138    
139        public void test_Create_Absolute_Asset_Missing()
140        {
141            IEngineService assetService = newService();
142            Location l = newLocation();
143    
144            replay();
145    
146            ClasspathAssetFactory factory = new ClasspathAssetFactory();
147            factory.setClassResolver(getClassResolver());
148            factory.setAssetService(assetService);
149            factory.setLocalizer(new DefaultResourceLocalizer());
150    
151            try
152            {
153                factory.createAbsoluteAsset(
154                        "/org/apache/tapestry/asset/does-not-exist.txt",
155                        Locale.ENGLISH,
156                        l);
157                unreachable();
158            }
159            catch (ApplicationRuntimeException ex)
160            {
161                assertEquals(
162                        "Missing classpath resource '/org/apache/tapestry/asset/does-not-exist.txt'.",
163                        ex.getMessage());
164                assertSame(l, ex.getLocation());
165            }
166    
167            verify();
168        }
169    
170        public void test_Create_Directory_Asset()
171        {
172            IEngineService assetService = newService();
173            Location l = newLocation();
174            
175            replay();
176            
177            ClasspathAssetFactory factory = new ClasspathAssetFactory();
178            factory.setClassResolver(getClassResolver());
179            factory.setAssetService(assetService);
180            factory.setLocalizer(new DefaultResourceLocalizer());
181            
182            String path = "/org/apache/tapestry/html/dojo";
183            
184            Resource subResource = new ClasspathResource(getClassResolver(), path);
185            IAsset asset = factory.createAsset(subResource, l);
186            
187            assertTrue(asset instanceof PrivateAsset);
188            assertEquals(path, asset
189                    .getResourceLocation().getPath());
190            assertSame(l, asset.getLocation());
191            
192            verify();
193        }
194        
195        public void test_Create_Relative_Directory_Asset()
196        {
197            IEngineService assetService = newService();
198            Resource shell = new ClasspathResource(getClassResolver(),
199                "/org/apache/tapestry/html/Shell.jwc");
200            Location l = new LocationImpl(shell);
201            IComponentSpecification spec = newSpec();
202            
203            replay();
204            
205            ClasspathAssetFactory factory = new ClasspathAssetFactory();
206            factory.setClassResolver(getClassResolver());
207            factory.setAssetService(assetService);
208            factory.setLocalizer(new DefaultResourceLocalizer());
209            
210            String path = "/dojo-0.4.3-custom-4.1.6/dojo.js";
211            
212            IAsset asset = factory.createAsset(shell, spec, path, 
213                    Locale.getDefault(),
214                    l);
215            
216            assertTrue(asset instanceof PrivateAsset);
217            assertEquals(path, asset
218                    .getResourceLocation().getPath());
219            assertSame(l, asset.getLocation());
220            
221            verify();
222        }
223        
224        public void test_Create_Relative_Directory_Missing_Asset()
225        {
226            IEngineService assetService = newService();
227            Resource shell = new ClasspathResource(getClassResolver(),
228                "/org/apache/tapestry/html/Shell.jwc");
229            Location l = new LocationImpl(shell);
230            IComponentSpecification spec = newSpec();
231            
232            replay();
233            
234            ClasspathAssetFactory factory = new ClasspathAssetFactory();
235            factory.setClassResolver(getClassResolver());
236            factory.setAssetService(assetService);
237            factory.setLocalizer(new DefaultResourceLocalizer());
238            
239            String path = "/dojo-0.4.3-custom-4.1.6/";
240            
241            IAsset asset = factory.createAsset(shell, spec, path, 
242                    Locale.getDefault(),
243                    l);
244            
245            assertTrue(asset instanceof PrivateAsset);
246            assertEquals(path, asset
247                    .getResourceLocation().getPath());
248            assertSame(l, asset.getLocation());
249            
250            verify();
251        }
252        
253        /**
254         * Tests relative sub-directory paths.
255         */
256        public void test_Relative_Directory_Path()
257        {
258            IEngineService assetService = newService();
259            Location l = newLocation();
260            
261            replay();
262            
263            ClasspathAssetFactory factory = new ClasspathAssetFactory();
264            factory.setClassResolver(getClassResolver());
265            factory.setAssetService(assetService);
266            factory.setLocalizer(new DefaultResourceLocalizer());
267            
268            Resource subResource = new ClasspathResource(getClassResolver(),
269                    "/org/apache/tapestry/asset/subresource/sub-resource.txt");
270            IAsset asset = factory.createAsset(subResource, l);
271            
272            assertTrue(asset instanceof PrivateAsset);
273            assertEquals("/org/apache/tapestry/asset/subresource/sub-resource.txt",
274                    asset.getResourceLocation().getPath());
275            assertSame(l, asset.getLocation());
276            
277            verify();
278        }
279        
280        private ClasspathResource newBaseResource()
281        {
282            return new ClasspathResource(getClassResolver(),
283                    "/org/apache/tapestry/asset/base-resource.txt");
284        }
285    
286        private IEngineService newService()
287        {
288            return newMock(IEngineService.class);
289        }
290    }