You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NLSTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.nls;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertSame;
  13. import java.util.Locale;
  14. import java.util.concurrent.Callable;
  15. import java.util.concurrent.CyclicBarrier;
  16. import java.util.concurrent.ExecutionException;
  17. import java.util.concurrent.ExecutorService;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.Future;
  20. import java.util.concurrent.TimeUnit;
  21. import org.junit.Test;
  22. public class NLSTest {
  23. @Test
  24. public void testNLSLocale() {
  25. NLS.setLocale(NLS.ROOT_LOCALE);
  26. GermanTranslatedBundle bundle = GermanTranslatedBundle.get();
  27. assertEquals(NLS.ROOT_LOCALE, bundle.effectiveLocale());
  28. NLS.setLocale(Locale.GERMAN);
  29. bundle = GermanTranslatedBundle.get();
  30. assertEquals(Locale.GERMAN, bundle.effectiveLocale());
  31. }
  32. @Test
  33. public void testJVMDefaultLocale() {
  34. Locale.setDefault(NLS.ROOT_LOCALE);
  35. NLS.useJVMDefaultLocale();
  36. GermanTranslatedBundle bundle = GermanTranslatedBundle.get();
  37. assertEquals(NLS.ROOT_LOCALE, bundle.effectiveLocale());
  38. Locale.setDefault(Locale.GERMAN);
  39. NLS.useJVMDefaultLocale();
  40. bundle = GermanTranslatedBundle.get();
  41. assertEquals(Locale.GERMAN, bundle.effectiveLocale());
  42. }
  43. @Test
  44. public void testThreadTranslationBundleInheritance() throws InterruptedException {
  45. class T extends Thread {
  46. GermanTranslatedBundle bundle;
  47. @Override
  48. public void run() {
  49. bundle = GermanTranslatedBundle.get();
  50. }
  51. }
  52. NLS.setLocale(NLS.ROOT_LOCALE);
  53. GermanTranslatedBundle mainThreadsBundle = GermanTranslatedBundle.get();
  54. T t = new T();
  55. t.start();
  56. t.join();
  57. assertSame(mainThreadsBundle, t.bundle);
  58. NLS.setLocale(Locale.GERMAN);
  59. mainThreadsBundle = GermanTranslatedBundle.get();
  60. t = new T();
  61. t.start();
  62. t.join();
  63. assertSame(mainThreadsBundle, t.bundle);
  64. }
  65. @Test
  66. public void testParallelThreadsWithDifferentLocales()
  67. throws InterruptedException, ExecutionException {
  68. final CyclicBarrier barrier = new CyclicBarrier(2);
  69. class GetBundle implements Callable<TranslationBundle> {
  70. private Locale locale;
  71. GetBundle(Locale locale) {
  72. this.locale = locale;
  73. }
  74. @Override
  75. public TranslationBundle call() throws Exception {
  76. NLS.setLocale(locale);
  77. barrier.await(); // wait for the other thread to set its locale
  78. return GermanTranslatedBundle.get();
  79. }
  80. }
  81. ExecutorService pool = Executors.newFixedThreadPool(2);
  82. try {
  83. Future<TranslationBundle> root = pool.submit(new GetBundle(
  84. NLS.ROOT_LOCALE));
  85. Future<TranslationBundle> german = pool.submit(new GetBundle(
  86. Locale.GERMAN));
  87. assertEquals(NLS.ROOT_LOCALE, root.get().effectiveLocale());
  88. assertEquals(Locale.GERMAN, german.get().effectiveLocale());
  89. } finally {
  90. pool.shutdown();
  91. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  92. }
  93. }
  94. }