|
Added
Link Here
|
| 1 |
package org.eclipse.e4.core.internal.tests.translation; |
| 2 |
|
| 3 |
import java.util.Locale; |
| 4 |
import java.util.MissingResourceException; |
| 5 |
|
| 6 |
import org.eclipse.e4.core.internal.tests.CoreTestsActivator; |
| 7 |
import org.eclipse.e4.core.services.translation.ITranslationService; |
| 8 |
import org.osgi.framework.BundleContext; |
| 9 |
import org.osgi.framework.ServiceReference; |
| 10 |
|
| 11 |
import junit.framework.TestCase; |
| 12 |
|
| 13 |
public class TestTranslationService extends TestCase { |
| 14 |
private ITranslationService service; |
| 15 |
|
| 16 |
@Override |
| 17 |
protected void setUp() throws Exception { |
| 18 |
super.setUp(); |
| 19 |
Locale.setDefault(Locale.ENGLISH); |
| 20 |
} |
| 21 |
|
| 22 |
public ITranslationService getService() { |
| 23 |
if (service == null) { |
| 24 |
BundleContext ctx = CoreTestsActivator.getDefault() |
| 25 |
.getBundleContext(); |
| 26 |
ServiceReference<ITranslationService> ref = ctx |
| 27 |
.getServiceReference(ITranslationService.class); |
| 28 |
service = ctx.getService(ref); |
| 29 |
} |
| 30 |
|
| 31 |
return service; |
| 32 |
} |
| 33 |
|
| 34 |
public void testBundleTranslations() { |
| 35 |
String category = "org.eclipse.e4.core.tests"; |
| 36 |
ITranslationService s = getService(); |
| 37 |
|
| 38 |
assertEquals("Value En", s.translate(category, "key1")); |
| 39 |
assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1")); |
| 40 |
|
| 41 |
Locale.setDefault(Locale.GERMAN); |
| 42 |
|
| 43 |
assertEquals("Wert De", s.translate(category, "key1")); |
| 44 |
assertEquals("Wert De", s.translate(Locale.GERMAN, category, "key1")); |
| 45 |
|
| 46 |
assertEquals("Wert De", |
| 47 |
s.translate(Locale.FRENCH, "org.eclipse.e4.core.tests", "key1")); |
| 48 |
|
| 49 |
assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1")); |
| 50 |
|
| 51 |
try { |
| 52 |
s.translate(category, "unknown"); |
| 53 |
fail("Unknown key has to fail with MissingResourceException"); |
| 54 |
} catch (MissingResourceException e) { |
| 55 |
} |
| 56 |
|
| 57 |
try { |
| 58 |
s.translate(Locale.ENGLISH, category, "unknown"); |
| 59 |
fail("Unknown key has to fail with MissingResourceException"); |
| 60 |
} catch (MissingResourceException e) { |
| 61 |
} |
| 62 |
|
| 63 |
assertEquals(2, s.translate(category, "key1", "key2").length); |
| 64 |
assertEquals("Wert De", s.translate(category, "key1", "key2")[0]); |
| 65 |
assertEquals("Wert De 2", s.translate(category, "key1", "key2")[1]); |
| 66 |
|
| 67 |
assertEquals(2, |
| 68 |
s.translate(Locale.FRENCH, category, "key1", "key2").length); |
| 69 |
assertEquals("Wert De", |
| 70 |
s.translate(Locale.FRENCH, category, "key1", "key2")[0]); |
| 71 |
assertEquals("Wert De 2", |
| 72 |
s.translate(Locale.FRENCH, category, "key1", "key2")[1]); |
| 73 |
|
| 74 |
assertEquals(2, |
| 75 |
s.translate(Locale.ENGLISH, category, "key1", "key2").length); |
| 76 |
assertEquals("Value En", |
| 77 |
s.translate(Locale.ENGLISH, category, "key1", "key2")[0]); |
| 78 |
assertEquals("Value En 2", |
| 79 |
s.translate(Locale.ENGLISH, category, "key1", "key2")[1]); |
| 80 |
} |
| 81 |
|
| 82 |
public void testTranslationProvider() { |
| 83 |
String category = "org.eclipse.e4.core.tests.customcat"; |
| 84 |
ITranslationService s = getService(); |
| 85 |
|
| 86 |
assertEquals("Cust Value En", s.translate(category, "key1")); |
| 87 |
assertEquals("Cust Value En", |
| 88 |
s.translate(Locale.ENGLISH, category, "key1")); |
| 89 |
|
| 90 |
Locale.setDefault(Locale.GERMAN); |
| 91 |
|
| 92 |
assertEquals("Cust Wert De", s.translate(category, "key1")); |
| 93 |
assertEquals("Cust Wert De", |
| 94 |
s.translate(Locale.GERMAN, category, "key1")); |
| 95 |
|
| 96 |
assertEquals("Cust Wert De", |
| 97 |
s.translate(Locale.FRENCH, category, "key1")); |
| 98 |
|
| 99 |
assertEquals("Cust Value En", |
| 100 |
s.translate(Locale.ENGLISH, category, "key1")); |
| 101 |
|
| 102 |
try { |
| 103 |
s.translate(category, "unknown"); |
| 104 |
fail("Unknown key has to fail with MissingResourceException"); |
| 105 |
} catch (MissingResourceException e) { |
| 106 |
} |
| 107 |
|
| 108 |
try { |
| 109 |
s.translate(Locale.ENGLISH, category, "unknown"); |
| 110 |
fail("Unknown key has to fail with MissingResourceException"); |
| 111 |
} catch (MissingResourceException e) { |
| 112 |
} |
| 113 |
|
| 114 |
assertEquals(2, s.translate(category, "key1", "key2").length); |
| 115 |
assertEquals("Cust Wert De", s.translate(category, "key1", "key2")[0]); |
| 116 |
assertEquals("Cust Wert De 2", s.translate(category, "key1", "key2")[1]); |
| 117 |
|
| 118 |
assertEquals(2, |
| 119 |
s.translate(Locale.FRENCH, category, "key1", "key2").length); |
| 120 |
assertEquals("Cust Wert De", |
| 121 |
s.translate(Locale.FRENCH, category, "key1", "key2")[0]); |
| 122 |
assertEquals("Cust Wert De 2", |
| 123 |
s.translate(Locale.FRENCH, category, "key1", "key2")[1]); |
| 124 |
|
| 125 |
assertEquals(2, |
| 126 |
s.translate(Locale.ENGLISH, category, "key1", "key2").length); |
| 127 |
assertEquals("Cust Value En", |
| 128 |
s.translate(Locale.ENGLISH, category, "key1", "key2")[0]); |
| 129 |
assertEquals("Cust Value En 2", |
| 130 |
s.translate(Locale.ENGLISH, category, "key1", "key2")[1]); |
| 131 |
} |
| 132 |
} |