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; |
Yi Tseng | ee2c854 | 2017-06-13 15:42:17 -0700 | [diff] [blame] | 23 | import org.junit.Ignore; |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame^] | 24 | import org.junit.Test; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.AbstractUiTest; |
| 26 | |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame^] | 27 | import java.util.List; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 28 | import java.util.Locale; |
| 29 | import java.util.ResourceBundle; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link LionUtils}. |
| 36 | */ |
| 37 | public class LionUtilsTest extends AbstractUiTest { |
| 38 | |
| 39 | private static Locale systemLocale; |
| 40 | |
| 41 | private ResourceBundle res; |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 42 | private Locale locale; |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 43 | |
| 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 Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame] | 105 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 106 | // --- RUNTIME Locale --- |
Jian Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 107 | @Test |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 108 | 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 Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 117 | // this method, because that is dependent on an environment variable. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 118 | // |
| 119 | // This method is here to allow manual verification of the Locale |
| 120 | // e.g. when running tests from IntelliJ, and setting the |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 121 | // ONOS_LOCALE env.var. via the "Edit Configurations..." dialog. |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 124 | |
| 125 | // --- LOCALE from String --- |
Simon Hunt | b804203 | 2017-06-13 15:03:23 -0700 | [diff] [blame] | 126 | @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 Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 172 | public void localeFromStringKoKR() { |
| 173 | title("localeFromStringKoKR"); |
| 174 | locale = LionUtils.localeFromString("ko_KR"); |
| 175 | checkLanguageCountry(locale, "ko", "KR"); |
| 176 | } |
| 177 | |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame^] | 178 | // -- 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 Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 199 | |
| 200 | // -- Testing loading of correct bundle, based on locale |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame^] | 201 | |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 202 | 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 Li | 67d47e8 | 2017-06-15 00:21:55 +0900 | [diff] [blame] | 214 | @Test |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 215 | 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 Tseng | ee2c854 | 2017-06-13 15:42:17 -0700 | [diff] [blame] | 250 | public void messagesInZhTw() { |
| 251 | title("messagesInZhTW"); |
| 252 | Locale.setDefault(Locale.TRADITIONAL_CHINESE); |
| 253 | checkLookups("電腦", "磁碟", "螢幕", "鍵盤"); |
| 254 | } |
Simon Hunt | a5b1454 | 2017-06-15 09:33:40 -0700 | [diff] [blame] | 255 | |
| 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 Wang | 9986c07 | 2017-06-15 09:48:39 +0800 | [diff] [blame] | 264 | @Test |
| 265 | @Ignore("Not chinese friendly, yet...") |
| 266 | public void messagesInZhCN() { |
| 267 | title("messagesInZhCN"); |
| 268 | Locale.setDefault(Locale.SIMPLIFIED_CHINESE); |
| 269 | checkLookups("电脑", "磁盘", "屏幕", "键盘"); |
| 270 | } |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 271 | } |