View Javadoc
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  
11  package org.eclipse.jgit.nls;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertSame;
15  
16  import java.util.Locale;
17  import java.util.concurrent.Callable;
18  import java.util.concurrent.CyclicBarrier;
19  import java.util.concurrent.ExecutionException;
20  import java.util.concurrent.ExecutorService;
21  import java.util.concurrent.Executors;
22  import java.util.concurrent.Future;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.junit.Test;
26  
27  public class NLSTest {
28  
29  	@Test
30  	public void testNLSLocale() {
31  		NLS.setLocale(NLS.ROOT_LOCALE);
32  		GermanTranslatedBundle bundle = GermanTranslatedBundle.get();
33  		assertEquals(NLS.ROOT_LOCALE, bundle.effectiveLocale());
34  
35  		NLS.setLocale(Locale.GERMAN);
36  		bundle = GermanTranslatedBundle.get();
37  		assertEquals(Locale.GERMAN, bundle.effectiveLocale());
38  	}
39  
40  	@Test
41  	public void testJVMDefaultLocale() {
42  		Locale.setDefault(NLS.ROOT_LOCALE);
43  		NLS.useJVMDefaultLocale();
44  		GermanTranslatedBundle bundle = GermanTranslatedBundle.get();
45  		assertEquals(NLS.ROOT_LOCALE, bundle.effectiveLocale());
46  
47  		Locale.setDefault(Locale.GERMAN);
48  		NLS.useJVMDefaultLocale();
49  		bundle = GermanTranslatedBundle.get();
50  		assertEquals(Locale.GERMAN, bundle.effectiveLocale());
51  	}
52  
53  	@Test
54  	public void testThreadTranslationBundleInheritance() throws InterruptedException {
55  
56  		class T extends Thread {
57  			GermanTranslatedBundle bundle;
58  			@Override
59  			public void run() {
60  				bundle = GermanTranslatedBundle.get();
61  			}
62  		}
63  
64  		NLS.setLocale(NLS.ROOT_LOCALE);
65  		GermanTranslatedBundle mainThreadsBundle = GermanTranslatedBundle.get();
66  		T t = new T();
67  		t.start();
68  		t.join();
69  		assertSame(mainThreadsBundle, t.bundle);
70  
71  		NLS.setLocale(Locale.GERMAN);
72  		mainThreadsBundle = GermanTranslatedBundle.get();
73  		t = new T();
74  		t.start();
75  		t.join();
76  		assertSame(mainThreadsBundle, t.bundle);
77  	}
78  
79  	@Test
80  	public void testParallelThreadsWithDifferentLocales()
81  			throws InterruptedException, ExecutionException {
82  
83  		final CyclicBarrier barrier = new CyclicBarrier(2);
84  
85  		class GetBundle implements Callable<TranslationBundle> {
86  
87  			private Locale locale;
88  
89  			GetBundle(Locale locale) {
90  				this.locale = locale;
91  			}
92  
93  			@Override
94  			public TranslationBundle call() throws Exception {
95  				NLS.setLocale(locale);
96  				barrier.await(); // wait for the other thread to set its locale
97  				return GermanTranslatedBundle.get();
98  			}
99  		}
100 
101 		ExecutorService pool = Executors.newFixedThreadPool(2);
102 		try {
103 			Future<TranslationBundle> root = pool.submit(new GetBundle(
104 					NLS.ROOT_LOCALE));
105 			Future<TranslationBundle> german = pool.submit(new GetBundle(
106 					Locale.GERMAN));
107 			assertEquals(NLS.ROOT_LOCALE, root.get().effectiveLocale());
108 			assertEquals(Locale.GERMAN, german.get().effectiveLocale());
109 		} finally {
110 			pool.shutdown();
111 			pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
112 		}
113 	}
114 }