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