Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.ui.lion; |
| 19 | |
| 20 | import org.junit.AfterClass; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.BeforeClass; |
| 23 | import org.junit.Test; |
Yi Tseng | ee2c854 | 2017-06-13 15:42:17 -0700 | [diff] [blame] | 24 | import org.junit.Ignore; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.AbstractUiTest; |
| 26 | |
| 27 | import java.util.Locale; |
| 28 | import java.util.ResourceBundle; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Unit tests for {@link LionUtils}. |
| 35 | */ |
| 36 | public class LionUtilsTest extends AbstractUiTest { |
| 37 | |
| 38 | private static Locale systemLocale; |
| 39 | |
| 40 | private ResourceBundle res; |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 41 | private Locale locale; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 42 | |
| 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 Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame] | 104 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 105 | // --- RUNTIME Locale --- |
Jian Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 106 | @Test |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 107 | 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 Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 116 | // this method, because that is dependent on an environment variable. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 117 | // |
| 118 | // This method is here to allow manual verification of the Locale |
| 119 | // e.g. when running tests from IntelliJ, and setting the |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 120 | // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 123 | |
| 124 | // --- LOCALE from String --- |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 125 | @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 Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 171 | public void localeFromStringKoKR() { |
| 172 | title("localeFromStringKoKR"); |
| 173 | locale = LionUtils.localeFromString("ko_KR"); |
| 174 | checkLanguageCountry(locale, "ko", "KR"); |
| 175 | } |
| 176 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 177 | |
| 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 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 | |
| 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 Tseng | ee2c854 | 2017-06-13 15:42:17 -0700 | [diff] [blame] | 227 | public void messagesInZhTw() { |
| 228 | title("messagesInZhTW"); |
| 229 | Locale.setDefault(Locale.TRADITIONAL_CHINESE); |
| 230 | checkLookups("電腦", "磁碟", "螢幕", "鍵盤"); |
| 231 | } |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 232 | |
| 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 Wang | 9986c07 | 2017-06-15 09:48:39 +0800 | [diff] [blame] | 241 | @Test |
| 242 | @Ignore("Not chinese friendly, yet...") |
| 243 | public void messagesInZhCN() { |
| 244 | title("messagesInZhCN"); |
| 245 | Locale.setDefault(Locale.SIMPLIFIED_CHINESE); |
| 246 | checkLookups("电脑", "磁盘", "屏幕", "键盘"); |
| 247 | } |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 248 | } |