From bb3671e597362b1e08c29b236e9af50b2210384e Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Fri, 4 Apr 2008 23:46:42 +0000 Subject: [PATCH] sample tests --- .../el/DefaultExpressionFactoryUtils.java | 14 +- spring-webflow-samples/booking-faces/ivy.xml | 39 ++-- .../booking/BookingFlowExecutionTests.java | 92 ++++++++ spring-webflow-samples/booking-mvc/.classpath | 221 +----------------- spring-webflow-samples/booking-mvc/ivy.xml | 26 +-- .../booking/BookingFlowExecutionTests.java | 92 ++++++++ .../DefaultExpressionParserFactory.java | 8 +- .../WebFlowOgnlExpressionParser.java | 6 +- 8 files changed, 233 insertions(+), 265 deletions(-) create mode 100644 spring-webflow-samples/booking-faces/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java create mode 100644 spring-webflow-samples/booking-mvc/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java diff --git a/spring-binding/src/main/java/org/springframework/binding/expression/el/DefaultExpressionFactoryUtils.java b/spring-binding/src/main/java/org/springframework/binding/expression/el/DefaultExpressionFactoryUtils.java index af09382e..678ce4a4 100644 --- a/spring-binding/src/main/java/org/springframework/binding/expression/el/DefaultExpressionFactoryUtils.java +++ b/spring-binding/src/main/java/org/springframework/binding/expression/el/DefaultExpressionFactoryUtils.java @@ -31,26 +31,30 @@ public class DefaultExpressionFactoryUtils { try { expressionFactoryClass = ClassUtils.forName(getDefaultExpressionFactoryClassName()); } catch (ClassNotFoundException e) { - throw new IllegalStateException( + IllegalStateException ise = new IllegalStateException( "The default ExpressionFactory class '" + getDefaultExpressionFactoryClassName() + "' could not be found in the classpath. " + "Please add this to your classpath or set the default ExpressionFactory class name to something that is in the classpath."); + ise.initCause(e); + throw ise; } catch (NoClassDefFoundError e) { - throw new IllegalStateException( + IllegalStateException ise = new IllegalStateException( "The default ExpressionFactory class '" + getDefaultExpressionFactoryClassName() + "' could not be found in the classpath. " + "Please add this to your classpath or set the default ExpressionFactory class name to something that is in the classpath."); + ise.initCause(e); + throw ise; } try { return (ExpressionFactory) expressionFactoryClass.newInstance(); } catch (Exception e) { - IllegalStateException iae = new IllegalStateException("An instance of the default ExpressionFactory '" + IllegalStateException ise = new IllegalStateException("An instance of the default ExpressionFactory '" + getDefaultExpressionFactoryClassName() + "' could not be instantiated. Check your EL implementation configuration."); - iae.initCause(e); - throw iae; + ise.initCause(e); + throw ise; } } } \ No newline at end of file diff --git a/spring-webflow-samples/booking-faces/ivy.xml b/spring-webflow-samples/booking-faces/ivy.xml index 139bf564..237b198f 100755 --- a/spring-webflow-samples/booking-faces/ivy.xml +++ b/spring-webflow-samples/booking-faces/ivy.xml @@ -21,35 +21,30 @@ + + + + + + - - - - - - + + + + + + + + - - - - - + + + - - - - - - - - - - - diff --git a/spring-webflow-samples/booking-faces/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java b/spring-webflow-samples/booking-faces/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java new file mode 100644 index 00000000..b5aab792 --- /dev/null +++ b/spring-webflow-samples/booking-faces/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java @@ -0,0 +1,92 @@ +package org.springframework.webflow.samples.booking; + +import org.easymock.EasyMock; +import org.springframework.faces.model.converter.FacesConversionService; +import org.springframework.webflow.config.FlowDefinitionResource; +import org.springframework.webflow.config.FlowDefinitionResourceFactory; +import org.springframework.webflow.core.collection.LocalAttributeMap; +import org.springframework.webflow.core.collection.MutableAttributeMap; +import org.springframework.webflow.test.MockExternalContext; +import org.springframework.webflow.test.MockFlowBuilderContext; +import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests; + +public class BookingFlowExecutionTests extends AbstractXmlFlowExecutionTests { + + private BookingService bookingService; + + private boolean persistCalled; + + protected void setUp() { + bookingService = EasyMock.createMock(BookingService.class); + } + + @Override + protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) { + return resourceFactory.createFileResource("src/main/webapp/WEB-INF/flows/booking/booking.xml"); + } + + @Override + protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) { + builderContext.registerBean("bookingService", bookingService); + builderContext.getFlowBuilderServices().setConversionService(new FacesConversionService()); + } + + public void testStartBookingFlow() { + Booking booking = createTestBooking(); + + EasyMock.expect(bookingService.createBooking(1L, "keith")).andReturn(booking); + + EasyMock.replay(bookingService); + + MutableAttributeMap input = new LocalAttributeMap(); + input.put("hotelId", "1"); + MockExternalContext context = new MockExternalContext(); + context.setCurrentUser("keith"); + startFlow(input, context); + + assertCurrentStateEquals("enterBookingDetails"); + assertResponseWrittenEquals("enterBookingDetails", context); + assertTrue(getRequiredFlowAttribute("booking") instanceof Booking); + + EasyMock.verify(bookingService); + } + + public void testEnterBookingDetails_Proceed() { + setCurrentState("enterBookingDetails"); + getFlowScope().put("booking", createTestBooking()); + + MockExternalContext context = new MockExternalContext(); + context.setEventId("proceed"); + resumeFlow(context); + + assertCurrentStateEquals("reviewBooking"); + assertResponseWrittenEquals("reviewBooking", context); + } + + public void testReviewBooking_Confirm() { + setCurrentState("reviewBooking"); + getFlowScope().put("booking", createTestBooking()); + getFlowScope().put("entityManager", new Object() { + public void persist(Booking booking) { + persistCalled = true; + } + }); + MockExternalContext context = new MockExternalContext(); + context.setEventId("confirm"); + resumeFlow(context); + + assertTrue(persistCalled); + assertFlowExecutionEnded(); + assertFlowExecutionOutcomeEquals("bookingConfirmed"); + } + + private Booking createTestBooking() { + Hotel hotel = new Hotel(); + hotel.setId(1L); + hotel.setName("Jameson Inn"); + User user = new User("keith", "pass", "Keith Donald"); + Booking booking = new Booking(hotel, user); + return booking; + } + +} diff --git a/spring-webflow-samples/booking-mvc/.classpath b/spring-webflow-samples/booking-mvc/.classpath index 309cad8b..ee222953 100755 --- a/spring-webflow-samples/booking-mvc/.classpath +++ b/spring-webflow-samples/booking-mvc/.classpath @@ -6,222 +6,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - \ No newline at end of file + diff --git a/spring-webflow-samples/booking-mvc/ivy.xml b/spring-webflow-samples/booking-mvc/ivy.xml index d935ce9b..48750e67 100755 --- a/spring-webflow-samples/booking-mvc/ivy.xml +++ b/spring-webflow-samples/booking-mvc/ivy.xml @@ -21,38 +21,36 @@ - - + + + - - + + + + + + - - - - - - - - - + + - + diff --git a/spring-webflow-samples/booking-mvc/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java b/spring-webflow-samples/booking-mvc/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java new file mode 100644 index 00000000..efffd991 --- /dev/null +++ b/spring-webflow-samples/booking-mvc/src/test/java/org/springframework/webflow/samples/booking/BookingFlowExecutionTests.java @@ -0,0 +1,92 @@ +package org.springframework.webflow.samples.booking; + +import org.easymock.EasyMock; +import org.springframework.faces.model.converter.FacesConversionService; +import org.springframework.webflow.config.FlowDefinitionResource; +import org.springframework.webflow.config.FlowDefinitionResourceFactory; +import org.springframework.webflow.core.collection.LocalAttributeMap; +import org.springframework.webflow.core.collection.MutableAttributeMap; +import org.springframework.webflow.test.MockExternalContext; +import org.springframework.webflow.test.MockFlowBuilderContext; +import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests; + +public class BookingFlowExecutionTests extends AbstractXmlFlowExecutionTests { + + private BookingService bookingService; + + private boolean persistCalled; + + protected void setUp() { + bookingService = EasyMock.createMock(BookingService.class); + } + + @Override + protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) { + return resourceFactory.createFileResource("src/main/webapp/WEB-INF/hotels/booking/booking.xml"); + } + + @Override + protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) { + builderContext.registerBean("bookingService", bookingService); + builderContext.getFlowBuilderServices().setConversionService(new FacesConversionService()); + } + + public void testStartBookingFlow() { + Booking booking = createTestBooking(); + + EasyMock.expect(bookingService.createBooking(1L, "keith")).andReturn(booking); + + EasyMock.replay(bookingService); + + MutableAttributeMap input = new LocalAttributeMap(); + input.put("hotelId", "1"); + MockExternalContext context = new MockExternalContext(); + context.setCurrentUser("keith"); + startFlow(input, context); + + assertCurrentStateEquals("enterBookingDetails"); + assertResponseWrittenEquals("enterBookingDetails", context); + assertTrue(getRequiredFlowAttribute("booking") instanceof Booking); + + EasyMock.verify(bookingService); + } + + public void testEnterBookingDetails_Proceed() { + setCurrentState("enterBookingDetails"); + getFlowScope().put("booking", createTestBooking()); + + MockExternalContext context = new MockExternalContext(); + context.setEventId("proceed"); + resumeFlow(context); + + assertCurrentStateEquals("reviewBooking"); + assertResponseWrittenEquals("reviewBooking", context); + } + + public void testReviewBooking_Confirm() { + setCurrentState("reviewBooking"); + getFlowScope().put("booking", createTestBooking()); + getFlowScope().put("entityManager", new Object() { + public void persist(Booking booking) { + persistCalled = true; + } + }); + MockExternalContext context = new MockExternalContext(); + context.setEventId("confirm"); + resumeFlow(context); + + assertTrue(persistCalled); + assertFlowExecutionEnded(); + assertFlowExecutionOutcomeEquals("bookingConfirmed"); + } + + private Booking createTestBooking() { + Hotel hotel = new Hotel(); + hotel.setId(1L); + hotel.setName("Jameson Inn"); + User user = new User("keith", "pass", "Keith Donald"); + Booking booking = new Booking(hotel, user); + return booking; + } + +} diff --git a/spring-webflow/src/main/java/org/springframework/webflow/expression/DefaultExpressionParserFactory.java b/spring-webflow/src/main/java/org/springframework/webflow/expression/DefaultExpressionParserFactory.java index f13e47a4..d7b2c7d0 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/expression/DefaultExpressionParserFactory.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/expression/DefaultExpressionParserFactory.java @@ -90,11 +90,15 @@ public final class DefaultExpressionParserFactory { ClassUtils.forName("ognl.Ognl"); return new WebFlowOgnlExpressionParser(); } catch (ClassNotFoundException ex) { - throw new IllegalStateException( + IllegalStateException ise = new IllegalStateException( "Unable to create the default expression parser for Spring Web Flow: Neither a Unified EL implementation or OGNL could be found."); + ise.initCause(ex); + throw ise; } catch (NoClassDefFoundError ex) { - throw new IllegalStateException( + IllegalStateException ise = new IllegalStateException( "Unable to create the default expression parser for Spring Web Flow: Neither a Unified EL implementation or OGNL could be found."); + ise.initCause(ex); + throw ise; } } } diff --git a/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java b/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java index b33c0336..a1c88b5f 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/expression/WebFlowOgnlExpressionParser.java @@ -17,8 +17,6 @@ package org.springframework.webflow.expression; import java.util.Map; -import javax.el.PropertyNotWritableException; - import ognl.ObjectPropertyAccessor; import ognl.OgnlException; import ognl.PropertyAccessor; @@ -143,10 +141,10 @@ public class WebFlowOgnlExpressionParser extends OgnlExpressionParser { String property = name.toString(); RequestContext requestContext = (RequestContext) target; if (property.equals("flowRequestContext")) { - throw new PropertyNotWritableException("The 'flowRequestContext' variable is not writeable"); + throw new OgnlException("The 'flowRequestContext' variable is not writeable"); } if (securityPresent && property.equals("currentUser")) { - throw new PropertyNotWritableException("The 'currentUser' variable is not writeable"); + throw new OgnlException("The 'currentUser' variable is not writeable"); } if (requestContext.getRequestScope().contains(property)) { if (logger.isDebugEnabled()) {