You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2017/08/04 23:25:35 UTC

[07/27] geode git commit: GEODE-3028: Fix the expected value of the test that depended on Locale

GEODE-3028: Fix the expected value of the test that depended on Locale

Fix the test that uses the English message as the expected value so that
it does not depend on the locale at runtime environment.

This closes #556


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/d77d69f6
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/d77d69f6
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/d77d69f6

Branch: refs/heads/feature/GEODE-3299
Commit: d77d69f65677bb3e9e7babb316a2bd84cbe9991c
Parents: e5f8e8c
Author: masaki.yamakawa <ma...@ulsystems.co.jp>
Authored: Mon May 29 08:17:10 2017 +0900
Committer: Kirk Lund <kl...@apache.org>
Committed: Wed Aug 2 10:04:02 2017 -0700

----------------------------------------------------------------------
 .../GMSAuthenticatorWithAuthenticatorTest.java  |  12 ++
 ...GMSAuthenticatorWithSecurityManagerTest.java |  12 ++
 .../internal/cache/ColocationHelperTest.java    |  14 ++
 .../tier/sockets/ServerConnectionTest.java      |  13 ++
 .../test/junit/rules/RestoreLocaleRule.java     | 106 ++++++++++++
 .../test/junit/rules/RestoreLocaleRuleTest.java | 172 +++++++++++++++++++
 6 files changed, 329 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithAuthenticatorTest.java b/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithAuthenticatorTest.java
index 96a9a5f..eb0fddb 100644
--- a/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithAuthenticatorTest.java
+++ b/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithAuthenticatorTest.java
@@ -17,13 +17,17 @@ package org.apache.geode.distributed.internal.membership.gms.auth;
 import static org.apache.geode.distributed.ConfigurationProperties.*;
 import static org.assertj.core.api.Assertions.*;
 
+import java.util.Locale;
 import java.util.Properties;
 
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import org.apache.geode.i18n.StringId;
 import org.apache.geode.test.junit.categories.SecurityTest;
 import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.rules.RestoreLocaleRule;
 
 /**
  * Unit tests GMSAuthenticator using old security.
@@ -31,6 +35,14 @@ import org.apache.geode.test.junit.categories.UnitTest;
 @Category({UnitTest.class, SecurityTest.class})
 public class GMSAuthenticatorWithAuthenticatorTest extends AbstractGMSAuthenticatorTestCase {
 
+  /**
+   * This test assumes Locale is in English. Before the test, change the locale of Locale and
+   * StringId to English and restore the original locale after the test.
+   */
+  @Rule
+  public final RestoreLocaleRule restoreLocale =
+      new RestoreLocaleRule(Locale.ENGLISH, l -> StringId.setLocale(l));
+
   @Override
   protected boolean isIntegratedSecurity() {
     return false;

http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithSecurityManagerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithSecurityManagerTest.java b/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithSecurityManagerTest.java
index 01ebc81..04eb885 100644
--- a/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithSecurityManagerTest.java
+++ b/geode-core/src/test/java/org/apache/geode/distributed/internal/membership/gms/auth/GMSAuthenticatorWithSecurityManagerTest.java
@@ -18,14 +18,18 @@ import static org.apache.geode.distributed.ConfigurationProperties.*;
 import static org.assertj.core.api.Assertions.*;
 import static org.mockito.Mockito.*;
 
+import java.util.Locale;
 import java.util.Properties;
 
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import org.apache.geode.i18n.StringId;
 import org.apache.geode.security.GemFireSecurityException;
 import org.apache.geode.test.junit.categories.SecurityTest;
 import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.rules.RestoreLocaleRule;
 
 /**
  * Unit tests GMSAuthenticator using new integrated security.
@@ -33,6 +37,14 @@ import org.apache.geode.test.junit.categories.UnitTest;
 @Category({UnitTest.class, SecurityTest.class})
 public class GMSAuthenticatorWithSecurityManagerTest extends AbstractGMSAuthenticatorTestCase {
 
+  /**
+   * This test assumes Locale is in English. Before the test, change the locale of Locale and
+   * StringId to English and restore the original locale after the test.
+   */
+  @Rule
+  public final RestoreLocaleRule restoreLocale =
+      new RestoreLocaleRule(Locale.ENGLISH, l -> StringId.setLocale(l));
+
   @Override
   protected boolean isIntegratedSecurity() {
     return true;

http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-core/src/test/java/org/apache/geode/internal/cache/ColocationHelperTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/ColocationHelperTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/ColocationHelperTest.java
index eb7e487..c16de31 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/ColocationHelperTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/ColocationHelperTest.java
@@ -23,6 +23,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+
+import java.util.Locale;
+
 import org.mockito.ArgumentCaptor;
 
 import org.apache.logging.log4j.Level;
@@ -36,17 +39,28 @@ import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 import org.apache.geode.cache.PartitionAttributes;
 import org.apache.geode.cache.Region;
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
+import org.apache.geode.i18n.StringId;
 import org.apache.geode.test.fake.Fakes;
 import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.rules.RestoreLocaleRule;
 
 @Category(UnitTest.class)
 public class ColocationHelperTest {
+  /**
+   * This test assumes Locale is in English. Before the test, change the locale of Locale and
+   * StringId to English and restore the original locale after the test.
+   */
+  @Rule
+  public final RestoreLocaleRule restoreLocale =
+      new RestoreLocaleRule(Locale.ENGLISH, l -> StringId.setLocale(l));
+
   private GemFireCacheImpl cache;
   private GemFireCacheImpl oldCacheInstance;
   private InternalDistributedSystem system;

http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionTest.java
index d3ef21f..7399a72 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionTest.java
@@ -26,13 +26,16 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import org.apache.geode.cache.Cache;
+import org.apache.geode.i18n.StringId;
 import org.apache.geode.internal.Version;
 import org.apache.geode.internal.cache.InternalCache;
 import org.apache.geode.internal.cache.tier.Acceptor;
 import org.apache.geode.internal.security.SecurityService;
 import org.apache.geode.security.AuthenticationRequiredException;
 import org.apache.geode.test.junit.categories.UnitTest;
+import org.apache.geode.test.junit.rules.RestoreLocaleRule;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.mockito.InjectMocks;
@@ -42,9 +45,19 @@ import org.mockito.MockitoAnnotations;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.util.Locale;
 
 @Category(UnitTest.class)
 public class ServerConnectionTest {
+
+  /**
+   * This test assumes Locale is in English. Before the test, change the locale of Locale and
+   * StringId to English and restore the original locale after the test.
+   */
+  @Rule
+  public final RestoreLocaleRule restoreLocale =
+      new RestoreLocaleRule(Locale.ENGLISH, l -> StringId.setLocale(l));
+
   @Mock
   private Message requestMsg;
 

http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-junit/src/main/java/org/apache/geode/test/junit/rules/RestoreLocaleRule.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/RestoreLocaleRule.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/RestoreLocaleRule.java
new file mode 100644
index 0000000..3497ef1
--- /dev/null
+++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/RestoreLocaleRule.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.geode.test.junit.rules;
+
+import java.util.Locale;
+import java.util.function.Consumer;
+
+import org.junit.rules.ExternalResource;
+
+/**
+ * The {@code RestoreLocale} rule undoes changes of locale when the test finishes (whether it passes
+ * or fails).
+ * <p>
+ * Let's assume the locale {@code US}. Now run the test
+ * 
+ * <pre>
+ *   public void YourTest {
+ *     &#064;Rule
+ *     public final TestRule restoreLocale = new RestoreLocale();
+ *
+ *     &#064;Test
+ *     public void overrideLocale() {
+ *       Locale.setDefaule(Locale.JAPAN);
+ *       assertThat(Locale.getDefault(), is(Locale.JAPAN));
+ *     }
+ *   }
+ * </pre>
+ * 
+ * After running the test, the locale will set to {@code US} again.
+ */
+public class RestoreLocaleRule extends ExternalResource {
+  private Locale originalLocale;
+
+  private final Locale initLocale;
+  private final Consumer<Locale> consumer;
+
+  /**
+   * Creates a {@code RestoreLocale} rule that restores locale. The initial locale at the time of
+   * the test is set to the specified locale. Specify {@code consumer} if you need to modify the
+   * Locale associated with it. {@code consumer} will be run using the original {@code Locale} after
+   * the test.
+   * 
+   * @param initLocale Initial locale at test run
+   * @param consumer Changing Locale associated with
+   */
+  public RestoreLocaleRule(Locale initLocale, Consumer<Locale> consumer) {
+    this.initLocale = initLocale;
+    this.consumer = consumer;
+  }
+
+  /**
+   * Creates a {@code RestoreLocale} rule that restores locale. The initial locale at the time of
+   * the test is set to the specified locale.
+   * 
+   * @param initLocale Initial locale at test run
+   */
+  public RestoreLocaleRule(Locale initLocale) {
+    this(initLocale, null);
+  }
+
+  /**
+   * Creates a {@code RestoreLocale} rule that restores locale. Specify {@code consumer} if you need
+   * to modify the Locale associated with it. {@code consumer} will be run using the original
+   * {@code Locale} after the test.
+   * 
+   * @param consumer Changing Locale associated with
+   */
+  public RestoreLocaleRule(Consumer<Locale> consumer) {
+    this(Locale.getDefault(), consumer);
+  }
+
+  /**
+   * Creates a {@code RestoreLocale} rule that restores locale.
+   */
+  public RestoreLocaleRule() {
+    this(Locale.getDefault(), null);
+  }
+
+  @Override
+  protected void before() throws Throwable {
+    originalLocale = Locale.getDefault();
+
+    Locale.setDefault(initLocale);
+    if (consumer != null)
+      consumer.accept(initLocale);
+  }
+
+  @Override
+  protected void after() {
+    Locale.setDefault(originalLocale);
+    if (consumer != null)
+      consumer.accept(originalLocale);
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/d77d69f6/geode-junit/src/test/java/org/apache/geode/test/junit/rules/RestoreLocaleRuleTest.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/test/java/org/apache/geode/test/junit/rules/RestoreLocaleRuleTest.java b/geode-junit/src/test/java/org/apache/geode/test/junit/rules/RestoreLocaleRuleTest.java
new file mode 100644
index 0000000..b45f3a2
--- /dev/null
+++ b/geode-junit/src/test/java/org/apache/geode/test/junit/rules/RestoreLocaleRuleTest.java
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.geode.test.junit.rules;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.Matchers.contains;
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Random;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.Result;
+
+import org.apache.geode.test.junit.categories.UnitTest;
+
+/**
+ * Unit tests for {@link RestoreLocaleRule}.
+ */
+@Category(UnitTest.class)
+public class RestoreLocaleRuleTest {
+
+  private static Locale notDefaultLocale;
+
+  static {
+    Locale[] locales = Locale.getAvailableLocales();
+    while (notDefaultLocale == null) {
+      Locale l = locales[new Random().nextInt(locales.length)];
+      if (l != Locale.getDefault())
+        notDefaultLocale = l;
+    }
+  }
+
+  @Test
+  public void shouldRestoreLocaleInAfter() throws Throwable {
+    Locale originalLocale = Locale.getDefault();
+    Result result = TestRunner.runTest(ShouldRestoreLocaleInAfter.class);
+
+    assertTrue(result.wasSuccessful());
+    assertThat(Locale.getDefault(), is(originalLocale));
+    assertThat(ShouldRestoreLocaleInAfter.localeInTest, is(notDefaultLocale));
+  }
+
+  @Test
+  public void setInitLocaleShouldRestoreLocaleInAfter() throws Throwable {
+    Locale originalLocale = Locale.getDefault();
+    Result result = TestRunner.runTest(SetInitLocaleShouldRestoreLocaleInAfter.class);
+
+    assertTrue(result.wasSuccessful());
+    assertThat(Locale.getDefault(), is(originalLocale));
+    assertThat(SetInitLocaleShouldRestoreLocaleInAfter.localeInTest, is(Locale.ENGLISH));
+  }
+
+  @Test
+  public void shouldRestoreLocaleInAfterWithConsumer() throws Throwable {
+    Locale originalLocale = Locale.getDefault();
+    Result result = TestRunner.runTest(ShouldRestoreLocaleInAfterWithConsumer.class);
+
+    assertTrue(result.wasSuccessful());
+    assertThat(Locale.getDefault(), is(originalLocale));
+    assertThat(ShouldRestoreLocaleInAfterWithConsumer.localeInTest, is(notDefaultLocale));
+    assertThat(ShouldRestoreLocaleInAfterWithConsumer.locales.size(), is(2));
+    assertThat(ShouldRestoreLocaleInAfterWithConsumer.locales,
+        contains(originalLocale, originalLocale));
+  }
+
+  @Test
+  public void setInitLocaleShouldRestoreLocaleInAfterWithConsumer() throws Throwable {
+    Locale originalLocale = Locale.getDefault();
+    Result result = TestRunner.runTest(SetInitLocaleShouldRestoreLocaleInAfterWithConsumer.class);
+
+    assertTrue(result.wasSuccessful());
+    assertThat(Locale.getDefault(), is(originalLocale));
+    assertThat(SetInitLocaleShouldRestoreLocaleInAfterWithConsumer.localeInTest,
+        is(Locale.CHINESE));
+    assertThat(SetInitLocaleShouldRestoreLocaleInAfterWithConsumer.locales.size(), is(2));
+    assertThat(SetInitLocaleShouldRestoreLocaleInAfterWithConsumer.locales,
+        contains(Locale.CHINESE, originalLocale));
+  }
+
+  /**
+   * Used by test {@link #shouldRestoreLocaleInAfter()}
+   */
+  public static class ShouldRestoreLocaleInAfter {
+
+    static Locale localeInTest = notDefaultLocale;
+
+    @Rule
+    public final RestoreLocaleRule restoreLocale = new RestoreLocaleRule();
+
+    @Test
+    public void doTest() throws Exception {
+      Locale.setDefault(localeInTest);
+      assertThat(Locale.getDefault(), is(localeInTest));
+    }
+  }
+
+  /**
+   * Used by test {@link #setInitLocaleShouldRestoreLocaleInAfter()}
+   */
+  public static class SetInitLocaleShouldRestoreLocaleInAfter {
+
+    static Locale localeInTest;
+
+    @Rule
+    public final RestoreLocaleRule restoreLocale = new RestoreLocaleRule(Locale.ENGLISH);
+
+    @Test
+    public void doTest() throws Exception {
+      localeInTest = Locale.getDefault();
+      assertThat(Locale.getDefault(), is(Locale.ENGLISH));
+    }
+  }
+
+  /**
+   * Used by test {@link #shouldRestoreLocaleInAfterWithConsumer()}
+   */
+  public static class ShouldRestoreLocaleInAfterWithConsumer {
+
+    static List<Locale> locales = new ArrayList<>();
+    static Locale localeInTest = notDefaultLocale;
+
+    @Rule
+    public final RestoreLocaleRule restoreLocale = new RestoreLocaleRule(l -> locales.add(l));
+
+    @Test
+    public void doTest() throws Exception {
+      Locale originalLocale = Locale.getDefault();
+      Locale.setDefault(localeInTest);
+      assertThat(Locale.getDefault(), is(localeInTest));
+      assertThat(locales.size(), is(1));
+      assertThat(locales, contains(originalLocale));
+    }
+  }
+
+  /**
+   * Used by test {@link #setInitLocaleShouldRestoreLocaleInAfterWithConsumer()}
+   */
+  public static class SetInitLocaleShouldRestoreLocaleInAfterWithConsumer {
+
+    static List<Locale> locales = new ArrayList<>();
+    static Locale localeInTest;
+
+    @Rule
+    public final RestoreLocaleRule restoreLocale =
+        new RestoreLocaleRule(Locale.CHINESE, l -> locales.add(l));
+
+    @Test
+    public void doTest() throws Exception {
+      localeInTest = Locale.getDefault();
+      assertThat(Locale.getDefault(), is(Locale.CHINESE));
+      assertThat(locales.size(), is(1));
+      assertThat(locales, contains(Locale.CHINESE));
+    }
+  }
+}