/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shale.usecases.rolodex;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import javax.faces.el.ValueBinding;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.shale.clay.component.Clay;
import org.apache.shale.clay.config.beans.ComponentBean;
import org.apache.shale.test.base.AbstractViewControllerTestCase;
public class RolodexTestCase extends AbstractViewControllerTestCase {
// Construct a new instance of this test case.
public RolodexTestCase(String name) {
super(name);
}
// Return the tests included in this test case.
public static Test suite() {
return (new TestSuite(RolodexTestCase.class));
}
private Rolodex viewController = null;
// setup the test case
protected void setUp() throws Exception {
super.setUp();
// create the target view controller to test
viewController = new Rolodex();
// simulate a managed bean
ValueBinding vb = application.createValueBinding("rolodex");
vb.setValue(facesContext, viewController);
// simulate a managed bean
vb = application.createValueBinding("rolodexDao");
vb.setValue(facesContext, new RolodexDao());
}
// tear down test case
protected void tearDown() throws Exception {
super.tearDown();
viewController = null;
}
// test the runtime method from creating a clay subtree
public void testCreateTabs() {
// simulate a method binding event being invoked from the
// "shapeValidator" event fired on the Clay component
//
// <clay:clay id="tabs" jsfid="RUNTIME"
// shapeValidator="#{rolodex.createTabs}" managedBeanName="rolodex" />
// create a fake clay root display element bean
ComponentBean displayElementRoot = new ComponentBean();
// create a mock component for the validator style/signature
// of method binding
Clay component = new Clay();
component.setId("RUNTIME");
component.setManagedBeanName("rolodex");
component.setShapeValidator("#{rolodex.createTabs}");
// make the last tab active
viewController.setSelectedTab(RolodexDao.TAB_INDEX.length - 1);
// simulate the the "shapeValidator" event is fired from the
// beginEncode method of the Clay component on the view controller.
viewController.createTabs(facesContext, component, displayElementRoot);
// Check the number of children. Each tab has 6 nodes multiplied times
// the
// number of tabs plus two for the unordered list tags.
int n = (RolodexDao.TAB_INDEX.length * 6) + 2;
assertEquals("#Children", n, displayElementRoot.getChildren().size());
}
// simulate clicking a tab
public void testChangeTab() {
QueryParam paramObj = new QueryParam();
for (int t = 0; t < RolodexDao.TAB_INDEX.length; t++) {
// tab links add a param of the tab index
paramObj.setTabIndex(String.valueOf(t));
ValueBinding vb = application.createValueBinding("queryParam");
vb.setValue(facesContext, paramObj);
// add a dummy contact
viewController.setSelectedContact(new Contact());
// simulate a action method binding event fired from a commandLink
// component
String forward = viewController.changeTab();
assertEquals("forward", "rolodex$test", forward);
// selected contact should be null after changing tabs
assertNull("selectedContact", viewController.getSelectedContact());
// make sure the tab is selected
assertEquals("selectedTab", t, viewController.getSelectedTab());
}
}
// test populating contacts for each tab index
public void testContactsForTab() {
// number of contacts per page
int[] knownGoodState = {12, 1, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < RolodexDao.TAB_INDEX.length; i++) {
viewController.setSelectedTab(i);
List contacts = viewController.getContactsForTab();
assertEquals("contacts on page", knownGoodState[i], contacts.size());
}
}
// simulates selecting a contact for edit on the page
public void testSelectContact() {
// force selection of the first tab
viewController.setSelectedTab(0);
// return the contacts on the first page
List contacts = viewController.getContactsForTab();
QueryParam paramObj = new QueryParam();
Iterator ci = contacts.iterator();
while (ci.hasNext()) {
Contact contact = (Contact) ci.next();
String nameParam = null;
// simulate encoded commandLink parameter
try {
nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
} catch (UnsupportedEncodingException e) {
}
// clear out the selected contact
viewController.setSelectedContact(null);
paramObj.setSelectedName(nameParam);
ValueBinding vb = application.createValueBinding("queryParam");
vb.setValue(facesContext, paramObj);
// invoke the method on the view controller as if it was fired
// via an action method binding event on the name collumn in the
// data table
String forward = viewController.selectContact();
assertEquals("forward", "rolodex$test", forward);
// make sure something was selected
assertNotNull("selectedContact", viewController
.getSelectedContact());
// compare the source name with the target name
assertEquals("contact.name", contact.getName(), viewController
.getSelectedContact().getName());
}
}
// simulates an new/add/delete operation
public void testNewAddDelete() {
// initializes a new contact for binding with the form
viewController.setSelectedContact(null);
viewController.newContact();
assertNotNull("newContact", viewController.getSelectedContact());
// name is the only PK
viewController.getSelectedContact().setName("VanMatre, Gary M.");
viewController.saveContact();
// should be on the last tab after the save
assertTrue(
"last tab",
viewController.getSelectedTab() == RolodexDao.TAB_INDEX.length - 1);
List contacts = viewController.getContactsForTab();
assertNotNull(contacts);
// should be 1 contact on this tab
assertTrue("#contacts", contacts.size() == 1);
Contact contact = (Contact) contacts.get(0);
String nameParam = null;
try {
nameParam = URLEncoder.encode(contact.getName(), "UTF-8");
} catch (UnsupportedEncodingException e) {
}
QueryParam paramObj = new QueryParam();
paramObj.setSelectedName(nameParam);
ValueBinding vb = application.createValueBinding("queryParam");
vb.setValue(facesContext, paramObj);
// invoke the method on the view controller as if it was fired
// via an action method binding event on the name collumn in the
// data table
String forward = viewController.selectContact();
assertEquals("forward", "rolodex$test", forward);
// make sure something was selected
assertNotNull("selectedContact", viewController.getSelectedContact());
// compare the source name with the target name
assertEquals("contact.name", contact.getName(), viewController
.getSelectedContact().getName());
// delete the selectedContact
viewController.deleteContact();
// get the contacts on the page after the delete
contacts = viewController.getContactsForTab();
// should be 0 contact on this tab
assertTrue("#contacts", contacts.size() == 0);
}
}