Title: [1215] trunk/core/jbehave-core: start of paranamer side (optional) binding to group-names and table header names

Diff

Modified: trunk/core/jbehave-core/pom.xml (1214 => 1215)

--- trunk/core/jbehave-core/pom.xml	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/pom.xml	2009-09-03 05:36:32 UTC (rev 1215)
@@ -15,6 +15,50 @@
       <groupId>org.apache.ant</groupId>
       <artifactId>ant</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.thoughtworks.paranamer</groupId>
+      <artifactId>paranamer</artifactId>
+      <version>2.1-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>1.2.1</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <shadedArtifactId>jbehave-core</shadedArtifactId>
+                            <artifactSet>
+                                <excludes>
+                                    <exclude>org.apache.ant:ant</exclude>
+                                    <exclude>junit:junit-dep</exclude>
+                                    <exclude>org.hamcrest:hamcrest-all</exclude>
+                                </excludes>
+                            </artifactSet>
+                            <relocations>
+                                <relocation>
+                                    <pattern>com.thoughtworks.paranamer</pattern>
+                                    <shadedPattern>org.jbehave.paranamer</shadedPattern>
+                                    <excludes>
+                                        <exclude>com.thoughtworks.paranamer.JavadocParanamer</exclude>
+                                        <exclude>com.thoughtworks.paranamer.DefaultParanamer</exclude>
+                                        <exclude>com.thoughtworks.paranamer.AdaptiveParanamer</exclude>
+                                    </excludes>
+                                </relocation>
+                            </relocations>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>

Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PrefixCapturingPatternBuilderBehaviour.java (1214 => 1215)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PrefixCapturingPatternBuilderBehaviour.java	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PrefixCapturingPatternBuilderBehaviour.java	2009-09-03 05:36:32 UTC (rev 1215)
@@ -74,7 +74,7 @@
     @Test
     public void shouldExtractParameterNamesFromStepPattern(){
     	StepPatternBuilder builder = new PrefixCapturingPatternBuilder();
-        String[] names  = builder.extractParameterNames("The grid $name looks like $grid");
+        String[] names  = builder.extractGroupNames("The grid $name looks like $grid");
         ensureThat(names.length, equalTo(2));
         ensureThat(names[0], equalTo("name"));
         ensureThat(names[1], equalTo("grid"));

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

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/CandidateStepBehaviour.java	2009-09-03 05:36:32 UTC (rev 1215)
@@ -149,39 +149,38 @@
     
     @Test
     public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrder() throws Exception {
-    	NamedParameterSteps steps = new NamedParameterSteps();
+    	NamedParameterStepsViaNamedAnnotation steps = new NamedParameterStepsViaNamedAnnotation();
         CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
-        		NamedParameterSteps.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        		NamedParameterStepsViaNamedAnnotation.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
         candidateStep.createFrom(tableValues, "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("FIXME")
     public void shouldMatchMethodParametersByAnnotatedNamesInverseOrder() throws Exception {
-    	NamedParameterSteps steps = new NamedParameterSteps();
+    	NamedParameterStepsViaNamedAnnotation steps = new NamedParameterStepsViaNamedAnnotation();
         CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
-        		NamedParameterSteps.methodFor("methodWithNamedParametersInInverseOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        		NamedParameterStepsViaNamedAnnotation.methodFor("methodWithNamedParametersInInverseOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
         candidateStep.createFrom(tableValues, "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 shouldCreateStepFromTableValues() throws Exception {
-    	NamedParameterSteps steps = new NamedParameterSteps();
+    public void shouldCreateStepFromTableValuesViaAnnotations() throws Exception {
+    	NamedParameterStepsViaNamedAnnotation steps = new NamedParameterStepsViaNamedAnnotation();
     	tableValues.put("ith", "first");
     	tableValues.put("nth", "ground");
         CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth",
-        		NamedParameterSteps.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        		NamedParameterStepsViaNamedAnnotation.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
         candidateStep.createFrom(tableValues, "When I live on the <ith> floor but some call it the <nth>").perform();
         ensureThat(steps.ith, equalTo("first"));
         ensureThat(steps.nth, equalTo("ground"));
     }
     
     
-    public static class NamedParameterSteps extends Steps {
+    public static class NamedParameterStepsViaNamedAnnotation extends Steps {
     	String ith;
         String nth;
 
@@ -196,7 +195,7 @@
         }
 
         public static Method methodFor(String methodName) throws IntrospectionException {
-            BeanInfo beanInfo = Introspector.getBeanInfo(NamedParameterSteps.class);
+            BeanInfo beanInfo = Introspector.getBeanInfo(NamedParameterStepsViaNamedAnnotation.class);
             for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
                 if (md.getMethod().getName().equals(methodName)) {
                     return md.getMethod();
@@ -205,5 +204,66 @@
             return null;
         }
     }
-    
+
+    @Test
+    public void shouldMatchMethodParametersByParanamerNamesInNaturalOrder() throws Exception {
+    	NamedParameterStepsViaParanamer steps = new NamedParameterStepsViaParanamer();
+        CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
+        		NamedParameterStepsViaParanamer.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        candidateStep.useParanamer(true);
+        candidateStep.createFrom(tableValues, "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 shouldMatchMethodParametersByParanamerInverseOrder() throws Exception {
+    	NamedParameterStepsViaParanamer steps = new NamedParameterStepsViaParanamer();
+        CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth",
+        		NamedParameterStepsViaParanamer.methodFor("methodWithNamedParametersInInverseOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        candidateStep.useParanamer(true);
+        candidateStep.createFrom(tableValues, "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 {
+    	NamedParameterStepsViaParanamer steps = new NamedParameterStepsViaParanamer();
+    	tableValues.put("ith", "first");
+    	tableValues.put("nth", "ground");
+        CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth",
+        		NamedParameterStepsViaParanamer.methodFor("methodWithNamedParametersInNaturalOrder"), steps, PATTERN_BUILDER, MONITOR, new ParameterConverters(), "Given", "When", "Then");
+        candidateStep.useParanamer(true);
+        candidateStep.createFrom(tableValues, "When I live on the <ith> floor but some call it the <nth>").perform();
+        ensureThat(steps.ith, equalTo("first"));
+        ensureThat(steps.nth, equalTo("ground"));
+    }
+
+
+    public static class NamedParameterStepsViaParanamer 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;
+        }
+
+        public static Method methodFor(String methodName) throws IntrospectionException {
+            BeanInfo beanInfo = Introspector.getBeanInfo(NamedParameterStepsViaParanamer.class);
+            for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
+                if (md.getMethod().getName().equals(methodName)) {
+                    return md.getMethod();
+                }
+            }
+            return null;
+        }
+    }
+
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PrefixCapturingPatternBuilder.java (1214 => 1215)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PrefixCapturingPatternBuilder.java	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PrefixCapturingPatternBuilder.java	2009-09-03 05:36:32 UTC (rev 1215)
@@ -88,7 +88,7 @@
         
     }
 
-	public String[] extractParameterNames(String pattern) {
+	public String[] extractGroupNames(String pattern) {
 		List<String> names = new ArrayList<String>();
 		for (Replacement replacement : findArgumentsToReplace(pattern)) {
 			names.add(replacement.name);

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/StepPatternBuilder.java (1214 => 1215)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/StepPatternBuilder.java	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/StepPatternBuilder.java	2009-09-03 05:36:32 UTC (rev 1215)
@@ -28,6 +28,6 @@
      * @param step the template step
      * @return an array of parameter names
      */
-	String[] extractParameterNames(String string);
+	String[] extractGroupNames(String string);
 
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java (1214 => 1215)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java	2009-08-29 22:14:43 UTC (rev 1214)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/CandidateStep.java	2009-09-03 05:36:32 UTC (rev 1215)
@@ -11,12 +11,17 @@
 import org.jbehave.scenario.annotations.Named;
 import org.jbehave.scenario.errors.PendingError;
 import org.jbehave.scenario.parser.StepPatternBuilder;
+import com.thoughtworks.paranamer.Paranamer;
+import com.thoughtworks.paranamer.NullParanamer;
+import com.thoughtworks.paranamer.CachingParanamer;
+import com.thoughtworks.paranamer.BytecodeReadingParanamer;
 
 /**
  * Creates step from its candidate string representations
  * 
  * @author Elizabeth Keogh
  * @author Mauro Talevi
+ * @author Paul Hammant
  */
 public class CandidateStep {
 
@@ -27,7 +32,8 @@
     private final String[] startingWords;
     private final Pattern pattern;
     private StepMonitor stepMonitor = new SilentStepMonitor();
-	private String[] parameterNames;
+	private String[] groupNames;
+    private Paranamer paranamer = new NullParanamer();
 
     public CandidateStep(String stepAsString, Method method, CandidateSteps steps, StepPatternBuilder patterBuilder,
             StepMonitor stepMonitor, ParameterConverters parameterConverters, String... startingWords) {
@@ -43,7 +49,7 @@
         this.parameterConverters = parameterConverters;
         this.startingWords = startingWords;
         this.pattern = patternBuilder.buildPattern(stepAsString);
-        this.parameterNames = patternBuilder.extractParameterNames(stepAsString);
+        this.groupNames = patternBuilder.extractGroupNames(stepAsString);
     }
 
     public void useStepMonitor(StepMonitor stepMonitor) {
@@ -71,41 +77,64 @@
         Matcher matcher = pattern.matcher(trimStartingWord(startingWord, stepAsString));
         matcher.find();
         Type[] types = method.getGenericParameterTypes();
-        String[] annotatedParameterNames = annotatedParameterNames();
+        String[] annotationNames = annotatedParameterNames();
+        String[] parameterNames = paranamer.lookupParameterNames(method, false);
+        final Object[] args = new Object[types.length];
         int groupCount = matcher.groupCount();
-		final Object[] args = new Object[types.length];
-        for (int group = 0; group < types.length; group++) {
-            int parameterIndex = parameterIndex(annotatedParameterNames, group);
-            int groupIndex = -1; 
-            Type type = null;
-            if ( parameterIndex != -1 ){ // we are using annotated parameters
-            	groupIndex = parameterIndex + 1;
-                type = types[parameterIndex];
-            } else {                    // default natural ordering
-                groupIndex = group + 1;
-                type = types[group];
-            }
-
+        for (int ix = 0; ix < types.length; ix++) {
+            int annotatedNameIx = parameterIndex(annotationNames, ix);
+            int parameterNameIx = parameterIndex(parameterNames, ix);
             String arg = null;
-            if ( useAnnotatedParameterNames(tableValues) ){
-            	arg = tableValues.get(annotatedParameterNames[parameterIndex]);
+            if (annotatedNameIx != -1 && isGroupName(annotationNames[ix])) {
+                arg = getGroup(matcher, annotationNames[ix]);
+            } else if (parameterNameIx != -1 && isGroupName(parameterNames[ix])) {
+                arg = getGroup(matcher, parameterNames[ix]);
+            } else if (annotatedNameIx != -1 && isTableFieldName(tableValues, annotationNames[ix])) {
+                arg = getTableValue(tableValues, annotationNames[ix]);
+            } else if (parameterNameIx != -1 && isTableFieldName(tableValues, parameterNames[ix])) {
+                arg = getTableValue(tableValues, parameterNames[ix]);
             } else {
-            	arg = matcher.group(groupIndex);            	
+                arg = matcher.group(ix + 1);
             }
-            Object converted = parameterConverters.convert(arg, type);
-            args[group] = converted;
+            args[ix] = parameterConverters.convert(arg, types[ix]);
         }
         return createStep(stepAsString, args);
     }
 
-	private boolean useAnnotatedParameterNames(Map<String, String> tableValues) {
-		return tableValues.size() > 0;
-	}
+    private String getTableValue(Map<String, String> tableValues, String name) {
+        return tableValues.get(name);
+    }
 
-    private int parameterIndex(String[] annotatedNames, int group) {
-    	String name = annotatedNames[group];    	
-    	for ( int index = 0; index < annotatedNames.length; index++ ){
-            String annotatedName = annotatedNames[index];
+    private boolean isTableFieldName(Map<String, String> tableValues, String name) {
+        return tableValues.get(name) != null;
+    }
+
+    private String getGroup(Matcher matcher, String name) {
+        for (int i = 0; i < groupNames.length; i++) {
+            String groupName = groupNames[i];
+            if (name.equals(groupName)) {
+                return matcher.group(i + 1);
+            }
+        }
+        throw new RuntimeException("no group for name");
+    }
+
+    private boolean isGroupName(String name) {
+        for (String groupName : groupNames) {
+            if (name.equals(groupName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private int parameterIndex(String[] names, int ix) {
+        if (names.length == 0) {
+            return -1;
+        }
+    	String name = names[ix];
+    	for ( int index = 0; index < names.length; index++ ){
+            String annotatedName = names[index];
             if ( annotatedName != null && name.equals(annotatedName) ){
     			return index;
     		}
@@ -185,4 +214,11 @@
     	return stepAsString;
     }
 
+    public void useParanamer(boolean useIt) {
+        if (useIt) {
+            this.paranamer = new CachingParanamer(new BytecodeReadingParanamer());
+        } else {
+            this.paranamer = new NullParanamer();
+        }
+    }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to