1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.util.Locale; |
19 | |
20 | import javax.swing.JMenu; |
21 | import javax.swing.JMenuItem; |
22 | |
23 | import net.sf.jomic.common.Settings; |
24 | |
25 | import junit.framework.TestCase; |
26 | |
27 | /** |
28 | * TestCase for LocaleTools. |
29 | * |
30 | * @author Thomas Aglassinger |
31 | */ |
32 | public class LocaleToolsTest extends TestCase |
33 | { |
34 | private LocaleTools localeTools; |
35 | |
36 | /* |
37 | * @see TestCase#setUp() |
38 | */ |
39 | protected void setUp() |
40 | throws Exception { |
41 | super.setUp(); |
42 | TestTools.instance(); |
43 | localeTools = LocaleTools.instance(); |
44 | localeTools.setLocale(Locale.ENGLISH); |
45 | } |
46 | |
47 | public void testAsByteText() { |
48 | testAsByteText(1, "1 byte"); |
49 | testAsByteText(1023, "1023 byte"); |
50 | testAsByteText(1024, "1 KB"); |
51 | testAsByteText(17000, "16.6 KB"); |
52 | } |
53 | |
54 | public void testGetMessageWith1Option() { |
55 | String text; |
56 | String optionValue = "some option"; |
57 | |
58 | text = localeTools.getMessage("test.withOption", optionValue); |
59 | assertEquals("the test parameter is: " + optionValue, text); |
60 | } |
61 | |
62 | public void testGetSimpleMessage() { |
63 | String text; |
64 | |
65 | text = localeTools.getMessage("test.simple"); |
66 | assertEquals("test", text); |
67 | } |
68 | |
69 | public void testLocaleListener() { |
70 | String expectedLocale = Locale.TRADITIONAL_CHINESE.toString(); |
71 | Settings settings = Settings.instance(); |
72 | |
73 | settings.setLocale(expectedLocale); |
74 | assertEquals("locale", expectedLocale.toLowerCase(), localeTools.getLocale().toString().toLowerCase()); |
75 | } |
76 | |
77 | public void testMenu() { |
78 | String menuLabel = localeTools.getMenuLabel(LocaleTools.MENU_FILE); |
79 | |
80 | assertNotNull("menuLabel", menuLabel); |
81 | |
82 | JMenu menu = localeTools.createMenu(LocaleTools.MENU_FILE); |
83 | |
84 | assertNotNull("menu", menu); |
85 | } |
86 | |
87 | public void testMenuItem() { |
88 | String itemLabel = localeTools.getMenuItemLabel(LocaleTools.MENU_FILE, "open"); |
89 | |
90 | assertNotNull("itemLabel", itemLabel); |
91 | |
92 | JMenuItem item = localeTools.createMenuItem(LocaleTools.MENU_FILE, "open"); |
93 | |
94 | assertNotNull("item", item); |
95 | } |
96 | |
97 | public void testParseBrokenLanguageCountryVariant() { |
98 | try { |
99 | localeTools.parseLanguageCountryVariant("___"); |
100 | assertTrue("expected IllegalArgumentException", false); |
101 | } catch (IllegalArgumentException expectedError) { |
102 | localeTools.parseLanguageCountryVariant(""); |
103 | } |
104 | } |
105 | |
106 | public void testParseLanguageCountryVariant() { |
107 | testParseLanguageCountryVariant("en", "en", "", ""); |
108 | testParseLanguageCountryVariant("de_AT", "de", "AT", ""); |
109 | testParseLanguageCountryVariant("de_CH_macos", "de", "CH", "macos"); |
110 | testParseLanguageCountryVariant("_AT", "", "AT", ""); |
111 | testParseLanguageCountryVariant("_AT_macos", "", "AT", "macos"); |
112 | testParseLanguageCountryVariant("__macos", "", "", "macos"); |
113 | testParseLanguageCountryVariant("hugo_was_here", "hugo", "was", "here"); |
114 | localeTools.parseLanguageCountryVariant(null); |
115 | localeTools.parseLanguageCountryVariant(""); |
116 | localeTools.parseLanguageCountryVariant("_"); |
117 | localeTools.parseLanguageCountryVariant("__"); |
118 | } |
119 | |
120 | public void testSetLocale() { |
121 | try { |
122 | testSetLocale(Locale.JAPANESE.toString(), "ja", null, null); |
123 | } finally { |
124 | localeTools.setLocale(Locale.ENGLISH); |
125 | } |
126 | } |
127 | |
128 | public void testSetUnknownLocale() { |
129 | try { |
130 | testSetLocale("xx_yy", "xx", "YY", null); |
131 | testSetLocale("hugo_was_here", "hugo", "WAS", "here"); |
132 | } finally { |
133 | localeTools.setLocale(Locale.ENGLISH); |
134 | } |
135 | } |
136 | |
137 | /* |
138 | * @see TestCase#tearDown() |
139 | */ |
140 | protected void tearDown() |
141 | throws Exception { |
142 | localeTools = null; |
143 | super.tearDown(); |
144 | } |
145 | |
146 | private void testAsByteText(long amount, String expected) { |
147 | assert expected != null; |
148 | String actual = localeTools.asByteText(amount); |
149 | |
150 | assertEquals("amount=" + amount, expected, actual); |
151 | } |
152 | |
153 | private void testParseLanguageCountryVariant( |
154 | String languageCountryVariant, String expectedLanguage, String expectedCountry, String expectedVariant) { |
155 | String[] items = localeTools.parseLanguageCountryVariant(languageCountryVariant); |
156 | |
157 | assertEquals(expectedLanguage, items[0]); |
158 | assertEquals(expectedCountry, items[1]); |
159 | assertEquals(expectedVariant, items[2]); |
160 | } |
161 | |
162 | private void testSetLocale( |
163 | String localeText, String expectedLanguage, String expectedCountry, String expectedVariant) { |
164 | assert localeText != null; |
165 | String[] expectedItems = new String[]{expectedLanguage, expectedCountry, expectedVariant}; |
166 | |
167 | try { |
168 | localeTools.setLocale(localeText); |
169 | |
170 | String[] actualItems = localeTools.parseLanguageCountryVariant(localeTools.getLocale().toString()); |
171 | |
172 | for (int i = 0; i < expectedItems.length; i += 1) { |
173 | String item = expectedItems[i]; |
174 | |
175 | if (item != null) { |
176 | assertEquals("localeText item " + i, item, actualItems[i]); |
177 | } |
178 | } |
179 | } finally { |
180 | localeTools.setLocale(Locale.ENGLISH); |
181 | } |
182 | } |
183 | |
184 | } |