Title: [1537] trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios: JBEHAVE-237: Added claims_with_null_calendar.scenario to verify NPE does not occur when calendar converter returns null.

Diff

Added: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/converters/CalendarConverter.java (0 => 1537)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/converters/CalendarConverter.java	                        (rev 0)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/converters/CalendarConverter.java	2010-02-02 16:26:48 UTC (rev 1537)
@@ -0,0 +1,47 @@
+package org.jbehave.examples.trader.converters;
+
+import java.lang.reflect.Type;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+import org.apache.commons.lang.StringUtils;
+import org.jbehave.scenario.steps.ParameterConverters;
+import org.jbehave.scenario.steps.ParameterConverters.ParameterConverter;
+
+public class CalendarConverter implements ParameterConverter {
+    
+    private final SimpleDateFormat dateFormat;
+ 
+    public CalendarConverter(String dateFormat) {
+        this.dateFormat = new SimpleDateFormat(dateFormat);
+    }
+ 
+    public boolean accept(Type type) {
+        if (type instanceof Class<?>) {
+            return Calendar.class.isAssignableFrom((Class<?>) type);
+        }
+        return false;
+    }
+ 
+    public Object convertValue(String value, Type type) {
+        try {
+            if (StringUtils.isBlank(value) || "none".equals(value)) return null;
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTime(dateFormat.parse(value));
+            return calendar;
+        } catch (ParseException e) {
+            throw new RuntimeException("Could not convert value "+value+" with format "+dateFormat.toPattern());
+        }
+    }
+
+    public static ParameterConverter monthDayYear() {
+        return new CalendarConverter("MM/dd/yyyy");
+    }
+    
+    public static ParameterConverters monthDayYearWrapped() {
+        return new ParameterConverters(monthDayYear());
+    }
+ 
+}
+

Added: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/ClaimsWithNullCalendar.java (0 => 1537)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/ClaimsWithNullCalendar.java	                        (rev 0)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/ClaimsWithNullCalendar.java	2010-02-02 16:26:48 UTC (rev 1537)
@@ -0,0 +1,81 @@
+package org.jbehave.examples.trader.scenarios;
+
+import static org.jbehave.scenario.reporters.ScenarioReporterBuilder.Format.CONSOLE;
+import static org.jbehave.scenario.reporters.ScenarioReporterBuilder.Format.HTML;
+import static org.jbehave.scenario.reporters.ScenarioReporterBuilder.Format.TXT;
+import static org.jbehave.scenario.reporters.ScenarioReporterBuilder.Format.XML;
+
+import java.util.Calendar;
+
+import org.jbehave.examples.trader.converters.CalendarConverter;
+import org.jbehave.scenario.JUnitScenario;
+import org.jbehave.scenario.PropertyBasedConfiguration;
+import org.jbehave.scenario.RunnableScenario;
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Named;
+import org.jbehave.scenario.annotations.Then;
+import org.jbehave.scenario.parser.ClasspathScenarioDefiner;
+import org.jbehave.scenario.parser.PatternScenarioParser;
+import org.jbehave.scenario.parser.ScenarioDefiner;
+import org.jbehave.scenario.parser.ScenarioNameResolver;
+import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver;
+import org.jbehave.scenario.reporters.FilePrintStreamFactory;
+import org.jbehave.scenario.reporters.ScenarioReporter;
+import org.jbehave.scenario.reporters.ScenarioReporterBuilder;
+import org.jbehave.scenario.steps.ParameterConverters;
+import org.jbehave.scenario.steps.SilentStepMonitor;
+import org.jbehave.scenario.steps.StepMonitor;
+import org.jbehave.scenario.steps.StepsConfiguration;
+import org.jbehave.scenario.steps.StepsFactory;
+
+public class ClaimsWithNullCalendar extends JUnitScenario {
+
+    private static ScenarioNameResolver converter = new UnderscoredCamelCaseResolver(".scenario");
+
+    public ClaimsWithNullCalendar(){
+        this(ClaimsWithNullCalendar.class);
+    }
+
+    public ClaimsWithNullCalendar(final Class<? extends RunnableScenario> scenarioClass) {
+        super(new PropertyBasedConfiguration() {
+            @Override
+            public ScenarioDefiner forDefiningScenarios() {
+                return new ClasspathScenarioDefiner(converter, new PatternScenarioParser(keywords()));
+            }
+
+            @Override
+            public ScenarioReporter forReportingScenarios() {
+                return new ScenarioReporterBuilder(new FilePrintStreamFactory(scenarioClass, converter))
+                            .with(CONSOLE)
+                            .with(TXT)
+                            .with(HTML)
+                            .with(XML)
+                            .build();
+            }
+
+        });
+
+        StepsConfiguration configuration = new StepsConfiguration();
+        StepMonitor monitor = new SilentStepMonitor();
+		configuration.useParameterConverters(new ParameterConverters(
+        		monitor, new CalendarConverter("dd/MM/yyyy"))); 
+		configuration.useMonitor(monitor);        
+        addSteps(new StepsFactory(configuration).createCandidateSteps(new CalendarSteps()));
+    }
+
+    public static class CalendarSteps {
+     
+
+        @Given("a plan with calendar date of <date>") 
+        public void aPlanWithCalendar(@Named("date") Calendar calendar) {
+            System.out.println(calendar);
+        }
+        
+        @Then("the claimant should receive an amount of <amount>")         
+        public void theClaimantReceivesAmount(@Named("amount") double amount) {
+            System.out.println(amount);
+        }
+
+    }
+
+}

Added: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/claims_with_null_calendar.scenario (0 => 1537)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/claims_with_null_calendar.scenario	                        (rev 0)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/claims_with_null_calendar.scenario	2010-02-02 16:26:48 UTC (rev 1537)
@@ -0,0 +1,8 @@
+Given a plan with calendar date of <date>
+Then the claimant should receive an amount of <amount>
+
+Examples:
+|date|amount|
+|none       |0.0|
+|01/06/2010 |2.15|
+


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to