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.jsf.core;
20  
21  import java.io.StringWriter;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.faces.application.ConfigurableNavigationHandler;
29  import javax.faces.application.NavigationCase;
30  import javax.faces.component.UIViewParameter;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.context.FacesContext;
33  import javax.faces.context.ResponseWriter;
34  import javax.faces.view.ViewMetadata;
35  
36  import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
37  import org.apache.myfaces.view.facelets.FaceletTestCase;
38  import org.apache.myfaces.view.facelets.bean.HelloWorld;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  public class ViewMetadataTestCase extends FaceletTestCase
43  {
44      
45      protected ConfigurableNavigationHandler navigationHandler;
46      
47      public static class MockViewNavigationHandlerNavigationHandler
48          extends ConfigurableNavigationHandler
49      {
50          
51          Map<String, Set<NavigationCase>> cases = new HashMap<String, Set<NavigationCase>>(); 
52  
53          @Override
54          public NavigationCase getNavigationCase(FacesContext context,
55                  String fromAction, String outcome)
56          {
57              Set<NavigationCase> casesSet = cases.get(outcome);
58              if (casesSet == null)
59              {
60                  return null;
61              }
62  
63              for (NavigationCase navCase : casesSet)
64              {
65                  if (fromAction == null)
66                  {
67                      return navCase;
68                  }
69                  else if (fromAction.equals(navCase.getFromAction()))
70                  {
71                      return navCase;
72                  }
73              }
74              return null;
75          }
76  
77          @Override
78          public Map<String, Set<NavigationCase>> getNavigationCases()
79          {
80              // TODO Auto-generated method stub
81              return cases;
82          }
83  
84          @Override
85          public void handleNavigation(FacesContext context, String fromAction,
86                  String outcome)
87          {
88              
89          }
90      }
91      
92      @Override
93      public void setUp() throws Exception
94      {
95          super.setUp();        
96          navigationHandler = new MockViewNavigationHandlerNavigationHandler();
97          application.setNavigationHandler(navigationHandler);
98      }
99  
100     @Override
101     public void tearDown() throws Exception
102     {
103         super.tearDown();
104         navigationHandler = null;
105     }
106     
107     @Test
108     public void testSimpleViewMetadata() throws Exception
109     {
110         HelloWorld helloWorld = new HelloWorld(); 
111         
112         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
113                 helloWorld);
114         
115         Set<NavigationCase> cases = new HashSet<NavigationCase>();
116         NavigationCase navCase = new NavigationCase("viewMetadata.xhtml",null,
117                 "somePage.xhtml",null, "somePage.xhtml", null,false,false);
118         cases.add(navCase);
119         navigationHandler.getNavigationCases().put("somePage.xhtml", cases);
120         navigationHandler.getNavigationCase(facesContext, null, "somePage.xhtml");
121         
122         ViewMetadata metadata = vdl.getViewMetadata(facesContext, "viewMetadata.xhtml");
123         UIViewRoot root = metadata.createMetadataView(facesContext);
124         
125         Collection<UIViewParameter> viewParameters = metadata.getViewParameters(root);
126         
127         Assert.assertEquals(1, viewParameters.size());
128         
129         //root.setViewId("viewMetadata.xhtml");
130         vdl.buildView(facesContext, root, "viewMetadata.xhtml");
131         facesContext.setViewRoot(root);
132         
133         StringWriter sw = new StringWriter();
134         ResponseWriter mrw = new HtmlResponseWriterImpl(sw,"text/html","UTF-8");
135         facesContext.setResponseWriter(mrw);
136         
137         root.encodeAll(facesContext);
138         sw.flush();
139         System.out.print(sw.toString());
140     }
141 }