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.tag.ui;
20  
21  import java.io.StringWriter;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.faces.component.UIViewRoot;
26  
27  import org.apache.myfaces.test.mock.MockResponseWriter;
28  import org.apache.myfaces.view.facelets.FaceletTestCase;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  public class UITestCase extends FaceletTestCase
33  {
34  
35      @Override
36      protected void setupComponents() throws Exception
37      {
38          application.addComponent(UIViewRoot.COMPONENT_TYPE, UIViewRoot.class
39                  .getName());
40          application.addComponent(ComponentRef.COMPONENT_TYPE,
41                  ComponentRef.class.getName());
42      }
43  
44      @Override
45      protected void setupConvertersAndValidators() throws Exception
46      {
47      }
48  
49      @Override
50      protected void setupRenderers() throws Exception
51      {
52      }
53  
54      @Test
55      public void testRelativePaths() throws Exception
56      {
57          UIViewRoot root = facesContext.getViewRoot();
58          vdl.buildView(facesContext, root, "parent.xml");
59  
60          StringWriter sw = new StringWriter();
61          MockResponseWriter mrw = new MockResponseWriter(sw);
62          facesContext.setResponseWriter(mrw);
63          root.encodeAll(facesContext);
64          sw.flush();
65          
66          Assert.assertTrue(sw.toString().equals("Hello World!"));
67          
68          //System.out.println("************************");
69          //System.out.println(sw.toString());
70          //System.out.println("************************");
71      }
72  
73      @Test
74      public void testCompositionTemplate() throws Exception
75      {
76          UIViewRoot root = facesContext.getViewRoot();
77          vdl.buildView(facesContext, root, "composition-template.xml");
78          
79          StringWriter sw = new StringWriter();
80          MockResponseWriter mrw = new MockResponseWriter(sw);
81          facesContext.setResponseWriter(mrw);
82          root.encodeAll(facesContext);
83          sw.flush();
84  
85          String response = sw.toString();
86          
87          Assert.assertTrue(response.contains("New Title"));
88          Assert.assertTrue(response.contains("New Body"));
89      }
90  
91      @Test
92      public void testCompositionTemplateSimple() throws Exception
93      {
94          UIViewRoot root = facesContext.getViewRoot();
95          vdl.buildView(facesContext, root, "composition-template-simple.xml");
96          
97          StringWriter sw = new StringWriter();
98          MockResponseWriter mrw = new MockResponseWriter(sw);
99          facesContext.setResponseWriter(mrw);
100         root.encodeAll(facesContext);
101         sw.flush();
102 
103         String response = sw.toString();
104         
105         Assert.assertTrue(response.contains("New Body"));
106     }
107 
108     @Test
109     public void testComponent() throws Exception
110     {
111         Map map = new HashMap();
112         facesContext.getExternalContext().getRequestMap().put("map", map);
113 
114         UIViewRoot root = facesContext.getViewRoot();
115         vdl.buildView(facesContext, root, "component.xml");
116 
117         Assert.assertEquals("only one child, the component", 1, root.getChildCount());
118         Assert.assertNotNull("bound to map", map.get("c"));
119     }
120 
121     /*
122     public void testComponentClient() throws Exception {
123         FacesContext faces = FacesContext.getCurrentInstance();
124         Map map = new HashMap();
125         faces.getExternalContext().getRequestMap().put("map", map);
126 
127         FaceletFactory f = FaceletFactory.getInstance();
128         Facelet at = f.getFacelet("component-client.xml");
129 
130         UIViewRoot root = faces.getViewRoot();
131         at.apply(faces, root);
132         
133         assertEquals("4 children, the component", 4, root.getChildCount());
134         assertNotNull("bound to map", map.get("c"));
135     }*/
136 
137 }