Permalink
Browse files

Added SetLocale example.

  • Loading branch information...
1 parent 5a6e3bc commit e74e59e9f8cd9b28d8cb2381c3363160c45ecbb5 @seize-the-dave committed Sep 25, 2012
@@ -5,13 +5,42 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
+/**
+ * The SetLocale rule allows setting of a Locale for the duration
+ * of a test.
+ *
+ * <pre>
+ * public static class UsesSetLocale {
+ * &#064;Rule
+ * SetLocale setLocale = new SetLocale(Locale.FRANCE);
+ *
+ * &#064;Test
+ * public void testExample() {
+ * Calendar cal= GregorianCalendar.getInstance();
+ * cal.set(2000, 0, 1);
+ *
+ * Assert.assertEquals("Month does not match", "janvier",
+ * cal.getDisplayName(MONTH, LONG, Locale.getDefault()));
+ * }
+ * }
+ * </pre>
+ */
public class SetLocale implements TestRule {
private final Locale locale;
+ /**
+ * Creates a new instance of {@code SetLocale} with the provided
+ * {@link Locale}.
+ *
+ * @param locale the locale to set.
+ */
public SetLocale(Locale locale) {
this.locale = locale;
}
+ /**
+ * {@inheritDoc}
+ */
public Statement apply(Statement base, Description description) {
return statement(base);
}
@@ -0,0 +1,26 @@
+package org.junit.rules;
+
+import static java.util.Calendar.LONG;
+import static java.util.Calendar.MONTH;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class SetLocaleExampleTest {
+ @Rule
+ public SetLocale setLocale= new SetLocale(Locale.FRENCH);
+
+ @Test
+ public void testExample() {
+ Calendar cal= GregorianCalendar.getInstance();
+ cal.set(2000, 0, 1);
+
+ Assert.assertEquals("Month does not match", "janvier",
+ cal.getDisplayName(MONTH, LONG, Locale.getDefault()));
+ }
+}

0 comments on commit e74e59e

Please sign in to comment.