1 package javax.faces.component;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import javax.faces.context.FacesContext;
11
12 import org.easymock.EasyMock;
13 import static org.easymock.EasyMock.*;
14 import static org.testng.Assert.*;
15 import org.testng.annotations.BeforeMethod;
16 import org.testng.annotations.Test;
17
18
19
20
21
22
23
24
25 public class UIComponentBaseProcessSaveRestoreStateTest extends AbstractUIComponentBaseTest
26 {
27 private static final String CHILD_STATE = "childState";
28 private static final String TESTIMPL_STATE = "testimplState";
29 private static final String FACET_STATE = "facetState";
30 private UIComponent _facet;
31 private UIComponent _child;
32
33 @Override
34 @BeforeMethod(alwaysRun = true)
35 protected void setUp() throws Exception
36 {
37 super.setUp();
38 _facet = _mocksControl.createMock(UIComponent.class);
39 _child = _mocksControl.createMock(UIComponent.class);
40 }
41
42 @Override
43 protected Collection<Method> getMockedMethods() throws Exception
44 {
45 Collection<Method> methods = super.getMockedMethods();
46 methods.add(UIComponentBase.class.getDeclaredMethod("getFacets", null));
47 methods.add(UIComponentBase.class.getDeclaredMethod("getChildren", null));
48 methods.add(UIComponentBase.class.getDeclaredMethod("getFacetCount", null));
49 methods.add(UIComponentBase.class.getDeclaredMethod("getChildCount", null));
50 methods.add(UIComponentBase.class.getDeclaredMethod("saveState", new Class[]{FacesContext.class}));
51 methods.add(UIComponentBase.class.getDeclaredMethod("restoreState", new Class[]{FacesContext.class,
52 Object.class}));
53 return methods;
54 }
55
56 @Test(expectedExceptions = {NullPointerException.class})
57 public void testSaveStateExpections() throws Exception
58 {
59 _testImpl.processSaveState(null);
60 }
61
62 @Test(expectedExceptions = {NullPointerException.class})
63 public void testRestoreStateExpections() throws Exception
64 {
65 _testImpl.processRestoreState(null, null);
66 }
67
68 @Test
69 public void testSaveRestoreStateWithTransientChilds() throws Exception
70 {
71 _testImpl.setTransient(true);
72 assertNull(_testImpl.processSaveState(_facesContext));
73
74 _testImpl.setTransient(false);
75 setUpChilds(true, true, true);
76 _mocksControl.replay();
77 Object state = _testImpl.processSaveState(_facesContext);
78 assertNotNull(state);
79 _mocksControl.verify();
80
81 _mocksControl.reset();
82 _testImpl.restoreState(EasyMock.same(_facesContext), EasyMock.eq(TESTIMPL_STATE));
83 _mocksControl.replay();
84 _testImpl.processRestoreState(_facesContext, state);
85 _mocksControl.verify();
86 }
87
88 @Test
89 public void testSaveRestoreState() throws Exception
90 {
91 _testImpl.setTransient(true);
92 assertNull(_testImpl.processSaveState(_facesContext));
93
94 _testImpl.setTransient(false);
95 setUpChilds(true, false, false);
96 _mocksControl.replay();
97 Object state = _testImpl.processSaveState(_facesContext);
98 assertNotNull(state);
99 _mocksControl.verify();
100
101 _mocksControl.reset();
102 setUpChilds(false, false, false);
103 _mocksControl.replay();
104 _testImpl.processRestoreState(_facesContext, state);
105 _mocksControl.verify();
106 }
107
108 private void setUpChilds(boolean saveState, boolean facetTransient, boolean childTransient)
109 {
110 if (saveState || !facetTransient)
111 {
112 Map<String, UIComponent> facetMap = new HashMap<String, UIComponent>();
113 facetMap.put("testFacet", _facet);
114 expect(_testImpl.getFacetCount()).andReturn(1).anyTimes();
115 expect(_testImpl.getFacets()).andReturn(facetMap).anyTimes();
116 expect(_facet.isTransient()).andReturn(facetTransient).anyTimes();
117 }
118 if (!facetTransient)
119 {
120 if (saveState)
121 expect(_facet.processSaveState(EasyMock.same(_facesContext))).andReturn(FACET_STATE);
122 else
123 _facet.processRestoreState(EasyMock.same(_facesContext), EasyMock.eq(FACET_STATE));
124 }
125 if (saveState || !childTransient)
126 {
127 List<UIComponent> childs = new ArrayList<UIComponent>();
128 childs.add(_child);
129 expect(_testImpl.getChildCount()).andReturn(1).anyTimes();
130 expect(_testImpl.getChildren()).andReturn(childs).anyTimes();
131 expect(_child.isTransient()).andReturn(childTransient).anyTimes();
132 }
133 if (!childTransient)
134 {
135 if (saveState)
136 expect(_child.processSaveState(EasyMock.same(_facesContext))).andReturn(CHILD_STATE);
137 else
138 _child.processRestoreState(EasyMock.same(_facesContext), EasyMock.eq(CHILD_STATE));
139 }
140 if (saveState)
141 expect(_testImpl.saveState(EasyMock.same(_facesContext))).andReturn(TESTIMPL_STATE);
142 else
143 _testImpl.restoreState(EasyMock.same(_facesContext), EasyMock.eq(TESTIMPL_STATE));
144 }
145 }