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.io.EOFException; |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | import java.text.DecimalFormat; |
22 | import java.text.ParseException; |
23 | |
24 | import org.apache.commons.logging.Log; |
25 | import org.apache.commons.logging.LogFactory; |
26 | |
27 | import junit.framework.TestCase; |
28 | |
29 | /** |
30 | * TestCase for StringTools. |
31 | * |
32 | * @author Thomas Aglassinger |
33 | */ |
34 | public class StringToolsTest extends TestCase |
35 | { |
36 | private Log logger; |
37 | private StringTools stringTools; |
38 | private TestTools testTools; |
39 | |
40 | public StringToolsTest(String name) { |
41 | super(name); |
42 | } |
43 | |
44 | protected void setUp() |
45 | throws Exception { |
46 | super.setUp(); |
47 | testTools = TestTools.instance(); |
48 | stringTools = StringTools.instance(); |
49 | logger = LogFactory.getLog(StringToolsTest.class); |
50 | } |
51 | |
52 | public void testAsHtmlAtributeValue() { |
53 | assertEquals("abc", stringTools.asHtmlEscaped("abc")); |
54 | assertEquals("", stringTools.asHtmlEscaped("")); |
55 | assertEquals(">"'<&", stringTools.asHtmlEscaped(">\"\'<&")); |
56 | assertEquals("süpä!", stringTools.asHtmlEscaped("s\u00fcp\u00e4!")); |
57 | assertEquals("\u0020\u007f€", stringTools.asHtmlEscaped("\u001f\u0020\u007f\u0080")); |
58 | assertEquals("", stringTools.asHtmlEscaped("")); |
59 | } |
60 | |
61 | public void testBrokenStringToIntArray() |
62 | throws IOException, ParseException { |
63 | try { |
64 | stringTools.stringToIntArray("[x"); |
65 | fail(); |
66 | } catch (NumberFormatException expectedError) { |
67 | logger.debug("ignoring expected error", expectedError); |
68 | } |
69 | try { |
70 | stringTools.stringToIntArray("[123["); |
71 | fail(); |
72 | } catch (ParseException expectedError) { |
73 | logger.debug("ignoring expected error", expectedError); |
74 | } |
75 | try { |
76 | stringTools.stringToIntArray("]"); |
77 | fail(); |
78 | } catch (ParseException expectedError) { |
79 | logger.debug("ignoring expected error", expectedError); |
80 | } |
81 | try { |
82 | stringTools.stringToIntArray(""); |
83 | fail(); |
84 | } catch (EOFException expectedError) { |
85 | logger.debug("ignoring expected error", expectedError); |
86 | } |
87 | try { |
88 | stringTools.stringToIntArray("["); |
89 | fail(); |
90 | } catch (EOFException expectedError) { |
91 | logger.debug("ignoring expected error", expectedError); |
92 | } |
93 | try { |
94 | stringTools.stringToIntArray("[1"); |
95 | fail(); |
96 | } catch (EOFException expectedError) { |
97 | logger.debug("ignoring expected error", expectedError); |
98 | } |
99 | try { |
100 | stringTools.stringToIntArray("[1,"); |
101 | fail(); |
102 | } catch (EOFException expectedError) { |
103 | logger.debug("ignoring expected error", expectedError); |
104 | } |
105 | } |
106 | |
107 | public void testCamelized() { |
108 | assertEquals("", stringTools.camelized("")); |
109 | assertEquals("x", stringTools.camelized("x")); |
110 | assertEquals("xY", stringTools.camelized("xY")); |
111 | assertEquals("someSmallText", stringTools.camelized("some-small-text")); |
112 | assertEquals("someUnderscore", stringTools.camelized("some_underscore")); |
113 | assertEquals("alreadyCamelized", stringTools.camelized("alreadyCamelized")); |
114 | } |
115 | |
116 | public void testColorString() { |
117 | assertEquals("#000000", stringTools.colorString(0)); |
118 | assertEquals("#123456", stringTools.colorString(0x123456)); |
119 | assertEquals("#7fffff", stringTools.colorString(0x7fffff)); |
120 | assertEquals("#800000", stringTools.colorString(0x800000)); |
121 | assertEquals("#800000", stringTools.colorString(-0x800000)); |
122 | assertEquals("#ffffff", stringTools.colorString(0xffffff)); |
123 | assertEquals("#ffffff", stringTools.colorString(-1)); |
124 | } |
125 | |
126 | public void testEqualsAnyOf() { |
127 | testEqualsAnyOf(false, new String[]{}, "", false); |
128 | testEqualsAnyOf(false, new String[]{}, "", true); |
129 | testEqualsAnyOf(false, new String[]{}, "red", false); |
130 | testEqualsAnyOf(false, new String[]{}, "red", true); |
131 | testEqualsAnyOf(true, new String[]{"red"}, "red", false); |
132 | testEqualsAnyOf(true, new String[]{"red"}, "red", true); |
133 | testEqualsAnyOf(false, new String[]{"red"}, "green", false); |
134 | testEqualsAnyOf(false, new String[]{"red"}, "green", true); |
135 | testEqualsAnyOf(false, new String[]{"red", "green", "blue"}, "", true); |
136 | testEqualsAnyOf(false, new String[]{"red", "green", "blue"}, "white", true); |
137 | testEqualsAnyOf(true, new String[]{"red", "green", "blue"}, "red", true); |
138 | testEqualsAnyOf(true, new String[]{"red", "green", "blue"}, "green", true); |
139 | testEqualsAnyOf(true, new String[]{"red", "green", "blue"}, "blue", true); |
140 | testEqualsAnyOf(false, new String[]{"blue", "green", "red"}, "", false); |
141 | testEqualsAnyOf(false, new String[]{"blue", "green", "red"}, "white", false); |
142 | testEqualsAnyOf(true, new String[]{"blue", "green", "red"}, "red", false); |
143 | testEqualsAnyOf(true, new String[]{"blue", "green", "red"}, "green", false); |
144 | testEqualsAnyOf(true, new String[]{"blue", "green", "red"}, "blue", false); |
145 | } |
146 | |
147 | public void testExtractNumberBackwards() { |
148 | testExtractNumberBackwards("1", 0, "1"); |
149 | testExtractNumberBackwards("a", 0, ""); |
150 | testExtractNumberBackwards("ab", 1, ""); |
151 | testExtractNumberBackwards("abc", 1, ""); |
152 | testExtractNumberBackwards("a123", 3, "123"); |
153 | testExtractNumberBackwards("a123b", 3, "123"); |
154 | } |
155 | |
156 | public void testGetDigitCount() { |
157 | testGetDigitCount(1, 0); |
158 | testGetDigitCount(1, 1); |
159 | testGetDigitCount(1, 9); |
160 | testGetDigitCount(2, 10); |
161 | testGetDigitCount(2, 11); |
162 | testGetDigitCount(2, 99); |
163 | testGetDigitCount(3, 100); |
164 | testGetDigitCount(10, Integer.MAX_VALUE); |
165 | |
166 | testGetDigitCount(2, -1); |
167 | testGetDigitCount(11, Integer.MIN_VALUE); |
168 | } |
169 | |
170 | public void testGetDigitCount(int expected, int value) { |
171 | assertEquals("number of digits in " + value, expected, stringTools.getDigitCount(value)); |
172 | } |
173 | |
174 | public void testGetLeadingZeroFormat() { |
175 | DecimalFormat format; |
176 | |
177 | format = stringTools.getLeadingZeroFormat(99); |
178 | assertEquals("00", format.format(0)); |
179 | assertEquals("01", format.format(1)); |
180 | assertEquals("99", format.format(99)); |
181 | |
182 | format = stringTools.getLeadingZeroFormat(100); |
183 | assertEquals("000", format.format(0)); |
184 | assertEquals("001", format.format(1)); |
185 | assertEquals("099", format.format(99)); |
186 | assertEquals("100", format.format(100)); |
187 | } |
188 | |
189 | public void testGetMnemonics() { |
190 | testGetMnemonics(new String[]{"a"}, new char[]{'a'}); |
191 | testGetMnemonics(new String[]{"a", "b"}, new char[]{'a', 'b'}); |
192 | testGetMnemonics(new String[]{"a", "ab"}, new char[]{'a', 'b'}); |
193 | testGetMnemonics(new String[]{"open file", "open directory"}, new char[]{'o', 'd'}); |
194 | } |
195 | |
196 | public void testHexString() { |
197 | assertEquals("0x00", stringTools.hexString(0, 2)); |
198 | assertEquals("0y00", stringTools.hexString(0, 2, "0y")); |
199 | assertEquals("0x0", stringTools.hexString(0, 1)); |
200 | assertEquals("0x12345678", stringTools.hexString(0x12345678, 1)); |
201 | assertEquals("0x0000000000000001", stringTools.hexString(1, 16)); |
202 | assertEquals("0x7fffffffffffffff", stringTools.hexString(Long.MAX_VALUE, 16)); |
203 | assertEquals("0x8000000000000000", stringTools.hexString(Long.MIN_VALUE, 16)); |
204 | } |
205 | |
206 | public void testIsNullOrEmpty() { |
207 | assertEquals(true, stringTools.isNullOrEmpty(null)); |
208 | assertEquals(true, stringTools.isNullOrEmpty("")); |
209 | assertEquals(true, stringTools.isNullOrEmpty(" \n\r\t")); |
210 | assertEquals(false, stringTools.isNullOrEmpty(" x ")); |
211 | } |
212 | |
213 | public void testIsWhiteSpace() { |
214 | assertEquals(false, stringTools.isWhiteSpace("")); |
215 | assertEquals(false, stringTools.isWhiteSpace("a")); |
216 | assertEquals(false, stringTools.isWhiteSpace(" a ")); |
217 | assertEquals(true, stringTools.isWhiteSpace(" \t\r\n")); |
218 | assertEquals(false, stringTools.isWhiteSpace(" \t\r\n x")); |
219 | assertEquals(false, stringTools.isWhiteSpace("x \t\r\n")); |
220 | } |
221 | |
222 | public void testSourced() { |
223 | assertEquals("null", stringTools.sourced((String) null)); |
224 | assertEquals("\"\"", stringTools.sourced("")); |
225 | assertEquals("\"hugo was here\"", stringTools.sourced("hugo was here")); |
226 | assertEquals("\"\\\'\"", stringTools.sourced("\'")); |
227 | assertEquals("\"\\b\"", stringTools.sourced("\b")); |
228 | assertEquals("\"\\f\"", stringTools.sourced("\f")); |
229 | assertEquals("\"\\n\"", stringTools.sourced("\n")); |
230 | assertEquals("\"\\r\"", stringTools.sourced("\r")); |
231 | assertEquals("\"\\t\"", stringTools.sourced("\t")); |
232 | assertEquals("\"\\\'\\\"\"", stringTools.sourced("\'\"")); |
233 | assertEquals("\"\\u0000\"", stringTools.sourced("\u0000")); |
234 | } |
235 | |
236 | public void testSourcedFile() { |
237 | assertEquals("null", stringTools.sourced((File) null)); |
238 | |
239 | File file = new File(System.getProperty("user.home")); |
240 | String path = file.getAbsolutePath(); |
241 | |
242 | path = "\"" + path.replaceAll("\\\\", "\\\\\\\\") + "\""; |
243 | assertEquals(path, stringTools.sourced(file)); |
244 | } |
245 | |
246 | public void testStringToIntArray() |
247 | throws IOException, ParseException { |
248 | testTools.assertEquals(new int[]{1, 2, 3}, stringTools.stringToIntArray("[1,2,3]")); |
249 | testTools.assertEquals(new int[]{1, 2, 3}, stringTools.stringToIntArray(" [ 1 , 2 , 3 ] ")); |
250 | } |
251 | |
252 | public void testTitled() { |
253 | assertEquals("", stringTools.titled("")); |
254 | assertEquals("Hello world", stringTools.titled("hello world")); |
255 | assertEquals("Hello world", stringTools.titled("Hello world")); |
256 | assertEquals("1hello world", stringTools.titled("1hello world")); |
257 | assertEquals(".hello world", stringTools.titled(".hello world")); |
258 | } |
259 | |
260 | public void testTrimPrefix() { |
261 | assertEquals("hugo", stringTools.trimPrefix("hugo", "sepp")); |
262 | assertEquals("o", stringTools.trimPrefix("hugo", "hug")); |
263 | assertEquals("", stringTools.trimPrefix("hugo", "hugo")); |
264 | assertEquals("hugo was here", stringTools.trimPrefix("hugo was here", "was here")); |
265 | assertEquals("hugo", stringTools.trimPrefix("hugo", "")); |
266 | assertEquals("", stringTools.trimPrefix("", "hugo")); |
267 | assertEquals("", stringTools.trimPrefix("", "")); |
268 | } |
269 | |
270 | protected void tearDown() |
271 | throws Exception { |
272 | stringTools = null; |
273 | testTools = null; |
274 | super.tearDown(); |
275 | } |
276 | |
277 | private void testEqualsAnyOf(boolean expected, String[] haystack, String needle, boolean sort) { |
278 | assert haystack != null; |
279 | assert needle != null; |
280 | boolean actual = stringTools.equalsAnyOf(haystack, needle, sort); |
281 | |
282 | assertEquals("needle=" + stringTools.sourced(needle), expected, actual); |
283 | } |
284 | |
285 | private void testExtractNumberBackwards(String name, int startIndex, String expected) { |
286 | assert name != null; |
287 | assert expected != null; |
288 | String actual = stringTools.extractNumberBackwards(name, startIndex); |
289 | |
290 | assertEquals("number extracted from " + stringTools.sourced(name) |
291 | + " starting at " + startIndex, expected, actual); |
292 | } |
293 | |
294 | private void testGetMnemonics(String[] labels, char[] expected) { |
295 | assert labels != null; |
296 | assert expected != null; |
297 | int labelsLength = labels.length; |
298 | int expectedLength = expected.length; |
299 | |
300 | assert labelsLength == expectedLength |
301 | : "labels.length=" + labelsLength + ", expected.length=" + expectedLength; |
302 | char[] actual = stringTools.getMnemonics(labels, null); |
303 | |
304 | for (int i = 0; i < labelsLength; i += 1) { |
305 | for (int j = i + 1; j < labelsLength; j += 1) { |
306 | assertFalse("mnemonics at " + i + " and " + j + " are the same: " + actual[i], actual[i] == actual[j]); |
307 | } |
308 | } |
309 | assertEquals(new String(expected), new String(actual)); |
310 | } |
311 | } |