Title: [1287] trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n: Verification of negative cases in i18n keywords behaviour.

Diff

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

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java	2009-10-04 11:28:53 UTC (rev 1286)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/I18nKeywordsBehaviour.java	2009-10-04 12:37:43 UTC (rev 1287)
@@ -3,10 +3,13 @@
 import static org.junit.Assert.assertEquals;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Locale;
 import java.util.Properties;
 
 import org.jbehave.scenario.definition.KeyWords;
+import org.jbehave.scenario.i18n.I18nKeyWords.KeywordNotFoundExcepion;
+import org.jbehave.scenario.i18n.I18nKeyWords.ResourceBundleNotFoundExcepion;
 import org.junit.Test;
 
 public class I18nKeywordsBehaviour {
@@ -14,23 +17,34 @@
 	private StringEncoder encoder = new StringEncoder("UTF-8", "UTF-8");
 
 	@Test
-	public void keywordsInEnglishAsDefault() throws IOException {
-		ensureKeywordsAreLocalisedFor(null);
+	public void shouldAllowKeywordsInEnglishAsDefault() throws IOException {
+		ensureKeywordsAreLocalisedFor(null, null);
 	}
 
 	@Test
-	public void keywordsInItalian() throws IOException {
-		ensureKeywordsAreLocalisedFor(new Locale("it"));
+	public void shouldAllowKeywordsInADifferentLocale() throws IOException {
+		ensureKeywordsAreLocalisedFor(new Locale("it"), null);
 	}
 
-	private void ensureKeywordsAreLocalisedFor(Locale locale)
+	@Test(expected = ResourceBundleNotFoundExcepion.class)
+	public void shouldFailIfResourceBundleIsNotFound() throws IOException {
+		ensureKeywordsAreLocalisedFor(new Locale("en"), "unknown");
+	}
+
+	@Test(expected = KeywordNotFoundExcepion.class)
+	public void shouldFailIfKeywordIsNotFound() throws IOException {
+		ensureKeywordsAreLocalisedFor(new Locale("es"), null);
+	}
+
+	private void ensureKeywordsAreLocalisedFor(Locale locale, String bundleName)
 			throws IOException {
+		KeyWords keywords = keyWordsFor(locale, bundleName);
 		Properties properties = bundleFor(locale);
-		KeyWords keywords = keyWordsFor(locale);
 		ensureKeywordIs(properties, "Scenario", keywords.scenario());
 		ensureKeywordIs(properties, "GivenScenarios", keywords.givenScenarios());
 		ensureKeywordIs(properties, "ExamplesTable", keywords.examplesTable());
-		ensureKeywordIs(properties, "ExamplesTableRow", keywords.examplesTableRow());
+		ensureKeywordIs(properties, "ExamplesTableRow", keywords
+				.examplesTableRow());
 		ensureKeywordIs(properties, "Given", keywords.given());
 		ensureKeywordIs(properties, "When", keywords.when());
 		ensureKeywordIs(properties, "Then", keywords.then());
@@ -40,22 +54,29 @@
 		ensureKeywordIs(properties, "Failed", keywords.failed());
 	}
 
-	private I18nKeyWords keyWordsFor(Locale locale) {
-		return (locale == null ? new I18nKeyWords() : new I18nKeyWords(locale));
+	private I18nKeyWords keyWordsFor(Locale locale, String bundleName) {
+		if ( bundleName == null ){
+			return (locale == null ? new I18nKeyWords() : new I18nKeyWords(locale));
+		} else {
+			return new I18nKeyWords(locale, new StringEncoder(), bundleName, Thread.currentThread().getContextClassLoader());
+		}
 	}
 
-	private Properties bundleFor(Locale locale) throws IOException {
+	private Properties bundleFor(Locale locale) throws IOException {		
 		Properties expected = new Properties();
 		String bundle = "org/jbehave/scenario/i18n/keywords_"
 				+ (locale == null ? "en" : locale.getLanguage())
 				+ ".properties";
-		expected.load(Thread.currentThread().getContextClassLoader()
-				.getResourceAsStream(bundle));
+		InputStream stream = Thread.currentThread().getContextClassLoader()
+				.getResourceAsStream(bundle);
+		if (stream != null) {
+			expected.load(stream);
+		}
 		return expected;
 	}
-
+	
 	private void ensureKeywordIs(Properties properties, String key, String value) {
-		assertEquals(encoder.encode(properties.getProperty(key)), value);
+		assertEquals(encoder.encode(properties.getProperty(key, value)), value);
 	}
 
 }

Added: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/keywords_es.properties (0 => 1287)

--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/keywords_es.properties	                        (rev 0)
+++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/i18n/keywords_es.properties	2009-10-04 12:37:43 UTC (rev 1287)
@@ -0,0 +1 @@
+Scenario=Escenario:

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

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java	2009-10-04 11:28:53 UTC (rev 1286)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/I18nKeyWords.java	2009-10-04 12:37:43 UTC (rev 1287)
@@ -8,56 +8,85 @@
 
 import org.jbehave.scenario.definition.KeyWords;
 
+/**
+ * Add i18n support to Keywords, allowing to read the keywords from resource bundles for a given locale.
+ */
 public class I18nKeyWords extends KeyWords {
 
+	private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
+	private static final StringEncoder DEFAULT_STRING_ENCODER = new StringEncoder();
 	private static final String DEFAULT_BUNDLE_NAME = "org/jbehave/scenario/i18n/keywords";
+	private static final ClassLoader DEFAULT_CLASS_LOADER = Thread
+			.currentThread().getContextClassLoader();
 
 	public I18nKeyWords() {
-        this(Locale.ENGLISH, new StringEncoder(), DEFAULT_BUNDLE_NAME,  Thread.currentThread().getContextClassLoader());
-    }
+		this(DEFAULT_LOCALE, DEFAULT_STRING_ENCODER, DEFAULT_BUNDLE_NAME,
+				DEFAULT_CLASS_LOADER);
+	}
 
-    public I18nKeyWords(Locale locale) {
-    	this(locale, new StringEncoder(),  DEFAULT_BUNDLE_NAME, Thread.currentThread().getContextClassLoader());
-    }
+	public I18nKeyWords(Locale locale) {
+		this(locale, DEFAULT_STRING_ENCODER, DEFAULT_BUNDLE_NAME,
+				DEFAULT_CLASS_LOADER);
+	}
 
-    public I18nKeyWords(Locale locale,  StringEncoder encoder) {
-    	this(locale, encoder, DEFAULT_BUNDLE_NAME, Thread.currentThread().getContextClassLoader());
-    }
+	public I18nKeyWords(Locale locale, StringEncoder encoder) {
+		this(locale, encoder, DEFAULT_BUNDLE_NAME, DEFAULT_CLASS_LOADER);
+	}
 
-    public I18nKeyWords(Locale locale, StringEncoder encoder, String bundleName, ClassLoader classLoader) {
-    	super(keywords(bundleName, locale, encoder, classLoader), encoder);
-    }
+	public I18nKeyWords(Locale locale, StringEncoder encoder,
+			String bundleName, ClassLoader classLoader) {
+		super(keywords(bundleName, locale, encoder, classLoader), encoder);
+	}
 
 	private static Map<String, String> keywords(String bundleName,
 			Locale locale, StringEncoder encoder, ClassLoader classLoader) {
-		ResourceBundle bundle = lookupBunde(bundleName, locale, classLoader);
+		ResourceBundle bundle = lookupBunde(bundleName.trim(), locale,
+				classLoader);
 		Map<String, String> keywords = new HashMap<String, String>();
-		for ( String key : KEYWORDS ) {
-			keywords.put(key, keyword(bundle, key, encoder));			
+		for (String key : KEYWORDS) {
+			keywords.put(key, keyword(key, bundle, encoder));
 		}
 		return keywords;
 	}
 
-	private static String keyword(ResourceBundle bundle, String name, StringEncoder encoder) {
-		return encoder.encode(bundle.getString(name));
+	private static String keyword(String name, ResourceBundle bundle,
+			StringEncoder encoder) {
+		try {
+			return encoder.encode(bundle.getString(name));
+		} catch (MissingResourceException e) {
+			throw new KeywordNotFoundExcepion(name, bundle);
+		}
 	}
 
-    private static ResourceBundle lookupBunde(String bundleName, Locale locale, ClassLoader classLoader) {
-        try {
-            return ResourceBundle.getBundle(bundleName.trim(), locale, classLoader);
-        } catch (MissingResourceException e) {
-            throw new ResourceBundleNotFoundExcepion(bundleName, locale, classLoader, e);
-        }
-    }
-    
+	private static ResourceBundle lookupBunde(String bundleName, Locale locale,
+			ClassLoader classLoader) {
+		try {
+			return ResourceBundle.getBundle(bundleName, locale, classLoader);
+		} catch (MissingResourceException e) {
+			throw new ResourceBundleNotFoundExcepion(bundleName, locale,
+					classLoader, e);
+		}
+	}
+
 	@SuppressWarnings("serial")
-	public static final class ResourceBundleNotFoundExcepion extends RuntimeException {
+	public static final class ResourceBundleNotFoundExcepion extends
+			RuntimeException {
 
-		public ResourceBundleNotFoundExcepion(String bundleName,
-				Locale locale, ClassLoader classLoader, MissingResourceException cause) {
-			super("Resource bundle "+bundleName+" not found for locale "+locale+" in classLoader "+classLoader, cause);
+		public ResourceBundleNotFoundExcepion(String bundleName, Locale locale,
+				ClassLoader classLoader, MissingResourceException cause) {
+			super("Resource bundle " + bundleName + " not found for locale "
+					+ locale + " in classLoader " + classLoader, cause);
 		}
 
 	}
 
+	@SuppressWarnings("serial")
+	public static final class KeywordNotFoundExcepion extends RuntimeException {
+
+		public KeywordNotFoundExcepion(String name, ResourceBundle bundle) {
+			super("Keyword " + name + " not found in resource bundle " + bundle);
+		}
+
+	}
+
 }

Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java (1286 => 1287)

--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java	2009-10-04 11:28:53 UTC (rev 1286)
+++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/i18n/StringEncoder.java	2009-10-04 12:37:43 UTC (rev 1287)
@@ -5,17 +5,31 @@
 
 import java.io.UnsupportedEncodingException;
 
+/**
+ * Supports encoding of strings using specified charsets for encoding (i.e. from
+ * String to byte[]) and decoding (i.e. from byte[] to String).
+ */
 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;
-	
+
+	/**
+	 * Creates an encoder using "UTF-8" for encoding and decoding
+	 */
 	public StringEncoder() {
 		this(UTF_8, UTF_8);
 	}
-	
+
+	/**
+	 * Creates an encoder using the specifed charsets for encoding and decoding
+	 * 
+	 * @param encoding
+	 *            the name of the encoding charset
+	 * @param decoding
+	 *            the name of the decoding charset
+	 */
 	public StringEncoder(String encoding, String decoding) {
 		this.encoding = encoding;
 		this.decoding = decoding;
@@ -28,7 +42,7 @@
 			throw new InvalidEncodingExcepion(value, e);
 		}
 	}
-	
+
 	@SuppressWarnings("serial")
 	public static final class InvalidEncodingExcepion extends RuntimeException {
 


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to