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.pool;
20
21 import javax.faces.application.ProjectStage;
22 import javax.faces.application.StateManager;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIViewRoot;
25 import javax.faces.event.PhaseId;
26 import javax.faces.render.RenderKitFactory;
27 import junit.framework.Assert;
28 import org.apache.myfaces.shared.config.MyfacesConfig;
29 import org.apache.myfaces.view.facelets.FaceletTestCase;
30 import org.apache.myfaces.view.facelets.ViewPoolProcessor;
31 import org.junit.Test;
32
33
34
35
36
37 public class ViewPoolFaceletsTestCase extends FaceletTestCase
38 {
39
40 @Override
41 protected void setUpServletObjects() throws Exception
42 {
43 super.setUpServletObjects();
44 servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, "true");
45 servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
46 servletContext.addInitParameter("javax.faces.FACELETS_LIBRARIES", "/test-facelet.taglib.xml");
47
48 servletContext.addInitParameter("org.apache.myfaces.CACHE_EL_EXPRESSIONS", "alwaysRecompile");
49 servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, "Production");
50 }
51
52 @Override
53 public void setUp() throws Exception
54 {
55 super.setUp();
56 ViewPoolProcessor.initialize(facesContext);
57 }
58
59 @Override
60 protected void setupComponents() throws Exception
61 {
62 super.setupComponents();
63 application.addComponent(UISimpleComponentA.COMPONENT_TYPE, UISimpleComponentA.class.getName());
64 }
65
66
67
68
69
70
71 @Test
72 public void testDynPageResourceCleanup1() throws Exception
73 {
74 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
75 ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
76
77
78 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
79 UIViewRoot root = facesContext.getViewRoot();
80 vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
81 processor.storeViewStructureMetadata(facesContext, root);
82
83
84 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.TRUE);
85 root = new UIViewRoot();
86 root.setViewId("/test");
87 root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
88 facesContext.setViewRoot(root);
89 vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
90
91 processor.storeViewStructureMetadata(facesContext, root);
92
93
94 facesContext.getAttributes().remove(root);
95 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
96 vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
97
98 UIComponent headerFacet = root.getFacet("head");
99 int oldCount = headerFacet.getChildCount();
100
101
102
103 Assert.assertEquals(0, oldCount);
104
105
106 ViewStructureMetadata metadata = processor.retrieveViewStructureMetadata(facesContext, root);
107 Assert.assertNotNull(metadata);
108 processor.clearTransientAndNonFaceletComponentsForDynamicView(facesContext, root, metadata);
109
110
111 Assert.assertEquals(0, headerFacet.getChildCount());
112 }
113
114
115
116
117
118
119 @Test
120 public void testDynPageResourceCleanup2() throws Exception
121 {
122 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
123 ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
124
125
126 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
127 UIViewRoot root = facesContext.getViewRoot();
128 vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
129 processor.storeViewStructureMetadata(facesContext, root);
130
131
132 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.TRUE);
133 root = new UIViewRoot();
134 root.setViewId("/test");
135 root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
136 facesContext.setViewRoot(root);
137 vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
138
139 processor.storeViewStructureMetadata(facesContext, root);
140
141
142 facesContext.getAttributes().remove(root);
143 facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
144 vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
145
146
147 UIComponent headerFacet = root.getFacet("head");
148 int oldCount = headerFacet.getChildCount();
149 Assert.assertEquals(1, oldCount);
150
151
152 ViewStructureMetadata metadata = processor.retrieveViewStructureMetadata(facesContext, root);
153 Assert.assertNotNull(metadata);
154 processor.clearTransientAndNonFaceletComponentsForDynamicView(facesContext, root, metadata);
155
156
157 Assert.assertEquals(0, headerFacet.getChildCount());
158 }
159
160 }