blob: 957fe73bd89dedca40b719b5a58ab413fe23af22 [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
Simon Hunt7379a3d2017-06-20 16:50:39 -070027import java.util.List;
Simon Hunt0c85f112017-06-12 21:02:17 -070028import java.util.Locale;
29import java.util.ResourceBundle;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertNotNull;
33
34/**
35 * Unit tests for {@link LionUtils}.
36 */
37public class LionUtilsTest extends AbstractUiTest {
38
39 private static Locale systemLocale;
40
41 private ResourceBundle res;
Simon Huntb8042032017-06-13 15:03:23 -070042 private Locale locale;
Simon Hunt0c85f112017-06-12 21:02:17 -070043
44 @BeforeClass
45 public static void classSetup() {
46 systemLocale = Locale.getDefault();
47 }
48
49 @AfterClass
50 public static void classTeardown() {
51 Locale.setDefault(systemLocale);
52 }
53
54 @Before
55 public void testSetup() {
56 // reset to a known default locale before starting each test
57 Locale.setDefault(Locale.US);
58 }
59
60 @Test
61 public void getBundleByClassAndName() {
62 title("getBundleByClassAndName");
63 res = LionUtils.getBundledResource(LionUtilsTest.class, "SomeResource");
64 assertNotNull("missing resource bundle", res);
65 String v1 = res.getString("key1");
66 String v2 = res.getString("key2");
67 print("v1 is %s, v2 is %s", v1, v2);
68 assertEquals("v1 value wrong", "value one", v1);
69 assertEquals("v2 value wrong", "value two", v2);
70
71 res = LionUtils.getBundledResource(LionUtils.class, "SomeOtherResource");
72 assertNotNull("missing OTHER resource bundle", res);
73 v1 = res.getString("key1");
74 v2 = res.getString("key2");
75 print("v1 is %s, v2 is %s", v1, v2);
76 assertEquals("v1 value wrong", "Hay", v1);
77 assertEquals("v2 value wrong", "Bee", v2);
78 }
79
80 @Test
81 public void getBundleByClassname() {
82 title("getBundleByClassname");
83 res = LionUtils.getBundledResource(LionUtils.class);
84 assertNotNull("missing resource bundle", res);
85 String v1 = res.getString("foo");
86 String v2 = res.getString("boo");
87 print("v1 is %s, v2 is %s", v1, v2);
88 assertEquals("v1 value wrong", "bar", v1);
89 assertEquals("v2 value wrong", "ghost", v2);
90 }
91
92 @Test
93 public void getBundleByFqcn() {
94 title("getBundleByFqcn");
95 String fqcn = "org.onosproject.ui.lion.LionUtils";
96 res = LionUtils.getBundledResource(fqcn);
97 assertNotNull("missing resource bundle", res);
98 String v1 = res.getString("foo");
99 String v2 = res.getString("boo");
100 print("v1 is %s, v2 is %s", v1, v2);
101 assertEquals("v1 value wrong", "bar", v1);
102 assertEquals("v2 value wrong", "ghost", v2);
103 }
104
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700105
Simon Hunta5b14542017-06-15 09:33:40 -0700106 // --- RUNTIME Locale ---
Jian Li67d47e82017-06-15 00:21:55 +0900107 @Test
Simon Huntb8042032017-06-13 15:03:23 -0700108 public void runtimeLocale() {
109 title("runtimeLocale");
110 Locale runtime = LionUtils.setupRuntimeLocale();
111 print("locale is [%s]", runtime);
112
113 // NOTE:
114 // Yeah, I know, "a unit test without asserts is not a unit test".
115 //
116 // But it would NOT be a good idea to assert the locale results in
Simon Hunta5b14542017-06-15 09:33:40 -0700117 // this method, because that is dependent on an environment variable.
Simon Huntb8042032017-06-13 15:03:23 -0700118 //
119 // This method is here to allow manual verification of the Locale
120 // e.g. when running tests from IntelliJ, and setting the
Simon Hunta5b14542017-06-15 09:33:40 -0700121 // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog.
Simon Huntb8042032017-06-13 15:03:23 -0700122 }
123
Simon Hunta5b14542017-06-15 09:33:40 -0700124
125 // --- LOCALE from String ---
Simon Huntb8042032017-06-13 15:03:23 -0700126 @Test(expected = NullPointerException.class)
127 public void localeFromStringNull() {
128 LionUtils.localeFromString(null);
129 }
130
131 private void checkLanguageCountry(Locale locale, String expL, String expC) {
132 assertEquals("Wrong language: " + expL, expL, locale.getLanguage());
133 assertEquals("Wrong country: " + expC, expC, locale.getCountry());
134 }
135
136 @Test
137 public void localeFromStringEmpty() {
138 title("localeFromStringEmpty");
139 locale = LionUtils.localeFromString("");
140 checkLanguageCountry(locale, "", "");
141 }
142
143 @Test
144 public void localeFromStringRu() {
145 title("localeFromStringRu");
146 locale = LionUtils.localeFromString("ru");
147 checkLanguageCountry(locale, "ru", "");
148 }
149
150 @Test
151 public void localeFromStringEnGB() {
152 title("localeFromStringEnGB");
153 locale = LionUtils.localeFromString("en_GB");
154 checkLanguageCountry(locale, "en", "GB");
155 }
156
157 @Test
158 public void localeFromStringItIT() {
159 title("localeFromStringItIT");
160 locale = LionUtils.localeFromString("it_IT");
161 checkLanguageCountry(locale, "it", "IT");
162 }
163
164 @Test
165 public void localeFromStringFrCA() {
166 title("localeFromStringFrCA");
167 locale = LionUtils.localeFromString("fr_CA");
168 checkLanguageCountry(locale, "fr", "CA");
169 }
170
171 @Test
Jian Li67d47e82017-06-15 00:21:55 +0900172 public void localeFromStringKoKR() {
173 title("localeFromStringKoKR");
174 locale = LionUtils.localeFromString("ko_KR");
175 checkLanguageCountry(locale, "ko", "KR");
176 }
177
Simon Hunt7379a3d2017-06-20 16:50:39 -0700178 // -- Testing generateLionBundles(...)
179
180 private static final String LION_BASE = "/org/onosproject/ui/lion/stitchtests";
181
182 private static final String[] LION_TAGS = {"CardGame1"};
183
184 @Test
185 public void generateLionBundles() {
186 title("generateLionBundles");
187 List<LionBundle> bundles =
188 LionUtils.generateLionBundles(LION_BASE, LION_TAGS);
189 print(bundles);
190 assertEquals("missing the bundle", 1, bundles.size());
191
192 LionBundle b = bundles.get(0);
193 assertEquals("wrong id", "CardGame1", b.id());
194 assertEquals("unexpected item count", 12, b.size());
195 assertEquals("missing 3oak", "Three of a Kind", b.getValue("three_oak"));
196 assertEquals("missing queen", "Queen", b.getValue("queen"));
197 assertEquals("missing clubs", "Clubs", b.getValue("clubs"));
198 }
Simon Hunta5b14542017-06-15 09:33:40 -0700199
200 // -- Testing loading of correct bundle, based on locale
Simon Hunt7379a3d2017-06-20 16:50:39 -0700201
Simon Hunta5b14542017-06-15 09:33:40 -0700202 private void checkLookups(String computer, String disk, String monitor,
203 String keyboard) {
204 res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
205 print("res locale is %s", res.getLocale().getLanguage());
206 print("a keyboard in this language is '%s'", res.getString("keyboard"));
207
208 assertEquals("wrong computer", computer, res.getString("computer"));
209 assertEquals("wrong disk", disk, res.getString("disk"));
210 assertEquals("wrong monitor", monitor, res.getString("monitor"));
211 assertEquals("wrong keyboard", keyboard, res.getString("keyboard"));
212 }
213
Jian Li67d47e82017-06-15 00:21:55 +0900214 @Test
Simon Hunta5b14542017-06-15 09:33:40 -0700215 public void messagesInEnglish() {
216 title("messagesInEnglish");
217 // use default locale
218 checkLookups("computer", "disk", "monitor", "keyboard");
219 }
220
221 @Test
222 public void messagesInGerman() {
223 title("messagesInGerman");
224 Locale.setDefault(Locale.GERMAN);
225 checkLookups("Computer", "Platte", "Monitor", "Tastatur");
226 }
227
228 @Test
229 public void messagesInItalian() {
230 title("messagesInItalian");
231 Locale.setDefault(Locale.ITALIAN);
232 checkLookups("Calcolatore", "Disco", "Schermo", "Tastiera");
233 }
234
235 // TODO: figure out why extended character sets are not handled properly
236 /* For example, the Korean test fails as follows
237
238 === messagesInKorean ===
239 res locale is ko
240 a keyboard in this language is '키보드'
241
242 org.junit.ComparisonFailure: wrong computer
243 Expected :컴퓨터
244 Actual :컴퓨터
245
246 */
247
248 @Test
249 @Ignore("Not chinese friendly, yet...")
Yi Tsengee2c8542017-06-13 15:42:17 -0700250 public void messagesInZhTw() {
251 title("messagesInZhTW");
252 Locale.setDefault(Locale.TRADITIONAL_CHINESE);
253 checkLookups("電腦", "磁碟", "螢幕", "鍵盤");
254 }
Simon Hunta5b14542017-06-15 09:33:40 -0700255
256 @Test
257 @Ignore("Not korean friendly, yet...")
258 public void messagesInKorean() {
259 title("messagesInKorean");
260 Locale.setDefault(Locale.KOREA);
261 checkLookups("컴퓨터", "디스크", "모니터", "키보드");
262 }
263
Frank Wang9986c072017-06-15 09:48:39 +0800264 @Test
265 @Ignore("Not chinese friendly, yet...")
266 public void messagesInZhCN() {
267 title("messagesInZhCN");
268 Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
269 checkLookups("电脑", "磁盘", "屏幕", "键盘");
270 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700271}