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; |
| 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; |
| 40 | |
| 41 | @BeforeClass |
| 42 | public static void classSetup() { |
| 43 | systemLocale = Locale.getDefault(); |
| 44 | } |
| 45 | |
| 46 | @AfterClass |
| 47 | public static void classTeardown() { |
| 48 | Locale.setDefault(systemLocale); |
| 49 | } |
| 50 | |
| 51 | @Before |
| 52 | public void testSetup() { |
| 53 | // reset to a known default locale before starting each test |
| 54 | Locale.setDefault(Locale.US); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void getBundleByClassAndName() { |
| 59 | title("getBundleByClassAndName"); |
| 60 | res = LionUtils.getBundledResource(LionUtilsTest.class, "SomeResource"); |
| 61 | assertNotNull("missing resource bundle", res); |
| 62 | String v1 = res.getString("key1"); |
| 63 | String v2 = res.getString("key2"); |
| 64 | print("v1 is %s, v2 is %s", v1, v2); |
| 65 | assertEquals("v1 value wrong", "value one", v1); |
| 66 | assertEquals("v2 value wrong", "value two", v2); |
| 67 | |
| 68 | res = LionUtils.getBundledResource(LionUtils.class, "SomeOtherResource"); |
| 69 | assertNotNull("missing OTHER resource bundle", res); |
| 70 | v1 = res.getString("key1"); |
| 71 | v2 = res.getString("key2"); |
| 72 | print("v1 is %s, v2 is %s", v1, v2); |
| 73 | assertEquals("v1 value wrong", "Hay", v1); |
| 74 | assertEquals("v2 value wrong", "Bee", v2); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void getBundleByClassname() { |
| 79 | title("getBundleByClassname"); |
| 80 | res = LionUtils.getBundledResource(LionUtils.class); |
| 81 | assertNotNull("missing resource bundle", res); |
| 82 | String v1 = res.getString("foo"); |
| 83 | String v2 = res.getString("boo"); |
| 84 | print("v1 is %s, v2 is %s", v1, v2); |
| 85 | assertEquals("v1 value wrong", "bar", v1); |
| 86 | assertEquals("v2 value wrong", "ghost", v2); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void getBundleByFqcn() { |
| 91 | title("getBundleByFqcn"); |
| 92 | String fqcn = "org.onosproject.ui.lion.LionUtils"; |
| 93 | res = LionUtils.getBundledResource(fqcn); |
| 94 | assertNotNull("missing resource bundle", res); |
| 95 | String v1 = res.getString("foo"); |
| 96 | String v2 = res.getString("boo"); |
| 97 | print("v1 is %s, v2 is %s", v1, v2); |
| 98 | assertEquals("v1 value wrong", "bar", v1); |
| 99 | assertEquals("v2 value wrong", "ghost", v2); |
| 100 | } |
| 101 | |
Simon Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame^] | 102 | private void checkLookups(String computer, String disk, String monitor, |
| 103 | String keyboard) { |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 104 | res = LionUtils.getBundledResource(LionUtils.class, "MyBundle"); |
| 105 | print("res locale is %s", res.getLocale().getLanguage()); |
Simon Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame^] | 106 | print("a keyboard in this language is '%s'", res.getString("keyboard")); |
| 107 | |
| 108 | assertEquals("wrong computer", computer, res.getString("computer")); |
| 109 | assertEquals("wrong disk", disk, res.getString("disk")); |
| 110 | assertEquals("wrong monitor", monitor, res.getString("monitor")); |
| 111 | assertEquals("wrong keyboard", keyboard, res.getString("keyboard")); |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
Simon Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame^] | 115 | public void messagesInEnglish() { |
| 116 | title("messagesInEnglish"); |
| 117 | // use default locale |
| 118 | checkLookups("computer", "disk", "monitor", "keyboard"); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void messagesInGerman() { |
| 123 | title("messagesInGerman"); |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 124 | Locale.setDefault(new Locale("de", "DE")); |
Simon Hunt | 7a8fe6e | 2017-06-13 13:46:44 -0700 | [diff] [blame^] | 125 | checkLookups("Computer", "Platte", "Monitor", "Tastatur"); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void messagesInItalian() { |
| 130 | title("messagesInItalian"); |
| 131 | Locale.setDefault(new Locale("it", "IT")); |
| 132 | checkLookups("Calcolatore", "Disco", "Schermo", "Tastiera"); |
Simon Hunt | 0c85f11 | 2017-06-12 21:02:17 -0700 | [diff] [blame] | 133 | } |
| 134 | } |