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.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IAsset;
022    import org.apache.tapestry.IRequestCycle;
023    import org.apache.tapestry.l10n.DefaultResourceLocalizer;
024    import org.apache.tapestry.spec.IComponentSpecification;
025    import org.apache.tapestry.web.WebContext;
026    import static org.easymock.EasyMock.expect;
027    import org.testng.annotations.Test;
028    
029    import java.net.URL;
030    import java.util.Locale;
031    
032    @Test
033    public class ContextAssetFactoryTest extends BaseComponentTestCase
034    {
035        protected Resource newResource()
036        {
037            return newMock(Resource.class);
038        }
039    
040        protected URL newURL()
041        {
042            return getClass().getResource("base-resource.txt");
043        }
044    
045        public void test_Create_Asset()
046        {
047            Resource base = newResource();
048            Resource relative = newResource();
049            Resource localized = newResource();
050            Location l = newLocation();
051            URL url = newURL();
052            IComponentSpecification spec = newSpec();
053    
054            trainGetRelativeResource(base, "/", base);
055            trainGetRelativeResource(base, "asset.png", relative);
056            trainGetLocalization(relative, Locale.FRENCH, localized);
057            expect(localized.getResourceURL()).andReturn(url).anyTimes();
058            
059            replay();
060    
061            ContextAssetFactory factory = new ContextAssetFactory();
062            factory.setLocalizer(new DefaultResourceLocalizer());
063    
064            factory.setContextPath("/context");
065    
066            IAsset asset = factory.createAsset(base, spec, "asset.png", Locale.FRENCH, l);
067    
068            assertTrue(asset instanceof ContextAsset);
069            assertSame(localized, asset.getResourceLocation());
070            assertSame(l, asset.getLocation());
071    
072            verify();
073        }
074    
075        public void test_Absolute_Asset_Exists()
076        {
077            Resource base = newResource();
078            Resource relative = newResource();
079            Resource localized = newResource();
080            Location l = newLocation();
081            URL url = newURL();
082            IComponentSpecification spec = newSpec();
083    
084            trainGetRelativeResource(base, "/", base);
085            trainGetRelativeResource(base, "/images/asset.png", relative);
086            trainGetLocalization(relative, Locale.FRENCH, localized);
087            expect(localized.getResourceURL()).andReturn(url).anyTimes();
088    
089            replay();
090    
091            ContextAssetFactory factory = new ContextAssetFactory();
092            factory.setLocalizer(new DefaultResourceLocalizer());
093    
094            factory.setContextPath("/context");
095    
096            assert factory.assetExists(spec, base, "/images/asset.png", Locale.FRENCH);
097    
098            verify();
099        }
100    
101        public void test_Create_Asset_Missing()
102        {
103            Resource base = newResource();
104            Resource relative = newResource();
105            Location l = newLocation();
106            IComponentSpecification spec = newMock(IComponentSpecification.class);
107            WebContext context = newMock(WebContext.class);
108    
109            trainGetRelativeResource(base, "/", base);
110            trainGetRelativeResource(base, "asset.png", relative);
111            trainGetLocalization(relative, Locale.FRENCH, null);
112    
113            trainGetRelativeResource(base, "asset.png", relative);
114            trainGetLocalization(relative, Locale.FRENCH, null);
115            expect(spec.getLocation()).andReturn(l);
116            expect(l.getResource()).andReturn(null);
117    
118            expect(context.getResource("/asset_fr.png")).andReturn(null);
119            expect(context.getResource("/asset.png")).andReturn(null);
120    
121            replay();
122    
123            ContextAssetFactory factory = new ContextAssetFactory();
124            factory.setLocalizer(new DefaultResourceLocalizer());
125            factory.setContextPath("/context");
126            factory.setWebContext(context);
127    
128            try
129            {
130                factory.createAsset(base, spec, "asset.png", Locale.FRENCH, l);
131                unreachable();
132            }
133            catch (ApplicationRuntimeException ex)
134            {
135                assertEquals(
136                        "Unable to locate resource 'asset.png' relative to EasyMock for interface org.apache.hivemind.Resource.",
137                        ex.getMessage());
138                assertSame(l, ex.getLocation());
139            }
140    
141            verify();
142        }
143        
144        public void test_Create_Absolute_Asset()
145        {
146            Location l = newLocation();
147            URL url = newURL();
148            WebContext context = newMock(WebContext.class);
149            trainGetResource(context, "/asset_fr.png", url);
150    
151            replay();
152    
153            ContextAssetFactory factory = new ContextAssetFactory();
154            factory.setLocalizer(new DefaultResourceLocalizer());
155            factory.setContextPath("/context");
156            factory.setWebContext(context);
157    
158            IAsset asset = factory.createAbsoluteAsset("/asset.png", Locale.FRENCH, l);
159    
160            assertTrue(asset instanceof ContextAsset);
161            assertEquals("/asset_fr.png", asset.getResourceLocation().getPath());
162            assertSame(l, asset.getLocation());
163    
164            verify();
165        }
166    
167        public void test_Create_Absolute_Asset_Missing()
168        {
169            Location l = newLocation();
170            WebContext context = newMock(WebContext.class);
171    
172            trainGetResource(context, "/asset_fr.png", null);
173            trainGetResource(context, "/asset.png", null);
174    
175            replay();
176    
177            ContextAssetFactory factory = new ContextAssetFactory();
178            factory.setLocalizer(new DefaultResourceLocalizer());
179            factory.setContextPath("/context");
180            factory.setWebContext(context);
181    
182            try
183            {
184                factory.createAbsoluteAsset("/asset.png", Locale.FRENCH, l);
185                unreachable();
186            }
187            catch (ApplicationRuntimeException ex)
188            {
189                assertEquals("Missing context resource '/asset.png'.", ex.getMessage());
190                assertSame(l, ex.getLocation());
191            }
192            verify();
193        }
194        
195        public void test_Create_Asset_Encode_URL()
196        {
197            Location l = newLocation();
198            URL url = newURL();
199            WebContext context = newMock(WebContext.class);
200            IRequestCycle rc = newMock(IRequestCycle.class);
201    
202            trainGetResource(context, "/asset_fr.png", url);
203            
204            replay();
205    
206            ContextAssetFactory factory = new ContextAssetFactory();
207            factory.setLocalizer(new DefaultResourceLocalizer());
208            factory.setContextPath("/context");
209            factory.setWebContext(context);
210            factory.setRequestCycle(rc);
211    
212            String assetUrl = factory.createAbsoluteAsset("/asset.png", Locale.FRENCH, l).buildURL();
213    
214            assertEquals(assetUrl, "/context/asset_fr.png");
215    
216            verify();
217        }
218    
219        private void trainGetLocalization(Resource resource, Locale locale, Resource localized)
220        {
221            expect(resource.getLocalization(locale)).andReturn(localized);
222        }
223    
224        protected void trainGetResourceURL(Resource resource, URL url)
225        {
226            expect(resource.getResourceURL()).andReturn(url);
227        }
228    
229        protected void trainGetResource(WebContext context, String path, URL url)
230        {
231            expect(context.getResource(path)).andReturn(url);
232        }
233    
234        protected void trainGetRelativeResource(Resource base, String path, Resource relative)
235        {
236            expect(base.getRelativeResource(path)).andReturn(relative);
237        }
238    
239        protected void trainEncodeURL(IRequestCycle rc, String URL, String encodedURL)
240        {
241            expect(rc.encodeURL(URL)).andReturn(encodedURL);
242        }
243    }