View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.usecases.rolodex;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.net.URLEncoder;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.faces.el.ValueBinding;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.shale.clay.component.Clay;
30  import org.apache.shale.clay.config.beans.ComponentBean;
31  import org.apache.shale.test.base.AbstractViewControllerTestCase;
32  
33  public class RolodexTestCase extends AbstractViewControllerTestCase {
34  
35      // Construct a new instance of this test case.
36      public RolodexTestCase(String name) {
37          super(name);
38      }
39  
40      // Return the tests included in this test case.
41      public static Test suite() {
42  
43          return (new TestSuite(RolodexTestCase.class));
44  
45      }
46  
47      private Rolodex viewController = null;
48  
49      // setup the test case
50      protected void setUp() throws Exception {
51          super.setUp();
52  
53          // create the target view controller to test
54          viewController = new Rolodex();
55  
56          // simulate a managed bean
57          ValueBinding vb = application.createValueBinding("rolodex");
58          vb.setValue(facesContext, viewController);
59  
60          // simulate a managed bean
61          vb = application.createValueBinding("rolodexDao");
62          vb.setValue(facesContext, new RolodexDao());
63  
64      }
65  
66      // tear down test case
67      protected void tearDown() throws Exception {
68          super.tearDown();
69  
70          viewController = null;
71      }
72  
73      // test the runtime method from creating a clay subtree
74      public void testCreateTabs() {
75  
76          // simulate a method binding event being invoked from the
77          // "shapeValidator" event fired on the Clay component
78          //
79          // <clay:clay id="tabs" jsfid="RUNTIME"
80          // shapeValidator="#{rolodex.createTabs}" managedBeanName="rolodex" />
81  
82          // create a fake clay root display element bean
83          ComponentBean displayElementRoot = new ComponentBean();
84  
85          // create a mock component for the validator style/signature
86          // of method binding
87          Clay component = new Clay();
88          component.setId("RUNTIME");
89          component.setManagedBeanName("rolodex");
90          component.setShapeValidator("#{rolodex.createTabs}");
91  
92          // make the last tab active
93          viewController.setSelectedTab(RolodexDao.TAB_INDEX.length - 1);
94  
95          // simulate the the "shapeValidator" event is fired from the
96          // beginEncode method of the Clay component on the view controller.
97          viewController.createTabs(facesContext, component, displayElementRoot);
98  
99          // Check the number of children. Each tab has 6 nodes multiplied times
100         // the
101         // number of tabs plus two for the unordered list tags.
102         int n = (RolodexDao.TAB_INDEX.length * 6) + 2;
103 
104         assertEquals("#Children", n, displayElementRoot.getChildren().size());
105 
106     }
107 
108     // simulate clicking a tab
109     public void testChangeTab() {
110 
111         QueryParam paramObj = new QueryParam();
112         for (int t = 0; t < RolodexDao.TAB_INDEX.length; t++) {
113 
114             // tab links add a param of the tab index
115             paramObj.setTabIndex(String.valueOf(t));
116             ValueBinding vb = application.createValueBinding("queryParam");
117             vb.setValue(facesContext, paramObj);
118 
119             // add a dummy contact
120             viewController.setSelectedContact(new Contact());
121 
122             // simulate a action method binding event fired from a commandLink
123             // component
124             String forward = viewController.changeTab();
125             assertEquals("forward", "rolodex$test", forward);
126 
127             // selected contact should be null after changing tabs
128             assertNull("selectedContact", viewController.getSelectedContact());
129 
130             // make sure the tab is selected
131             assertEquals("selectedTab", t, viewController.getSelectedTab());
132 
133         }
134 
135     }
136 
137     // test populating contacts for each tab index
138     public void testContactsForTab() {
139 
140         // number of contacts per page
141         int[] knownGoodState = {12, 1, 0, 0, 0, 0, 0, 0, 0 };
142 
143         for (int i = 0; i < RolodexDao.TAB_INDEX.length; i++) {
144             viewController.setSelectedTab(i);
145             List contacts = viewController.getContactsForTab();
146             assertEquals("contacts on page", knownGoodState[i], contacts.size());
147         }
148 
149     }
150 
151     // simulates selecting a contact for edit on the page
152     public void testSelectContact() {
153         // force selection of the first tab
154         viewController.setSelectedTab(0);
155         // return the contacts on the first page
156         List contacts = viewController.getContactsForTab();
157 
158         QueryParam paramObj = new QueryParam();
159         Iterator ci = contacts.iterator();
160         while (ci.hasNext()) {
161             Contact contact = (Contact) ci.next();
162             String nameParam = null;
163             // simulate encoded commandLink parameter
164             try {
165                 nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
166             } catch (UnsupportedEncodingException e) {
167             }
168             // clear out the selected contact
169             viewController.setSelectedContact(null);
170 
171             paramObj.setSelectedName(nameParam);
172             ValueBinding vb = application.createValueBinding("queryParam");
173             vb.setValue(facesContext, paramObj);
174 
175             // invoke the method on the view controller as if it was fired
176             // via an action method binding event on the name collumn in the
177             // data table
178             String forward = viewController.selectContact();
179             assertEquals("forward", "rolodex$test", forward);
180 
181             // make sure something was selected
182             assertNotNull("selectedContact", viewController
183                     .getSelectedContact());
184 
185             // compare the source name with the target name
186             assertEquals("contact.name", contact.getName(), viewController
187                     .getSelectedContact().getName());
188 
189         }
190     }
191 
192     // simulates an new/add/delete operation
193     public void testNewAddDelete() {
194 
195         // initializes a new contact for binding with the form
196         viewController.setSelectedContact(null);
197         viewController.newContact();
198         assertNotNull("newContact", viewController.getSelectedContact());
199 
200         // name is the only PK
201         viewController.getSelectedContact().setName("VanMatre, Gary M.");
202         viewController.saveContact();
203 
204         // should be on the last tab after the save
205         assertTrue(
206                 "last tab",
207                 viewController.getSelectedTab() == RolodexDao.TAB_INDEX.length - 1);
208 
209         List contacts = viewController.getContactsForTab();
210         assertNotNull(contacts);
211 
212         // should be 1 contact on this tab
213         assertTrue("#contacts", contacts.size() == 1);
214         Contact contact = (Contact) contacts.get(0);
215         String nameParam = null;
216         try {
217             nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
218         } catch (UnsupportedEncodingException e) {
219         }
220 
221         QueryParam paramObj = new QueryParam();
222         paramObj.setSelectedName(nameParam);
223         ValueBinding vb = application.createValueBinding("queryParam");
224         vb.setValue(facesContext, paramObj);
225 
226         // invoke the method on the view controller as if it was fired
227         // via an action method binding event on the name collumn in the
228         // data table
229         String forward = viewController.selectContact();
230         assertEquals("forward", "rolodex$test", forward);
231 
232         // make sure something was selected
233         assertNotNull("selectedContact", viewController.getSelectedContact());
234 
235         // compare the source name with the target name
236         assertEquals("contact.name", contact.getName(), viewController
237                 .getSelectedContact().getName());
238 
239         // delete the selectedContact
240         viewController.deleteContact();
241 
242         // get the contacts on the page after the delete
243         contacts = viewController.getContactsForTab();
244 
245         // should be 0 contact on this tab
246         assertTrue("#contacts", contacts.size() == 0);
247 
248     }
249 
250 }