View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.application;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URI;
26  import org.apache.myfaces.test.base.AbstractJsfTestCase;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  import javax.faces.application.Resource;
31  import java.net.URL;
32  import java.util.Locale;
33  import java.util.logging.Level;
34  import java.util.logging.Logger;
35  import org.apache.myfaces.shared.resource.ExternalContextResourceLoader;
36  import org.apache.myfaces.shared.resource.ResourceLoader;
37  import org.apache.myfaces.shared.resource.ResourceMeta;
38  import org.apache.myfaces.shared.resource.ResourceMetaImpl;
39  
40  /**
41   * Test cases for org.apache.myfaces.application.ResourceHandlerImpl.
42   *
43   * @author Jakob Korherr
44   */
45  public class ResourceHandlerImplTest extends AbstractJsfTestCase
46  {
47  
48      private ResourceHandlerImpl resourceHandler;
49  
50      public ResourceHandlerImplTest(String name)
51      {
52          super(name);
53      }
54  
55      @Override
56      protected void setUp() throws Exception
57      {
58          super.setUp();
59  
60          resourceHandler = new ResourceHandlerImpl();
61          request.setPathElements("/xxx", "/yyy", "/test.xhtml", null);
62      }
63  
64      @Override
65      protected void tearDown() throws Exception
66      {
67          resourceHandler = null;
68  
69          super.tearDown();    
70      }
71  
72      @Test
73      public void testCreateResource_ResourceNotNull() throws Exception
74      {
75          Resource resource = resourceHandler.createResource("testResource.xhtml");
76  
77          Assert.assertNotNull(resource);
78      }
79  
80      @Test
81      public void testCreateResource_cacheHonorsLocale() throws Exception
82      {
83          // setup message bundle to use
84          application.setMessageBundle("org/apache/myfaces/application/resourcehandler/messages");
85  
86          // get english resource
87          application.setDefaultLocale(Locale.ENGLISH);
88          Resource resourceEn = resourceHandler.createResource("testResource.xhtml");
89          URL urlEn = resourceEn.getURL();
90  
91          // get german resource
92          application.setDefaultLocale(Locale.GERMAN);
93          Resource resourceDe = resourceHandler.createResource("testResource.xhtml");
94          URL urlDe = resourceDe.getURL();
95  
96          // URLs MUST be different, since there is an english and a german version of the resource
97          Assert.assertFalse("Resources must be different", urlEn.equals(urlDe));
98      }
99  
100     @Test
101     public void testDeriveResourceMeta1() throws Exception
102     {
103         application.setMessageBundle("org/apache/myfaces/application/resourcehandler/messages");
104         
105         ResourceLoader loader = new ResourceLoader("/resources") {
106 
107             @Override
108             public String getResourceVersion(String path)
109             {
110                 return null;
111             }
112 
113             @Override
114             public String getLibraryVersion(String path)
115             {
116                 return null;
117             }
118 
119             public URL getResourceURL(String resourceId)
120             {
121                 try
122                 {
123                     return new URL("file://"+resourceId);
124                 }
125                 catch (MalformedURLException ex)
126                 {
127                     Logger.getLogger(ResourceHandlerImplTest.class.getName()).log(Level.SEVERE, null, ex);
128                 }
129                 return null;
130             }
131             
132             @Override
133             public URL getResourceURL(ResourceMeta resourceMeta)
134             {
135                 try
136                 {
137                     return new URL("file://"+resourceMeta.getResourceIdentifier());
138                 }
139                 catch (MalformedURLException ex)
140                 {
141                     Logger.getLogger(ResourceHandlerImplTest.class.getName()).log(Level.SEVERE, null, ex);
142                 }
143                 return null;
144             }
145 
146             @Override
147             public InputStream getResourceInputStream(ResourceMeta resourceMeta)
148             {
149                 return null;
150             }
151 
152             @Override
153             public ResourceMeta createResourceMeta(String prefix, String libraryName, 
154                 String libraryVersion, String resourceName, String resourceVersion)
155             {
156                 return new ResourceMetaImpl(prefix, libraryName, 
157                     libraryVersion, resourceName, resourceVersion);
158             }
159 
160             @Override
161             public boolean libraryExists(String libraryName)
162             {
163                 return true;
164             }
165         };
166         
167         application.setDefaultLocale(Locale.ENGLISH);
168         ResourceMeta resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/1_0_2/myres.js/1_3.js");
169         Assert.assertNotNull(resource);
170         Assert.assertEquals("en", resource.getLocalePrefix());
171         Assert.assertEquals("mylib", resource.getLibraryName());
172         Assert.assertEquals("1_0_2", resource.getLibraryVersion());
173         Assert.assertEquals("myres.js", resource.getResourceName());
174         Assert.assertEquals("1_3", resource.getResourceVersion());
175         
176         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/1_0_2/myres.js");
177         Assert.assertNotNull(resource);
178         Assert.assertEquals("en", resource.getLocalePrefix());
179         Assert.assertEquals("mylib", resource.getLibraryName());
180         Assert.assertEquals("1_0_2", resource.getLibraryVersion());
181         Assert.assertEquals("myres.js", resource.getResourceName());
182         Assert.assertNull(resource.getResourceVersion());        
183         
184         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/myres.js/1_3.js");
185         Assert.assertNotNull(resource);
186         Assert.assertEquals("en", resource.getLocalePrefix());
187         Assert.assertEquals("mylib", resource.getLibraryName());
188         Assert.assertNull(resource.getLibraryVersion());
189         Assert.assertEquals("myres.js", resource.getResourceName());
190         Assert.assertEquals("1_3", resource.getResourceVersion());
191 
192         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/myres.js");
193         Assert.assertNotNull(resource);
194         Assert.assertEquals("en", resource.getLocalePrefix());
195         Assert.assertEquals("mylib", resource.getLibraryName());
196         Assert.assertEquals("myres.js", resource.getResourceName());
197         Assert.assertNull(resource.getLibraryVersion());
198         Assert.assertNull(resource.getResourceVersion());
199 
200         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/myres.js");
201         Assert.assertNotNull(resource);
202         Assert.assertNull(resource.getLibraryName());
203         Assert.assertNull(resource.getLibraryVersion());
204         Assert.assertNull(resource.getResourceVersion());
205         Assert.assertEquals("en", resource.getLocalePrefix());
206         Assert.assertEquals("myres.js", resource.getResourceName());
207         
208         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "mylib/myres.js");
209         Assert.assertNotNull(resource);
210         Assert.assertNull(resource.getLocalePrefix());
211         Assert.assertNull(resource.getLibraryVersion());
212         Assert.assertNull(resource.getResourceVersion());
213         Assert.assertEquals("mylib", resource.getLibraryName());
214         Assert.assertEquals("myres.js", resource.getResourceName());
215         
216         resource = resourceHandler.deriveResourceMeta(facesContext, loader, "myres.js");
217         Assert.assertNotNull(resource);
218         Assert.assertNull(resource.getLocalePrefix());
219         Assert.assertNull(resource.getLibraryName());
220         Assert.assertNull(resource.getLibraryVersion());
221         Assert.assertNull(resource.getResourceVersion());        
222         Assert.assertEquals("myres.js", resource.getResourceName());
223     }
224 }