From 5f8d83dd5dd45943c619037e240d55af47de9a4c Mon Sep 17 00:00:00 2001
From: Joachim Nilsson <Joachim.Nilsson@molybden.se>
Date: Thu, 26 Apr 2012 01:02:09 +0200
Subject: [PATCH] [testcase bugfix] Corrected testcases that were using
 getMethods and expecting returned methods in order.

---
 .../steps/CompositeStepCandidateBehaviour.java     |    8 ++--
 .../jbehave/core/steps/StepCandidateBehaviour.java |   47 +++++++++++++------
 2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeStepCandidateBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeStepCandidateBehaviour.java
index d1e98bf..9e36db6 100755
--- a/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeStepCandidateBehaviour.java
+++ b/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeStepCandidateBehaviour.java
@@ -19,13 +19,13 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertNotNull;
 
-public class CompositeStepCandidateBehaviour {
+public class CompositeStepCandidateBehaviour extends StepCandidateBehaviour{
 
     @Test
     public void shouldMatchCompositeStepsAndCreateComposedStepsUsingMatchedParameters() {
         CompositeSteps steps = new CompositeSteps();
         List<StepCandidate> candidates = steps.listCandidates();
-        StepCandidate candidate = candidates.get(0);
+        StepCandidate candidate = extractCandidateThatMatchesStep(candidates, "Given $customer has previously bought a $product");
         assertThat(candidate.isComposite(), is(true));
         Map<String, String> noNamedParameters = new HashMap<String, String>();
         List<Step> composedSteps = new ArrayList<Step>();
@@ -66,7 +66,7 @@ public class CompositeStepCandidateBehaviour {
     public void shouldMatchCompositeStepsAndCreateComposedStepsUsingNamedParameters() {
         CompositeStepsUsingNamedParameters steps = new CompositeStepsUsingNamedParameters();
         List<StepCandidate> candidates = steps.listCandidates();
-        StepCandidate candidate = candidates.get(0);
+        StepCandidate candidate = extractCandidateThatMatchesStep(candidates, "Given <customer> has previously bough a <product>");
         assertThat(candidate.isComposite(), is(true));
         Map<String, String> namedParameters = new HashMap<String, String>();
         namedParameters.put("customer", "Mr Jones");
@@ -229,7 +229,7 @@ public class CompositeStepCandidateBehaviour {
     public void shouldMatchCompositeStepsWhenStepParameterIsProvided(){
         CompositeStepsParameterMatching steps = new CompositeStepsParameterMatching();
         List<StepCandidate> candidates = steps.listCandidates();
-        StepCandidate candidate = candidates.get(0);
+        StepCandidate candidate = extractCandidateThatMatchesStep(candidates, "When I login");
         assertThat(candidate.isComposite(), is(true));
         Map<String, String> noNamedParameters = new HashMap<String, String>();
         List<Step> composedSteps = new ArrayList<Step>();
diff --git a/jbehave-core/src/test/java/org/jbehave/core/steps/StepCandidateBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/steps/StepCandidateBehaviour.java
index c3eed12..5e02950 100755
--- a/jbehave-core/src/test/java/org/jbehave/core/steps/StepCandidateBehaviour.java
+++ b/jbehave-core/src/test/java/org/jbehave/core/steps/StepCandidateBehaviour.java
@@ -39,6 +39,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.instanceOf;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.not;
 import static org.hamcrest.Matchers.nullValue;
 
@@ -67,6 +68,15 @@ public class StepCandidateBehaviour {
         return new StepCandidate(patternAsString, 0, stepType, method, stepsType, stepsFactory, keywords,
                 new RegexPrefixCapturingPatternParser(), new ParameterConverters(), parameterControls);
     }
+    
+    protected StepCandidate extractCandidateThatMatchesStep(List<StepCandidate> candidates, String stepAsString) {
+    	for (StepCandidate candidate : candidates) {
+    		if (candidate.matches(stepAsString)){
+    			return candidate ;
+    		}
+		}
+		return null;
+	}    
 
     @Test
     public void shouldMatchStepWithoutParameters() throws Exception {
@@ -382,10 +392,12 @@ public class StepCandidateBehaviour {
         NamedTypeSteps steps = new NamedTypeSteps();
         List<StepCandidate> candidates = steps.listCandidates();
         assertThat(candidates.size(), equalTo(2));
-        performStep(candidates.get(0), "Given foo named xyz");
-        performStep(candidates.get(0), "And foo named xyz");
-        performStep(candidates.get(1), "When foo named Bar");
-        performStep(candidates.get(1), "And foo named Bar");
+        StepCandidate step0 = extractCandidateThatMatchesStep(candidates, "Given foo named $name");
+        performStep(step0, "Given foo named xyz");
+        performStep(step0, "And foo named xyz");
+        StepCandidate step1 = extractCandidateThatMatchesStep(candidates, "When foo named $name");
+        performStep(step1, "When foo named Bar");
+        performStep(step1, "And foo named Bar");
         assertThat(steps.givenName, equalTo("xyz"));
         assertThat(steps.givenTimes, equalTo(2));
         assertThat(steps.whenName, equalTo("Bar"));
@@ -429,10 +441,12 @@ public class StepCandidateBehaviour {
         NamedTypeSteps steps = new NamedTypeSteps(configuration);
         List<StepCandidate> candidates = steps.listCandidates();
         assertThat(candidates.size(), equalTo(2));
-        candidates.get(0).createMatchedStep("Given foo named xyz", namedParameters).perform(null);
-        candidates.get(0).createMatchedStep("And foo named xyz", namedParameters).perform(null);
-        candidates.get(1).createMatchedStep("When foo named Bar", namedParameters).perform(null);
-        candidates.get(1).createMatchedStep("And foo named Bar", namedParameters).perform(null);
+        StepCandidate step0 = extractCandidateThatMatchesStep(candidates, "Given foo named $name");
+        step0.createMatchedStep("Given foo named xyz", namedParameters).perform(null);
+        step0.createMatchedStep("And foo named xyz", namedParameters).perform(null);
+        StepCandidate step1 = extractCandidateThatMatchesStep(candidates, "When foo named $name");
+        step1.createMatchedStep("When foo named Bar", namedParameters).perform(null);
+        step1.createMatchedStep("And foo named Bar", namedParameters).perform(null);
         assertThat(steps.givenName, nullValue());
         assertThat(steps.givenTimes, equalTo(0));
         assertThat(steps.whenName, nullValue());
@@ -444,21 +458,24 @@ public class StepCandidateBehaviour {
         PendingSteps steps = new PendingSteps();
         List<StepCandidate> candidates = steps.listCandidates();
         assertThat(candidates.size(), equalTo(2));
-        assertThat(candidates.get(0).matches("Given a pending step"), is(true));
-        assertThat(candidates.get(0).isPending(), is(true));
-        assertThat(candidates.get(1).matches("Given a non pending step"), is(true));
-        assertThat(candidates.get(1).isPending(), is(false));
+        StepCandidate pending = extractCandidateThatMatchesStep(candidates, "Given a pending step");
+        assertThat(pending, notNullValue());
+        assertThat(pending.isPending(), is(true));
+        StepCandidate nonPending = extractCandidateThatMatchesStep(candidates, "Given a non pending step");
+        assertThat(nonPending, notNullValue());
+        assertThat(nonPending.isPending(), is(false));
     }
 
-    @Test(expected = StartingWordNotFound.class)
+	@Test(expected = StartingWordNotFound.class)
     public void shouldNotCreateStepOfWrongType() {
         NamedTypeSteps steps = new NamedTypeSteps();
         List<StepCandidate> candidates = steps.listCandidates();
         assertThat(candidates.size(), equalTo(2));
-        candidates.get(0).createMatchedStep("Given foo named xyz", namedParameters).perform(null);
+        StepCandidate step = extractCandidateThatMatchesStep(candidates, "Given foo named $name");
+        step.createMatchedStep("Given foo named xyz", namedParameters).perform(null);
         assertThat(steps.givenName, equalTo("xyz"));
         assertThat(steps.whenName, nullValue());
-        candidates.get(0).createMatchedStep("Then foo named xyz", namedParameters).perform(null);
+        step.createMatchedStep("Then foo named xyz", namedParameters).perform(null);
     }
 
     static class NamedTypeSteps extends Steps {
-- 
1.7.7.1.msysgit.0

