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.web;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.net.URL;
020    import java.util.Locale;
021    
022    import org.apache.hivemind.Resource;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.testng.annotations.Test;
025    
026    /**
027     * Tests for {@link org.apache.tapestry.web.WebContextResource}.
028     * 
029     * @author Howard M. Lewis Ship
030     * @since 4.0
031     */
032    @Test
033    public class WebContextResourceTest extends BaseComponentTestCase
034    {
035        private WebContext newContext()
036        {
037            return newMock(WebContext.class);
038        }
039    
040        public void testConstructor()
041        {
042            WebContext context = newContext();
043    
044            replay();
045    
046            Resource r = new WebContextResource(context, "/foo/bar/baz_en.html", Locale.ENGLISH);
047    
048            assertEquals("context:/foo/bar/baz_en.html", r.toString());
049    
050            assertEquals("/foo/bar/baz_en.html", r.getPath());
051    
052            assertEquals("baz_en.html", r.getName());
053    
054            assertEquals(Locale.ENGLISH, r.getLocale());
055    
056            verify();
057        }
058    
059        public void testLocalizationExists() throws Exception
060        {
061            WebContext context = newContext();
062    
063            trainGetResource(context, "/foo/bar/baz_en.html", new URL("http://foo.com"));
064    
065            replay();
066    
067            Resource r1 = new WebContextResource(context, "/foo/bar/baz.html");
068    
069            Resource r2 = r1.getLocalization(Locale.ENGLISH);
070    
071            assertEquals("/foo/bar/baz_en.html", r2.getPath());
072            assertEquals(Locale.ENGLISH, r2.getLocale());
073    
074            verify();
075        }
076    
077        private void trainGetResource(WebContext context, String path, URL url)
078        {
079            expect(context.getResource(path)).andReturn(url);
080        }
081    
082        public void testLocalizationSame() throws Exception
083        {
084            WebContext context = newContext();
085    
086            trainGetResource(context, "/foo/bar/baz_en.html", null);
087            trainGetResource(context, "/foo/bar/baz.html", new URL("http://foo.com"));
088    
089            replay();
090    
091            Resource r1 = new WebContextResource(context, "/foo/bar/baz.html");
092    
093            Resource r2 = r1.getLocalization(Locale.ENGLISH);
094    
095            assertSame(r2, r1);
096    
097            verify();
098        }
099    
100        public void testLocalizationMissing() throws Exception
101        {
102            WebContext context = newContext();
103    
104            trainGetResource(context, "/foo/bar/baz_en.html", null);
105            trainGetResource(context, "/foo/bar/baz.html", null);
106    
107            replay();
108    
109            Resource r1 = new WebContextResource(context, "/foo/bar/baz.html");
110    
111            assertNull(r1.getLocalization(Locale.ENGLISH));
112    
113            verify();
114        }
115    
116        public void testGetRelativeResource()
117        {
118            WebContext context = newContext();
119    
120            replay();
121    
122            Resource r1 = new WebContextResource(context, "/foo/bar/baz.html");
123            Resource r2 = r1.getRelativeResource("baz.gif");
124    
125            assertEquals("/foo/bar/baz.gif", r2.getPath());
126    
127            verify();
128        }
129    
130        public void testGetExtensionlessResource() throws Exception
131        {
132            WebContext context = newContext();
133    
134            trainGetResource(context, "/foo/bar/baz_en", new URL("http://foo.com"));
135    
136            replay();
137    
138            Resource r1 = new WebContextResource(context, "/foo/bar/baz");
139    
140            Resource r2 = r1.getLocalization(Locale.ENGLISH);
141    
142            assertEquals("/foo/bar/baz_en", r2.getPath());
143            assertEquals(Locale.ENGLISH, r2.getLocale());
144    
145            verify();
146        }
147    }