Author: sergeyhalipov
Date: 2007-11-20 12:34:11 -0500 (Tue, 20 Nov 2007)
New Revision: 4116
Added:
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/OrderingListComponentTest.java
Removed:
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/JSFComponentTest.java
Log:
Ordering list: JUnit tests.
Deleted:
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/JSFComponentTest.java
===================================================================
---
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/JSFComponentTest.java 2007-11-20
16:31:25 UTC (rev 4115)
+++
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/JSFComponentTest.java 2007-11-20
17:34:11 UTC (rev 4116)
@@ -1,53 +0,0 @@
-/**
- * License Agreement.
- *
- * Rich Faces - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.component;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import javax.faces.component.UIComponent;
-
-/**
- * Unit test for simple Component.
- */
-public class JSFComponentTest
- extends TestCase
-{
- /**
- * Create the test case
- *
- * @param testName name of the test case
- */
- public JSFComponentTest( String testName )
- {
- super( testName );
- }
-
-
- /**
- * Rigourous Test :-)
- */
- public void testComponent()
- {
- assertTrue( true );
- }
-}
Copied:
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/OrderingListComponentTest.java
(from rev 4078,
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/JSFComponentTest.java)
===================================================================
---
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/OrderingListComponentTest.java
(rev 0)
+++
branches/3.1.x/ui/orderingList/src/test/java/org/richfaces/component/OrderingListComponentTest.java 2007-11-20
17:34:11 UTC (rev 4116)
@@ -0,0 +1,263 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UICommand;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIForm;
+import javax.faces.component.html.HtmlCommandLink;
+import javax.faces.component.html.HtmlForm;
+import javax.faces.context.FacesContext;
+import javax.faces.el.EvaluationException;
+import javax.faces.el.PropertyNotFoundException;
+import javax.faces.el.ValueBinding;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.component.html.HtmlOrderingList;
+
+import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+
+/**
+ * @author Siarhej Chalipau
+ *
+ */
+public class OrderingListComponentTest extends AbstractAjax4JsfTestCase {
+
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public OrderingListComponentTest( String testName )
+ {
+ super( testName );
+ }
+
+ private UIForm form = null;
+ private UIOrderingList orderingList = null;
+ private OrderingListBean bean = null;
+ private UICommand command = null;
+ private UIOrderingList orderingList2 = null;
+
+ public void setUp() throws Exception {
+ super.setUp();
+
+ application.addComponent("org.richfaces.OrderingList",
"org.richfaces.component.html.HtmlOrderingList");
+
+ form = new HtmlForm();
+ form.setId("form");
+ facesContext.getViewRoot().getChildren().add(form);
+
+ command = new HtmlCommandLink();
+ command.setId("command");
+ form.getChildren().add(command);
+
+ bean = new OrderingListBean();
+
+ orderingList =
(UIOrderingList)application.createComponent("org.richfaces.OrderingList");
+ orderingList.setControlsType("link");
+ orderingList.setVar("item");
+ orderingList.setValueBinding("value", new ValueBinding() {
+
+ public Class getType(FacesContext arg0) throws EvaluationException,
+ PropertyNotFoundException {
+ return String.class;
+ }
+
+ public Object getValue(FacesContext arg0)
+ throws EvaluationException, PropertyNotFoundException {
+ return bean.getValue();
+ }
+
+ public boolean isReadOnly(FacesContext arg0)
+ throws EvaluationException, PropertyNotFoundException {
+ return false;
+ }
+
+ public void setValue(FacesContext arg0, Object arg1)
+ throws EvaluationException, PropertyNotFoundException {
+ assertTrue(arg1 instanceof List);
+ bean.setValue((List)arg1);
+ }
+
+ });
+
+ form.getChildren().add(orderingList);
+
+ orderingList2 =
(UIOrderingList)application.createComponent("org.richfaces.OrderingList");
+ }
+
+ public void tearDown() throws Exception {
+ bean = null;
+ orderingList = null;
+ orderingList2 = null;
+ command = null;
+ form = null;
+
+ super.tearDown();
+ }
+
+ /**
+ * Tests if component accepts request parameters and stores them in
submittedValue().
+ * If component is immediate, validation (possibly with conversion) should occur on
that phase.
+ *
+ * @throws Exception
+ */
+ public void testDecode() throws Exception {
+ HtmlPage page = renderView();
+ assertNotNull(page);
+
+ HtmlAnchor anchor =
(HtmlAnchor)page.getDocumentElement().getHtmlElementById(command.getClientId(facesContext));
+ anchor.click();
+ externalContext.addRequestParameterMap(orderingList.getClientId(facesContext),
"1sa,0");
+ orderingList.processDecodes(facesContext);
+ Object submittedValue = orderingList.getSubmittedValue();
+ assertNotNull(submittedValue);
+ assertTrue(submittedValue instanceof Object[]);
+ assertEquals(2, ((Object[])submittedValue).length);
+
+ Object valueHolder = ((Object[])submittedValue)[0];
+ assertNotNull(valueHolder);
+ assertTrue(valueHolder instanceof UIOrderingList.SubmittedValue);
+ assertEquals("1,0sa", valueHolder.toString());
+
+ orderingList.setImmediate(true);
+ orderingList.addValidator(new Validator() {
+
+ public void validate(FacesContext arg0, UIComponent arg1,
+ Object arg2) throws ValidatorException {
+ FacesMessage mess = new FacesMessage("Fake test message.");
+ throw new ValidatorException(mess);
+
+ }
+
+ });
+
+ page = renderView();
+ anchor =
(HtmlAnchor)page.getDocumentElement().getHtmlElementById(command.getClientId(facesContext));
+ anchor.click();
+ externalContext.addRequestParameterMap(orderingList.getClientId(facesContext),
"1sa,0");
+ orderingList.processDecodes(facesContext);
+ assertTrue(facesContext.getMessages().hasNext());
+ }
+
+ /**
+ * Tests if component handles value bindings correctly
+ *
+ * @throws Exception
+ */
+ public void testUpdate() throws Exception {
+ HtmlPage page = renderView();
+ assertNotNull(page);
+
+ HtmlAnchor anchor =
(HtmlAnchor)page.getDocumentElement().getHtmlElementById(command.getClientId(facesContext));
+ anchor.click();
+ externalContext.addRequestParameterMap(orderingList.getClientId(facesContext),
"1sa,0");
+ orderingList.processDecodes(facesContext);
+ orderingList.processValidators(facesContext);
+ orderingList.processUpdates(facesContext);
+
+ List newValue = bean.getValue();
+ assertNotNull(newValue);
+ assertEquals(2, newValue.size());
+ assertEquals(newValue.get(0), "2");
+ assertEquals(newValue.get(1), "1");
+ }
+
+ /**
+ * Tests if component handles validation correctly
+ *
+ * @throws Exception
+ */
+ public void testValidate() throws Exception {
+ orderingList.addValidator(new Validator() {
+
+ public void validate(FacesContext arg0, UIComponent arg1,
+ Object arg2) throws ValidatorException {
+ FacesMessage mess = new FacesMessage("Fake test message.");
+ throw new ValidatorException(mess);
+
+ }
+
+ });
+
+ HtmlPage page = renderView();
+ assertNotNull(page);
+
+ HtmlAnchor anchor =
(HtmlAnchor)page.getDocumentElement().getHtmlElementById(command.getClientId(facesContext));
+ anchor.click();
+ externalContext.addRequestParameterMap(orderingList.getClientId(facesContext),
"1sa,0");
+ orderingList.processDecodes(facesContext);
+ orderingList.processValidators(facesContext);
+ assertTrue(facesContext.getMessages().hasNext());
+
+ Object submittedValue = orderingList.getSubmittedValue();
+ assertNotNull(submittedValue);
+ assertTrue(submittedValue instanceof Object[]);
+ assertEquals(2, ((Object[])submittedValue).length);
+
+ Object valueHolder = ((Object[])submittedValue)[0];
+ assertNotNull(valueHolder);
+ assertTrue(valueHolder instanceof UIOrderingList.SubmittedValue);
+ assertEquals("1,0sa", valueHolder.toString());
+ }
+
+ public void testSaveRestore() throws Exception {
+ String [] value = new String [] {"1"};
+ orderingList.setValueBinding("value", null);
+ orderingList.setValue(value);
+ assertEquals(value, orderingList.getValue());
+ assertNull(orderingList.getValueBinding("value"));
+
+ Object state = orderingList.saveState(facesContext);
+
+ orderingList2.restoreState(facesContext, state);
+ Object value2 = orderingList2.getValue();
+ assertNotNull(value2);
+ assertEquals(value, value2);
+ }
+
+ protected class OrderingListBean {
+ private List value = null;
+
+ public OrderingListBean() {
+ value = new ArrayList();
+ value.add("1");
+ value.add("2");
+ }
+
+ public List getValue() {
+ return value;
+ }
+
+ public void setValue(List value) {
+ this.value = value;
+ }
+ }
+}