- Revision
- 1225
- Author
- mauro
- Date
- 2009-09-05 17:52:30 -0500 (Sat, 05 Sep 2009)
Log Message
JBEHAVE-164: Added I18nKeyWords to allow keyworks to be localised.
Modified Paths
- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java
- trunk/core/pom.xml
Added Paths
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/EmptyResourceBundle.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords.properties
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_es_ES.properties
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_it.properties
Diff
Modified: trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java (1224 => 1225)
--- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java 2009-09-05 15:58:21 UTC (rev 1224) +++ trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -28,7 +28,7 @@ public PlayersCanHazTurns(final ClassLoader classLoader, OAndXUniverse universe) { super(new MostUsefulConfiguration() { public KeyWords keywords() { - return new KeyWords("I can haz:", "Gief I can haz:", "Gief", "Wen", "Den", "Tbl", "And"); + return new KeyWords("I can haz:", "Gief I can haz:", "Ehemples:", "Gief", "Wen", "Den", "And"); } public ClasspathScenarioDefiner forDefiningScenarios() { return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(), new PatternScenarioParser(this),
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java (1224 => 1225)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java 2009-09-05 15:58:21 UTC (rev 1224) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -2,6 +2,8 @@ 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.parser.ClasspathScenarioDefiner; import org.jbehave.scenario.parser.PatternScenarioParser; import org.jbehave.scenario.parser.ScenarioDefiner; @@ -26,6 +28,11 @@ public ScenarioReporter forReportingScenarios() { return new PrintStreamScenarioReporter(); } + + @Override + public KeyWords keywords() { + return new I18nKeyWords(); + } }, new TraderSteps(classLoader)); }
Added: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java (0 => 1225)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java (rev 0) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,49 @@ +package org.jbehave.scenario.i18n; + +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import org.jbehave.scenario.definition.KeyWords; +import org.junit.Test; + + +public class I18nKeywordsBehaviour { + + @Test + public void keywordsInDefaultLocale(){ + KeyWords keywords = new I18nKeyWords(); + assertEquals("Scenario:", keywords.scenario()); + assertEquals("GivenScenarios:", keywords.givenScenarios()); + assertEquals("Examples:", keywords.table()); + assertEquals("Given", keywords.given()); + assertEquals("When", keywords.when()); + assertEquals("Then", keywords.then()); + assertEquals("And", keywords.and()); + } + + @Test + public void keywordsInItalian(){ + KeyWords keywords = new I18nKeyWords(Locale.ITALIAN); + assertEquals("Scenario:", keywords.scenario()); + assertEquals("DatiScenari:", keywords.givenScenarios()); + assertEquals("Esempi:", keywords.table()); + assertEquals("Dato", keywords.given()); + assertEquals("Quando", keywords.when()); + assertEquals("Allora", keywords.then()); + assertEquals("E", keywords.and()); + } + + @Test + public void keywordsInSpanish(){ + KeyWords keywords = new I18nKeyWords(new Locale("es", "ES", "")); + assertEquals("Escenario:", keywords.scenario()); + assertEquals("DadosEscenarios:", keywords.givenScenarios()); + assertEquals("Ejemplos:", keywords.table()); + assertEquals("Dado", keywords.given()); + assertEquals("Cuando", keywords.when()); + assertEquals("Entonces", keywords.then()); + assertEquals("Y", keywords.and()); + } + +}
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java (1224 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java 2009-09-05 15:58:21 UTC (rev 1224) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -1,5 +1,9 @@ package org.jbehave.scenario.definition; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + /** * Provides the keywords which allow parsers to find steps in scenarios and * match those steps with candidates through the Given, When and Then @@ -7,6 +11,15 @@ */ public class KeyWords { + static final String SCENARIO = "Scenario"; + static final String GIVEN_SCENARIOS = "GivenScenarios"; + static final String GIVEN = "Given"; + static final String WHEN = "When"; + static final String THEN = "Then"; + static final String AND = "And"; + static final String TABLE = "Table"; + protected static final List<String> KEYWORDS = Arrays.asList(SCENARIO, GIVEN_SCENARIOS, GIVEN, WHEN, THEN, AND, TABLE); + private final String scenario; private final String givenScenarios; private final String given; @@ -15,14 +28,19 @@ private final String table; private final String[] others; - public KeyWords(String scenario, String givenScenarios, String given, String when, - String then, String table, String... others) { + public KeyWords(Map<String, String> keywords) { + this(keywords.get(SCENARIO), keywords.get(GIVEN_SCENARIOS), keywords + .get(TABLE), keywords.get(GIVEN), keywords.get(WHEN), keywords.get(THEN), keywords.get(AND)); + } + + public KeyWords(String scenario, String givenScenarios, String table, + String given, String when, String then, String... others) { this.scenario = scenario; this.givenScenarios = givenScenarios; + this.table = table; this.given = given; this.when = when; this.then = then; - this.table = table; this.others = others; } @@ -50,9 +68,12 @@ return then; } + public String and() { + return others[0]; + } + public String[] others() { return others; } - }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java (1224 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java 2009-09-05 15:58:21 UTC (rev 1224) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -3,7 +3,7 @@ public class ScenarioGivenWhenThenAnd extends KeyWords { public ScenarioGivenWhenThenAnd() { - super("Scenario:", "GivenScenarios:", "Given", "When", "Then", "Examples:", "And"); + super("Scenario:", "GivenScenarios:", "Examples:", "Given", "When", "Then", "And"); } }
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/EmptyResourceBundle.java (0 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/EmptyResourceBundle.java (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/EmptyResourceBundle.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,20 @@ +/* + * Copyright (c) terms as published in http://waffle.codehaus.org/license.html + */ +package org.jbehave.scenario.i18n; + +import java.util.ListResourceBundle; + +/** + * Empty resource bundle, used as <a + * href="" when + * no resource bundle is found. + * + * @author Mauro Talevi + */ +class EmptyResourceBundle extends ListResourceBundle { + @Override + protected Object[][] getContents() { + return new Object[][] { { "", "" } }; + } +}
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java (0 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,52 @@ +package org.jbehave.scenario.i18n; + +import static java.util.ResourceBundle.getBundle; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +import org.jbehave.scenario.definition.KeyWords; + +public class I18nKeyWords extends KeyWords { + + private static final ResourceBundle EMPTY_BUNDLE = new EmptyResourceBundle(); + private static final String DEFAULT_BUNDLE_NAME = "org/jbehave/scenario/i18n/keywords"; + + public I18nKeyWords() { + this(DEFAULT_BUNDLE_NAME, Locale.getDefault()); + } + + public I18nKeyWords(Locale locale) { + this(DEFAULT_BUNDLE_NAME, locale); + } + + public I18nKeyWords(String bundleName, Locale locale) { + super(keywords(bundleName, locale)); + } + + private static Map<String, String> keywords(String bundleName, + Locale locale) { + ResourceBundle bundle = lookupBunde(bundleName, locale); + Map<String, String> keywords = new HashMap<String, String>(); + for ( String key : KEYWORDS ) { + keywords.put(key, keyword(bundle, key)); + } + return keywords; + } + + private static String keyword(ResourceBundle bundle, String name) { + return bundle.getString(name); + } + + private static ResourceBundle lookupBunde(String bundleName, Locale locale) { + try { + return getBundle(bundleName.trim(), locale); + } catch (MissingResourceException e) { + return EMPTY_BUNDLE; + } + } + +}
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords.properties (0 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords.properties (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords.properties 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,7 @@ +Scenario=Scenario: +GivenScenarios=GivenScenarios: +Table=Examples: +Given=Given +When=When +Then=Then +And=And
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_es_ES.properties (0 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_es_ES.properties (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_es_ES.properties 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,7 @@ +Scenario=Escenario: +GivenScenarios=DadosEscenarios: +Table=Ejemplos: +Given=Dado +When=Cuando +Then=Entonces +And=Y
Added: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_it.properties (0 => 1225)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_it.properties (rev 0) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/keywords_it.properties 2009-09-05 22:52:30 UTC (rev 1225) @@ -0,0 +1,7 @@ +Scenario=Scenario: +GivenScenarios=DatiScenari: +Table=Esempi: +Given=Dato +When=Quando +Then=Allora +And=E
Modified: trunk/core/pom.xml (1224 => 1225)
--- trunk/core/pom.xml 2009-09-05 15:58:21 UTC (rev 1224) +++ trunk/core/pom.xml 2009-09-05 22:52:30 UTC (rev 1225) @@ -68,6 +68,15 @@ <build> <sourceDirectory>src/java</sourceDirectory> <testSourceDirectory>src/behaviour</testSourceDirectory> + <resources> + <resource> + <directory>${project.build.sourceDirectory}</directory> + <filtering>false</filtering> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </resource> + </resources> <testResources> <testResource> <directory>${project.build.testSourceDirectory}</directory>
To unsubscribe from this list please visit:
