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  
20  package org.apache.myfaces.view.facelets;
21  
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import javax.faces.component.UIViewRoot;
26  
27  import org.apache.myfaces.view.facelets.util.FastWriter;
28  import org.apache.myfaces.test.mock.MockResponseWriter;
29  import org.junit.Test;
30  
31  public class EncodingTestCase extends FaceletTestCase
32  {
33  
34      @Override
35      protected void setupComponents() throws Exception
36      {
37          application.addComponent(UIViewRoot.COMPONENT_TYPE, UIViewRoot.class
38                  .getName());
39      }
40  
41      @Override
42      protected void setupConvertersAndValidators() throws Exception
43      {
44      }
45  
46      @Override
47      protected void setupRenderers() throws Exception
48      {
49      }
50  
51      @Test
52      public void testPattern() throws Exception
53      {
54          Pattern p = Pattern
55                  .compile("^<\\?xml.+?version=['\"](.+?)['\"](.+?encoding=['\"]((.+?))['\"])?.*?\\?>");
56          String[] d = new String[] { "<?xml version=\"1.0\" ?>",
57                  "<?xml version='1.0' ?>",
58                  "<?xml version='1.0' encoding='iso-8859-1'?>" };
59          for (int i = 0; i < d.length; i++)
60          {
61              Matcher m = p.matcher(d[i]);
62              //System.out.println(d[i] + " " + m.matches());
63              if (m.matches())
64              {
65                  for (int j = 0; j < m.groupCount(); j++)
66                  {
67                      //System.out.println('\t' + m.group(j));
68                  }
69              }
70          }
71      }
72  
73      @Test
74      public void testEncoding() throws Exception
75      {
76          request.setAttribute("name", "Mr. Hookom");
77          UIViewRoot root = facesContext.getViewRoot();
78          vdl.buildView(facesContext, root, "encoding.xml");
79          FastWriter fw = new FastWriter();
80          MockResponseWriter mrw = new MockResponseWriter(fw);
81          facesContext.setResponseWriter(mrw);
82          root.encodeAll(facesContext);
83          //System.out.println(fw);
84      }
85  
86  }