blob: 289ec52362e942372f2d42e06794b2cee609bd06 [file] [log] [blame]
Simon Hunt0c85f112017-06-12 21:02:17 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.ui.lion;
19
20import org.junit.AfterClass;
21import org.junit.Before;
22import org.junit.BeforeClass;
Yi Tsengee2c8542017-06-13 15:42:17 -070023import org.junit.Ignore;
Simon Hunt7379a3d2017-06-20 16:50:39 -070024import org.junit.Test;
Simon Hunt0c85f112017-06-12 21:02:17 -070025import org.onosproject.ui.AbstractUiTest;
26
27import java.util.Locale;
28import java.util.ResourceBundle;
29
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertNotNull;
32
33/**
34 * Unit tests for {@link LionUtils}.
35 */
36public class LionUtilsTest extends AbstractUiTest {
37
38 private static Locale systemLocale;
39
40 private ResourceBundle res;
Simon Huntb8042032017-06-13 15:03:23 -070041 private Locale locale;
Simon Hunt0c85f112017-06-12 21:02:17 -070042
43 @BeforeClass
44 public static void classSetup() {
45 systemLocale = Locale.getDefault();
46 }
47
48 @AfterClass
49 public static void classTeardown() {
50 Locale.setDefault(systemLocale);
51 }
52
53 @Before
54 public void testSetup() {
55 // reset to a known default locale before starting each test
56 Locale.setDefault(Locale.US);
57 }
58
59 @Test
60 public void getBundleByClassAndName() {
61 title("getBundleByClassAndName");
62 res = LionUtils.getBundledResource(LionUtilsTest.class, "SomeResource");
63 assertNotNull("missing resource bundle", res);
64 String v1 = res.getString("key1");
65 String v2 = res.getString("key2");
66 print("v1 is %s, v2 is %s", v1, v2);
67 assertEquals("v1 value wrong", "value one", v1);
68 assertEquals("v2 value wrong", "value two", v2);
69
70 res = LionUtils.getBundledResource(LionUtils.class, "SomeOtherResource");
71 assertNotNull("missing OTHER resource bundle", res);
72 v1 = res.getString("key1");
73 v2 = res.getString("key2");
74 print("v1 is %s, v2 is %s", v1, v2);
75 assertEquals("v1 value wrong", "Hay", v1);
76 assertEquals("v2 value wrong", "Bee", v2);
77 }
78
79 @Test
80 public void getBundleByClassname() {
81 title("getBundleByClassname");
82 res = LionUtils.getBundledResource(LionUtils.class);
83 assertNotNull("missing resource bundle", res);
84 String v1 = res.getString("foo");
85 String v2 = res.getString("boo");
86 print("v1 is %s, v2 is %s", v1, v2);
87 assertEquals("v1 value wrong", "bar", v1);
88 assertEquals("v2 value wrong", "ghost", v2);
89 }
90
91 @Test
92 public void getBundleByFqcn() {
93 title("getBundleByFqcn");
94 String fqcn = "org.onosproject.ui.lion.LionUtils";
95 res = LionUtils.getBundledResource(fqcn);
96 assertNotNull("missing resource bundle", res);
97 String v1 = res.getString("foo");
98 String v2 = res.getString("boo");
99 print("v1 is %s, v2 is %s", v1, v2);
100 assertEquals("v1 value wrong", "bar", v1);
101 assertEquals("v2 value wrong", "ghost", v2);
102 }
103
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700104
Simon Hunta5b14542017-06-15 09:33:40 -0700105 // --- RUNTIME Locale ---
Jian Li67d47e82017-06-15 00:21:55 +0900106 @Test
Simon Huntb8042032017-06-13 15:03:23 -0700107 public void runtimeLocale() {
108 title("runtimeLocale");
109 Locale runtime = LionUtils.setupRuntimeLocale();
110 print("locale is [%s]", runtime);
111
112 // NOTE:
113 // Yeah, I know, "a unit test without asserts is not a unit test".
114 //
115 // But it would NOT be a good idea to assert the locale results in
Simon Hunta5b14542017-06-15 09:33:40 -0700116 // this method, because that is dependent on an environment variable.
Simon Huntb8042032017-06-13 15:03:23 -0700117 //
118 // This method is here to allow manual verification of the Locale
119 // e.g. when running tests from IntelliJ, and setting the
Simon Hunta5b14542017-06-15 09:33:40 -0700120 // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog.
Simon Huntb8042032017-06-13 15:03:23 -0700121 }
122
Simon Hunta5b14542017-06-15 09:33:40 -0700123
124 // --- LOCALE from String ---
Simon Huntb8042032017-06-13 15:03:23 -0700125 @Test(expected = NullPointerException.class)
126 public void localeFromStringNull() {
127 LionUtils.localeFromString(null);
128 }
129
130 private void checkLanguageCountry(Locale locale, String expL, String expC) {
131 assertEquals("Wrong language: " + expL, expL, locale.getLanguage());
132 assertEquals("Wrong country: " + expC, expC, locale.getCountry());
133 }
134
135 @Test
136 public void localeFromStringEmpty() {
137 title("localeFromStringEmpty");
138 locale = LionUtils.localeFromString("");
139 checkLanguageCountry(locale, "", "");
140 }
141
142 @Test
143 public void localeFromStringRu() {
144 title("localeFromStringRu");
145 locale = LionUtils.localeFromString("ru");
146 checkLanguageCountry(locale, "ru", "");
147 }
148
149 @Test
150 public void localeFromStringEnGB() {
151 title("localeFromStringEnGB");
152 locale = LionUtils.localeFromString("en_GB");
153 checkLanguageCountry(locale, "en", "GB");
154 }
155
156 @Test
157 public void localeFromStringItIT() {
158 title("localeFromStringItIT");
159 locale = LionUtils.localeFromString("it_IT");
160 checkLanguageCountry(locale, "it", "IT");
161 }
162
163 @Test
164 public void localeFromStringFrCA() {
165 title("localeFromStringFrCA");
166 locale = LionUtils.localeFromString("fr_CA");
167 checkLanguageCountry(locale, "fr", "CA");
168 }
169
170 @Test
Jian Li67d47e82017-06-15 00:21:55 +0900171 public void localeFromStringKoKR() {
172 title("localeFromStringKoKR");
173 locale = LionUtils.localeFromString("ko_KR");
174 checkLanguageCountry(locale, "ko", "KR");
175 }
176
Simon Hunta5b14542017-06-15 09:33:40 -0700177
178 // -- Testing loading of correct bundle, based on locale
Simon Hunt7379a3d2017-06-20 16:50:39 -0700179
Simon Hunta5b14542017-06-15 09:33:40 -0700180 private void checkLookups(String computer, String disk, String monitor,
181 String keyboard) {
182 res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
183 print("res locale is %s", res.getLocale().getLanguage());
184 print("a keyboard in this language is '%s'", res.getString("keyboard"));
185
186 assertEquals("wrong computer", computer, res.getString("computer"));
187 assertEquals("wrong disk", disk, res.getString("disk"));
188 assertEquals("wrong monitor", monitor, res.getString("monitor"));
189 assertEquals("wrong keyboard", keyboard, res.getString("keyboard"));
190 }
191
Jian Li67d47e82017-06-15 00:21:55 +0900192 @Test
Simon Hunta5b14542017-06-15 09:33:40 -0700193 public void messagesInEnglish() {
194 title("messagesInEnglish");
195 // use default locale
196 checkLookups("computer", "disk", "monitor", "keyboard");
197 }
198
199 @Test
200 public void messagesInGerman() {
201 title("messagesInGerman");
202 Locale.setDefault(Locale.GERMAN);
203 checkLookups("Computer", "Platte", "Monitor", "Tastatur");
204 }
205
206 @Test
207 public void messagesInItalian() {
208 title("messagesInItalian");
209 Locale.setDefault(Locale.ITALIAN);
210 checkLookups("Calcolatore", "Disco", "Schermo", "Tastiera");
211 }
212
213 // TODO: figure out why extended character sets are not handled properly
214 /* For example, the Korean test fails as follows
215
216 === messagesInKorean ===
217 res locale is ko
218 a keyboard in this language is '키보드'
219
220 org.junit.ComparisonFailure: wrong computer
221 Expected :컴퓨터
222 Actual :컴퓨터
223
224 */
225
226 @Test
227 @Ignore("Not chinese friendly, yet...")
Yi Tsengee2c8542017-06-13 15:42:17 -0700228 public void messagesInZhTw() {
229 title("messagesInZhTW");
230 Locale.setDefault(Locale.TRADITIONAL_CHINESE);
231 checkLookups("電腦", "磁碟", "螢幕", "鍵盤");
232 }
Simon Hunta5b14542017-06-15 09:33:40 -0700233
234 @Test
235 @Ignore("Not korean friendly, yet...")
236 public void messagesInKorean() {
237 title("messagesInKorean");
238 Locale.setDefault(Locale.KOREA);
239 checkLookups("컴퓨터", "디스크", "모니터", "키보드");
240 }
241
Frank Wang9986c072017-06-15 09:48:39 +0800242 @Test
243 @Ignore("Not chinese friendly, yet...")
244 public void messagesInZhCN() {
245 title("messagesInZhCN");
246 Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
247 checkLookups("电脑", "磁盘", "屏幕", "键盘");
248 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700249}