[richfaces-svn-commits] JBoss Rich Faces SVN: r4431 - in branches/3.1.x: ui/listShuttle/src/test/java/org/richfaces/component and 1 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Mon Dec 3 15:37:54 EST 2007


Author: sergeyhalipov
Date: 2007-12-03 15:37:53 -0500 (Mon, 03 Dec 2007)
New Revision: 4431

Added:
   branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/MockExternalContext.java
Modified:
   branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java
   branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/component/ListShuttleComponentTest.java
   branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/renderkit/ListShuttleRenderingTest.java
Log:
List shuttle JUnit tests (not finished).
http://jira.jboss.com/jira/browse/RF-1178

Modified: branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java
===================================================================
--- branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java	2007-12-03 17:09:06 UTC (rev 4430)
+++ branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/AbstractAjax4JsfTestCase.java	2007-12-03 20:37:53 UTC (rev 4431)
@@ -179,6 +179,9 @@
 		while (resources.hasMoreElements()) {
 			parser.parse((URL) resources.nextElement());
 		}
+		
+		externalContext = new MockExternalContext(externalContext);
+		facesContext.setExternalContext(externalContext);
 	}
 
 	protected WebClient createWebClient() {

Added: branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/MockExternalContext.java
===================================================================
--- branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/MockExternalContext.java	                        (rev 0)
+++ branches/3.1.x/framework/test/src/main/java/org/ajax4jsf/tests/MockExternalContext.java	2007-12-03 20:37:53 UTC (rev 4431)
@@ -0,0 +1,58 @@
+/**
+ * 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.ajax4jsf.tests;
+
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.shale.test.mock.MockExternalContext12;
+
+/**
+ * @author Siarhej Chalipau
+ *
+ */
+public class MockExternalContext extends MockExternalContext12 {
+
+	public MockExternalContext(org.apache.shale.test.mock.MockExternalContext baseContext) {
+		super((ServletContext)baseContext.getContext(), 
+				(HttpServletRequest)baseContext.getRequest(), (HttpServletResponse)baseContext.getResponse());
+	}
+	
+	public Map getRequestParameterValuesMap() {
+		// Method is unimplemented in org.apache.shale.test.mock.MockExternalContext and
+		// throws there UnsupportedOperationException
+		Map map = new HashMap();
+		HttpServletRequest request = (HttpServletRequest)getRequest();
+		for ( Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
+			String name = (String) e.nextElement();
+			map.put(name, request.getParameterValues(name));
+		}
+		return Collections.unmodifiableMap(map);
+	}
+
+}

Modified: branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/component/ListShuttleComponentTest.java
===================================================================
--- branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/component/ListShuttleComponentTest.java	2007-12-03 17:09:06 UTC (rev 4430)
+++ branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/component/ListShuttleComponentTest.java	2007-12-03 20:37:53 UTC (rev 4431)
@@ -21,36 +21,168 @@
 
 package org.richfaces.component;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.faces.component.UICommand;
+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 org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
 
+import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+
 /**
  * Unit test for simple Component.
  */
 public class ListShuttleComponentTest extends AbstractAjax4JsfTestCase {
+	private UIForm form = null;
+	private UIListShuttle listShuttle = null;
+	private ListShuttleBean sourceBean = null;
+	private ListShuttleBean targetBean = null;
+	private UICommand command = null;
+	
     /**
      * Create the test case
      *
      * @param testName name of the test case
      */
-    public ListShuttleComponentTest( String testName )
-    {
+    public ListShuttleComponentTest( String testName ) {
         super( testName );
     }
     
     public void setUp() throws Exception {
     	super.setUp();
+    	
+        application.addComponent(UIListShuttle.COMPONENT_TYPE, "org.richfaces.component.html.HtmlListShuttle");
+    	
+    	form = new HtmlForm();
+        form.setId("form");
+        facesContext.getViewRoot().getChildren().add(form);
+        
+        command = new HtmlCommandLink();
+        command.setId("command");
+        command.setValue("test command");
+        form.getChildren().add(command);
+        
+        sourceBean = new ListShuttleBean(false);
+        targetBean = new ListShuttleBean();
+        
+        listShuttle = (UIListShuttle)application.createComponent(UIListShuttle.COMPONENT_TYPE);
+        listShuttle.setId("listShuttle");
+        listShuttle.setVar("item");
+        listShuttle.setValueBinding("sourceValue", new ValueBinding() {
+
+			public Class getType(FacesContext arg0) throws EvaluationException,
+					PropertyNotFoundException {
+				return String.class;
+			}
+
+			public Object getValue(FacesContext arg0)
+					throws EvaluationException, PropertyNotFoundException {
+				return sourceBean.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);
+				sourceBean.setValue((List)arg1);
+			}
+    		
+    	});
+        
+        listShuttle.setValueBinding("targetValue", new ValueBinding() {
+
+			public Class getType(FacesContext arg0) throws EvaluationException,
+					PropertyNotFoundException {
+				return String.class;
+			}
+
+			public Object getValue(FacesContext arg0)
+					throws EvaluationException, PropertyNotFoundException {
+				return targetBean.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);
+				targetBean.setValue((List)arg1);
+			}
+        	
+        });
+        
+        form.getChildren().add(listShuttle);
     }
     
     public void tearDown() throws Exception {
+    	form = null;
+    	listShuttle = null;
+    	sourceBean = null;
+    	targetBean = null;
+    	command = null;
+    	
     	super.tearDown();
     }
 
 
     /**
-     * Rigourous Test :-)
+     * 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 testComponent()
-    {
-        assertTrue( true );
+    public void testDecode() throws Exception {
+        HtmlPage view = renderView();
+        assertNotNull(view);
+        
+        HtmlAnchor anchor = (HtmlAnchor)view.getDocumentElement().getHtmlElementById(command.getClientId(facesContext));
+        anchor.click();
+        externalContext.addRequestParameterMap(listShuttle.getClientId(facesContext), "1sa,0");
+        listShuttle.processDecodes(facesContext);
+        Object submittedValue = listShuttle.getSubmittedValue();
+        assertNotNull(submittedValue);
+        
+        assertTrue(submittedValue instanceof Object[]);
+        assertEquals(2, ((Object[])submittedValue).length);
     }
+    
+    protected class ListShuttleBean {
+    	private List value = null;
+    	
+    	public ListShuttleBean() {
+    		this(true);
+    	}
+    	
+    	public ListShuttleBean(boolean empty) {
+			value = new ArrayList();
+			if (!empty) {
+				value.add("1");
+				value.add("2");
+			}
+		}
+
+		public List getValue() {
+			return value;
+		}
+
+		public void setValue(List value) {
+			this.value = value;
+		}	
+    }
 }

Modified: branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/renderkit/ListShuttleRenderingTest.java
===================================================================
--- branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/renderkit/ListShuttleRenderingTest.java	2007-12-03 17:09:06 UTC (rev 4430)
+++ branches/3.1.x/ui/listShuttle/src/test/java/org/richfaces/renderkit/ListShuttleRenderingTest.java	2007-12-03 20:37:53 UTC (rev 4431)
@@ -193,7 +193,7 @@
         
         inputs = view.getDocumentElement().getHtmlElementsByAttribute("input", "name", listShuttle.getClientId(facesContext));
         assertNotNull(inputs);
-        assertEquals(1, inputs.size());
+        assertEquals(2, inputs.size());
     }
 
     /**




More information about the richfaces-svn-commits mailing list