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