Title: [1096] trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps: JBEHAVE-158: Initial implementation of StepDocGenerator

Diff

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

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/StepDocGeneratorBehaviour.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/StepDocGeneratorBehaviour.java	2009-02-22 12:02:09 UTC (rev 1096)
@@ -0,0 +1,52 @@
+package org.jbehave.scenario.steps;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.jbehave.Ensure.ensureThat;
+
+import java.util.List;
+
+import org.junit.Test;
+
+public class StepDocGeneratorBehaviour {
+	
+    @Test
+    public void shouldGenerateStepDocsInPriorityOrder() {
+        StepDocGenerator generator = new DefaultStepDocGenerator();
+        MySteps steps = new MySteps();
+        List<StepDoc> stepdocs = generator.generate(steps.getClass());
+        ensureThat(stepdocs.get(0).getPattern(), equalTo("a given"));
+        ensureThat(stepdocs.get(0).getAliasPatterns(), equalTo(asList("a given alias", "another given alias")));
+        ensureThat(stepdocs.get(1).getPattern(), equalTo("a when"));
+        ensureThat(stepdocs.get(1).getAliasPatterns(), equalTo(asList("a when alias", "another when alias")));
+        ensureThat(stepdocs.get(2).getPattern(), equalTo("a then"));
+        ensureThat(stepdocs.get(2).getAliasPatterns(), equalTo(asList("a then alias", "another then alias")));
+    }    
+    
+    public static class MySteps extends Steps {
+        
+        private int givens;
+        private int whens;
+        private int thens;
+        
+        @org.jbehave.scenario.annotations.Given("a given")
+        @org.jbehave.scenario.annotations.Aliases(values={"a given alias", "another given alias"})
+        public void given() {
+            givens++;
+        }
+
+        @org.jbehave.scenario.annotations.When("a when")
+        @org.jbehave.scenario.annotations.Aliases(values={"a when alias", "another when alias"})
+        public void when() {
+            whens++;
+        }
+        
+        @org.jbehave.scenario.annotations.Then("a then")
+        @org.jbehave.scenario.annotations.Aliases(values={"a then alias", "another then alias"})
+        public void then() {
+            thens++;
+        }
+                
+    }
+    
+}

Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/DefaultStepDocGenerator.java (0 => 1096)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/DefaultStepDocGenerator.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/DefaultStepDocGenerator.java	2009-02-22 12:02:09 UTC (rev 1096)
@@ -0,0 +1,43 @@
+package org.jbehave.scenario.steps;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jbehave.scenario.annotations.Aliases;
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Then;
+import org.jbehave.scenario.annotations.When;
+
+public class DefaultStepDocGenerator implements StepDocGenerator {
+
+	public List<StepDoc> generate(Class<?> stepsClass) {
+		List<StepDoc> stepdocs = new LinkedList<StepDoc>();
+		for (Method method : stepsClass.getMethods()) {
+			if (method.isAnnotationPresent(Given.class)) {
+				stepdocs.add(new StepDoc(Given.class, method.getAnnotation(Given.class).value(), 
+						aliases(method)));
+			}
+			if (method.isAnnotationPresent(When.class)) {
+				stepdocs.add(new StepDoc(When.class, method.getAnnotation(When.class).value(), 
+						aliases(method)));
+			}
+			if (method.isAnnotationPresent(Then.class)) {
+				stepdocs.add(new StepDoc(Then.class, method.getAnnotation(Then.class).value(), 
+						aliases(method)));
+			}
+		}
+		Collections.sort(stepdocs);
+		return stepdocs;		
+	}
+
+	private String[] aliases(Method method) {
+		if (method.isAnnotationPresent(Aliases.class)) {
+			return method.getAnnotation(Aliases.class).values();
+		}
+		return new String[]{};
+	}
+
+
+}

Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDoc.java (0 => 1096)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDoc.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDoc.java	2009-02-22 12:02:09 UTC (rev 1096)
@@ -0,0 +1,61 @@
+package org.jbehave.scenario.steps;
+
+import static java.util.Arrays.asList;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Then;
+import org.jbehave.scenario.annotations.When;
+
+public class StepDoc implements Comparable<StepDoc> {
+
+	private final Class<? extends Annotation> annotation;
+	private final String pattern;
+	private final String[] aliasPatterns;
+	private Integer priority = 0;
+
+	public StepDoc(Class<? extends Annotation> annotation, String pattern,
+			String[] aliasPatterns) {
+		this.annotation = annotation;
+		this.pattern = pattern;
+		this.aliasPatterns = aliasPatterns;
+		assignPriority();
+	}
+
+	private void assignPriority() {
+		if (annotation.equals(Given.class)) {
+			priority = 1;
+		} else if (annotation.equals(When.class)) {
+			priority = 2;
+		} else if (annotation.equals(Then.class)) {
+			priority = 3;
+		}
+
+	}
+
+	public Class<? extends Annotation> getAnnotation() {
+		return annotation;
+	}
+
+	public String getPattern() {
+		return pattern;
+	}
+
+	public List<String> getAliasPatterns() {
+		return asList(aliasPatterns);
+	}
+
+	@Override
+	public String toString() {
+		StringBuffer sb = new StringBuffer();
+		sb.append("[StepDoc pattern=").append(pattern).append(", aliases=")
+				.append(asList(aliasPatterns)).append("]");
+		return sb.toString();
+	}
+
+	public int compareTo(StepDoc that) {
+		return this.priority.compareTo(that.priority);
+	}
+}

Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDocGenerator.java (0 => 1096)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDocGenerator.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/StepDocGenerator.java	2009-02-22 12:02:09 UTC (rev 1096)
@@ -0,0 +1,9 @@
+package org.jbehave.scenario.steps;
+
+import java.util.List;
+
+public interface StepDocGenerator {
+
+	List<StepDoc> generate(Class<?> stepsClass);
+
+}


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to