Title: [1388] trunk/core/distribution/src/site/content: JBEHAVE-209: Added ParameterConverter for ExamplesTable and configured it as one of the default converters.

Diff

Modified: trunk/core/distribution/src/site/content/sitemap.xml (1387 => 1388)

--- trunk/core/distribution/src/site/content/sitemap.xml	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/distribution/src/site/content/sitemap.xml	2009-11-25 13:11:41 UTC (rev 1388)
@@ -32,6 +32,7 @@
     <page>parameter-injection.html</page>
     <page>parameter-converters.html</page>
     <page>table-examples.html</page>
+    <page>table-parameters.html</page>
     <page>given-scenarios.html</page>
   </section>
   <section>

Modified: trunk/core/distribution/src/site/content/table-examples.html (1387 => 1388)

--- trunk/core/distribution/src/site/content/table-examples.html	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/distribution/src/site/content/table-examples.html	2009-11-25 13:11:41 UTC (rev 1388)
@@ -8,7 +8,7 @@
 
 <h2>Table Examples</h2>
 
-<p>Scenario writer often find themselves repeating scenarios, or
+<p>Scenario writers often find themselves repeating scenarios, or
 parts thereof, by simply changing some parameter values. These are ideal
 candidates for using JBehave Table Examples feature. Let's look at the
 example:</p>

Added: trunk/core/distribution/src/site/content/table-parameters.html (0 => 1388)

--- trunk/core/distribution/src/site/content/table-parameters.html	                        (rev 0)
+++ trunk/core/distribution/src/site/content/table-parameters.html	2009-11-25 13:11:41 UTC (rev 1388)
@@ -0,0 +1,63 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+<title>Table Parameters</title>
+</head>
+
+<body>
+
+<h2>Table Parameters</h2>
+
+<p>Scenario writers may want to express a parameter in a tabular structure.
+For example:</p>
+
+<pre class="brush: bdd">
+Given the traders: 
+|name|rank|
+|Larry|Stooge 3|
+|Moe|Stooge 1|
+|Curly|Stooge 2|
+When a wildcard search ".*y" is executed
+Then the traders returned are:
+|name|rank|
+|Larry|Stooge 3|
+|Curly|Stooge 2|
+</pre>
+
+<p>Now, JBehave supports multi-line parameters out-of-the-box.   Next, we need only combine this feature with
+the table parsing functionality already seen in the <a href="" examples</a>:</p>
+
+<pre class="brush: java">
+    @Given("the traders: $tradersTable")
+    public void theTraders(ExamplesTable tradersTable) {
+        // tradersTable is automatically parsed using the multi-line String input
+        // next we need to interpret its content to create a list of traders
+        // then we can use the traders create in subsequent steps
+        this.traders = toTraders(tradersTable);
+    }
+
+    private List toTraders(ExamplesTable table) {
+        List traders = new ArrayList();
+        List rows = table.getRows();
+        for (Map row : rows) {
+            String name = row.get("name");
+            String rank = row.get("rank");
+            traders.add(new Trader(name, rank));
+        }
+        Collections.sort(traders);
+        return traders;
+    }
+</pre>
+
+<p><b>Note</b>: We are using the same table parsing functionality of the <a href="" examples</a>, 
+via the re-use of the <a href=""
+but there is a fundamental difference between these two use cases:  with table examples the scenario is run for each line of the 
+table (using in each execution the parameter values of the given row), while in using table parameters we are simply using 
+the tabular structure as a parameter, and how this structure is interpreted is up to the scenario writer.</p>
+
+<div class="clear">
+<hr />
+</div>
+
+</body>
+</html>
\ No newline at end of file

Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java (1387 => 1388)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java	2009-11-25 13:11:41 UTC (rev 1388)
@@ -56,9 +56,9 @@
     }
 
     @Given("the traders: %tradersTable")
-    public void theTraders(String tradersTable) {
+    public void theTraders(ExamplesTable tradersTable) {
         traders.clear();
-        traders.addAll(parseTraders(tradersTable));
+        traders.addAll(toTraders(tradersTable));
     }
 
     @When("a wildcard search \"%regex\" is executed")
@@ -72,13 +72,12 @@
     }
     
     @Then("the traders returned are: %tradersTable")
-    public void theTradersReturnedAre(String tradersTable) {
-        List<Trader> expected = parseTraders(tradersTable);
+    public void theTradersReturnedAre(ExamplesTable tradersTable) {
+        List<Trader> expected = toTraders(tradersTable);
         assertEquals(expected.toString(), searchedTraders.toString());
     }
 
-    private List<Trader> parseTraders(String tradersTable) {
-        ExamplesTable table = new ExamplesTable(tradersTable);
+    private List<Trader> toTraders(ExamplesTable table) {
         List<Trader> traders = new ArrayList<Trader>();
         List<Map<String, String>> rows = table.getRows();
         for (Map<String, String> row : rows) {

Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java (1387 => 1388)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/ParameterConvertersBehaviour.java	2009-11-25 13:11:41 UTC (rev 1388)
@@ -11,7 +11,10 @@
 import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.List;
+import java.util.Map;
 
+import org.jbehave.scenario.definition.ExamplesTable;
+import org.jbehave.scenario.steps.ParameterConverters.ExamplesTableConverter;
 import org.jbehave.scenario.steps.ParameterConverters.NumberListConverter;
 import org.junit.Test;
 
@@ -66,5 +69,23 @@
 		ensureThat(list.get(2), equalTo(numberFormat.parse("6.1f")));
 		ensureThat(list.get(3), equalTo(numberFormat.parse("8.00")));
 	}
-
+	
+    @Test
+    public void shouldConvertMultilineTableParameter()
+            throws ParseException, IntrospectionException {
+        ParameterConverters converters = new ParameterConverters(
+                new ExamplesTableConverter());
+        Type type = SomeSteps.methodFor("aMethodWithExamplesTable")
+                .getGenericParameterTypes()[0];
+        ExamplesTable table = (ExamplesTable) converters.convert(
+                "|col1|col2|\n|row11|row12|\n|row21|row22|\n", type);
+        ensureThat(table.getRowCount(), equalTo(2));
+        Map<String, String> row1 = table.getRow(0);
+        ensureThat(row1.get("col1"), equalTo("row11"));
+        ensureThat(row1.get("col2"), equalTo("row12"));
+        Map<String, String> row2 = table.getRow(1);
+        ensureThat(row2.get("col1"), equalTo("row21"));
+        ensureThat(row2.get("col2"), equalTo("row22"));
+    }
+	
 }

Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java (1387 => 1388)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/SomeSteps.java	2009-11-25 13:11:41 UTC (rev 1388)
@@ -7,6 +7,8 @@
 import java.lang.reflect.Method;
 import java.util.List;
 
+import org.jbehave.scenario.definition.ExamplesTable;
+
 public class SomeSteps extends Steps {
     Object args;
 
@@ -58,6 +60,10 @@
         this.args = args;
     }
 
+    public void aMethodWithExamplesTable(ExamplesTable args) {
+        this.args = args;
+    }
+
     public static Method methodFor(String methodName) throws IntrospectionException {
         BeanInfo beanInfo = Introspector.getBeanInfo(SomeSteps.class);
         for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java (1387 => 1388)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java	2009-11-25 11:02:52 UTC (rev 1387)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/ParameterConverters.java	2009-11-25 13:11:41 UTC (rev 1388)
@@ -11,6 +11,8 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.jbehave.scenario.definition.ExamplesTable;
+
 /**
  * Facade responsible for converting parameter values to Java objects.
  * 
@@ -24,7 +26,7 @@
 	private static final String COMMA = ",";
 	private static final List<ParameterConverter> DEFAULT_CONVERTERS = asList(
 			new NumberConverter(), new NumberListConverter(),
-			new StringListConverter());
+			new StringListConverter(), new ExamplesTableConverter());
 	private final StepMonitor monitor;
 	private final List<ParameterConverter> converters = new ArrayList<ParameterConverter>();
 
@@ -190,4 +192,18 @@
 		return trimmed;
 	}
 
+    public static class ExamplesTableConverter implements ParameterConverter {
+        public boolean accept(Type type) {
+            if (type instanceof Class<?>) {
+                return ExamplesTable.class.isAssignableFrom((Class<?>) type);
+            }
+            return false;
+        }
+
+        public Object convertValue(String value, Type type) {
+            return new ExamplesTable(value);
+        }
+
+    }
+	
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to