4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.i18n
;
27 import static org
.junit
.Assert
.*;
29 import java
.io
.UnsupportedEncodingException
;
30 import java
.util
.Locale
;
32 import net
.sourceforge
.aprog
.tools
.Tools
;
34 import org
.junit
.Test
;
37 * Automated tests using JUnit 4 for {@link Translator}.
39 * @author codistmonk (creation 2010-06-03)
41 public final class TranslatorTest
{
44 public final void testTranslateObject() {
45 final String translationKey1
= "public_key";
46 final String textPropertyName1
= "publicText";
47 final String translationKey2
= "package_key";
48 final String textPropertyName2
= "packageText";
49 final Translator translator
= new Translator();
51 translator
.setLocale(Locale
.ENGLISH
);
53 final Translatable translatable
= translator
.translate(
59 assertEquals("", translatable
.getPackageText());
61 translator
.translate(translatable
, textPropertyName2
, translationKey2
, MESSAGES_BASE
);
63 assertEquals("Public key", translatable
.getPublicText());
64 assertEquals("Package key", translatable
.getPackageText());
66 translator
.setLocale(Locale
.FRENCH
);
68 assertEquals("Clé publique", translatable
.getPublicText());
69 assertEquals("Clé package", translatable
.getPackageText());
71 translator
.setLocale(Locale
.ENGLISH
);
73 assertEquals("Public key", translatable
.getPublicText());
74 assertEquals("Package key", translatable
.getPackageText());
78 public final void testTranslateObjectWithParameterizedMessage() {
79 final String translationKey
= "life_universe_everything";
80 final String textPropertyName
= "parameterizedText";
81 final Translator translator
= new Translator();
83 translator
.setLocale(Locale
.ENGLISH
);
85 final Translatable translatable
= translator
.translate(
92 assertEquals("Answer: 42", translatable
.getParameterizedText());
94 translator
.setLocale(Locale
.FRENCH
);
96 assertEquals("Réponse : 42", translatable
.getParameterizedText());
98 translator
.setLocale(Locale
.ENGLISH
);
100 assertEquals("Answer: 42", translatable
.getParameterizedText());
104 public final void testUntranslateObject() {
105 final String translationKey1
= "public_key";
106 final String textPropertyName1
= "publicText";
107 final String translationKey2
= "package_key";
108 final String textPropertyName2
= "packageText";
109 final Translator translator
= new Translator();
111 translator
.setLocale(Locale
.ENGLISH
);
113 final Translatable translatable
= translator
.translate(
119 assertEquals("", translatable
.getPackageText());
121 translator
.translate(translatable
, textPropertyName2
, translationKey2
, MESSAGES_BASE
);
123 assertEquals("Public key", translatable
.getPublicText());
124 assertEquals("Package key", translatable
.getPackageText());
126 translator
.setLocale(Locale
.FRENCH
);
128 assertEquals("Clé publique", translatable
.getPublicText());
129 assertEquals("Clé package", translatable
.getPackageText());
131 translator
.untranslate(translatable
, textPropertyName1
);
132 translator
.setLocale(Locale
.ENGLISH
);
134 assertEquals(translationKey1
, translatable
.getPublicText());
135 assertEquals("Package key", translatable
.getPackageText());
140 public final void testTranslateMessage() {
141 final String translationKey
= "life_universe_everything";
142 final Translator translator
= new Translator();
145 translator
.setLocale(Locale
.ENGLISH
);
147 final String result
= translator
.translate(translationKey
, MESSAGES_BASE
, 42);
149 assertNotNull(result
);
150 assertEquals("Answer: 42", result
);
153 translator
.setLocale(Locale
.FRENCH
);
155 final String result
= translator
.translate(translationKey
, MESSAGES_BASE
, 42);
157 assertNotNull(result
);
158 assertEquals("Réponse : 42", result
);
163 public final void testGetBestAvailableLocale() {
164 final Translator translator
= new Translator();
166 translator
.collectAvailableLocales(MESSAGES_BASE
);
168 translator
.setLocale(new Locale("fr", "FR"));
170 assertEquals(new Locale("fr"), translator
.getBestAvailableLocale());
172 translator
.setLocale(new Locale("en", "US"));
174 assertEquals(new Locale("en"), translator
.getBestAvailableLocale());
178 public final void testCreateLocale() {
180 final Locale result
= Translator
.newLocale("fr");
182 assertNotNull(result
);
183 assertEquals(Locale
.FRENCH
, result
);
186 final Locale result
= Translator
.newLocale("fr_CA");
188 assertNotNull(result
);
189 assertEquals(Locale
.CANADA_FRENCH
, result
);
192 final Locale result
= Translator
.newLocale("fr_FR_parisien");
194 assertNotNull(result
);
195 assertEquals(new Locale("fr", "FR", "parisien"), result
);
200 public final void testGetLanguageCountryVariant() {
201 final String language
= "fr";
202 final String country
= "FR";
203 final String variant
= "parisien";
206 final String result
= Translator
.getLanguageCountryVariant(new Locale(language
));
208 assertNotNull(result
);
209 assertEquals(language
, result
);
213 final String result
= Translator
.getLanguageCountryVariant(new Locale(language
, country
));
215 assertNotNull(result
);
216 assertEquals(language
+ "_" + country
, result
);
220 final String result
= Translator
.getLanguageCountryVariant(new Locale(language
, country
, variant
));
222 assertNotNull(result
);
223 assertEquals(language
+ "_" + country
+ "_" + variant
, result
);
229 public final void testIso88591ToUTF8() throws UnsupportedEncodingException
{
231 final String iso88591
= new String(new byte[] { (byte) 0xCE, (byte) 0xA9 }, "ISO-8859-1");
232 final String convertedString
= Translator
.iso88591ToUTF8(iso88591
);
233 final String expectedResult
= "Ω";
235 assertNotNull(convertedString
);
236 assertNotSame(convertedString
, iso88591
);
237 assertEquals("Ω", iso88591
);
238 assertFalse(expectedResult
.equals(iso88591
));
239 assertEquals(expectedResult
, convertedString
);
242 final String iso88591
= new String(new byte[] { (byte) 0xC3, (byte) 0xA9, (byte) 0xE2, (byte) 0x80, (byte) 0x99 }, "ISO-8859-1");
243 final String convertedString
= Translator
.iso88591ToUTF8(iso88591
);
244 final String expectedResult
= "é’";
246 assertNotNull(convertedString
);
247 assertNotSame(convertedString
, iso88591
);
248 assertFalse(expectedResult
.equals(iso88591
));
249 assertEquals(expectedResult
, convertedString
);
253 public static final String MESSAGES_BASE
= Tools
.getCallerPackagePath() + "Messages";
257 * @author codistmonk (creation 2010-06-03)
259 private static final class Translatable
{
261 private String publicText
;
263 private String parameterizedText
;
265 private String packageText
;
268 this.publicText
= "";
269 this.packageText
= "";
270 this.parameterizedText
= "";
276 * <br>A non-null value
279 public final String
getPublicText() {
280 return this.publicText
;
286 * <br>Should not be null
287 * <br>Shared parameter
289 @SuppressWarnings("unused")
290 public final void setPublicText(final String publicText
) {
291 this.publicText
= publicText
;
297 * <br>A non-null value
300 public final String
getParameterizedText() {
301 return this.parameterizedText
;
306 * @param parameterizedText
307 * <br>Should not be null
308 * <br>Shared parameter
310 @SuppressWarnings("unused")
311 public final void setParameterizedText(final String parameterizedText
) {
312 this.parameterizedText
= parameterizedText
;
318 * <br>A non-null value
321 final String
getPackageText() {
322 return this.packageText
;
328 * <br>Should not be null
329 * <br>Shared parameter
331 @SuppressWarnings("unused")
332 final void setPackageText(final String packageText
) {
333 this.packageText
= packageText
;