Title: [1237] trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters: Refactored PrintStreamScenarioReporter to allow custom output patterns
Revision
1237
Author
mauro
Date
2009-09-10 06:13:20 -0500 (Thu, 10 Sep 2009)

Log Message

Refactored PrintStreamScenarioReporter to allow custom output patterns

Modified Paths

Diff

Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PrintStreamScenarioReporterBehaviour.java (1236 => 1237)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PrintStreamScenarioReporterBehaviour.java	2009-09-10 08:04:36 UTC (rev 1236)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PrintStreamScenarioReporterBehaviour.java	2009-09-10 11:13:20 UTC (rev 1237)
@@ -5,6 +5,7 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.util.Properties;
 
 import org.jbehave.scenario.i18n.I18nKeyWords;
 import org.junit.Test;
@@ -39,7 +40,7 @@
         exception.printStackTrace(new PrintStream(stackTrace));
         
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ScenarioReporter reporter = new PrintStreamScenarioReporter(new PrintStream(out), new I18nKeyWords(), true);
+        ScenarioReporter reporter = new PrintStreamScenarioReporter(new PrintStream(out), new Properties(), new I18nKeyWords(), true);
         reporter.beforeScenario("A title");
         reporter.successful("Given I have a balance of $50");
         reporter.successful("When I request $20");
@@ -75,21 +76,12 @@
     public void shouldOutputStepsAndResultToPrintStreamWithCustomPatterns() {
         IllegalAccessException exception = new IllegalAccessException("Leave my money alone!");
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ScenarioReporter reporter = new PrintStreamScenarioReporter(new PrintStream(out)){
+        Properties patterns = new Properties();
+        patterns.setProperty("pending", "{0} - {1} - need to implement me");
+        patterns.setProperty("failed", "{0} <<< {1}");
+        patterns.setProperty("notPerformed", "{0} : {1} (because of previous pending)");
 
-			@Override
-			protected String patternFor(String key, String defaultPattern) {
-				if ( key.equals("pending") ){
-					return "{0} (NOT YET IMPLEMENTED)";
-				} else if ( key.equals("failed") ){
-					return "{0} <<< FAILED";					
-				} else if ( key.equals("notPerformed") ){
-					return "{0} (NOT EXECUTED DUE TO PENDING)";					
-				}
-				return defaultPattern;
-			}
-        	
-        };
+		ScenarioReporter reporter = new PrintStreamScenarioReporter(new PrintStream(out),  patterns, new I18nKeyWords(), true);
         reporter.successful("Given I have a balance of $50");
         reporter.successful("When I request $20");
         reporter.failed("When I ask Liz for a loan of $100", exception);
@@ -100,7 +92,8 @@
                 "Given I have a balance of $50" + NL +
                 "When I request $20" + NL +
                 "When I ask Liz for a loan of $100 <<< FAILED" + NL +
-                "Then I should have a balance of $30 (NOT YET IMPLEMENTED)" + NL +
-                "Then I should have $20 (NOT EXECUTED DUE TO PENDING)" + NL));
+                "Then I should have a balance of $30 - PENDING - need to implement me" + NL +
+                "Then I should have $20 : NOT PERFORMED (because of previous pending)" + NL));
+        
     }
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java (1236 => 1237)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java	2009-09-10 08:04:36 UTC (rev 1236)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java	2009-09-10 11:13:20 UTC (rev 1237)
@@ -4,6 +4,7 @@
 import java.text.MessageFormat;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 
 import org.jbehave.scenario.definition.Blurb;
 import org.jbehave.scenario.definition.ExamplesTable;
@@ -18,6 +19,7 @@
 public class PrintStreamScenarioReporter implements ScenarioReporter {
 
 	private final PrintStream output;
+	private final Properties outputPatterns;
 	private final KeyWords keywords;
 	private final boolean reportErrors;
 	private Throwable cause;
@@ -27,12 +29,13 @@
 	}
 
 	public PrintStreamScenarioReporter(PrintStream output) {
-		this(output, new I18nKeyWords(), false);
+		this(output, new Properties(), new I18nKeyWords(), false);
 	}
 
-	public PrintStreamScenarioReporter(PrintStream output, KeyWords keywords, boolean reportErrors) {
+	public PrintStreamScenarioReporter(PrintStream output, Properties outputPatterns, KeyWords keywords, boolean reportErrors) {
 		this.output = output;
 		this.keywords = keywords;
+		this.outputPatterns = outputPatterns;
 		this.reportErrors = reportErrors;
 	}
 
@@ -67,7 +70,7 @@
 	}
 
 	public void afterStory() {
-		output.println(format("afterStory", "" ));
+		output.println(format("afterStory", ""));
 	}
 
 	public void beforeStory(Blurb blurb) {
@@ -85,15 +88,25 @@
 	}
 
 	public void examplesTableRow(Map<String, String> tableRow) {
-		output.println(format("tableRow", "{0} {1}\n", keywords.examplesTableRow(), tableRow));
+		output.println(format("examplesTableRow", "{0} {1}\n", keywords.examplesTableRow(), tableRow));
 	}
 
+	/**
+	 * Formats event output by key, conventionally equal to the method name.
+	 * 
+	 * @param key the event key
+	 * @param defaultPattern the default pattern to return if a custom pattern is not found
+	 * @param args the args used to format output
+	 * @return A formatted event output
+	 */
 	protected String format(String key, String defaultPattern, Object... args) {
 		return MessageFormat.format(patternFor(key, defaultPattern), args);
 	}
 
 	/**
-	 * Provide format patterns for the output by key, conventionally equal to the method name.
+	 * Looks up the format pattern for the event output by key, conventionally equal to the method name.
+	 * The pattern is used by the {#format(String,String,Object...)} method and by default is formatted
+	 * using the {...@link MessageFormat#format()} method.
 	 * If no pattern is found for key or needs to be overridden, the default pattern should be returned. 
 	 * 
 	 * @param key the format pattern key
@@ -101,6 +114,9 @@
 	 * @return The format patter for the given key
 	 */
 	protected String patternFor(String key, String defaultPattern) {
+		if ( outputPatterns.containsKey(key) ){
+			return outputPatterns.getProperty(key);
+		}
 		return defaultPattern;
 	}
 


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to