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.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
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
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 }