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.view.facelets.compiler;
20  
21  import java.io.File;
22  import java.net.URL;
23  import org.apache.myfaces.config.element.facelets.FaceletTagLibrary;
24  
25  import org.apache.myfaces.shared.config.MyfacesConfig;
26  import org.apache.myfaces.view.facelets.FaceletTestCase;
27  import org.apache.myfaces.view.facelets.tag.TagLibrary;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  
32  public class TagLibraryTestCase extends FaceletTestCase
33  {
34      public final static String TAGLIB_SCHEMA_PATH = "/org/apache/myfaces/resource/web-facelettaglibrary_2_0.xsd";
35      
36      private URL _validLibUrl = null;
37      private URL _invalidLibUrl = null;
38      private URL _invalidOldLibUrl = null;
39  
40      public void setUp() throws Exception {
41          super.setUp();
42          _validLibUrl = resolveUrl("/testlib.taglib.xml");
43          _invalidLibUrl = resolveUrl("/testlib_invalid.taglib.xml");
44          _invalidOldLibUrl = resolveUrl("/testlib_old_invalid.taglib.xml");        
45  
46          // set document root for loading schema file as resource
47          ClassLoader cl = Thread.currentThread().getContextClassLoader();
48          String path = cl.getResource(TAGLIB_SCHEMA_PATH.substring(1)).getPath();
49          File documentRoot = new File(path.substring(0, path.indexOf(TAGLIB_SCHEMA_PATH)));
50          servletContext.setDocumentRoot(documentRoot);
51      }
52  
53      @Test
54      public void testLoadValidLibraryWithValidation() throws Exception
55      {
56          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_VALIDATE_XML, "true");
57  
58          FaceletTagLibrary faceletTagLib = TagLibraryConfigUnmarshallerImpl.create(
59              externalContext, _validLibUrl);
60          TagLibrary lib = TagLibraryConfig.create(facesContext, faceletTagLib);
61          Assert.assertTrue(lib.containsNamespace("http://myfaces.apache.org/testlib"));
62      }
63  
64      @Test
65      public void testLoadValidLibraryWithoutValidation() throws Exception
66      {
67          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_VALIDATE_XML, "false");
68  
69          FaceletTagLibrary faceletTagLib = TagLibraryConfigUnmarshallerImpl.create(
70              externalContext, _validLibUrl);
71          TagLibrary lib = TagLibraryConfig.create(facesContext, faceletTagLib);
72          Assert.assertTrue(lib.containsNamespace("http://myfaces.apache.org/testlib"));
73      }
74      /*
75      public void testLoadInvalidLibraryWithValidation() throws Exception
76      {
77          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_VALIDATE_XML, "true");
78  
79          try {
80              TagLibraryConfig.create(_invalidLibUrl);
81              fail("IOException expected");
82          } catch (IOException ioe) {
83              assertTrue(ioe.getCause() instanceof SAXException);
84          }
85  
86      }
87  
88      public void testLoadInvalidLibraryWithoutValidation() throws Exception
89      {
90          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_VALIDATE_XML, "false");
91  
92          TagLibrary lib = TagLibraryConfig.create(_invalidLibUrl);
93          assertTrue(lib.containsNamespace("http://myfaces.apache.org/testlib_invalid"));
94      }
95  
96      public void testLoadInvalidOldLibraryWithValidation() throws Exception
97      {
98          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_VALIDATE_XML, "true");
99  
100         try {
101             TagLibraryConfig.create(_invalidOldLibUrl);
102             fail("IOException expected");
103         } catch (IOException ioe) {
104             assertTrue(ioe.getCause() instanceof SAXException);
105         }
106     }
107     */
108 }