blob: 84f98a1c8181e39b0bcfda21db7a8ff54b0c258d [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;
Simon Hunt7379a3d2017-06-20 16:50:39 -070023import org.junit.Test;
Simon Hunt0c85f112017-06-12 21:02:17 -070024import org.onosproject.ui.AbstractUiTest;
25
26import java.util.Locale;
27import java.util.ResourceBundle;
28
29import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertNotNull;
31
32/**
33 * Unit tests for {@link LionUtils}.
34 */
35public class LionUtilsTest extends AbstractUiTest {
36
37 private static Locale systemLocale;
38
39 private ResourceBundle res;
Simon Huntb8042032017-06-13 15:03:23 -070040 private Locale locale;
Simon Hunt0c85f112017-06-12 21:02:17 -070041
42 @BeforeClass
43 public static void classSetup() {
44 systemLocale = Locale.getDefault();
45 }
46
47 @AfterClass
48 public static void classTeardown() {
49 Locale.setDefault(systemLocale);
50 }
51
52 @Before
53 public void testSetup() {
54 // reset to a known default locale before starting each test
55 Locale.setDefault(Locale.US);
56 }
57
58 @Test
59 public void getBundleByClassAndName() {
60 title("getBundleByClassAndName");
61 res = LionUtils.getBundledResource(LionUtilsTest.class, "SomeResource");
62 assertNotNull("missing resource bundle", res);
63 String v1 = res.getString("key1");
64 String v2 = res.getString("key2");
65 print("v1 is %s, v2 is %s", v1, v2);
66 assertEquals("v1 value wrong", "value one", v1);
67 assertEquals("v2 value wrong", "value two", v2);
68
69 res = LionUtils.getBundledResource(LionUtils.class, "SomeOtherResource");
70 assertNotNull("missing OTHER resource bundle", res);
71 v1 = res.getString("key1");
72 v2 = res.getString("key2");
73 print("v1 is %s, v2 is %s", v1, v2);
74 assertEquals("v1 value wrong", "Hay", v1);
75 assertEquals("v2 value wrong", "Bee", v2);
76 }
77
78 @Test
79 public void getBundleByClassname() {
80 title("getBundleByClassname");
81 res = LionUtils.getBundledResource(LionUtils.class);
82 assertNotNull("missing resource bundle", res);
83 String v1 = res.getString("foo");
84 String v2 = res.getString("boo");
85 print("v1 is %s, v2 is %s", v1, v2);
86 assertEquals("v1 value wrong", "bar", v1);
87 assertEquals("v2 value wrong", "ghost", v2);
88 }
89
90 @Test
91 public void getBundleByFqcn() {
92 title("getBundleByFqcn");
93 String fqcn = "org.onosproject.ui.lion.LionUtils";
94 res = LionUtils.getBundledResource(fqcn);
95 assertNotNull("missing resource bundle", res);
96 String v1 = res.getString("foo");
97 String v2 = res.getString("boo");
98 print("v1 is %s, v2 is %s", v1, v2);
99 assertEquals("v1 value wrong", "bar", v1);
100 assertEquals("v2 value wrong", "ghost", v2);
101 }
102
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700103
Simon Hunta5b14542017-06-15 09:33:40 -0700104 // --- RUNTIME Locale ---
Jian Li67d47e82017-06-15 00:21:55 +0900105 @Test
Simon Huntb8042032017-06-13 15:03:23 -0700106 public void runtimeLocale() {
107 title("runtimeLocale");
108 Locale runtime = LionUtils.setupRuntimeLocale();
109 print("locale is [%s]", runtime);
110
111 // NOTE:
112 // Yeah, I know, "a unit test without asserts is not a unit test".
113 //
114 // But it would NOT be a good idea to assert the locale results in
Simon Hunta5b14542017-06-15 09:33:40 -0700115 // this method, because that is dependent on an environment variable.
Simon Huntb8042032017-06-13 15:03:23 -0700116 //
117 // This method is here to allow manual verification of the Locale
118 // e.g. when running tests from IntelliJ, and setting the
Simon Hunta5b14542017-06-15 09:33:40 -0700119 // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog.
Simon Huntb8042032017-06-13 15:03:23 -0700120 }
121
Simon Hunta5b14542017-06-15 09:33:40 -0700122
123 // --- LOCALE from String ---
Simon Huntb8042032017-06-13 15:03:23 -0700124 @Test(expected = NullPointerException.class)
125 public void localeFromStringNull() {
126 LionUtils.localeFromString(null);
127 }
128
129 private void checkLanguageCountry(Locale locale, String expL, String expC) {
130 assertEquals("Wrong language: " + expL, expL, locale.getLanguage());
131 assertEquals("Wrong country: " + expC, expC, locale.getCountry());
132 }
133
134 @Test
135 public void localeFromStringEmpty() {
136 title("localeFromStringEmpty");
137 locale = LionUtils.localeFromString("");
138 checkLanguageCountry(locale, "", "");
139 }
140
141 @Test
142 public void localeFromStringRu() {
143 title("localeFromStringRu");
144 locale = LionUtils.localeFromString("ru");
145 checkLanguageCountry(locale, "ru", "");
146 }
147
148 @Test
149 public void localeFromStringEnGB() {
150 title("localeFromStringEnGB");
151 locale = LionUtils.localeFromString("en_GB");
152 checkLanguageCountry(locale, "en", "GB");
153 }
154
155 @Test
156 public void localeFromStringItIT() {
157 title("localeFromStringItIT");
158 locale = LionUtils.localeFromString("it_IT");
159 checkLanguageCountry(locale, "it", "IT");
160 }
161
162 @Test
163 public void localeFromStringFrCA() {
164 title("localeFromStringFrCA");
165 locale = LionUtils.localeFromString("fr_CA");
166 checkLanguageCountry(locale, "fr", "CA");
167 }
168
169 @Test
Jian Li67d47e82017-06-15 00:21:55 +0900170 public void localeFromStringKoKR() {
171 title("localeFromStringKoKR");
172 locale = LionUtils.localeFromString("ko_KR");
173 checkLanguageCountry(locale, "ko", "KR");
174 }
175
Simon Hunta5b14542017-06-15 09:33:40 -0700176
177 // -- Testing loading of correct bundle, based on locale
Simon Hunt7379a3d2017-06-20 16:50:39 -0700178
Simon Hunta5b14542017-06-15 09:33:40 -0700179 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
Simon Hunta5b14542017-06-15 09:33:40 -0700212 @Test
Yi Tsengee2c8542017-06-13 15:42:17 -0700213 public void messagesInZhTw() {
214 title("messagesInZhTW");
215 Locale.setDefault(Locale.TRADITIONAL_CHINESE);
216 checkLookups("電腦", "磁碟", "螢幕", "鍵盤");
217 }
Simon Hunta5b14542017-06-15 09:33:40 -0700218
219 @Test
Simon Hunta5b14542017-06-15 09:33:40 -0700220 public void messagesInKorean() {
221 title("messagesInKorean");
222 Locale.setDefault(Locale.KOREA);
223 checkLookups("컴퓨터", "디스크", "모니터", "키보드");
224 }
225
Frank Wang9986c072017-06-15 09:48:39 +0800226 @Test
Frank Wang9986c072017-06-15 09:48:39 +0800227 public void messagesInZhCN() {
228 title("messagesInZhCN");
229 Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
230 checkLookups("电脑", "磁盘", "屏幕", "键盘");
231 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700232}