001: /*
002: * StringUtilTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.util.LinkedList;
015: import java.util.List;
016: import junit.framework.TestCase;
017:
018: /**
019: *
020: * @author support@sql-workbench.net
021: */
022: public class StringUtilTest extends TestCase {
023:
024: public StringUtilTest(String testName) {
025: super (testName);
026: }
027:
028: public void testMakeFilename() {
029: try {
030: String fname = StringUtil.makeFilename("TABLE_NAME");
031: assertEquals("table_name", fname);
032:
033: fname = StringUtil.makeFilename("TABLE_\\NAME");
034: assertEquals("table_name", fname);
035:
036: fname = StringUtil.makeFilename("TABLE_<>NAME");
037: assertEquals("table_name", fname);
038:
039: } catch (Exception e) {
040: e.printStackTrace();
041: fail(e.getMessage());
042: }
043: }
044:
045: public void testReplace() {
046: try {
047: String s = StringUtil.replace(null, "gaga", "gogo");
048: assertNull(s);
049:
050: s = StringUtil.replace("gaga", null, "gogo");
051: assertEquals("gaga", s);
052:
053: s = StringUtil.replace("gaga", "gogo", null);
054: assertEquals("gaga", s);
055: } catch (Exception e) {
056: e.printStackTrace();
057: fail(e.getMessage());
058: }
059: }
060:
061: public void testEndsWith() {
062: try {
063: String s = "this is a test";
064: assertTrue(StringUtil.endsWith(s, "test"));
065: assertFalse(StringUtil.endsWith(s, "testing"));
066:
067: assertFalse(StringUtil.endsWith("bla", "blabla"));
068: assertTrue(StringUtil.endsWith("bla", "bla"));
069:
070: assertFalse(StringUtil.endsWith("est", "test"));
071: assertFalse(StringUtil.endsWith("no est", "test"));
072: } catch (Exception e) {
073: e.printStackTrace();
074: fail(e.getMessage());
075: }
076: }
077:
078: public void testIndexOf() {
079: try {
080: String s = ".this. is a test";
081: int pos = StringUtil.indexOf(s, '.');
082: assertEquals(0, pos);
083:
084: s = "this. is. a. test.";
085: pos = StringUtil.indexOf(s, '.');
086: assertEquals(4, pos);
087:
088: s = "this. is. a test";
089: pos = StringUtil.indexOf(s, '.', 2);
090: assertEquals(8, pos);
091:
092: } catch (Exception e) {
093: e.printStackTrace();
094: fail(e.getMessage());
095: }
096: }
097:
098: public void testLastIndexOf() {
099: try {
100: String s = "this is a test.";
101: int pos = StringUtil.lastIndexOf(s, '.');
102: assertEquals(s.length() - 1, pos);
103:
104: s = "this. is. a. test.";
105: pos = StringUtil.lastIndexOf(s, '.');
106: assertEquals(s.length() - 1, pos);
107:
108: s = "this is a test";
109: pos = StringUtil.lastIndexOf(s, '.');
110: assertEquals(-1, pos);
111:
112: StringBuilder b = new StringBuilder("this is a test.");
113: pos = StringUtil.lastIndexOf(b, '.');
114: assertEquals(b.length() - 1, pos);
115:
116: b = new StringBuilder("this. is a test");
117: pos = StringUtil.lastIndexOf(b, '.');
118: assertEquals(4, pos);
119:
120: } catch (Exception e) {
121: e.printStackTrace();
122: fail(e.getMessage());
123: }
124: }
125:
126: public void testDecodeUnicode() {
127: try {
128: String value = "Incorrect \\ string";
129: String decoded = StringUtil.decodeUnicode(value);
130: assertEquals(value, decoded);
131:
132: value = "Test \\u00E4\\u00E5";
133: decoded = StringUtil.decodeUnicode(value);
134: assertEquals("Test \u00E4\u00E5", decoded);
135:
136: value = "Wrong \\uxyz encoded";
137: decoded = StringUtil.decodeUnicode(value);
138: assertEquals(value, decoded);
139:
140: value = "Wrong \\u04";
141: decoded = StringUtil.decodeUnicode(value);
142: assertEquals("Wrong string not decoded", value, decoded);
143:
144: value = "test \\u";
145: decoded = StringUtil.decodeUnicode(value);
146: assertEquals("Wrong string not decoded", value, decoded);
147:
148: value = "test \\u wrong";
149: decoded = StringUtil.decodeUnicode(value);
150: assertEquals("Wrong string not decoded", value, decoded);
151:
152: decoded = StringUtil.decodeUnicode("\\r\\ntest");
153: assertEquals("Single char not replaced correctly",
154: "\r\ntest", decoded);
155:
156: decoded = StringUtil.decodeUnicode("Hello \\t World");
157: assertEquals("Single char not replaced correctly",
158: "Hello \t World", decoded);
159:
160: decoded = StringUtil.decodeUnicode("test\\t");
161: assertEquals("Single char not replaced correctly",
162: "test\t", decoded);
163:
164: decoded = StringUtil.decodeUnicode("test\\x");
165: assertEquals("Single char not replaced correctly",
166: "test\\x", decoded);
167:
168: decoded = StringUtil.decodeUnicode("test\\");
169: assertEquals("Single char not replaced correctly",
170: "test\\", decoded);
171:
172: decoded = StringUtil.decodeUnicode("test\\\\");
173: assertEquals("test\\", decoded);
174:
175: value = "abc\\\\def";
176: decoded = StringUtil.decodeUnicode(value);
177: assertEquals("abc\\def", decoded);
178:
179: } catch (Exception e) {
180: e.printStackTrace();
181: fail(e.getMessage());
182: }
183:
184: }
185:
186: public void testEncodeUnicode() {
187: String value = "\u00E4";
188: String enc = StringUtil.escapeUnicode(value, null,
189: CharacterRange.RANGE_7BIT, false);
190: assertEquals("Umlaut not replaced", "\\u00E4", enc);
191:
192: value = "\n";
193: enc = StringUtil.escapeUnicode(value, null,
194: CharacterRange.RANGE_7BIT, true);
195: assertEquals("NL not replaced", "\\u000A", enc);
196:
197: enc = StringUtil.escapeUnicode(value, null,
198: CharacterRange.RANGE_7BIT, false);
199: assertEquals("NL not replaced", "\\n", enc);
200:
201: value = "abcdefghijk";
202: enc = StringUtil.escapeUnicode(value, null,
203: CharacterRange.RANGE_7BIT, true);
204: assertEquals("NL not replaced", value, enc);
205:
206: value = "abc;def;ghi";
207: enc = StringUtil.escapeUnicode(value, ";",
208: CharacterRange.RANGE_7BIT, true);
209: assertEquals("Additional characters not replaced",
210: "abc\\u003Bdef\\u003Bghi", enc);
211: //System.out.println("enc=" + enc);
212:
213: }
214:
215: public void testMakePlainLF() {
216: String line = "line1\r\nline2";
217: String newline = StringUtil.makePlainLinefeed(line);
218: assertEquals("No LF", "line1\nline2", newline);
219: }
220:
221: public void testRtrim() {
222: String s = "bla";
223: assertEquals(s, StringUtil.rtrim(s));
224:
225: s = " \tbla";
226: assertEquals(s, StringUtil.rtrim(s));
227:
228: s = "bla \t\n";
229: assertEquals("bla", StringUtil.rtrim(s));
230:
231: s = "bla \t\nbla";
232: assertEquals(s, StringUtil.rtrim(s));
233:
234: s = " \n\r\t";
235: assertEquals("", StringUtil.rtrim(s));
236:
237: s = "";
238: assertEquals(s, StringUtil.rtrim(s));
239: }
240:
241: public void testEqualString() {
242: String one = "bla";
243: String two = null;
244:
245: assertEquals(false, StringUtil.equalString(one, two));
246: assertEquals(false, StringUtil.equalString(two, one));
247:
248: assertEquals(false, StringUtil.equalStringIgnoreCase(one, two));
249: assertEquals(false, StringUtil.equalStringIgnoreCase(two, one));
250:
251: one = "bla";
252: two = "bla";
253:
254: assertEquals(true, StringUtil.equalString(one, two));
255: assertEquals(true, StringUtil.equalStringIgnoreCase(two, one));
256:
257: one = "bla";
258: two = "BLA";
259:
260: assertEquals(false, StringUtil.equalString(one, two));
261: assertEquals(true, StringUtil.equalStringIgnoreCase(two, one));
262:
263: one = "bla";
264: two = "blub";
265:
266: assertEquals(false, StringUtil.equalString(one, two));
267: assertEquals(false, StringUtil.equalStringIgnoreCase(two, one));
268:
269: }
270:
271: public void testCaseCheck() {
272: assertEquals(false, StringUtil.isUpperCase("This is a test"));
273: assertEquals(true, StringUtil.isMixedCase("This is a test"));
274: assertEquals(false, StringUtil.isLowerCase("This is a test"));
275:
276: assertEquals(
277: true,
278: StringUtil
279: .isLowerCase("this is a test 12345 #+*-.,;:!\"$%&/()=?"));
280: assertEquals(
281: true,
282: StringUtil
283: .isUpperCase("THIS IS A TEST 12345 #+*-.,;:!\"$%&/()=?"));
284: assertEquals(true, StringUtil.isUpperCase("1234567890"));
285: assertEquals(true, StringUtil.isLowerCase("1234567890"));
286: }
287:
288: public void testGetRealLineLenght() {
289: int len = StringUtil.getRealLineLength("bla\r");
290: assertEquals(3, len);
291:
292: len = StringUtil.getRealLineLength("bla\r\n");
293: assertEquals(3, len);
294:
295: len = StringUtil.getRealLineLength("bla\r\n\n");
296: assertEquals(3, len);
297:
298: len = StringUtil.getRealLineLength("bla \r\n\n\r");
299: assertEquals(4, len);
300:
301: len = StringUtil.getRealLineLength("bla");
302: assertEquals(3, len);
303:
304: len = StringUtil.getRealLineLength("\r\n");
305: assertEquals(0, len);
306:
307: len = StringUtil.getRealLineLength("\n");
308: assertEquals(0, len);
309: }
310:
311: public void testIsWhitespace() {
312: try {
313: String s = "bla";
314: assertEquals(false, StringUtil.isWhitespace(s));
315:
316: s = " bla ";
317: assertEquals(false, StringUtil.isWhitespace(s));
318:
319: s = " \n \n";
320: assertEquals(true, StringUtil.isWhitespace(s));
321:
322: s = " \t\r\n ;";
323: assertEquals(false, StringUtil.isWhitespace(s));
324:
325: s = "";
326: assertEquals(false, StringUtil.isWhitespace(s));
327:
328: } catch (Exception e) {
329: e.printStackTrace();
330: fail(e.getMessage());
331: }
332: }
333:
334: public void testTrimBuffer() {
335: try {
336: StringBuilder b = new StringBuilder("bla");
337: StringUtil.trimTrailingWhitespace(b);
338: assertEquals("Buffer was changed", "bla", b.toString());
339:
340: b = new StringBuilder("bla bla ");
341: StringUtil.trimTrailingWhitespace(b);
342: assertEquals("Whitespace not removed", "bla bla", b
343: .toString());
344:
345: b = new StringBuilder("bla bla \t");
346: StringUtil.trimTrailingWhitespace(b);
347: assertEquals("Whitespace not removed", "bla bla", b
348: .toString());
349:
350: b = new StringBuilder("bla bla \t\n\r \t");
351: StringUtil.trimTrailingWhitespace(b);
352: assertEquals("Whitespace not removed", "bla bla", b
353: .toString());
354:
355: } catch (Exception e) {
356: e.printStackTrace();
357: fail(e.getMessage());
358: }
359: }
360:
361: public void testToArray() {
362: try {
363: List<String> elements = new LinkedList<String>();
364: elements.add("one");
365: elements.add("two");
366: elements.add("three");
367:
368: String[] result = StringUtil.toArray(elements);
369: assertEquals(result.length, 3);
370: assertEquals(result[1], "two");
371: } catch (Exception e) {
372: e.printStackTrace();
373: }
374: }
375:
376: public void testGetDoubleValue() {
377: try {
378: double value = StringUtil.getDoubleValue("123.45", -1);
379: assertEquals(123.45, value, 0.01);
380:
381: value = StringUtil.getDoubleValue(" 123.45 ", -1);
382: assertEquals(123.45, value, 0.01);
383:
384: value = StringUtil.getDoubleValue("bla", -66);
385: assertEquals(-66, value, 0.01);
386:
387: } catch (Exception e) {
388: e.printStackTrace();
389: fail(e.getMessage());
390: }
391: }
392:
393: public void testGetIntValue() {
394: try {
395: int iValue = StringUtil.getIntValue(" 123 ", -1);
396: assertEquals(123, iValue);
397:
398: iValue = StringUtil.getIntValue("42", -1);
399: assertEquals(42, iValue);
400:
401: iValue = StringUtil.getIntValue("bla", -24);
402: assertEquals(-24, iValue);
403: } catch (Exception e) {
404: e.printStackTrace();
405: fail(e.getMessage());
406: }
407: }
408:
409: public void testStringToList() {
410: String list = "1,2,3";
411: List l = StringUtil.stringToList(list, ",", true, true, true);
412: assertEquals("Wrong number of elements returned", 3, l.size());
413:
414: list = "1,2,,3";
415: l = StringUtil.stringToList(list, ",", true, true, true);
416: assertEquals("Empty element not removed", 3, l.size());
417:
418: list = "1,2, ,3";
419: l = StringUtil.stringToList(list, ",", false);
420: assertEquals("Empty element removed", 4, l.size());
421:
422: list = "1,2,,3";
423: l = StringUtil.stringToList(list, ",", false);
424: assertEquals("Null element not removed", 3, l.size());
425:
426: list = " 1 ,2,3";
427: l = StringUtil.stringToList(list, ",", true);
428: assertEquals("Null element not removed", 3, l.size());
429: assertEquals(" 1 ", l.get(0));
430:
431: l = StringUtil.stringToList(list, ",", true, true);
432: assertEquals("Element not trimmed", "1", l.get(0));
433:
434: list = "1,\"2,5\",3";
435: l = StringUtil.stringToList(list, ",", true, true, true);
436: assertEquals("Quoted string not recognized", "2,5", l.get(1));
437:
438: list = "library.jar";
439: l = StringUtil.stringToList(list, ";", true, true, false);
440: assertEquals("Single element list not correct", 1, l.size());
441: assertEquals("Single element list not correct", "library.jar",
442: l.get(0));
443:
444: }
445:
446: public void testHasOpenQuotes() {
447: String value = "this line does not have quotes";
448: assertEquals("Wrong check for non-quotes", false, StringUtil
449: .hasOpenQuotes(value, '\''));
450:
451: value = "this line 'does' have quotes";
452: assertEquals("Wrong check for quotes", false, StringUtil
453: .hasOpenQuotes(value, '\''));
454:
455: value = "this line leaves a 'quote open";
456: assertEquals("Wrong check for open quotes", true, StringUtil
457: .hasOpenQuotes(value, '\''));
458: }
459:
460: public void testIsNumber() {
461: boolean isNumber = StringUtil.isNumber("1");
462: assertEquals(true, isNumber);
463:
464: isNumber = StringUtil.isNumber("1.234");
465: assertEquals(true, isNumber);
466:
467: isNumber = StringUtil.isNumber("1.xxx");
468: assertEquals(false, isNumber);
469:
470: isNumber = StringUtil.isNumber("bla");
471: assertEquals(false, isNumber);
472: }
473:
474: public void testMaxString() {
475: String s = StringUtil.getMaxSubstring("Dent", 4, null);
476: assertEquals("Truncated", "Dent", s);
477:
478: s = StringUtil.getMaxSubstring("Dent1", 4, null);
479: assertEquals("Truncated", "Dent", s);
480:
481: s = StringUtil.getMaxSubstring("Den", 4, null);
482: assertEquals("Truncated", "Den", s);
483:
484: s = StringUtil.getMaxSubstring("Beeblebrox", 5, null);
485: assertEquals("Truncated", "Beebl", s);
486:
487: s = StringUtil.getMaxSubstring("Beeblebrox", 5, "...");
488: assertEquals("Truncated", "Beebl...", s);
489:
490: }
491:
492: public void testTrimQuotes() {
493: String s = StringUtil.trimQuotes(" \"bla\" ");
494: assertEquals("bla", s);
495: s = StringUtil.trimQuotes(" \"bla ");
496: assertEquals(" \"bla ", s);
497: s = StringUtil.trimQuotes(" 'bla' ");
498: assertEquals("bla", s);
499: }
500:
501: public void testPadRight() {
502: String result = StringUtil.padRight("someStuff", 20);
503: assertEquals(20, result.length());
504: assertTrue(result.startsWith("someStuff"));
505: }
506:
507: public void testFormatNumber() {
508: String result = StringUtil.formatNumber(10, 10, true);
509: assertEquals(10, result.length());
510: assertEquals("10 ", result);
511:
512: result = StringUtil.formatNumber(10, 10, false);
513: assertEquals(10, result.length());
514: assertEquals(" 10", result);
515:
516: result = StringUtil.formatNumber(100000, 5, false);
517: assertEquals(6, result.length());
518: assertEquals("100000", result);
519:
520: }
521: }
|