Title: [1494] trunk/core/jbehave-core: @Named from JSR330 can be used instead of the builtin JB one.

Diff

Modified: trunk/core/jbehave-core/pom.xml (1493 => 1494)

--- trunk/core/jbehave-core/pom.xml	2010-01-05 11:40:23 UTC (rev 1493)
+++ trunk/core/jbehave-core/pom.xml	2010-01-05 12:12:56 UTC (rev 1494)
@@ -40,6 +40,12 @@
       <artifactId>freemarker</artifactId>
       <version>2.3.16</version>
     </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>

Added: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/AnnotationNamedParameterSteps.java (0 => 1494)

--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/AnnotationNamedParameterSteps.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/AnnotationNamedParameterSteps.java	2010-01-05 12:12:56 UTC (rev 1494)
@@ -0,0 +1,19 @@
+package org.jbehave.scenario.steps;
+
+import org.jbehave.scenario.annotations.Named;
+
+class AnnotationNamedParameterSteps extends Steps {
+    String ith;
+    String nth;
+
+    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;
+    }
+
+}

Modified: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java (1493 => 1494)

--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2010-01-05 11:40:23 UTC (rev 1493)
+++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2010-01-05 12:12:56 UTC (rev 1494)
@@ -21,7 +21,6 @@
 import java.util.Map;
 
 import org.jbehave.scenario.annotations.Given;
-import org.jbehave.scenario.annotations.Named;
 import org.jbehave.scenario.annotations.When;
 import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder;
 import org.jbehave.scenario.parser.StepPatternBuilder;
@@ -199,6 +198,39 @@
     }
         
     @Test
+    public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrderForJsr330Named() throws Exception {
+    	Jsr330AnnotationNamedParameterSteps steps = new Jsr330AnnotationNamedParameterSteps();
+        CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
+        		WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords);
+        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 shouldMatchMethodParametersByAnnotatedNamesInverseOrderForJsr330Named() throws Exception {
+    	Jsr330AnnotationNamedParameterSteps steps = new Jsr330AnnotationNamedParameterSteps();
+        CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
+        		WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords);
+        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 shouldCreateStepFromTableValuesViaAnnotationsForJsr330Named() throws Exception {
+    	Jsr330AnnotationNamedParameterSteps steps = new Jsr330AnnotationNamedParameterSteps();
+    	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", Jsr330AnnotationNamedParameterSteps.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 {
         shouldMatchMethodParametersByParanamerSomeOrder("methodWithNamedParametersInNaturalOrder");
     }
@@ -269,38 +301,6 @@
 
     }
 
-    static class AnnotationNamedParameterSteps extends Steps {
-    	String ith;
-        String nth;
-
-        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;
-        }
-
-    }
-
-    static class ParanamerNamedParameterSteps extends Steps {
-    	String ith;
-        String nth;
-
-        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;
-        }
-
-    }
-
     static Method stepMethodFor(String methodName, Class<? extends Steps> stepsClass) throws IntrospectionException {
         BeanInfo beanInfo = Introspector.getBeanInfo(stepsClass);
         for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {

Added: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/Jsr330AnnotationNamedParameterSteps.java (0 => 1494)

--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/Jsr330AnnotationNamedParameterSteps.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/Jsr330AnnotationNamedParameterSteps.java	2010-01-05 12:12:56 UTC (rev 1494)
@@ -0,0 +1,19 @@
+package org.jbehave.scenario.steps;
+
+import javax.inject.Named;
+
+class Jsr330AnnotationNamedParameterSteps extends Steps {
+    String ith;
+    String nth;
+
+    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;
+    }
+
+}

Added: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/ParanamerNamedParameterSteps.java (0 => 1494)

--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/ParanamerNamedParameterSteps.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/ParanamerNamedParameterSteps.java	2010-01-05 12:12:56 UTC (rev 1494)
@@ -0,0 +1,17 @@
+package org.jbehave.scenario.steps;
+
+class ParanamerNamedParameterSteps extends Steps {
+    String ith;
+    String nth;
+
+    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;
+    }
+
+}

Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java (1493 => 1494)

--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java	2010-01-05 11:40:23 UTC (rev 1493)
+++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java	2010-01-05 12:12:56 UTC (rev 1494)
@@ -216,10 +216,11 @@
         String[] names = new String[parameterAnnotations.length];
         for (int x = 0; x < parameterAnnotations.length; x++) {
             Annotation[] annotations = parameterAnnotations[x];
-            for (int y = 0; y < annotations.length; y++) {
-                Annotation annotation = annotations[y];
+            for (Annotation annotation : annotations) {
                 if (annotation.annotationType().isAssignableFrom(Named.class)) {
                     names[x] = ((Named) annotation).value();
+                } else if ("javax.inject.Named".equals(annotation.annotationType().getName())) {
+                    names[x] = Jsr330Helper.getNamedValue(annotation);
                 }
             }
         }
@@ -322,4 +323,14 @@
 
     }
 
+    /**
+     * This is a different class, because the @Inject jar may not be in the classpath.
+     */
+    public static class Jsr330Helper {
+        private static String getNamedValue(Annotation ann) {
+            return ((javax.inject.Named) ann).value();
+        }
+
+    }
+
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to