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