|
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 |
// Expected to fail because Bug 330602 in Equinox |
| 47 |
// assertEquals("Wert De",s.translate(Locale.FRENCH, |
| 48 |
// "org.eclipse.e4.core.tests", "key1")); |
| 49 |
|
| 50 |
assertEquals("Value En", s.translate(Locale.ENGLISH, category, "key1")); |
| 51 |
|
| 52 |
try { |
| 53 |
s.translate(category, "unknown"); |
| 54 |
fail("Unknown key has to fail with MissingResourceException"); |
| 55 |
} catch (MissingResourceException e) { |
| 56 |
} |
| 57 |
|
| 58 |
try { |
| 59 |
s.translate(Locale.ENGLISH, category, "unknown"); |
| 60 |
fail("Unknown key has to fail with MissingResourceException"); |
| 61 |
} catch (MissingResourceException e) { |
| 62 |
} |
| 63 |
|
| 64 |
assertEquals(2, s.translate(category, "key1", "key2").length); |
| 65 |
assertEquals("Wert De", s.translate(category, "key1", "key2")[0]); |
| 66 |
assertEquals("Wert De 2", s.translate(category, "key1", "key2")[1]); |
| 67 |
|
| 68 |
// Expected to fail because Bug 330602 in Equinox |
| 69 |
// assertEquals(2, s.translate(Locale.FRENCH, category, "key1", "key2").length); |
| 70 |
// assertEquals("Wert De", |
| 71 |
// s.translate(Locale.FRENCH, category, "key1", "key2")[0]); |
| 72 |
// assertEquals("Wert De 2", |
| 73 |
// s.translate(Locale.FRENCH, category, "key1", "key2")[1]); |
| 74 |
|
| 75 |
assertEquals(2, 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 |
// Expected to fail because waiting for bug 330602 and the key defined |
| 100 |
// to inculde the en bundle |
| 101 |
// assertEquals("Cust Value En", s.translate(Locale.ENGLISH, category, |
| 102 |
// "key1")); |
| 103 |
|
| 104 |
try { |
| 105 |
s.translate(category, "unknown"); |
| 106 |
fail("Unknown key has to fail with MissingResourceException"); |
| 107 |
} catch (MissingResourceException e) { |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
s.translate(Locale.ENGLISH, category, "unknown"); |
| 112 |
fail("Unknown key has to fail with MissingResourceException"); |
| 113 |
} catch (MissingResourceException e) { |
| 114 |
} |
| 115 |
|
| 116 |
assertEquals(2, s.translate(category, "key1", "key2").length); |
| 117 |
assertEquals("Cust Wert De", s.translate(category, "key1", "key2")[0]); |
| 118 |
assertEquals("Cust Wert De 2", s.translate(category, "key1", "key2")[1]); |
| 119 |
|
| 120 |
assertEquals(2, |
| 121 |
s.translate(Locale.FRENCH, category, "key1", "key2").length); |
| 122 |
assertEquals("Cust Wert De", |
| 123 |
s.translate(Locale.FRENCH, category, "key1", "key2")[0]); |
| 124 |
assertEquals("Cust Wert De 2", |
| 125 |
s.translate(Locale.FRENCH, category, "key1", "key2")[1]); |
| 126 |
|
| 127 |
// Expected to fail because waiting for bug 330602 and the key defined |
| 128 |
// to inculde the en bundle |
| 129 |
|
| 130 |
// assertEquals(2, s.translate(Locale.ENGLISH, category, "key1", |
| 131 |
// "key2").length); |
| 132 |
// assertEquals("Cust Value En", |
| 133 |
// s.translate(Locale.ENGLISH, category, "key1", "key2")[0]); |
| 134 |
// assertEquals("Cust Value En 2", |
| 135 |
// s.translate(Locale.ENGLISH, category, "key1", "key2")[1]); |
| 136 |
} |
| 137 |
} |