You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2007/10/26 11:41:45 UTC
svn commit: r588584 - in /myfaces/core/branches/1_2_1:
api/src/main/java/javax/faces/convert/ api/src/test/java/javax/faces/convert/
impl/src/main/java/org/apache/myfaces/application/
impl/src/test/java/org/apache/myfaces/application/
Author: mmarinschek
Date: Fri Oct 26 02:41:44 2007
New Revision: 588584
URL: http://svn.apache.org/viewvc?rev=588584&view=rev
Log:
https://issues.apache.org/jira/browse/MYFACES-1684 (MYFACES-1684): myfaces 1.2 cant handle enum type - thanks to Achim Hügen and Michael Kurz
Added:
myfaces/core/branches/1_2_1/api/src/test/java/javax/faces/convert/EnumConverterTest.java
Modified:
myfaces/core/branches/1_2_1/api/src/main/java/javax/faces/convert/EnumConverter.java
myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java
Modified: myfaces/core/branches/1_2_1/api/src/main/java/javax/faces/convert/EnumConverter.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/api/src/main/java/javax/faces/convert/EnumConverter.java?rev=588584&r1=588583&r2=588584&view=diff
==============================================================================
--- myfaces/core/branches/1_2_1/api/src/main/java/javax/faces/convert/EnumConverter.java (original)
+++ myfaces/core/branches/1_2_1/api/src/main/java/javax/faces/convert/EnumConverter.java Fri Oct 26 02:41:44 2007
@@ -48,7 +48,7 @@
if (facesContext == null) throw new NullPointerException("facesContext can not be null");
if (uiComponent == null) throw new NullPointerException("uiComponent can not be null");
if (value == null) return "";
-
+ checkTargetClass(facesContext, uiComponent, value);
for (Object enumConstant : targetClass.getEnumConstants()) {
if (enumConstant == value) return enumConstant.toString();
@@ -61,16 +61,10 @@
if (facesContext == null) throw new NullPointerException("facesContext");
if (uiComponent == null) throw new NullPointerException("uiComponent");
if (value == null) return null;
- if (targetClass == null) {
- Object[] params = new Object[]{value, _MessageUtils.getLabel(facesContext, uiComponent)};
- throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
- ENUM_NO_CLASS_ID,
- params));
- }
-
value = value.trim();
if (value.length() == 0) return null;
-
+ checkTargetClass(facesContext, uiComponent, value);
+
// we know targetClass and value can't be null, so we can use Enum.valueOf
// instead of the hokey looping called for in the javadoc
try {
@@ -85,6 +79,15 @@
params));
}
}
+
+ private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object value) {
+ if (targetClass == null) {
+ Object[] params = new Object[]{value, _MessageUtils.getLabel(facesContext, uiComponent)};
+ throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
+ ENUM_NO_CLASS_ID,
+ params));
+ }
+ }
// find the first constant value of the targetClass and return as a String
private String firstConstantOfEnum() {
Added: myfaces/core/branches/1_2_1/api/src/test/java/javax/faces/convert/EnumConverterTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/api/src/test/java/javax/faces/convert/EnumConverterTest.java?rev=588584&view=auto
==============================================================================
--- myfaces/core/branches/1_2_1/api/src/test/java/javax/faces/convert/EnumConverterTest.java (added)
+++ myfaces/core/branches/1_2_1/api/src/test/java/javax/faces/convert/EnumConverterTest.java Fri Oct 26 02:41:44 2007
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2007 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 javax.faces.convert;
+
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ * This testcase test <code>javax.faces.convert.EnumConverter</code>.
+ *
+ * @author Michael Kurz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class EnumConverterTest extends AbstractJsfTestCase {
+ private enum testEnum {ITEM1, ITEM2};
+ private EnumConverter converter;
+
+ public EnumConverterTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ converter = new EnumConverter(testEnum.class);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ converter = null;
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
+ */
+ public void testGetAsObject() {
+ UIInput input = new UIInput();
+ Object convertedObj = converter.getAsObject(FacesContext.getCurrentInstance(), input, "ITEM2");
+ assertEquals(convertedObj, testEnum.ITEM2);
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
+ */
+ public void testGetAsObjectNull() {
+ UIInput input = new UIInput();
+ Object convertedObj = converter.getAsObject(FacesContext.getCurrentInstance(), input, null);
+ assertNull(convertedObj);
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
+ */
+ public void testGetAsObjectNoEnum() {
+ UIInput input = new UIInput();
+ try {
+ converter.getAsObject(FacesContext.getCurrentInstance(), input, "NO_ENUM_CONST");
+ fail("Converter exception should be thrown");
+ } catch (ConverterException e) {
+ // should be thrown
+ }
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsObject(FacesContext, javax.faces.component.UIComponent, String)}.
+ */
+ public void testGetAsObjectNoClassSet() {
+ Converter testConverter = new EnumConverter();
+ UIInput input = new UIInput();
+ try {
+ testConverter.getAsObject(FacesContext.getCurrentInstance(), input, "ITEM2");
+ fail("Converter exception should be thrown");
+ } catch (ConverterException e) {
+ // should be thrown
+ }
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
+ */
+ public void testGetAsString() {
+ UIInput input = new UIInput();
+ String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, testEnum.ITEM1);
+ assertEquals(convertedStr, testEnum.ITEM1.toString());
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
+ */
+ public void testGetAsStringNull() {
+ UIInput input = new UIInput();
+ String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, null);
+ assertEquals(convertedStr, "");
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
+ */
+ public void testGetAsStringNoEnum() {
+ UIInput input = new UIInput();
+ String convertedStr = converter.getAsString(FacesContext.getCurrentInstance(), input, "HALLO");
+ assertEquals(convertedStr, "HALLO");
+ }
+
+ /**
+ * Test method for
+ * {@link javax.faces.convert.EnumConverter#getAsString(FacesContext, javax.faces.component.UIComponent, Object)}.
+ */
+ public void testGetAsStringNoClassSet() {
+ Converter testConverter = new EnumConverter();
+ UIInput input = new UIInput();
+ try {
+ testConverter.getAsString(FacesContext.getCurrentInstance(), input, testEnum.ITEM1);
+ fail("Converter exception should be thrown");
+ } catch (ConverterException e) {
+ // should be thrown
+ }
+ }
+}
Modified: myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java?rev=588584&r1=588583&r2=588584&view=diff
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java (original)
+++ myfaces/core/branches/1_2_1/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java Fri Oct 26 02:41:44 2007
@@ -698,6 +698,12 @@
// Locate a Converter registered for the target class itself.
String converterClassName = _converterClassNameToClassMap.get(targetClass);
+ // Get EnumConverter for enum classes with no special converter, check
+ // here as recursive call with java.lang.Enum will not work
+ if (converterClassName == null && targetClass.isEnum()) {
+ converterClassName = _converterClassNameToClassMap.get(Enum.class);
+ }
+
// Locate a Converter registered for interfaces that are
// implemented by the target class (directly or indirectly).
if (converterClassName == null)
Modified: myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java?rev=588584&r1=588583&r2=588584&view=diff
==============================================================================
--- myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java (original)
+++ myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java Fri Oct 26 02:41:44 2007
@@ -34,6 +34,8 @@
import javax.faces.component.UIOutput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.EnumConverter;
import javax.faces.el.ReferenceSyntaxException;
import javax.faces.el.VariableResolver;
@@ -226,4 +228,19 @@
};
assertSame(bundle, myapp.getResourceBundle(context, var));
}
+
+ private enum MyEnum {VALUE1, VALUE2};
+
+ /**
+ * Test method for
+ * {@link javax.faces.application.Application#createConverter(java.lang.Class)}.
+ */
+ public void testCreateEnumConverter() throws Exception
+ {
+ app.addConverter(Enum.class, EnumConverter.class.getName());
+
+ Converter converter = app.createConverter(MyEnum.class);
+ assertNotNull(converter);
+ assertEquals(converter.getClass(), EnumConverter.class);
+ }
}