Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 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 | |
| 18 | package org.onosproject.ui.lion; |
| 19 | |
| 20 | import org.junit.AfterClass; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.BeforeClass; |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.AbstractUiTest; |
| 25 | |
| 26 | import java.util.Locale; |
| 27 | import java.util.ResourceBundle; |
| 28 | |
| 29 | import static org.junit.Assert.assertEquals; |
| 30 | import static org.junit.Assert.assertNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for {@link LionUtils}. |
| 34 | */ |
| 35 | public class LionUtilsTest extends AbstractUiTest { |
| 36 | |
| 37 | private static Locale systemLocale; |
| 38 | |
| 39 | private ResourceBundle res; |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 40 | private Locale locale; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 41 | |
| 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 Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame] | 103 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 104 | // --- RUNTIME Locale --- |
Jian Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 105 | @Test |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 106 | 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 Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 115 | // this method, because that is dependent on an environment variable. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 116 | // |
| 117 | // This method is here to allow manual verification of the Locale |
| 118 | // e.g. when running tests from IntelliJ, and setting the |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 119 | // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 122 | |
| 123 | // --- LOCALE from String --- |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 124 | @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 Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 170 | public void localeFromStringKoKR() { |
| 171 | title("localeFromStringKoKR"); |
| 172 | locale = LionUtils.localeFromString("ko_KR"); |
| 173 | checkLanguageCountry(locale, "ko", "KR"); |
| 174 | } |
| 175 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 176 | |
| 177 | // -- Testing loading of correct bundle, based on locale |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 178 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 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 Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 191 | @Test |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 192 | 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 Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 212 | @Test |
Yi Tseng | ee2c854 | 2017-06-13 15:42:17 -0700 | [diff] [blame] | 213 | public void messagesInZhTw() { |
| 214 | title("messagesInZhTW"); |
| 215 | Locale.setDefault(Locale.TRADITIONAL_CHINESE); |
| 216 | checkLookups("電腦", "磁碟", "螢幕", "鍵盤"); |
| 217 | } |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 218 | |
| 219 | @Test |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 220 | public void messagesInKorean() { |
| 221 | title("messagesInKorean"); |
| 222 | Locale.setDefault(Locale.KOREA); |
| 223 | checkLookups("컴퓨터", "디스크", "모니터", "키보드"); |
| 224 | } |
| 225 | |
Frank Wang | 9986c07 | 2017-06-15 09:48:39 +0800 | [diff] [blame] | 226 | @Test |
Frank Wang | 9986c07 | 2017-06-15 09:48:39 +0800 | [diff] [blame] | 227 | public void messagesInZhCN() { |
| 228 | title("messagesInZhCN"); |
| 229 | Locale.setDefault(Locale.SIMPLIFIED_CHINESE); |
| 230 | checkLookups("电脑", "磁盘", "屏幕", "键盘"); |
| 231 | } |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 232 | } |