blob: 2ac324db78eda86e18da0c25dd19a35f03df4e40 [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 private void checkLookups(String computer, String disk, String monitor,
105 String keyboard) {
Simon Hunt0c85f112017-06-12 21:02:17 -0700106 res = LionUtils.getBundledResource(LionUtils.class, "MyBundle");
107 print("res locale is %s", res.getLocale().getLanguage());
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700108 print("a keyboard in this language is '%s'", res.getString("keyboard"));
109
110 assertEquals("wrong computer", computer, res.getString("computer"));
111 assertEquals("wrong disk", disk, res.getString("disk"));
112 assertEquals("wrong monitor", monitor, res.getString("monitor"));
113 assertEquals("wrong keyboard", keyboard, res.getString("keyboard"));
Simon Hunt0c85f112017-06-12 21:02:17 -0700114 }
115
116 @Test
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700117 public void messagesInEnglish() {
118 title("messagesInEnglish");
119 // use default locale
120 checkLookups("computer", "disk", "monitor", "keyboard");
121 }
122
123 @Test
124 public void messagesInGerman() {
125 title("messagesInGerman");
Simon Hunt0c85f112017-06-12 21:02:17 -0700126 Locale.setDefault(new Locale("de", "DE"));
Simon Hunt7a8fe6e2017-06-13 13:46:44 -0700127 checkLookups("Computer", "Platte", "Monitor", "Tastatur");
128 }
129
130 @Test
131 public void messagesInItalian() {
132 title("messagesInItalian");
133 Locale.setDefault(new Locale("it", "IT"));
134 checkLookups("Calcolatore", "Disco", "Schermo", "Tastiera");
Simon Hunt0c85f112017-06-12 21:02:17 -0700135 }
Yi Tsengee2c8542017-06-13 15:42:17 -0700136
137 @Test
Jian Li67d47e82017-06-15 00:21:55 +0900138 @Ignore("Not korean friendly, yet...")
139 public void messagesInKorean() {
140 title("messagesInKorean");
141 Locale.setDefault(Locale.KOREA);
142 checkLookups("컴퓨터", "디스크", "모니터", "키보드");
143 }
144
145 @Test
Simon Huntb8042032017-06-13 15:03:23 -0700146 public void runtimeLocale() {
147 title("runtimeLocale");
148 Locale runtime = LionUtils.setupRuntimeLocale();
149 print("locale is [%s]", runtime);
150
151 // NOTE:
152 // Yeah, I know, "a unit test without asserts is not a unit test".
153 //
154 // But it would NOT be a good idea to assert the locale results in
155 // this method, because that is dependent on environment variables.
156 //
157 // This method is here to allow manual verification of the Locale
158 // e.g. when running tests from IntelliJ, and setting the
159 // env-vars via the "Edit Configurations..." dialog.
160 }
161
162 @Test(expected = NullPointerException.class)
163 public void localeFromStringNull() {
164 LionUtils.localeFromString(null);
165 }
166
167 private void checkLanguageCountry(Locale locale, String expL, String expC) {
168 assertEquals("Wrong language: " + expL, expL, locale.getLanguage());
169 assertEquals("Wrong country: " + expC, expC, locale.getCountry());
170 }
171
172 @Test
173 public void localeFromStringEmpty() {
174 title("localeFromStringEmpty");
175 locale = LionUtils.localeFromString("");
176 checkLanguageCountry(locale, "", "");
177 }
178
179 @Test
180 public void localeFromStringRu() {
181 title("localeFromStringRu");
182 locale = LionUtils.localeFromString("ru");
183 checkLanguageCountry(locale, "ru", "");
184 }
185
186 @Test
187 public void localeFromStringEnGB() {
188 title("localeFromStringEnGB");
189 locale = LionUtils.localeFromString("en_GB");
190 checkLanguageCountry(locale, "en", "GB");
191 }
192
193 @Test
194 public void localeFromStringItIT() {
195 title("localeFromStringItIT");
196 locale = LionUtils.localeFromString("it_IT");
197 checkLanguageCountry(locale, "it", "IT");
198 }
199
200 @Test
201 public void localeFromStringFrCA() {
202 title("localeFromStringFrCA");
203 locale = LionUtils.localeFromString("fr_CA");
204 checkLanguageCountry(locale, "fr", "CA");
205 }
206
207 @Test
Jian Li67d47e82017-06-15 00:21:55 +0900208 public void localeFromStringKoKR() {
209 title("localeFromStringKoKR");
210 locale = LionUtils.localeFromString("ko_KR");
211 checkLanguageCountry(locale, "ko", "KR");
212 }
213
214 @Test
Yi Tsengee2c8542017-06-13 15:42:17 -0700215 @Ignore("Jenkins not chinese friendly, yet...")
216 public void messagesInZhTw() {
217 title("messagesInZhTW");
218 Locale.setDefault(Locale.TRADITIONAL_CHINESE);
219 checkLookups("電腦", "磁碟", "螢幕", "鍵盤");
220 }
Simon Hunt0c85f112017-06-12 21:02:17 -0700221}