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.component;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.faces.application.StateManager;
29  import javax.faces.component.UIColumn;
30  import javax.faces.component.UIData;
31  import javax.faces.component.UIDataTest.RowData;
32  import javax.faces.component.UIInput;
33  import javax.faces.component.UIViewRoot;
34  import javax.faces.event.PhaseId;
35  import javax.faces.render.Renderer;
36  import static junit.framework.TestCase.assertEquals;
37  
38  import org.apache.myfaces.Assert;
39  import org.apache.myfaces.TestRunner;
40  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
41  import org.easymock.classextension.EasyMock;
42  import org.easymock.classextension.IMocksControl;
43  import org.junit.Test;
44  
45  public class UIRepeatTest extends AbstractJsfTestCase
46  {
47  
48      public UIRepeatTest()
49      {
50          super();
51      }
52  
53      private IMocksControl _mocksControl;
54      private UIData _testImpl;
55  
56      @Override
57      public void setUp() throws Exception
58      {
59          super.setUp();
60          _mocksControl = EasyMock.createControl();
61          _testImpl = new UIData();
62      }
63  
64      /**
65       * Test method for
66       * {@link javax.faces.component.UIData#setValueExpression(java.lang.String, javax.el.ValueExpression)}.
67       */
68      public void testValueExpression()
69      {
70          assertSetValueExpressionException(IllegalArgumentException.class, "rowIndex");
71          assertSetValueExpressionException(NullPointerException.class, null);
72      }
73  
74      private void assertSetValueExpressionException(Class<? extends Throwable> expected, final String name)
75      {
76          Assert.assertException(expected, new TestRunner()
77          {
78              public void run() throws Throwable
79              {
80                  _testImpl.setValueExpression(name, null);
81              }
82          });
83      }
84  
85      /**
86       * Test state save and restore cycle taking in consideration portlet case.
87       * 
88       * In portlets, saveState() could be called on INVOKE_APPLICATION phase and
89       * restoreState() could be called in RENDER_RESPONSE phase.
90       * 
91       * This test is active when PSS is disabled.
92       * 
93       * @throws Exception 
94       */
95      @Test
96      public void testSaveAndRestorePortletLifecycleWithoutPss1() throws Exception
97      {
98          List<RowData> model = new ArrayList<RowData>();
99          model.add(new RowData("text1","style1"));
100         model.add(new RowData("text2","style2"));
101         model.add(new RowData("text3","style3"));
102         model.add(new RowData("text4","style4"));
103         
104         //Put on request map to be resolved later
105         request.setAttribute("list", model);
106         
107         UIViewRoot root = facesContext.getViewRoot();
108         createSimpleTable(root);
109         UIRepeat table = (UIRepeat) root.getChildren().get(0);
110         //UIColumn column = (UIColumn) table.getChildren().get(0);
111         UIInput text = (UIInput) table.getChildren().get(0);
112         
113         facesContext.setCurrentPhaseId(PhaseId.APPLY_REQUEST_VALUES);
114         
115         //Check the value expressions are working and change the component state 
116         for (int i = 0; i < model.size(); i++)
117         {
118             RowData rowData = model.get(i); 
119             table.setRowIndex(i);
120             assertEquals(rowData.getText(), text.getValue());
121             text.setSubmittedValue("value"+(i+1));
122             //text.getAttributes().put("style", rowData.getStyle());
123         }
124         
125         //Reset row index
126         table.setRowIndex(-1);
127         
128         facesContext.setCurrentPhaseId(PhaseId.INVOKE_APPLICATION);
129         
130         Object state = table.saveState(facesContext);
131         
132         ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
133         ObjectOutputStream oos = new ObjectOutputStream(baos);
134         oos.writeObject(state);
135         oos.flush();
136         baos.flush();
137         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
138         ObjectInputStream ois = new ObjectInputStream(bais);
139         Object restoredState = (Object) ois.readObject();
140         oos.close();
141         ois.close();
142         
143         facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
144         
145         facesContext.setViewRoot(new UIViewRoot());
146         root = facesContext.getViewRoot();
147         createSimpleTable(root);
148         table = (UIRepeat) root.getChildren().get(0);
149         //column = (UIColumn) table.getChildren().get(0);
150         text = (UIInput) table.getChildren().get(0);
151         
152         table.restoreState(facesContext, restoredState);
153 
154         //Check the values were not lost
155         for (int i = 0; i < model.size(); i++)
156         {
157             RowData rowData = model.get(i); 
158             table.setRowIndex(i);
159             assertEquals("value"+(i+1), text.getSubmittedValue());
160             //assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
161         }
162     }
163     
164     private void createSimpleTable(UIViewRoot root)
165     {
166         createSimpleTable(root, false);
167     }
168     
169     private void createSimpleTable(UIViewRoot root, boolean rowStatePreserved)
170     {
171         UIRepeat table = new UIRepeat();
172         //UIColumn column = new UIColumn();
173         UIInput text = new UIInput();
174         
175         //This is only required if markInitiaState fix is not used 
176         root.setId(root.createUniqueId());
177         table.setId(root.createUniqueId());
178         //column.setId(root.createUniqueId());
179         text.setId(root.createUniqueId());
180         
181         table.setVar("row");
182         /*
183         if (rowStatePreserved)
184         {
185             table.setRowStatePreserved(true);
186         }*/
187         table.setValueExpression("value", application.
188                 getExpressionFactory().createValueExpression(
189                         facesContext.getELContext(),"#{list}",List.class));
190         
191         text.setValueExpression("value", application.
192                 getExpressionFactory().createValueExpression(
193                         facesContext.getELContext(),"#{row.text}",String.class));
194         
195         root.getChildren().add(table);
196         //table.getChildren().add(column);
197         //column.getChildren().add(text);
198         table.getChildren().add(text);
199     }
200 }