blob: 24712778035130000775a6b70c5e89af825e9f3a [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;
23import org.junit.Test;
Yi Tsengee2c8542017-06-13 15:42:17 -070024import org.junit.Ignore;
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
179 private void checkLookups(String computer, String disk, String monitor,
180 String keyboard) {
181 res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
182 print("res locale is %s", res.getLocale().getLanguage());
183 print("a keyboard in this language is '%s'", res.getString("keyboard"));
184
185 assertEquals("wrong computer", computer, res.getString("computer"));
186 assertEquals("wrong disk", disk, res.getString("disk"));
187 assertEquals("wrong monitor", monitor, res.getString("monitor"));
188 assertEquals("wrong keyboard", keyboard, res.getString("keyboard"));
189 }
190
Jian Li67d47e82017-06-15 00:21:55 +0900191 @Test
Simon Hunta5b14542017-06-15 09:33:40 -0700192 public void messagesInEnglish() {
193 title("messagesInEnglish");
194 // use default locale
195 checkLookups("computer", "disk", "monitor", "keyboard");
196 }
197
198 @Test
199 public void messagesInGerman() {
200 title("messagesInGerman");
201 Locale.setDefault(Locale.GERMAN);
202 checkLookups("Computer", "Platte", "Monitor", "Tastatur");
203 }
204
205 @Test
206 public void messagesInItalian() {
207 title("messagesInItalian");
208 Locale.setDefault(Locale.ITALIAN);
209 checkLookups("Calcolatore", "Disco", "Schermo", "Tastiera");
210 }
211
212 // TODO: figure out why extended character sets are not handled properly
213 /* For example, the Korean test fails as follows
214
215 === messagesInKorean ===
216 res locale is ko
217 a keyboard in this language is '키보드'
218
219 org.junit.ComparisonFailure: wrong computer
220 Expected :컴퓨터
221 Actual :컴퓨터
222
223 */
224
225 @Test
226 @Ignore("Not chinese friendly, yet...")
Yi Tsengee2c8542017-06-13 15:42:17 -0700227 public void messagesInZhTw() {
228 title("messagesInZhTW");
229 Locale.setDefault(Locale.TRADITIONAL_CHINESE);
230 checkLookups("電腦", "磁碟", "螢幕", "鍵盤");
231 }
Simon Hunta5b14542017-06-15 09:33:40 -0700232
233 @Test
234 @Ignore("Not korean friendly, yet...")
235 public void messagesInKorean() {
236 title("messagesInKorean");
237 Locale.setDefault(Locale.KOREA);
238 checkLookups("컴퓨터", "디스크", "모니터", "키보드");
239 }
240
Frank Wang9986c072017-06-15 09:48:39 +0800241 @Test
242 @Ignore("Not chinese friendly, yet...")
243 public void messagesInZhCN() {
244 title("messagesInZhCN");
245 Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
246 checkLookups("电脑", "磁盘", "屏幕", "键盘");
247 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700248}