1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }