Title: [1531] sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico: Some tidying up.

Diff

Modified: sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java (1530 => 1531)

--- sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java	2010-01-20 16:21:45 UTC (rev 1530)
+++ sandbox/spikes/di-spike/jbehave-pico/src/behaviour/java/org/jbehave/scenario/steps/pico/PicoStepsFactoryBehaviour.java	2010-01-20 23:36:21 UTC (rev 1531)
@@ -15,68 +15,54 @@
 
 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;
+    private static Field stepsInstance;
 
     @Before
-    public void steup() throws NoSuchFieldException {
-        instMethod = Steps.class.getDeclaredField("instance");
-        instMethod.setAccessible(true);
+    public void setUp() throws NoSuchFieldException {
+        stepsInstance = Steps.class.getDeclaredField("instance");
+        stepsInstance.setAccessible(true);
     }
 
 
     @Test
-    public void ensureThatContainerCanBeUsedToMakeSteps() throws NoSuchFieldException, IllegalAccessException {
+    public void ensureThatStepsCanBeCreated() 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);
+        CandidateSteps[] steps = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
+        assertFooStepsFound(steps);
     }
 
     @Test
-    public void ensureThatContainerCanBeUsedToMakeStepsWithACtorDep() throws NoSuchFieldException, IllegalAccessException {
+    public void ensureThatStepsWithConstructorDependencyCanBeCreated() throws NoSuchFieldException, IllegalAccessException {
         MutablePicoContainer parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
-        parent.as(Characteristics.USE_NAMES).addComponent(ExtendedFooSteps.class);
+        parent.as(Characteristics.USE_NAMES).addComponent(FooStepsWithDependency.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);
+        CandidateSteps[] steps = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
+        assertFooStepsFound(steps);
+        assertEquals(42, (int) ((FooStepsWithDependency) stepsInstance.get(steps[0])).integer);
     }
 
-    private void assertOneFooStepsComponent(CandidateSteps[] foo) throws NoSuchFieldException, IllegalAccessException {
-        assertEquals(1, foo.length);
-        assertTrue(foo[0] instanceof Steps);
-        Object instance = instMethod.get(foo[0]);
+    private void assertFooStepsFound(CandidateSteps[] steps) throws NoSuchFieldException, IllegalAccessException {
+        assertEquals(1, steps.length);
+        assertTrue(steps[0] instanceof Steps);
+        Object instance = stepsInstance.get(steps[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 {
+    @Test(expected=AbstractInjector.UnsatisfiableDependenciesException.class)
+    public void ensureThatStepsWithMissingDependenciesCannotBeCreated() throws NoSuchFieldException, IllegalAccessException {
         MutablePicoContainer parent = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
-        parent.as(Characteristics.USE_NAMES).addComponent(ExtendedFooSteps.class);
+        parent.as(Characteristics.USE_NAMES).addComponent(FooStepsWithDependency.class);
 
-        CandidateSteps[] foo = new CandidateSteps[0];
-        try {
-            foo = new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
-            fail("should have barfed");
-        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
-            // expected
-        }
+        new PicoStepsFactory(new StepsConfiguration(), parent).createCandidateSteps();
     }
 
     public static class FooSteps {
@@ -87,5 +73,12 @@
 
     }
 
+    public static class FooStepsWithDependency extends FooSteps {
+        private final Integer integer;
 
+        public FooStepsWithDependency(Integer steps) {
+            this.integer = steps;
+        }
+
+    }
 }

Modified: sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java (1530 => 1531)

--- sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java	2010-01-20 16:21:45 UTC (rev 1530)
+++ sandbox/spikes/di-spike/jbehave-pico/src/main/java/org/jbehave/scenario/steps/pico/PicoStepsFactory.java	2010-01-20 23:36:21 UTC (rev 1531)
@@ -22,19 +22,18 @@
 
     public CandidateSteps[] createCandidateSteps() {
         List<Steps> steps = new ArrayList<Steps>();
-        for (ComponentAdapter<?> componentAdapter : parent.getComponentAdapters()) {
-            Method[] methods = componentAdapter.getComponentImplementation().getMethods();
-            if (isStep(methods)) {
-                steps.add(new Steps(configuration, parent.getComponent(componentAdapter.getComponentKey())));
+        for (ComponentAdapter<?> adapter : parent.getComponentAdapters()) {
+            if (containsStepsAnnotations(adapter.getComponentImplementation())) {
+                steps.add(new Steps(configuration, parent.getComponent(adapter.getComponentKey())));
             }
         }
         return steps.toArray(new CandidateSteps[steps.size()]);
     }
 
-    private boolean isStep(Method[] methods) {
-        for (Method method : methods) {
-            for (Annotation ann : method.getAnnotations()) {
-                if (ann.annotationType().getName().startsWith("org.jbehave.scenario.annotations")) {
+    private boolean containsStepsAnnotations(Class<?> componentClass) {
+        for (Method method : componentClass.getMethods()) {
+            for (Annotation annotation : method.getAnnotations()) {
+                if (annotation.annotationType().getName().startsWith("org.jbehave.scenario.annotations")) {
                     return true;
                 }
             }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to