Title: [1511] sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico: reworked pico based extension to jb

Diff

Deleted: sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStepBehaviour.java (1510 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStepBehaviour.java	2010-01-10 01:18:25 UTC (rev 1510)
+++ sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStepBehaviour.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -1,430 +0,0 @@
-package org.jbehave.scenario.steps.pico;
-
-import static java.util.Arrays.asList;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.jbehave.Ensure.ensureThat;
-import static org.jbehave.Ensure.not;
-import static org.jbehave.scenario.steps.StepType.GIVEN;
-import static org.jbehave.scenario.steps.StepType.THEN;
-import static org.jbehave.scenario.steps.StepType.WHEN;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.MethodDescriptor;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.inject.Named;
-
-import org.jbehave.scenario.annotations.Given;
-import org.jbehave.scenario.annotations.When;
-import org.jbehave.scenario.definition.ExamplesTable;
-import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder;
-import org.jbehave.scenario.parser.StepPatternBuilder;
-import org.jbehave.scenario.reporters.ScenarioReporter;
-import org.jbehave.scenario.steps.CandidateStep;
-import org.jbehave.scenario.steps.ParameterConverters;
-import org.jbehave.scenario.steps.Step;
-import org.jbehave.scenario.steps.StepResult;
-import org.jbehave.scenario.steps.StepType;
-import org.jbehave.scenario.steps.Steps;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.picocontainer.Characteristics;
-import org.picocontainer.DefaultPicoContainer;
-import org.picocontainer.MutablePicoContainer;
-import org.picocontainer.behaviors.Caching;
-import org.picocontainer.containers.EmptyPicoContainer;
-import org.picocontainer.injectors.ConstructorInjection;
-
-public class PicoEnabledCandidateStepBehaviour {
-
-    private static final StepPatternBuilder PATTERN_BUILDER = new PrefixCapturingPatternBuilder();
-    private static final String NL = System.getProperty("line.separator");
-	private Map<String, String> tableRow = new HashMap<String, String>();
-	private Map<StepType, String> startingWords = startingWords();
-    private MutablePicoContainer parent;
-
-	private Map<StepType, String> startingWords() {
-		Map<StepType, String> map = new HashMap<StepType, String>();
-		map.put(GIVEN, "Given");
-		map.put(WHEN, "When");
-		map.put(THEN, "Then");
-		return map;
-	}
-
-    @Before
-    public void setup() {
-        parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
-        parent.as(Characteristics.NO_CACHE).addComponent(new PicoEnabledStepsConfiguration(parent));
-    }
-
-
-    @Test
-    public void shouldMatchASimpleString() throws Exception {
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I laugh", GIVEN, SomeSteps.class.getMethod("aMethod"),
-                null, PATTERN_BUILDER, new ParameterConverters(), startingWords, new EmptyPicoContainer());
-        ensureThat(candidateStep.matches("Given I laugh"));
-    }
-
-    @Test
-    public void shouldMatchAStringWithArguments() throws Exception {
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("windows on the $nth floor", WHEN, SomeSteps.class
-				        .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords, new EmptyPicoContainer());
-        ensureThat(candidateStep.matches("When windows on the 1st floor"));
-        ensureThat(not(candidateStep.matches("When windows on the 1st floor are open")));
-    }
-
-    @Test
-    public void shouldProvideARealStepUsingTheMatchedString() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(SomeSteps.class);
-        SomeSteps someSteps = parent.getComponent(SomeSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $args floor", THEN, SomeSteps.class.getMethod(
-				        "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor");
-        step.perform();
-        ensureThat((String) someSteps.args, equalTo("1st"));
-    }
-
-    @Test
-    public void shouldMatchMultilineStrings() throws Exception {
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("the grid should look like $grid", THEN, SomeSteps.class
-				        .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords, new EmptyPicoContainer());
-        ensureThat(candidateStep.matches("Then the grid should look like " + NL + "...." + NL + "...." + NL));
-    }
-
-    @Test
-    public void shouldConvertArgsToAppropriateNumbers() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(SomeSteps.class);
-        SomeSteps someSteps = parent.getComponent(SomeSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I should live in no. $args", THEN, SomeSteps.class.getMethod(
-				        "aMethodWith", int.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform();
-        ensureThat((Integer) someSteps.args, equalTo(14));
-
-        candidateStep = new PicoEnabledCandidateStep("I should live in no. $args", THEN, SomeSteps.class.getMethod("aMethodWith",
-				        long.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform();
-        ensureThat((Long) someSteps.args, equalTo(14L));
-
-        candidateStep = new PicoEnabledCandidateStep("I should live in no. $args", THEN, SomeSteps.class.getMethod("aMethodWith",
-				        double.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform();
-        ensureThat((Double) someSteps.args, equalTo(14.0));
-
-        candidateStep = new PicoEnabledCandidateStep("I should live in no. $args", THEN, SomeSteps.class.getMethod("aMethodWith",
-				        float.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform();
-        ensureThat((Float) someSteps.args, equalTo(14.0f));
-    }
-
-    @Test
-    public void shouldProvideAStepWithADescriptionThatMatchesTheCandidateStep() throws Exception {
-        ScenarioReporter reporter = mock(ScenarioReporter.class);
-        parent.as(Characteristics.USE_NAMES).addComponent(SomeSteps.class);
-        SomeSteps someSteps = parent.getComponent(SomeSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $nth floor", THEN, SomeSteps.class.getMethod(
-				        "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor");
-
-        StepResult result = step.perform();
-        result.describeTo(reporter);
-        verify(reporter).successful("Then I live on the 1st floor");
-    }
-
-    @Test
-    public void shouldConvertStringParameterValuesToUseSystemNewline() throws Exception {
-        String windowsNewline = "\r\n";
-        String unixNewline = "\n";
-        String systemNewline = System.getProperty("line.separator");
-        SomeSteps someSteps = new SomeSteps(null);
-        CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", THEN, SomeSteps.class.getMethod(
-				        "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords);
-        Step step = candidateStep.createFrom(tableRow, "Then the grid should look like" + windowsNewline + ".." + unixNewline
-                + ".." + windowsNewline);
-        step.perform();
-        ensureThat((String) someSteps.args, equalTo(".." + systemNewline + ".." + systemNewline));
-    }
-
-
-    @Test
-    @Ignore
-    public void shouldConvertArgsToListOfNumbers() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(SomeSteps.class);
-        SomeSteps someSteps = parent.getComponent(SomeSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("windows on the $nth floors",
-        		WHEN, stepMethodFor("aMethodWithListOfLongs", SomeSteps.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When windows on the 1L,2L,3L floors").perform();
-        ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1L, 2L, 3L).toString()));
-
-        candidateStep = new PicoEnabledCandidateStep("windows on the $nth floors", WHEN,
-                stepMethodFor("aMethodWithListOfIntegers", SomeSteps.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform();
-        ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1, 2, 3).toString()));
-
-        candidateStep = new PicoEnabledCandidateStep("windows on the $nth floors", WHEN,
-                stepMethodFor("aMethodWithListOfDoubles", SomeSteps.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When windows on the 1.1,2.2,3.3 floors").perform();
-        ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1, 2.2, 3.3).toString()));
-
-        candidateStep = new PicoEnabledCandidateStep("windows on the $nth floors", WHEN,
-                stepMethodFor("aMethodWithListOfFloats", SomeSteps.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When windows on the 1.1f,2.2f,3.3f floors").perform();
-        ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1f, 2.2f, 3.3f).toString()));
-
-    }
-
-    @Test
-    @Ignore
-    public void shouldConvertArgsToListOfStrings() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(SomeSteps.class);
-        SomeSteps someSteps = parent.getComponent(SomeSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("windows on the $nth floors",
-        		WHEN, stepMethodFor("aMethodWithListOfStrings", SomeSteps.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform();
-        ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList("1", "2", "3").toString()));
-    }
-
-    @Test
-    public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrder() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(AnnotationNamedParameterSteps.class);
-        AnnotationNamedParameterSteps steps = parent.getComponent(AnnotationNamedParameterSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $ith floor but some call it the $nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    public void shouldMatchMethodParametersByAnnotatedNamesInverseOrder() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(AnnotationNamedParameterSteps.class);
-        AnnotationNamedParameterSteps steps = parent.getComponent(AnnotationNamedParameterSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $ith floor but some call it the $nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    @Ignore
-    public void shouldCreateStepFromTableValuesViaAnnotations() throws Exception {
-    	AnnotationNamedParameterSteps steps = new AnnotationNamedParameterSteps(parent.getComponent(PicoEnabledStepsConfiguration.class));
-    	tableRow.put("ith", "first");
-    	tableRow.put("nth", "ground");
-        CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords);
-        candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    public void shouldMatchMethodParametersByParanamerNamesInNaturalOrder() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(ParanamerNamedParameterSteps.class);
-        ParanamerNamedParameterSteps steps = parent.getComponent(ParanamerNamedParameterSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $ith floor but some call it the $nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        Step step = candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground");
-        step.perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    public void shouldMatchMethodParametersByParanamerInverseOrder() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(ParanamerNamedParameterSteps.class);
-        ParanamerNamedParameterSteps steps = parent.getComponent(ParanamerNamedParameterSteps.class);
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the $ith floor but some call it the $nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    public void shouldCreateStepFromTableValuesViaParanamer() throws Exception {
-        parent.as(Characteristics.USE_NAMES).addComponent(ParanamerNamedParameterSteps.class);
-        ParanamerNamedParameterSteps steps = parent.getComponent(ParanamerNamedParameterSteps.class);
-    	tableRow.put("ith", "first");
-    	tableRow.put("nth", "ground");
-        PicoEnabledCandidateStep candidateStep = new PicoEnabledCandidateStep("I live on the ith floor but some call it the nth",
-        		WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords, parent);
-        candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform();
-        ensureThat(steps.ith, equalTo("first"));
-        ensureThat(steps.nth, equalTo("ground"));
-    }
-
-    @Test
-    public void shouldCreateStepsOfDifferentTypesWithSameMatchingPattern() throws Throwable {
-        parent.addComponent(NamedTypeSteps.class);
-        NamedTypeSteps steps = parent.getComponent(NamedTypeSteps.class);
-        CandidateStep[] candidateSteps = steps.getSteps();
-        ensureThat(candidateSteps.length, equalTo(2));
-        guard(candidateSteps[0].createFrom(tableRow, "Given foo named xyz").perform());
-        guard(candidateSteps[1].createFrom(tableRow, "When foo named Bar").perform());
-        ensureThat(steps.givenName, equalTo("xyz"));
-        ensureThat(steps.whenName, equalTo("Bar"));
-    }
-
-    private StepResult guard(StepResult result) throws Throwable {
-        if (result instanceof StepResult.Failed) {
-            throw result.getThrowable();
-        }
-        return result;
-    }
-
-	@Test(expected= CandidateStep.StartingWordNotFound.class)
-    public void shouldNotCreateStepOfWrongType() {
-        parent.addComponent(NamedTypeSteps.class);
-        NamedTypeSteps steps = parent.getComponent(NamedTypeSteps.class);
-        CandidateStep[] candidateSteps = steps.getSteps();
-        ensureThat(candidateSteps.length, equalTo(2));
-        candidateSteps[0].createFrom(tableRow, "Given foo named xyz").perform();
-        ensureThat(steps.givenName, equalTo("xyz"));
-        ensureThat(steps.whenName, nullValue());
-        candidateSteps[0].createFrom(tableRow, "Then foo named xyz").perform();
-    }
-
-    public static class NamedTypeSteps extends PicoEnabledSteps {
-        String givenName;
-        String whenName;
-
-        public NamedTypeSteps(PicoEnabledStepsConfiguration configuration) {
-            super(configuration);
-        }
-
-        @Given("foo named $name")
-        public void givenFoo(String name) {
-        	givenName = name;
-        }
-
-        @When("foo named $name")
-        public void whenFoo(String name) {
-        	whenName = name;
-        }
-
-    }
-
-    public static class AnnotationNamedParameterSteps extends PicoEnabledSteps {
-    	String ith;
-        String nth;
-
-        public AnnotationNamedParameterSteps(PicoEnabledStepsConfiguration configuration) {
-            super(configuration);
-        }
-
-        public void methodWithNamedParametersInNaturalOrder(@Named("ith") String ithName, @Named("nth") String nthName){
-    		this.ith = ithName;
-    		this.nth = nthName;
-        }
-
-        public void methodWithNamedParametersInInverseOrder(@Named("nth") String nthName, @Named("ith") String ithName){
-    		this.ith = ithName;
-    		this.nth = nthName;
-        }
-
-    }
-
-    public static class ParanamerNamedParameterSteps extends PicoEnabledSteps {
-    	String ith;
-        String nth;
-
-        public ParanamerNamedParameterSteps(PicoEnabledStepsConfiguration configuration) {
-            super(configuration);
-        }
-
-        public void methodWithNamedParametersInNaturalOrder(String ith, String nth){
-    		this.ith = ith;
-    		this.nth = nth;
-        }
-
-        public void methodWithNamedParametersInInverseOrder(String nth, String ith){
-    		this.ith = ith;
-    		this.nth = nth;
-        }
-
-    }
-
-    public static class SomeSteps extends PicoEnabledSteps {
-
-        public SomeSteps(PicoEnabledStepsConfiguration configuration) {
-            super(configuration);
-        }
-
-        public Object args;
-
-        public void aMethod() {
-
-        }
-
-        public void aMethodWith(String args) {
-            this.args = args;
-        }
-
-        public void aMethodWith(double args) {
-            this.args = args;
-        }
-
-        public void aMethodWith(long args) {
-            this.args = args;
-        }
-
-        public void aMethodWith(int args) {
-            this.args = args;
-        }
-
-        public void aMethodWith(float args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfStrings(List<String> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfLongs(List<Long> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfIntegers(List<Integer> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfDoubles(List<Double> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfFloats(List<Float> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithListOfNumbers(List<Number> args) {
-            this.args = args;
-        }
-
-        public void aMethodWithExamplesTable(ExamplesTable args) {
-            this.args = args;
-        }
-
-    }
-
-    static Method stepMethodFor(String methodName, Class<? extends Steps> stepsClass) throws IntrospectionException {
-        BeanInfo beanInfo = Introspector.getBeanInfo(stepsClass);
-        for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
-            if (md.getMethod().getName().equals(methodName)) {
-                return md.getMethod();
-            }
-        }
-        return null;
-    }
-    
-
-}

Added: sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java (0 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java	                        (rev 0)
+++ sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -0,0 +1,91 @@
+package org.jbehave.scenario.steps.pico;
+
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.steps.CandidateSteps;
+import org.jbehave.scenario.steps.Steps;
+import org.jbehave.scenario.steps.StepsConfiguration;
+import org.junit.Before;
+import org.junit.Test;
+import org.picocontainer.Characteristics;
+import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.behaviors.Caching;
+import org.picocontainer.injectors.AbstractInjector;
+import org.picocontainer.injectors.ConstructorInjection;
+
+import java.lang.reflect.Field;
+
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class PicoStepsFactoryBehaviour {
+
+    private static Field instMethod;
+
+    @Before
+    public void steup() throws NoSuchFieldException {
+        instMethod = Steps.class.getDeclaredField("instance");
+        instMethod.setAccessible(true);
+    }
+
+
+    @Test
+    public void ensureThatContainerCanBeUsedToMakeSteps() throws NoSuchFieldException, IllegalAccessException {
+        MutablePicoContainer parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
+        parent.as(Characteristics.USE_NAMES).addComponent(FooSteps.class);
+
+        CandidateSteps[] foo = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
+        assertOneFooStepsComponent(foo);
+    }
+
+    @Test
+    public void ensureThatContainerCanBeUsedToMakeStepsWithACtorDep() throws NoSuchFieldException, IllegalAccessException {
+        MutablePicoContainer parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
+        parent.as(Characteristics.USE_NAMES).addComponent(ExtendedFooSteps.class);
+        parent.addComponent(Integer.class, 42);
+
+        CandidateSteps[] foo = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
+        assertOneFooStepsComponent(foo);
+        assertEquals(42, (int) ((ExtendedFooSteps) instMethod.get(foo[0])).num);
+    }
+
+    private void assertOneFooStepsComponent(CandidateSteps[] foo) throws NoSuchFieldException, IllegalAccessException {
+        assertEquals(1, foo.length);
+        assertTrue(foo[0] instanceof Steps);
+        Object instance = instMethod.get(foo[0]);
+        assertTrue(instance instanceof FooSteps);
+    }
+
+    public static class ExtendedFooSteps extends FooSteps {
+        private final Integer num;
+
+        public ExtendedFooSteps(Integer foo) {
+            this.num = foo;
+        }
+    }
+
+    @Test
+    public void ensureThatMissingDepsMakeItBarf() throws NoSuchFieldException, IllegalAccessException {
+        MutablePicoContainer parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
+        parent.as(Characteristics.USE_NAMES).addComponent(ExtendedFooSteps.class);
+
+        CandidateSteps[] foo = new CandidateSteps[0];
+        try {
+            foo = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
+            fail("should have barfed");
+        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+            // expected
+        }
+    }
+
+    public static class FooSteps {
+
+        @Given("a trader of name %trader")
+        public void aTrader(String trader) {
+        }
+
+    }
+
+
+}

Deleted: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStep.java (1510 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStep.java	2010-01-10 01:18:25 UTC (rev 1510)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledCandidateStep.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -1,82 +0,0 @@
-package org.jbehave.scenario.steps.pico;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Map;
-import java.util.regex.Matcher;
-
-import org.jbehave.scenario.errors.PendingError;
-import org.jbehave.scenario.parser.StepPatternBuilder;
-import org.jbehave.scenario.steps.CandidateStep;
-import org.jbehave.scenario.steps.CandidateSteps;
-import org.jbehave.scenario.steps.ParameterConverters;
-import org.jbehave.scenario.steps.Step;
-import org.jbehave.scenario.steps.StepMonitor;
-import org.jbehave.scenario.steps.StepResult;
-import org.jbehave.scenario.steps.StepType;
-import org.picocontainer.Characteristics;
-import org.picocontainer.DefaultPicoContainer;
-import org.picocontainer.MutablePicoContainer;
-import org.picocontainer.PicoContainer;
-import org.picocontainer.injectors.MethodInjection;
-import org.picocontainer.injectors.Reinjection;
-import org.picocontainer.lifecycle.NullLifecycleStrategy;
-
-public class PicoEnabledCandidateStep extends CandidateStep {
-
-    private PicoContainer parent;
-
-    public PicoEnabledCandidateStep(String patternAsString, StepType stepType, Method method, CandidateSteps steps,
-                                    StepPatternBuilder patternBuilder, ParameterConverters parameterConverters,
-                                    Map<StepType, String> startingWords, PicoContainer parent) {
-        super(patternAsString, stepType, method, steps, patternBuilder, parameterConverters, startingWords);
-        this.parent = parent;
-    }
-
-    @Override
-    protected Step createStep(final String stepAsString, Map<String, String> tableRow, Matcher matcher, final Method method,
-                              final StepMonitor stepMonitor, String[] groupNames) {
-
-        final MutablePicoContainer stepContainer = new DefaultPicoContainer(new Reinjection(new MethodInjection(method), parent), new NullLifecycleStrategy(), parent);
-
-        // populate named entries from table data
-        for (String key : tableRow.keySet()) {
-            stepContainer.addConfig(key, tableRow.get(key));
-        }
-
-        // populate named entries from regex match
-        for (String groupName : groupNames) {
-        	String value = getGroup(matcher, groupName);
-			stepContainer.addConfig(groupName, parameterConverters.convert(value, String.class));
-        }
-
-        return new Step() {
-            public StepResult perform() {
-                try {
-                    stepMonitor.performing(stepAsString);
-                    stepContainer.as(Characteristics.USE_NAMES).addComponent(method.getDeclaringClass());
-                    stepContainer.getComponent(method.getDeclaringClass());
-                    return StepResult.success(stepAsString);
-                } catch (Throwable t) {
-                    return failureWithOriginalException(stepAsString, t);
-                }
-            }
-
-            private StepResult failureWithOriginalException(final String stepAsString, Throwable t) {
-                if (t instanceof InvocationTargetException && t.getCause() != null) {
-                    if (t.getCause() instanceof PendingError) {
-                        return StepResult.pending(stepAsString, (PendingError) t.getCause());
-                    } else {
-                        return StepResult.failure(stepAsString, t.getCause());
-                    }
-                }
-                return StepResult.failure(stepAsString, t);
-            }
-
-            public StepResult doNotPerform() {
-                return StepResult.notPerformed(stepAsString);
-            }
-
-        };
-    }
-}

Deleted: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledException.java (1510 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledException.java	2010-01-10 01:18:25 UTC (rev 1510)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledException.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -1,10 +0,0 @@
-package org.jbehave.scenario.steps.pico;
-
-...@suppresswarnings("serial")
-public class PicoEnabledException extends RuntimeException {
-
-    public PicoEnabledException(String message) {
-        super(message);
-    }
-
-}

Deleted: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledSteps.java (1510 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledSteps.java	2010-01-10 01:18:25 UTC (rev 1510)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledSteps.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -1,28 +0,0 @@
-package org.jbehave.scenario.steps.pico;
-
-import java.lang.reflect.Method;
-
-import org.jbehave.scenario.steps.CandidateStep;
-import org.jbehave.scenario.steps.StepType;
-import org.jbehave.scenario.steps.Steps;
-import org.jbehave.scenario.steps.StepsConfiguration;
-import org.picocontainer.PicoContainer;
-
-public class PicoEnabledSteps extends Steps {
-
-    public PicoEnabledSteps(PicoEnabledStepsConfiguration configuration) {
-        super(configuration);
-    }
-
-    protected CandidateStep createCandidateStep(Method method, StepType stepType, String stepPatternAsString, StepsConfiguration configuration) {
-        if (!(configuration instanceof PicoEnabledStepsConfiguration)) {
-            throw new PicoEnabledException("StepsConfiguration parameter should be instanceof PicoEnabledStepsConfiguration");
-        }
-        PicoContainer parent = ((PicoEnabledStepsConfiguration) configuration).getParentContainer();
-        return new PicoEnabledCandidateStep(stepPatternAsString, stepType, method,
-                this, configuration.getPatternBuilder(),
-                configuration.getParameterConverters(),
-                configuration.getStartingWordsByType(), parent);
-    }
-
-}

Deleted: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledStepsConfiguration.java (1510 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledStepsConfiguration.java	2010-01-10 01:18:25 UTC (rev 1510)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoEnabledStepsConfiguration.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -1,16 +0,0 @@
-package org.jbehave.scenario.steps.pico;
-
-import org.jbehave.scenario.steps.StepsConfiguration;
-import org.picocontainer.PicoContainer;
-
-public class PicoEnabledStepsConfiguration extends StepsConfiguration {
-    private final PicoContainer parent;
-
-    public PicoEnabledStepsConfiguration(PicoContainer parent) {
-        this.parent = parent;
-    }
-
-    public PicoContainer getParentContainer() {
-        return parent;
-    }
-}

Added: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java (0 => 1511)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java	                        (rev 0)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java	2010-01-10 02:11:31 UTC (rev 1511)
@@ -0,0 +1,53 @@
+package org.jbehave.scenario.steps.pico;
+
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Then;
+import org.jbehave.scenario.annotations.When;
+import org.jbehave.scenario.steps.CandidateSteps;
+import org.jbehave.scenario.steps.Steps;
+import org.jbehave.scenario.steps.StepsConfiguration;
+import org.picocontainer.ComponentAdapter;
+import org.picocontainer.MutablePicoContainer;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class PicoStepsFactory {
+    private final StepsConfiguration configuration;
+    private final MutablePicoContainer parent;
+
+    public PicoStepsFactory(StepsConfiguration configuration, MutablePicoContainer parent) {
+        this.configuration = configuration;
+        this.parent = parent;
+    }
+
+    public CandidateSteps[] createCandidateSteps() {
+
+        List<CandidateSteps> steps = new ArrayList<CandidateSteps>();
+
+        Collection<ComponentAdapter<?>> adapters = parent.getComponentAdapters();
+        for (ComponentAdapter<?> componentAdapter : adapters) {
+            Class<?> impl = componentAdapter.getComponentImplementation();
+            Method[] methods = impl.getMethods();
+            if (isStep(methods)) {
+                steps.add(new Steps(configuration, parent.getComponent(componentAdapter.getComponentKey())));
+            }
+        }
+        return steps.toArray(new CandidateSteps[steps.size()]);
+    }
+
+    private boolean isStep(Method[] methods) {
+        for (Method method : methods) {
+            Annotation[] anns = method.getAnnotations();
+            for (Annotation ann : anns) {
+                if (ann.annotationType() == Given.class || ann.annotationType() == When.class || ann.annotationType() == Then.class) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to