Title: [1282] trunk/core/examples/trader: Refactored I18nKeyWords to use injectable and configurable StringEncoder, in place of UTF8ResourceBundle.

Diff

Modified: trunk/core/examples/trader/pom.xml (1281 => 1282)

--- trunk/core/examples/trader/pom.xml	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/examples/trader/pom.xml	2009-09-26 16:16:45 UTC (rev 1282)
@@ -31,10 +31,10 @@
             <phase>integration-test</phase>
             <configuration>
               <scenarioIncludes>
-                <scenarioInclude>**/scenarios/I18n*.java</scenarioInclude>
+                <scenarioInclude>**/scenarios/*.java</scenarioInclude>
               </scenarioIncludes>
               <scenarioExcludes>
-                <scenarioExclude>**/*Steps.java</scenarioExclude>
+                <scenarioExclude>**/scenarios/None.java</scenarioExclude>
               </scenarioExcludes>
               <skip>false</skip>
             </configuration>
@@ -47,10 +47,10 @@
             <phase>integration-test</phase>
             <configuration>
               <scenarioIncludes>
-                <scenarioInclude>org/jbehave/examples/trader/scenarios/*.java</scenarioInclude>
+                <scenarioInclude>**/scenarios/*.java</scenarioInclude>
               </scenarioIncludes>
               <scenarioExcludes>
-                <scenarioExclude>**/*Steps.java</scenarioExclude>
+                <scenarioExclude>**/scenarios/None.java</scenarioExclude>
               </scenarioExcludes>
               <skip>true</skip>
             </configuration>

Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderScenario.java (1281 => 1282)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderScenario.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderScenario.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -1,11 +1,16 @@
 package org.jbehave.examples.trader;
 
+import static org.jbehave.scenario.i18n.StringEncoder.ISO_8859_1;
+import static org.jbehave.scenario.i18n.StringEncoder.UTF_8;
+
 import java.util.Locale;
 
+import org.hamcrest.text.StringEndsWith;
 import org.jbehave.scenario.JUnitScenario;
 import org.jbehave.scenario.PropertyBasedConfiguration;
 import org.jbehave.scenario.definition.KeyWords;
 import org.jbehave.scenario.i18n.I18nKeyWords;
+import org.jbehave.scenario.i18n.StringEncoder;
 import org.jbehave.scenario.parser.ClasspathScenarioDefiner;
 import org.jbehave.scenario.parser.PatternScenarioParser;
 import org.jbehave.scenario.parser.ScenarioDefiner;
@@ -31,13 +36,13 @@
 
 			@Override
 			public ScenarioReporter forReportingScenarios() {
-				// report outcome in Brazilian Portuguese (to System.out)
-				return new PrintStreamScenarioReporter(new I18nKeyWords(new Locale("pt")));
+				// report outcome in Portuguese (to System.out)
+				return new PrintStreamScenarioReporter(new I18nKeyWords(new Locale("pt"), new StringEncoder(UTF_8, ISO_8859_1)));
 			}
 
 			@Override
 			public KeyWords keywords() {
-				// use Brazilian Portuguese for keywords
+				// use Portuguese for keywords
 				return new I18nKeyWords(new Locale("pt"));
 			}
 

Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderSteps.java (1281 => 1282)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderSteps.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PtTraderSteps.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -23,17 +23,17 @@
         super(new StepsConfiguration(new I18nKeyWords(new Locale("pt"))));
     }
 
-    @Given("h‡ uma a‹o com s’mbolo $symbol e um limite de $threshold")
+    @Given("há uma ação com símbolo $symbol e um limite de $threshold")
     public void aStock(@Named("symbol") String symbol, @Named("threshold") double threshold) {
         stock = new Stock(symbol, threshold);
     }
 
-    @When("a a‹o Ž oferecida ao preo de $price")
+    @When("a ação é oferecida ao preço de $price")
     public void stockIsTraded(@Named("price") double price) {
         stock.tradeAt(price);
     }
 
-    @Then("o estado de alerta Ž $status")
+    @Then("o estado de alerta é $status")
     public void alertStatusIs(@Named("status") String status) {
         ensureThat(stock.getStatus().name(), equalTo(status));
     }

Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java (1281 => 1282)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -3,7 +3,6 @@
 import static org.junit.Assert.assertEquals;
 
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.util.Locale;
 import java.util.Properties;
 
@@ -12,6 +11,8 @@
 
 public class I18nKeywordsBehaviour {
 
+	private StringEncoder encoder = new StringEncoder("UTF-8", "UTF-8");
+	
 	@Test
 	public void keywordsInEnglishAsDefault() throws IOException {
 		ensureKeywordsAreLocalisedFor(null);
@@ -66,15 +67,7 @@
 	}
 
 	private void ensureKeywordIs(Properties properties, String key, String value) {
-		assertEquals(utf8(properties.getProperty(key)), value);
+		assertEquals(encoder.encode(properties.getProperty(key)), value);
 	}
 
-	private String utf8(String value) {
-		try {
-			return new String(value.getBytes("ISO-8859-1"), "UTF-8");
-		} catch (UnsupportedEncodingException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java (1281 => 1282)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -5,6 +5,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.jbehave.scenario.i18n.StringEncoder;
+
 /**
  * Provides the keywords which allow parsers to find steps in scenarios and
  * match those steps with candidates through the Given, When and Then
@@ -34,12 +36,14 @@
 	private final String then;
 	private final String examplesTable;
 	private final String[] others;
+	private StringEncoder encoder;
 
-	public KeyWords(Map<String, String> keywords) {
+	public KeyWords(Map<String, String> keywords, StringEncoder encoder) {
 		this(keywords.get(SCENARIO), keywords.get(GIVEN_SCENARIOS), keywords
 				.get(EXAMPLES_TABLE), keywords.get(GIVEN), keywords.get(WHEN),
 				keywords.get(THEN), keywords.get(AND), keywords.get(PENDING),
 				keywords.get(NOT_PERFORMED), keywords.get(FAILED), keywords.get(EXAMPLES_TABLE_ROW));
+		this.encoder = encoder;
 	}
 
 	public KeyWords(String scenario, String givenScenarios,
@@ -102,4 +106,11 @@
 		return others;
 	}
 
+	public String encode(String value) {
+		if ( encoder != null ){
+			return encoder.encode(value);
+		}
+		return value;
+	}
+
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java (1281 => 1282)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -14,34 +14,38 @@
 	private static final String DEFAULT_BUNDLE_NAME = "org/jbehave/scenario/i18n/keywords";
 
 	public I18nKeyWords() {
-        this(DEFAULT_BUNDLE_NAME, Locale.ENGLISH);
+        this(DEFAULT_BUNDLE_NAME, Locale.ENGLISH, new StringEncoder());
     }
 
     public I18nKeyWords(Locale locale) {
-    	this(DEFAULT_BUNDLE_NAME, locale);
+    	this(DEFAULT_BUNDLE_NAME, locale,  new StringEncoder());
     }
 
-    public I18nKeyWords(String bundleName, Locale locale) {
-    	super(keywords(bundleName, locale));
+    public I18nKeyWords(Locale locale,  StringEncoder encoder) {
+    	this(DEFAULT_BUNDLE_NAME, locale, encoder);
     }
 
+    public I18nKeyWords(String bundleName, Locale locale, StringEncoder encoder) {
+    	super(keywords(bundleName, locale, encoder), encoder);
+    }
+
 	private static Map<String, String> keywords(String bundleName,
-			Locale locale) {
+			Locale locale, StringEncoder encoder) {
 		ResourceBundle bundle = lookupBunde(bundleName, locale);
 		Map<String, String> keywords = new HashMap<String, String>();
 		for ( String key : KEYWORDS ) {
-			keywords.put(key, keyword(bundle, key));			
+			keywords.put(key, keyword(bundle, key, encoder));			
 		}
 		return keywords;
 	}
 
-	private static String keyword(ResourceBundle bundle, String name) {
-		return bundle.getString(name);
+	private static String keyword(ResourceBundle bundle, String name, StringEncoder encoder) {
+		return encoder.encode(bundle.getString(name));
 	}
 
     private static ResourceBundle lookupBunde(String bundleName, Locale locale) {
         try {
-            return UTF8ResourceBundle.getBundle(bundleName.trim(), locale);
+            return ResourceBundle.getBundle(bundleName.trim(), locale);
         } catch (MissingResourceException e) {
             return EMPTY_BUNDLE;
         }

Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java (0 => 1282)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java	                        (rev 0)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -0,0 +1,42 @@
+/**
+ * 
+ */
+package org.jbehave.scenario.i18n;
+
+import java.io.UnsupportedEncodingException;
+
+public class StringEncoder {
+
+	public static final String ISO_8859_1 = "ISO-8859-1";
+	public static final String UTF_8 = "UTF-8";
+	private String encoding;
+	private String decoding;
+	
+	public StringEncoder() {
+		this(UTF_8, UTF_8);
+	}
+	
+	public StringEncoder(String encoding, String decoding) {
+		this.encoding = encoding;
+		this.decoding = decoding;
+	}
+
+	public String encode(String value) {
+		try {
+			return new String(value.getBytes(encoding), decoding);
+		} catch (UnsupportedEncodingException e) {
+			throw new InvalidEncodingExcepion(value, e);
+		}
+	}
+	
+	@SuppressWarnings("serial")
+	public static final class InvalidEncodingExcepion extends RuntimeException {
+
+		public InvalidEncodingExcepion(String value,
+				UnsupportedEncodingException cause) {
+			super(value, cause);
+		}
+
+	}
+
+}
\ No newline at end of file

Deleted: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/UTF8ResourceBundle.java (1281 => 1282)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/UTF8ResourceBundle.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/UTF8ResourceBundle.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -1,62 +0,0 @@
-package org.jbehave.scenario.i18n;
-
-import java.io.UnsupportedEncodingException;
-import java.util.Enumeration;
-import java.util.Locale;
-import java.util.PropertyResourceBundle;
-import java.util.ResourceBundle;
-
-public class UTF8ResourceBundle {
-
-	public static ResourceBundle getBundle(String baseName) {
-		return utf8ResourceBundle(ResourceBundle.getBundle(baseName));
-	}
-
-	public static ResourceBundle getBundle(String baseName, Locale locale) {
-		return utf8ResourceBundle(ResourceBundle.getBundle(baseName,
-				locale));
-	}
-
-	private static ResourceBundle utf8ResourceBundle(
-			ResourceBundle bundle) {
-		if (!(bundle instanceof PropertyResourceBundle))
-			return bundle;
-
-		return new UTF8ResourceBundleDecorator((PropertyResourceBundle) bundle);
-	}
-
-	private static class UTF8ResourceBundleDecorator extends ResourceBundle {
-
-		private static final String ISO_8859_1 = "ISO-8859-1";
-		private static final String UTF_8 = "UTF-8";
-		
-		private final PropertyResourceBundle delegate;
-
-		private UTF8ResourceBundleDecorator(PropertyResourceBundle bundle) {
-			this.delegate = bundle;
-		}
-
-		public Enumeration<String> getKeys() {
-			return delegate.getKeys();
-		}
-
-		protected Object handleGetObject(String key) {
-			String value = (String) delegate.handleGetObject(key);
-			try {
-				return new String(value.getBytes(ISO_8859_1), UTF_8);
-			} catch (UnsupportedEncodingException e) {
-				throw new InvalidEncodingExcepion(value, e);
-			}
-		}
-	}
-
-	@SuppressWarnings("serial")
-	private static final class InvalidEncodingExcepion extends RuntimeException {
-
-		public InvalidEncodingExcepion(String value,
-				UnsupportedEncodingException cause) {
-			super(value, cause);
-		}
-
-	}
-}

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java (1281 => 1282)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java	2009-09-26 13:55:00 UTC (rev 1281)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java	2009-09-26 16:16:45 UTC (rev 1282)
@@ -67,7 +67,7 @@
 public class Steps implements CandidateSteps {
 
 	private final StepsConfiguration configuration;
-
+	
 	/**
 	 * Creates Steps with default configuration
 	 */
@@ -128,17 +128,17 @@
 		List<CandidateStep> steps = new ArrayList<CandidateStep>();
 		for (Method method : stepsClass.getMethods()) {
 			if (method.isAnnotationPresent(Given.class)) {
-				String value = method.getAnnotation(Given.class).value();
+				String value = encode(method.getAnnotation(Given.class).value());
 				createCandidateStep(steps, method, value);
 				createCandidateStepsFromAliases(steps, method);
 			}
 			if (method.isAnnotationPresent(When.class)) {
-				String value = method.getAnnotation(When.class).value();
+				String value = encode(method.getAnnotation(When.class).value());
 				createCandidateStep(steps, method, value);
 				createCandidateStepsFromAliases(steps, method);
 			}
 			if (method.isAnnotationPresent(Then.class)) {
-				String value = method.getAnnotation(Then.class).value();
+				String value = encode(method.getAnnotation(Then.class).value());
 				createCandidateStep(steps, method, value);
 				createCandidateStepsFromAliases(steps, method);
 			}
@@ -146,6 +146,10 @@
 		return steps.toArray(new CandidateStep[steps.size()]);
 	}
 
+	private String encode(String value) {
+		return configuration.getKeywords().encode(value);
+	}
+
 	private void createCandidateStep(List<CandidateStep> steps, Method method,
 			String stepAsString) {
 		checkForDuplicateCandidateSteps(steps, stepAsString);


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to