Author: craigmcc Date: Mon Jan 16 16:19:49 2006 New Revision: 369613 URL: http://svn.apache.org/viewcvs?rev=369613&view=rev Log: Improve the annotated managed beans implementation by adding partial support for <list-entries> elements. Currently, this works only for lists, but not (as the JSF spec requires) for arrays. Still no support for <map-entries> elements yet.
Added: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java (with props) struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml (with props) struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java (with props) Modified: struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/Bundle.properties struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverImpl.java struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParserTestCase.java Modified: struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/Bundle.properties URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/Bundle.properties?rev=369613&r1=369612&r2=369613&view=diff ============================================================================== --- struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/Bundle.properties (original) +++ struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/Bundle.properties Mon Jan 16 16:19:49 2006 @@ -19,6 +19,16 @@ lifecycle.initialized=Starting up Shale Tiger extensions # VariableHandlerImpl +convert.exception=Attempting to convert a value of {0} to type {1} has resulted in a ConversionException +convert.format=Attempting to convert a value of {0} to type {1} has resultined in a NumberFormatException +list.access=A list entries definition specifies a value type class {0} that does not have a public zero-arguments constructor +list.array=A list entries definition specifies a value type class {0} that is an array, which is not currently supported +list.class=A list entries definition specifies a value type class {0} that cannot be found +list.get=An attempt to retrieve a property named {0} from a managed bean named {1} has resulted in an exception +list.instantiate=A list entries definition specified a value type class {0} that was successfully loaded, but no instance can be created +list.list=A list entries definition specifies a type class {0} that is not an implementation of java.util.List +list.listProperty=A property named {0} on a managed bean named {1} has type {2} that does not implement java.util.List +list.get=An attempt to set a property named {0} on a managed bean named {1} has resulted in an exception variable.access=Managed bean definition {0} specifies a managed bean class {1} that does not have a public zero-arguments constructor variable.class=Managed bean definition {0} specifies a managed bean class {1} that cannot be found variable.evaluate=Managed bean definition {0} specifies a managed property {1} whose value expression {2} threw an exception when evaluated Modified: struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverImpl.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverImpl.java?rev=369613&r1=369612&r2=369613&view=diff ============================================================================== --- struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverImpl.java (original) +++ struts/shale/trunk/tiger/src/java/org/apache/shale/tiger/faces/VariableResolverImpl.java Mon Jan 16 16:19:49 2006 @@ -16,19 +16,27 @@ package org.apache.shale.tiger.faces; +import java.util.ArrayList; +import java.util.List; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.el.EvaluationException; import javax.faces.el.ValueBinding; import javax.faces.el.VariableResolver; import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.beanutils.ConversionException; +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.shale.tiger.config.FacesConfigConfig; import org.apache.shale.tiger.managed.Bean; import org.apache.shale.tiger.managed.Value; +import org.apache.shale.tiger.managed.config.ListEntriesConfig; +import org.apache.shale.tiger.managed.config.ListEntryConfig; import org.apache.shale.tiger.managed.config.ManagedBeanConfig; import org.apache.shale.tiger.managed.config.ManagedPropertyConfig; +import org.apache.shale.tiger.managed.config.MapEntriesConfig; import org.apache.shale.util.Messages; /** @@ -62,11 +70,9 @@ * <p>FIXME - Incomplete implemetnation of standard managed beans * functionality in the following areas:</p> * <ul> - * <li>Support for lists or maps as managed beans.</li> - * <li>Processing a <code>list-entries</code> or <code>map-entries</code> - * element nested in a <code>managed-bean</code> element.</li> - * <li>Processing a <code>list-entries</code> or <code>map-entries</code> - * elemente nested in a <code>managed-property</code> element.</li> + * <li>Partial support for list entries on managed beans and managed properties. + * It currently works for lists, but not for arrays.</li> + * <li>Support for map entries on managed beans and managed properties.</li> * </ul> * * <p><strong>IMPLEMENTATION NOTE</strong> - There is no <code>faces-config.xml</code> @@ -185,6 +191,60 @@ /** + * <p>Convert the specified value to the specified type.</p> + * + * @param context <code>FacesContext</code> for the current request + * @param type Type to which the value should be converted, or + * <code>null</code> to leave it as a string + * @param value Value to be converted + * + * @exception EvaluationException if an evaluation error occurs + */ + private Object convert(FacesContext context, Class type, String value) { + + // If the type is not specified, return the value unchanged + if (type == null) { + return value; + } + + // Handle primitive type conversions explicitly + try { + if ((type == Boolean.TYPE) || (type == Boolean.class)) { + return Boolean.valueOf(value); + } else if ((type == Byte.TYPE) || (type == Byte.class)) { + return Byte.valueOf(value); + } else if ((type == Double.TYPE) || (type == Double.class)) { + return Double.valueOf(value); + } else if ((type == Float.TYPE) || (type == Float.class)) { + return Float.valueOf(value); + } else if ((type == Integer.TYPE) || (type == Integer.class)) { + return Integer.valueOf(value); + } else if ((type == Long.TYPE) || (type == Long.class)) { + return Long.valueOf(value); + } else if ((type == Short.TYPE) || (type == Short.class)) { + return Short.valueOf(value); + } + } catch (NumberFormatException e) { + throw new EvaluationException(messages(). + getMessage("convert.format", + context.getViewRoot().getLocale(), + new Object[] { value, type.getName() }), e); + } + + // For all other cases, use the conversion utility + try { + return ConvertUtils.convert(value, type); + } catch (ConversionException e) { + throw new EvaluationException(messages(). + getMessage("convert.exception", + context.getViewRoot().getLocale(), + new Object[] { value, type.getName() }), e); + } + + } + + + /** * <p>Create, configure, and return a new instance based on the * specified managed bean, after storing it in the configured * scope (if any).</p> @@ -205,11 +265,27 @@ Object instance = instance(context, mb); // Configure properties as necessary - // FIXME - list entries and map entries are not supported on beans + // FIXME - map entries are not supported on beans for (ManagedPropertyConfig mp : mb.getProperties().values()) { property(context, mb, mp, instance); } + // Configure list entries as necessary + ListEntriesConfig listEntries = mb.getListEntries(); + if (listEntries != null) { + // FIXME - arrays are not yet supported + if (!List.class.isAssignableFrom(instance.getClass())) { + throw new EvaluationException(messages(). + getMessage("list.list", + context.getViewRoot().getLocale(), + new Object[] { instance.getClass().getName() })); + } + list(context, listEntries, (List) instance); + } + + // Configure map entries as necessary + // FIXME + // Place the bean into a scope, if necessary scope(context, mb, instance); @@ -268,6 +344,50 @@ /** + * <p>Populate the contents of the specified <code>List</code> from the + * specified list entries configuration information.</p> + * + * @param context <code>FacesContext</code> for the current request + * @param config [EMAIL PROTECTED] ListEntriesConfig} describing this list + * @param list <code>List</code> instance to have entries appended + * + * @exception EvaluationException if an evaluation error occurs + */ + private void list(FacesContext context, ListEntriesConfig config, List list) { + + // Determine the type to which list entries should be conveted, if any + String valueType = config.getValueType(); + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + Class type = null; + try { + if (valueType != null) { + type = cl.loadClass(valueType); + } + } catch (ClassNotFoundException e) { + throw new EvaluationException(messages(). + getMessage("list.class", + context.getViewRoot().getLocale(), + new Object[] { valueType }), e); + } + + // Add a list entry for each configuration element that is present + for (ListEntryConfig entry : config.getEntries()) { + if (entry.isNullValue()) { + list.add(null); + } else if (entry.isExpression()) { + // Evaluate the specified value binding expression + ValueBinding vb = + context.getApplication().createValueBinding(entry.getValue()); + list.add(vb.getValue(context)); + } else { + list.add(convert(context, type, entry.getValue())); + } + } + + } + + + /** * <p>Return the <code>Log</code> instance to be used for this class, * instantiating a new one if necessary.</p> */ @@ -311,8 +431,55 @@ ManagedPropertyConfig mp, Object instance) throws EvaluationException { + // Configure list entries as necessary + ListEntriesConfig listEntries = mp.getListEntries(); + if (listEntries != null) { + + // Acquire the value of the specified property from the + // specified instance, if it exists + Object property = null; + try { + property = PropertyUtils.getProperty(instance, mp.getName()); + } catch (NoSuchMethodException e) { + ; // Fall through to creating our own list + } catch (Exception e) { + throw new EvaluationException(messages(). + getMessage("list.get", + context.getViewRoot().getLocale(), + new Object[] { mp.getName(), mb.getName()}), e); + } + if (property == null) { + property = new ArrayList(); + } + + // FIXME - arrays are not yet supported + if (!List.class.isAssignableFrom(property.getClass())) { + throw new EvaluationException(messages(). + getMessage("list.listProperty", + context.getViewRoot().getLocale(), + new Object[] { mp.getName(), mb.getName(), property.getClass().getName() })); + } + + // Accumulate the new values for the list + list(context, listEntries, (List) property); + + // Store the value of the property + try { + BeanUtils.setProperty(instance, mp.getName(), property); + } catch (Exception e) { + throw new EvaluationException(messages(). + getMessage("list.set", + context.getViewRoot().getLocale(), + new Object[] { mp.getName(), mb.getName()}), e); + } + + // We are through with this property, so return + return; + + } + // Does this managed property specify initialization? - // FIXME - list entries and map entries are not supported on properties + // FIXME - map entries are not supported on properties if ((mp.getValue() == null) && !mp.isNullValue()) { return; } Modified: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParserTestCase.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParserTestCase.java?rev=369613&r1=369612&r2=369613&view=diff ============================================================================== --- struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParserTestCase.java (original) +++ struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/FacesConfigParserTestCase.java Mon Jan 16 16:19:49 2006 @@ -1,5 +1,5 @@ /* - * Copyright 2004-2005 The Apache Software Foundation. + * Copyright 2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.shale.tiger.managed.config.ListEntriesConfig; +import org.apache.shale.tiger.managed.config.ListEntryConfig; import org.apache.shale.tiger.managed.config.ManagedBeanConfig; import org.apache.shale.tiger.managed.config.ManagedPropertyConfig; @@ -76,6 +78,172 @@ // ------------------------------------------------------------ Test Methods + + + // Test parsing configuration resource that has lists in it + public void testList() throws Exception { + + ManagedBeanConfig mb = null; + ManagedPropertyConfig mp = null; + ListEntriesConfig entries = null; + ListEntryConfig entry = null; + + // Wire up our FacesConfigConfig instance + parser.setFacesConfig(facesConfig); + assertTrue(facesConfig == parser.getFacesConfig()); + + // Make sure we do validating parses + parser.setValidating(true); + assertTrue(parser.isValidating()); + + // Parse resource 4 + parser.setResource(this.getClass().getResource + ("/org/apache/shale/tiger/config/test-config-4.xml")); + assertNotNull(parser.getResource()); + parser.parse(); + assertEquals(4, facesConfig.getManagedBeans().size()); + + // Validate bean "explicitSqlDateList" + mb = facesConfig.getManagedBean("explicitSqlDateList"); + assertNotNull(mb); + assertEquals("explicitSqlDateList", mb.getName()); + assertEquals("none", mb.getScope()); + assertEquals("java.util.Vector", mb.getType()); + assertNotNull(mb.getListEntries()); + assertNull(mb.getMapEntries()); + assertEquals(0, mb.getProperties().size()); + + entries = mb.getListEntries(); + assertEquals("java.sql.Date", entries.getValueType()); + assertEquals(4, entries.getEntries().size()); + entry = entries.getEntries().get(0); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("2006-01-02", entry.getValue()); + entry = entries.getEntries().get(1); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("2006-03-04", entry.getValue()); + entry = entries.getEntries().get(2); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(entry.isNullValue()); + assertNull(entry.getValue()); + entry = entries.getEntries().get(3); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("2006-05-06", entry.getValue()); + + // Validate bean "explicitStringList" + mb = facesConfig.getManagedBean("explicitStringList"); + assertNotNull(mb); + assertEquals("explicitStringList", mb.getName()); + assertEquals("request", mb.getScope()); + assertEquals("java.util.LinkedList", mb.getType()); + assertNotNull(mb.getListEntries()); + assertNull(mb.getMapEntries()); + assertEquals(0, mb.getProperties().size()); + + entries = mb.getListEntries(); + assertEquals("java.lang.String", entries.getValueType()); + assertEquals(5, entries.getEntries().size()); + entry = entries.getEntries().get(0); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("foo", entry.getValue()); + entry = entries.getEntries().get(1); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("bar", entry.getValue()); + entry = entries.getEntries().get(2); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(entry.isNullValue()); + assertNull(entry.getValue()); + entry = entries.getEntries().get(3); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("baz", entry.getValue()); + entry = entries.getEntries().get(4); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("bop", entry.getValue()); + + // Validate bean "implicitStringList" + mb = facesConfig.getManagedBean("implicitStringList"); + assertNotNull(mb); + assertEquals("implicitStringList", mb.getName()); + assertEquals("session", mb.getScope()); + assertEquals("java.util.ArrayList", mb.getType()); + assertNotNull(mb.getListEntries()); + assertNull(mb.getMapEntries()); + assertEquals(0, mb.getProperties().size()); + + entries = mb.getListEntries(); + assertEquals(null, entries.getValueType()); + assertEquals(5, entries.getEntries().size()); + entry = entries.getEntries().get(0); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("bop", entry.getValue()); + entry = entries.getEntries().get(1); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(entry.isNullValue()); + assertNull(entry.getValue()); + entry = entries.getEntries().get(2); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("baz", entry.getValue()); + entry = entries.getEntries().get(3); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("bar", entry.getValue()); + entry = entries.getEntries().get(4); + assertNotNull(entry); + assertTrue(!entry.isExpression()); + assertTrue(!entry.isNullValue()); + assertEquals("foo", entry.getValue()); + + // Validate bean "listPropertiesBean" + mb = facesConfig.getManagedBean("listPropertiesBean"); + assertNotNull(mb); + assertEquals("listPropertiesBean", mb.getName()); + assertEquals("application", mb.getScope()); + assertEquals("org.apache.shale.tiger.config.TestBean4", mb.getType()); + assertNull(mb.getListEntries()); + assertNull(mb.getMapEntries()); + assertEquals(2, mb.getProperties().size()); + + mp = mb.getProperty("emptyList"); + assertNotNull(mp); + assertEquals("emptyList", mp.getName()); + assertNull(mp.getValue()); + assertTrue(!mp.isExpression()); + assertTrue(!mp.isNullValue()); + assertNotNull(mp.getListEntries()); + assertEquals(5, mp.getListEntries().getEntries().size()); + + mp = mb.getProperty("fullList"); + assertNotNull(mp); + assertEquals("fullList", mp.getName()); + assertNull(mp.getValue()); + assertTrue(!mp.isExpression()); + assertTrue(!mp.isNullValue()); + assertNotNull(mp.getListEntries()); + assertEquals(5, mp.getListEntries().getEntries().size()); + + } // Test pristine instance of the parser Added: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java?rev=369613&view=auto ============================================================================== --- struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java (added) +++ struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java Mon Jan 16 16:19:49 2006 @@ -0,0 +1,44 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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.tiger.config; + +import java.util.List; +import java.util.Vector; + +/** + * <p>JavaBean class with list properties for testing.</p> + */ +public class TestBean4 { + + /** Creates a new instance of TestBean */ + public TestBean4() { + } + + private List emptyList = null; + public List getEmptyList() { return this.emptyList; } + public void setEmptyList(List emptyList) { this.emptyList = emptyList; } + + private List fullList = new Vector(); + { + fullList.add("First"); + fullList.add("Second"); + } + public List getFullList() { return this.fullList; } + public void setFullList(List fullList) { this.fullList = fullList; } + + +} Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/TestBean4.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml?rev=369613&view=auto ============================================================================== --- struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml (added) +++ struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml Mon Jan 16 16:19:49 2006 @@ -0,0 +1,101 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + + Copyright 2006 The Apache Software Foundation. + + Licensed 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. + + $Id$ + +--> + +<!DOCTYPE faces-config PUBLIC + '-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN' + 'http://java.sun.com/dtd/web-facesconfig_1_1.dtd'> + +<faces-config> + + <!-- Configure a variety of managed beans that are actually lists --> + + <managed-bean> + <managed-bean-name>explicitSqlDateList</managed-bean-name> + <managed-bean-class>java.util.Vector</managed-bean-class> + <managed-bean-scope>none</managed-bean-scope> + <list-entries> + <value-class>java.sql.Date</value-class> + <value>2006-01-02</value> + <value>2006-03-04</value> + <null-value/> + <value>2006-05-06</value> + </list-entries> + </managed-bean> + + <managed-bean> + <managed-bean-name>explicitStringList</managed-bean-name> + <managed-bean-class>java.util.LinkedList</managed-bean-class> + <managed-bean-scope>request</managed-bean-scope> + <list-entries> + <value-class>java.lang.String</value-class> + <value>foo</value> + <value>bar</value> + <null-value/> + <value>baz</value> + <value>bop</value> + </list-entries> + </managed-bean> + + <managed-bean> + <managed-bean-name>implicitStringList</managed-bean-name> + <managed-bean-class>java.util.ArrayList</managed-bean-class> + <managed-bean-scope>session</managed-bean-scope> + <list-entries> + <value>bop</value> + <null-value/> + <value>baz</value> + <value>bar</value> + <value>foo</value> + </list-entries> + </managed-bean> + + + <!-- Configure a managed bean with list properties --> + + + <managed-bean> + <managed-bean-name>listPropertiesBean</managed-bean-name> + <managed-bean-class>org.apache.shale.tiger.config.TestBean4</managed-bean-class> + <managed-bean-scope>application</managed-bean-scope> + <managed-property> + <property-name>emptyList</property-name> + <list-entries> + <value>foo</value> + <value>bar</value> + <null-value/> + <value>baz</value> + <value>bop</value> + </list-entries> + </managed-property> + <managed-property> + <property-name>fullList</property-name> + <list-entries> + <value>foo</value> + <value>bar</value> + <null-value/> + <value>baz</value> + <value>bop</value> + </list-entries> + </managed-property> + </managed-bean> + +</faces-config> Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/config/test-config-4.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java?rev=369613&view=auto ============================================================================== --- struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java (added) +++ struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java Mon Jan 16 16:19:49 2006 @@ -0,0 +1,187 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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.tiger.faces; + +import java.io.File; +import java.sql.Date; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Vector; +import javax.servlet.ServletContextEvent; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.apache.shale.test.base.AbstractJsfTestCase; +import org.apache.shale.tiger.config.FacesConfigConfig; +import org.apache.shale.tiger.config.TestBean; +import org.apache.shale.tiger.config.TestBean4; +import org.apache.shale.tiger.managed.config.ManagedBeanConfig; +import org.apache.shale.tiger.managed.config.ManagedPropertyConfig; + +/** + * <p>Test case for <code>org.apache.shale.tiger.faces.VariableResolverImpl</code> + * when processing resource <code>/WEB-INF/test-config-4.xml</code>.</p> + */ +public class VariableResolverImpl4TestCase extends AbstractJsfTestCase { + + + // ------------------------------------------------------------ Constructors + + + // Construct a new instance of this test case + public VariableResolverImpl4TestCase(String name) { + super(name); + } + + + // ---------------------------------------------------- Overall Test Methods + + + // Set up instance variables required by this test case. + public void setUp() { + + // Set up mock web application environment + super.setUp(); + servletContext.addInitParameter("javax.faces.CONFIG_FILES", + "/WEB-INF/test-config-4.xml"); + File root = new File(System.getProperty("basedir") + "/target/test-webapp"); + servletContext.setDocumentRoot(root); + + // Process our configuration information + listener = new LifecycleListener(); + listener.contextInitialized(new ServletContextEvent(servletContext)); + + // Create resolver instance to be tested + // (Force NPEs on delegation use cases by default) + resolver = new VariableResolverImpl(null); + + } + + + // Return the tests included in this test case. + public static Test suite() { + return new TestSuite(VariableResolverImpl4TestCase.class); + } + + + // Tear down instance variables required by this test case + public void tearDown() { + + // Release tested instances + resolver = null; + + // Finalize our context listener + listener.contextDestroyed(new ServletContextEvent(servletContext)); + listener = null; + + // Tear down the mock web application environment + super.tearDown(); + + } + + + // ------------------------------------------------------ Instance Variables + + + // LifecycleListener instance to be tested + LifecycleListener listener = null; + + // VariableResolverImpl instance to be tested + VariableResolverImpl resolver = null; + + + // ------------------------------------------------------------ Test Methods + + + // Test creating bean "explicitSqlDateList" + public void testExplicitSqlDateList() { + + Object instance = resolver.resolveVariable(facesContext, "explicitSqlDateList"); + assertNotNull(instance); + assertTrue(instance instanceof Vector); + List list = (List) instance; + assertEquals(4, list.size()); + + assertEquals(new Date(106, 0, 2), list.get(0)); + assertEquals(new Date(106, 2, 4), list.get(1)); + assertNull(list.get(2)); + assertEquals(new Date(106, 4, 6), list.get(3)); + + } + + + // Test creating bean "explicitStringList" + public void testExplicitStringList() { + + Object instance = resolver.resolveVariable(facesContext, "explicitStringList"); + assertNotNull(instance); + assertTrue(instance instanceof LinkedList); + List list = (List) instance; + assertEquals(5, list.size()); + + assertEquals("foo", list.get(0)); + assertEquals("bar", list.get(1)); + assertNull(list.get(2)); + assertEquals("baz", list.get(3)); + assertEquals("bop", list.get(4)); + + } + + + // Test creating bean "implicitStringList" + public void testImplicitStringList() { + + Object instance = resolver.resolveVariable(facesContext, "implicitStringList"); + assertNotNull(instance); + assertTrue(instance instanceof ArrayList); + List list = (List) instance; + assertEquals(5, list.size()); + + assertEquals("bop", list.get(0)); + assertNull(list.get(1)); + assertEquals("baz", list.get(2)); + assertEquals("bar", list.get(3)); + assertEquals("foo", list.get(4)); + + } + + + // Test creating bean "listPropertiesBean" + public void testListPropertiesBean() { + + Object instance = resolver.resolveVariable(facesContext, "listPropertiesBean"); + assertNotNull(instance); + assertTrue(instance instanceof TestBean4); + TestBean4 bean = (TestBean4) instance; + + List emptyList = bean.getEmptyList(); + assertNotNull(emptyList); + assertTrue(emptyList instanceof ArrayList); + assertEquals(5, emptyList.size()); + + List fullList = bean.getFullList(); + assertNotNull(fullList); + assertTrue(fullList instanceof Vector); + assertEquals(7, fullList.size()); + + } + + +} Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/shale/trunk/tiger/src/test/org/apache/shale/tiger/faces/VariableResolverImpl4TestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]