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.contracts;
20  
21  import java.util.List;
22  import java.util.Set;
23  import javax.faces.application.Resource;
24  import javax.faces.application.ResourceHandler;
25  import javax.faces.application.StateManager;
26  import javax.faces.application.ViewResource;
27  
28  import org.apache.myfaces.config.RuntimeConfig;
29  
30  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
31  import org.apache.myfaces.shared.config.MyfacesConfig;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  public class ContractsCreateResourceMyFacesRequestTestCase extends AbstractMyFacesRequestTestCase
36  {
37  
38      @Override
39      protected boolean isScanAnnotations()
40      {
41          return true;
42      }
43  
44      @Override
45      protected void setUpWebConfigParams() throws Exception
46      {
47          super.setUpWebConfigParams();
48          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.application.contracts");
49          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
50          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
51          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
52          servletContext.addInitParameter("javax.faces.CONFIG_FILES", "/blue-faces-config.xml");
53      }
54      
55      @Test
56      public void testDefaultConfiguration() throws Exception
57      {
58          startViewRequest("/index.xhtml");
59          RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
60          
61          Set<String> allContracts = runtimeConfig.getResourceLibraryContracts();
62          Set<String> externalContextContracts = runtimeConfig.getExternalContextResourceLibraryContracts();
63          Set<String> classloaderContracts = runtimeConfig.getClassLoaderResourceLibraryContracts();
64  
65          Assert.assertTrue(allContracts.contains("yellow"));
66          Assert.assertTrue(allContracts.contains("blue"));
67          Assert.assertTrue(allContracts.contains("red"));
68  
69          Assert.assertTrue(classloaderContracts.contains("yellow"));
70          Assert.assertTrue(classloaderContracts.contains("blue"));
71          Assert.assertTrue(externalContextContracts.contains("red"));
72          
73          List<String> defaultContracts = runtimeConfig.getContractMappings().get("*");
74          
75          Assert.assertFalse(defaultContracts.contains("yellow"));
76          Assert.assertTrue(defaultContracts.contains("blue"));
77          Assert.assertFalse(defaultContracts.contains("red"));
78          
79          processLifecycleExecute();
80          executeBuildViewCycle(facesContext);
81          
82          List<String> contractsList = facesContext.getResourceLibraryContracts();
83          Assert.assertFalse(contractsList.contains("yellow"));
84          Assert.assertTrue(contractsList.contains("blue"));
85          Assert.assertFalse(contractsList.contains("red"));
86          
87          ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
88          
89          ViewResource resource1 = resourceHandler.createViewResource(facesContext, "/panel.xhtml");
90          Assert.assertNotNull(resource1);
91          Assert.assertTrue(resource1.getURL().toString().contains("panel.xhtml"));
92          
93          Resource resource2 = resourceHandler.createResource("myjs.js", "mylib");
94          Assert.assertNotNull(resource2);
95          
96          boolean libraryFound = resourceHandler.libraryExists("mylib");
97          Assert.assertTrue(libraryFound);
98          
99          endRequest();
100     }
101 
102 }