diff --git a/build.properties b/build.properties index 2d58a54a..dbc04e00 100644 --- a/build.properties +++ b/build.properties @@ -4,7 +4,7 @@ natural.name=spring-webflow project.key=SWF ivy.cache.dir=${basedir}/../ivy-cache integration.repo.dir=${basedir}/../integration-repo -source.version=1.4 +source.version=1.5 javadoc.exclude.package.names=org/springframework/webflow/samples/** # For when releasing diff --git a/spring-binding/.settings/org.eclipse.jdt.core.prefs b/spring-binding/.settings/org.eclipse.jdt.core.prefs index 56da8f05..873535ea 100644 --- a/spring-binding/.settings/org.eclipse.jdt.core.prefs +++ b/spring-binding/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,4 @@ -#Thu Jul 10 09:39:34 BST 2008 +#Tue Aug 12 10:58:43 EDT 2008 eclipse.preferences.version=1 org.eclipse.jdt.core.codeComplete.argumentPrefixes= org.eclipse.jdt.core.codeComplete.argumentSuffixes= @@ -9,22 +9,22 @@ org.eclipse.jdt.core.codeComplete.localSuffixes= org.eclipse.jdt.core.codeComplete.staticFieldPrefixes= org.eclipse.jdt.core.codeComplete.staticFieldSuffixes= org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.4 +org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.doc.comment.support=enabled org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning -org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.deprecation=warning org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning @@ -79,7 +79,7 @@ org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=di org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.3 +org.eclipse.jdt.core.compiler.source=1.5 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 diff --git a/spring-binding/src/main/java/org/springframework/binding/convert/converters/StringToEnum.java b/spring-binding/src/main/java/org/springframework/binding/convert/converters/StringToEnum.java new file mode 100644 index 00000000..9aaf7193 --- /dev/null +++ b/spring-binding/src/main/java/org/springframework/binding/convert/converters/StringToEnum.java @@ -0,0 +1,37 @@ +/* + * Copyright 2004-2008 the original author or authors. + * + * 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.springframework.binding.convert.converters; + +/** + * Converts from a textual representation to a {@link Enum}. The text should be the enum's label. + * + * @author Scott Andrews + */ +public class StringToEnum extends StringToObject { + + public StringToEnum() { + super(Enum.class); + } + + protected Object toObject(String string, Class targetClass) throws Exception { + return Enum.valueOf(targetClass, string); + } + + protected String toString(Object object) throws Exception { + return object.toString(); + } + +} \ No newline at end of file diff --git a/spring-binding/src/main/java/org/springframework/binding/convert/service/DefaultConversionService.java b/spring-binding/src/main/java/org/springframework/binding/convert/service/DefaultConversionService.java index d300bbb0..3dd48b78 100644 --- a/spring-binding/src/main/java/org/springframework/binding/convert/service/DefaultConversionService.java +++ b/spring-binding/src/main/java/org/springframework/binding/convert/service/DefaultConversionService.java @@ -29,6 +29,7 @@ import org.springframework.binding.convert.converters.StringToByte; import org.springframework.binding.convert.converters.StringToCharacter; import org.springframework.binding.convert.converters.StringToDate; import org.springframework.binding.convert.converters.StringToDouble; +import org.springframework.binding.convert.converters.StringToEnum; import org.springframework.binding.convert.converters.StringToFloat; import org.springframework.binding.convert.converters.StringToInteger; import org.springframework.binding.convert.converters.StringToLabeledEnum; @@ -36,6 +37,7 @@ import org.springframework.binding.convert.converters.StringToLocale; import org.springframework.binding.convert.converters.StringToLong; import org.springframework.binding.convert.converters.StringToShort; import org.springframework.core.enums.LabeledEnum; +import org.springframework.util.ClassUtils; /** * Default, local implementation of a conversion service. Will automatically register from string converters for @@ -72,6 +74,9 @@ public class DefaultConversionService extends GenericConversionService { addConverter(new StringToLabeledEnum()); addConverter(new ObjectToCollection(this)); addConverter(new NumberToNumber()); + if (ClassUtils.isPresent("java.lang.Enum", this.getClass().getClassLoader())) { + addConverter(new StringToEnum()); + } } protected void addDefaultAliases() { diff --git a/spring-binding/src/test/java/org/springframework/binding/convert/converters/StringToEnumTests.java b/spring-binding/src/test/java/org/springframework/binding/convert/converters/StringToEnumTests.java new file mode 100644 index 00000000..905b7e48 --- /dev/null +++ b/spring-binding/src/test/java/org/springframework/binding/convert/converters/StringToEnumTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2004-2008 the original author or authors. + * + * 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.springframework.binding.convert.converters; + +import junit.framework.TestCase; + +/** + * Test case for the default conversion service. + * + * @author Scott Andrews + */ +public class StringToEnumTests extends TestCase { + + StringToEnum converter = new StringToEnum(); + + public void testStringToEnum() throws Exception { + TestEnum test = (TestEnum) converter.toObject("SUCCESS", TestEnum.class); + assertEquals(TestEnum.SUCCESS, test); + } + + public void testBadStringToEnum() throws Exception { + try { + converter.toObject("FAIL", TestEnum.class); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // expected + } + } + + public void testEnumToString() throws Exception { + String test = converter.toString(TestEnum.SUCCESS); + assertEquals("SUCCESS", test); + } + + enum TestEnum { + SUCCESS; + } + +} diff --git a/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java b/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java index 426678dd..5ada66e7 100644 --- a/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java +++ b/spring-binding/src/test/java/org/springframework/binding/convert/service/DefaultConversionServiceTests.java @@ -236,7 +236,7 @@ public class DefaultConversionServiceTests extends TestCase { assertSame(formatterConverter, se.getConverter()); } } - assertEquals(14, converters.size()); + assertEquals(15, converters.size()); } private static class CustomConverter implements Converter {