diff --git a/spring-webflow/changelog.txt b/spring-webflow/changelog.txt index 9c7716bf..5c6ac611 100644 --- a/spring-webflow/changelog.txt +++ b/spring-webflow/changelog.txt @@ -15,6 +15,7 @@ Package org.springframework.binding Package org.springframework.webflow.action * FormAction methods doBind() and createBinder() now declare "throws Exception". * FormAction now consistently uses the getters to get property values (SWF-251). +* ResultObjectBasedEventFactory now uses java.lang.Enum.name() to get the name of a Java 5 enum (SWF-250). Package org.springframework.webflow.config * Fixed bug in WebFlowConfigNamespaceHandler that caused the type attribute of the repository tag diff --git a/spring-webflow/src/main/java/org/springframework/webflow/action/ResultObjectBasedEventFactory.java b/spring-webflow/src/main/java/org/springframework/webflow/action/ResultObjectBasedEventFactory.java index 93a18020..9f6dfe81 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/action/ResultObjectBasedEventFactory.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/action/ResultObjectBasedEventFactory.java @@ -85,8 +85,8 @@ public class ResultObjectBasedEventFactory extends EventFactorySupport implement return event(source, resultId, getResultAttributeName(), resultObject); } else if (isJdk5Enum(resultObject.getClass())) { - // java.lang.Enum.toString() returns the name! - return event(source, resultObject.toString(), getResultAttributeName(), resultObject); + String eventId = EnumNameResolver.getEnumName(resultObject); + return event(source, eventId, getResultAttributeName(), resultObject); } else if (isString(resultObject.getClass())) { return event(source, (String)resultObject); @@ -134,4 +134,14 @@ public class ResultObjectBasedEventFactory extends EventFactorySupport implement private boolean isEvent(Class type) { return Event.class.isAssignableFrom(type); } + + /** + * Simple helper class with Java 5 specific code factored out to keep + * the containing class JDK 1.3 compatible. + */ + private static class EnumNameResolver { + public static String getEnumName(Object enumValue) { + return ((java.lang.Enum)enumValue).name(); + } + } } \ No newline at end of file diff --git a/spring-webflow/src/test/java/org/springframework/webflow/action/ResultObjectBasedEventFactoryTests.java b/spring-webflow/src/test/java/org/springframework/webflow/action/ResultObjectBasedEventFactoryTests.java new file mode 100644 index 00000000..254fbcc0 --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/action/ResultObjectBasedEventFactoryTests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2004-2007 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.webflow.action; + +import java.util.Date; + +import junit.framework.TestCase; + +import org.springframework.core.enums.StaticLabeledEnum; +import org.springframework.webflow.execution.Event; +import org.springframework.webflow.test.MockRequestContext; + +/** + * Test case for {@link ResultObjectBasedEventFactory}. + * + * @author Erwin Vervaet + */ +public class ResultObjectBasedEventFactoryTests extends TestCase { + + private ResultObjectBasedEventFactory factory = new ResultObjectBasedEventFactory(); + + public void testNull() { + Event event = factory.createResultEvent(this, null, new MockRequestContext()); + assertEquals(factory.getNullEventId(), event.getId()); + } + + public void testBoolean() { + Event event = factory.createResultEvent(this, Boolean.TRUE, new MockRequestContext()); + assertEquals(factory.getYesEventId(), event.getId()); + event = factory.createResultEvent(this, Boolean.FALSE, new MockRequestContext()); + assertEquals(factory.getNoEventId(), event.getId()); + } + + public void testLabeledEnum() { + Event event = factory.createResultEvent(this, MyLabeledEnum.A, new MockRequestContext()); + assertEquals("A", event.getId()); + assertSame(MyLabeledEnum.A, event.getAttributes().get("result")); + } + + public static class MyLabeledEnum extends StaticLabeledEnum { + public static final MyLabeledEnum A = new MyLabeledEnum(0, "A"); + public static final MyLabeledEnum B = new MyLabeledEnum(0, "B"); + + private MyLabeledEnum(int code, String label) { + super(code, label); + } + + public String toString() { + return "MyLabeledEnum " + getLabel(); + } + } + +/* + public void testJava5Enum() { + Event event = factory.createResultEvent(this, MyEnum.A, new MockRequestContext()); + assertEquals("A", event.getId()); + assertSame(MyEnum.A, event.getAttributes().get("result")); + } + + public static enum MyEnum { + A, + B; + + public String toString() { + return "MyEnum " + name(); + } + } +*/ + + public void testString() { + Event event = factory.createResultEvent(this, "foobar", new MockRequestContext()); + assertEquals("foobar", event.getId()); + } + + public void testEvent() { + Event orig = new Event(this, "test"); + Event event = factory.createResultEvent(this, orig, new MockRequestContext()); + assertSame(orig, event); + } + + public void testUnsupported() { + try { + factory.createResultEvent(this, new Date(), new MockRequestContext()); + fail(); + } + catch (IllegalArgumentException e) { + // expected + } + } +}