Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
315   618   49   10.16
2   454   0.16   31
31     1.58  
1    
 
  StringUtilityTest       Line # 33 315 49 95.1% 0.9511494
 
  (26)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.utility;
17   
18    import java.io.IOException;
19    import java.util.HashSet;
20    import java.util.Map;
21    import java.util.Set;
22    import java.util.TreeMap;
23    import java.util.TreeSet;
24   
25    import org.qedeq.base.test.QedeqTestCase;
26   
27    /**
28    * Test {@link StringUtility}.
29    *
30    * @version $Revision: 1.1 $
31    * @author Michael Meyling
32    */
 
33    public class StringUtilityTest extends QedeqTestCase {
34   
35    /*
36    * @see TestCase#setUp()
37    */
 
38  26 toggle protected void setUp() throws Exception {
39  26 super.setUp();
40    }
41   
42    /*
43    * @see TestCase#tearDown()
44    */
 
45  26 toggle protected void tearDown() throws Exception {
46  26 super.tearDown();
47    }
48   
49    /**
50    * Test replace(String, String, String).
51    *
52    * @throws Exception
53    */
 
54  1 toggle public void testReplace() throws Exception {
55  1 assertEquals("", StringUtility.replace("", "12345", "89"));
56  1 assertEquals("", StringUtility.replace((String) null, "12345", "89"));
57  1 assertEquals("", StringUtility.replace((String) null, null, null));
58  1 assertEquals("", StringUtility.replace("", null, "89"));
59  1 assertEquals("", StringUtility.replace("", "2", null));
60  1 assertEquals("", StringUtility.replace("", "", null));
61  1 assertEquals("", StringUtility.replace("", "", ""));
62  1 assertEquals("", StringUtility.replace("", null, ""));
63  1 assertEquals("", StringUtility.replace((String) null, "", ""));
64  1 assertEquals("", StringUtility.replace("", "1", "1"));
65  1 assertEquals("", StringUtility.replace("", "12", "12"));
66  1 assertEquals("1", StringUtility.replace("1", "1", "1"));
67  1 assertEquals("12", StringUtility.replace("12", "12", "12"));
68  1 assertEquals("", StringUtility.replace("1", "1", ""));
69  1 assertEquals("", StringUtility.replace("12", "12", ""));
70  1 assertEquals("", StringUtility.replace("1", "1", null));
71  1 assertEquals("", StringUtility.replace("12", "12", null));
72  1 assertEquals("13", StringUtility.replace("12", "2", "3"));
73  1 assertEquals("1", StringUtility.replace("12", "2", null));
74  1 assertEquals("12", StringUtility.replace("12", "", "7"));
75  1 assertEquals("12", StringUtility.replace("12", null, "7"));
76  1 assertEquals("145", StringUtility.replace("12345", "23", null));
77  1 assertEquals("12345", StringUtility.replace("12345", "23", "23"));
78  1 assertEquals("12AA12AA12", StringUtility.replace("12012012", "0", "AA"));
79  1 assertEquals("AA12AA12AA12AA", StringUtility.replace("0120120120", "0", "AA"));
80  1 assertEquals("AA12ABA12AA12AA", StringUtility.replace("012ABA120120", "0", "AA"));
81  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", "", "AA"));
82  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", null, "AA"));
83  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", null, null));
84  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", "", "AA"));
85  1 assertEquals("12ABA1212", StringUtility.replace("012ABA120120", "0", null));
86  1 assertEquals("12ABA1212", StringUtility.replace("012ABA120120", "0", ""));
87  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", "", ""));
88  1 assertEquals("A3A2A3A2A3A20", StringUtility.replace("0120120120", "01", "A3A"));
89  1 assertEquals("0ABA1200", StringUtility.replace("012ABA120120", "012", "0"));
90  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", "", "012"));
91  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", null, "012"));
92  1 assertEquals("", StringUtility.replace("012ABA120120", "012ABA120120", ""));
93  1 assertEquals("", StringUtility.replace("012ABA120120", "012ABA120120", null));
94  1 assertEquals("012ABA120120", StringUtility.replace("012ABA120120", "012ABA120120",
95    "012ABA120120"));
96    }
97   
98    /**
99    * Test replace(String, String, String).
100    *
101    * @throws Exception
102    */
 
103  1 toggle public void testReplaceStringBuffer() throws Exception {
104  1 StringBuffer buffer = new StringBuffer();
105  1 doReplace(buffer, "", "", "12345", "89");
106  1 doReplace(buffer, "", "", null, null);
107  1 doReplace(buffer, "", "", null, "89");
108  1 doReplace(buffer, "", "", "2", null);
109  1 doReplace(buffer, "", "", "", null);
110  1 doReplace(buffer, "", "", "", "");
111  1 doReplace(buffer, "", "", "1", "1");
112  1 doReplace(buffer, "", "", "12", "12");
113  1 doReplace(buffer, "1", "1", "1", "1");
114  1 doReplace(buffer, "12", "12", "12", "12");
115  1 doReplace(buffer, "", "1", "1", "");
116  1 doReplace(buffer, "", "12", "12", "");
117  1 doReplace(buffer, "", "1", "1", null);
118  1 doReplace(buffer, "", "12", "12", null);
119  1 doReplace(buffer, "13", "12", "2", "3");
120  1 doReplace(buffer, "1", "12", "2", null);
121  1 doReplace(buffer, "12", "12", "", "7");
122  1 doReplace(buffer, "12", "12", null, "7");
123  1 doReplace(buffer, "145", "12345", "23", null);
124  1 doReplace(buffer, "12345", "12345", "23", "23");
125  1 doReplace(buffer, "12AA12AA12", "12012012", "0", "AA");
126  1 doReplace(buffer, "AA12AA12AA12AA", "0120120120", "0", "AA");
127  1 doReplace(buffer, "AA12ABA12AA12AA", "012ABA120120", "0", "AA");
128  1 doReplace(buffer, "012ABA120120", "012ABA120120", "", "AA");
129  1 doReplace(buffer, "012ABA120120", "012ABA120120", null, "AA");
130  1 doReplace(buffer, "012ABA120120", "012ABA120120", null, null);
131  1 doReplace(buffer, "012ABA120120", "012ABA120120", "", "AA");
132  1 doReplace(buffer, "12ABA1212", "012ABA120120", "0", null);
133  1 doReplace(buffer, "12ABA1212", "012ABA120120", "0", "");
134  1 doReplace(buffer, "012ABA120120", "012ABA120120", "", "");
135  1 doReplace(buffer, "A3A2A3A2A3A20", "0120120120", "01", "A3A");
136  1 doReplace(buffer, "0ABA1200", "012ABA120120", "012", "0");
137  1 doReplace(buffer, "012ABA120120", "012ABA120120", "", "012");
138  1 doReplace(buffer, "012ABA120120", "012ABA120120", null, "012");
139  1 doReplace(buffer, "", "012ABA120120", "012ABA120120", "");
140  1 doReplace(buffer, "", "012ABA120120", "012ABA120120", null);
141  1 doReplace(buffer, "012ABA120120", "012ABA120120", "012ABA120120",
142    "012ABA120120");
143  1 try {
144  1 doReplace(null, "AA12ABA12AA12AA", "012ABA120120", "0", "AA");
145  0 fail("NullPointerException expected");
146    } catch (NullPointerException e) {
147    // expected;
148    }
149    }
150   
 
151  38 toggle private void doReplace(final StringBuffer buffer, final String expected, final String text,
152    final String search, final String replacement) {
153  38 buffer.setLength(0);
154  37 buffer.append(text);
155  37 StringUtility.replace(buffer, search, replacement);
156  37 assertEquals(expected, buffer.toString());
157    }
158   
 
159  1 toggle public void testToStringObjectArray() throws Exception {
160  1 assertEquals("(\"a\", 1, null, \"gu\")", StringUtility.toString(new Object[] {
161    "a", new Integer(1), null, "gu"}));
162  1 assertEquals("()", StringUtility.toString((Object[]) null));
163    }
164   
 
165  1 toggle public void testToStringSet() throws Exception {
166  1 final TreeSet set = new TreeSet();
167  1 set.add("a");
168  1 set.add("1");
169  1 set.add("");
170  1 set.add("gu");
171  1 assertEquals("{\"\", \"1\", \"a\", \"gu\"}", StringUtility.toString(set));
172  1 assertEquals("{}", StringUtility.toString((Set) null));
173  1 final HashSet set2 = new HashSet();
174  1 set2.add("a");
175  1 set2.add(new Integer(1));
176  1 set2.add(null);
177  1 set2.add("gu");
178  1 final String result = StringUtility.toString(set2);
179  1 assertTrue(result.indexOf("null") >= 0);
180  1 assertTrue(result.indexOf("1") >= 0);
181  1 assertTrue(result.indexOf("\"a\"") >= 0);
182  1 assertTrue(result.indexOf("\"gu\"") >= 0);
183    }
184   
 
185  1 toggle public void testToStringMap() throws Exception {
186  1 final TreeMap map = new TreeMap();
187  1 map.put("v1", "a");
188  1 map.put("v2", new Integer(1));
189  1 map.put("v4", null);
190  1 map.put("v5", "gu");
191  1 assertEquals("{v1=\"a\", v2=1, v4=null, v5=\"gu\"}", StringUtility.toString(map));
192  1 assertEquals("{}", StringUtility.toString((Map) null));
193    }
194   
 
195  1 toggle public void testAsLines() throws Exception {
196  1 final HashSet set = new HashSet();
197  1 set.add("a");
198  1 set.add(new Integer(1));
199  1 set.add(null);
200  1 set.add("gu");
201  1 final String[] array = StringUtility.split(StringUtility.asLines(set), "\n");
202  1 assertTrue(StringUtility.isIn("null", array));
203  1 assertTrue(StringUtility.isIn("1", array));
204  1 assertTrue(StringUtility.isIn("a", array));
205  1 assertTrue(StringUtility.isIn("gu", array));
206  1 assertEquals("", StringUtility.asLines((Set) null));
207    }
208   
209    /**
210    * Test {@link StringUtility#quote(String)}.
211    *
212    * @throws Exception Test failed.
213    */
 
214  1 toggle public void testQuote() throws Exception {
215  1 assertEquals("\"\"", StringUtility.quote(""));
216  1 assertEquals("\"\"\"\"", StringUtility.quote("\""));
217  1 assertEquals("\"a\"", StringUtility.quote("a"));
218  1 assertEquals("\"\"\"a\"\"\"", StringUtility.quote("\"a\""));
219  1 assertEquals("\"b\"\"a\"\"c\"", StringUtility.quote("b\"a\"c"));
220  1 try {
221  1 StringUtility.quote(null);
222  0 fail("NullPointerException expected");
223    } catch (NullPointerException e) {
224    // expected
225    }
226    }
227   
228    /**
229    * Test {@link StringUtility#isLetterDigitString(String)}.
230    *
231    * @throws Exception Test failed.
232    */
 
233  1 toggle public void testIsLetterDigitString() throws Exception {
234  1 assertFalse(StringUtility.isLetterDigitString(""));
235  1 assertTrue(StringUtility.isLetterDigitString("a"));
236  1 assertFalse(StringUtility.isLetterDigitString("1a"));
237  1 assertTrue(StringUtility.isLetterDigitString("a1"));
238  1 assertFalse(StringUtility.isLetterDigitString(" 1"));
239  1 assertFalse(StringUtility.isLetterDigitString("a 1"));
240  1 assertTrue(StringUtility.isLetterDigitString("AllOneTwo3"));
241  1 assertTrue(StringUtility.isLetterDigitString("Z111111999999999"));
242  1 assertFalse(StringUtility.isLetterDigitString("$A111111999999999"));
243  1 try {
244  1 StringUtility.isLetterDigitString(null);
245  0 fail("NullPointerException expected");
246    } catch (NullPointerException e) {
247    // expected
248    }
249    }
250   
251    /**
252    * Test {@link StringUtility#getSpaces(int))}.
253    *
254    * @throws Exception Test failed.
255    */
 
256  1 toggle public void testGetSpaces() throws Exception {
257  1 eq("", StringUtility.getSpaces(0));
258  1 eq(" ", StringUtility.getSpaces(1));
259  1 eq(" ", StringUtility.getSpaces(2));
260  1 eq(" ", StringUtility.getSpaces(3));
261  1 eq(" ", StringUtility.getSpaces(4));
262  1 eq(" ", StringUtility.getSpaces(5));
263  1 eq(" ", StringUtility.getSpaces(6));
264  1 eq(" ", StringUtility.getSpaces(7));
265  1 eq(" ", StringUtility.getSpaces(8));
266  1 eq(" ", StringUtility.getSpaces(9));
267  1 eq(" ",
268    StringUtility.getSpaces(80));
269  1 eq("", StringUtility.getSpaces(-1));
270  1 eq("", StringUtility.getSpaces(-999999));
271    }
272   
 
273  13 toggle public void eq(final String expected, final StringBuffer spaces) throws Exception {
274  13 assertEquals(expected.length(), spaces.length());
275  13 assertEquals(expected, spaces.toString());
276    }
277   
278    /**
279    * Test {@link StringUtility#getClassName(Class)}.
280    *
281    * @throws Exception Test failed.
282    */
 
283  1 toggle public void testGetClassName() throws Exception {
284  1 assertEquals("IOException", StringUtility.getClassName(IOException.class));
285    }
286   
287    /**
288    * Test {@link StringUtility#deleteLineLeadingWhitespace(StringBuffer)}.
289    *
290    * @throws Exception Test failed.
291    */
 
292  1 toggle public void testDeleteLineLeadingWhitespace() throws Exception {
293  1 eq("", "");
294  1 eq("A", "A");
295  1 eq(" A", "A");
296  1 eq("\tA", "A");
297  1 String muffin1 = " Do you know the muffin man,\n"
298    + " The muffin man, the muffin man,\n"
299    + " Do you know the muffin man,\n"
300    + " Who lives on Drury Lane?\n";
301  1 String muffin2 = "Do you know the muffin man,\n"
302    + "The muffin man, the muffin man,\n"
303    + "Do you know the muffin man,\n"
304    + "Who lives on Drury Lane?\n";
305  1 String muffin3 = "Do you know the muffin man,\n"
306    + " The muffin man, the muffin man,\n"
307    + " Do you know the muffin man,\n"
308    + " Who lives on Drury Lane?\n";
309  1 String muffin4 = "Do you know the muffin man,\n"
310    + " The muffin man, the muffin man,\n"
311    + " Do you know the muffin man,\n"
312    + " Who lives on Drury Lane?\n";
313  1 eq(muffin1, muffin2);
314  1 eq("\n" + muffin1, "\n" + muffin2);
315  1 eq("\n" + muffin1 + "\n", "\n" + muffin2 + "\n");
316  1 eq("\n " + muffin1 + "\n", "\n" + muffin3 + "\n");
317   
318  1 eq(muffin1.substring(1), muffin4);
319  1 eq("\n\t\n" + muffin1 + "\n", "\n\t\n" + muffin2 + "\n");
320  1 eq("\015\012" + muffin1, "\015\012" + muffin2);
321  1 eq("\015\012" + " Hello Again" + "\015\012" + " Said the Knight" + "\015\012",
322    "\015\012" + "Hello Again" + "\015\012" + "Said the Knight" + "\015\012");
323    }
324   
 
325  12 toggle public void eq(final String input, final String expected) throws Exception {
326  12 final StringBuffer buffer = new StringBuffer(input);
327  12 StringUtility.deleteLineLeadingWhitespace(buffer);
328  12 assertEquals(expected, buffer.toString());
329    }
330   
331    /**
332    * Test {@link StringUtility#escapeProperty(String)}.
333    *
334    * @throws Exception Test failed.
335    */
 
336  1 toggle public void testEscapeProperty() throws Exception {
337  1 assertEquals("\\u00E4\\u00FC\\u00F6\\u00DF\\u20AC\\u00C4\\u00DC\\u00D6\\u00B3",
338    StringUtility.escapeProperty("\u00E4\u00FC\u00F6\u00DF\u20AC\u00C4\u00DC\u00D6\u00B3"));
339    }
340   
 
341  1 toggle public void testAlignRightLongInt() throws Exception {
342  1 try {
343  1 StringUtility.alignRight(13l, 0);
344  0 fail("Exception expected");
345    } catch (RuntimeException e) {
346    // expected
347    }
348  1 try {
349  1 StringUtility.alignRight(13l, -10);
350  0 fail("Exception expected");
351    } catch (RuntimeException e) {
352    // expected
353    }
354  1 assertEquals("13", StringUtility.alignRight(13l, 2));
355  1 assertEquals("13", StringUtility.alignRight(13l, 1));
356  1 assertEquals(" 13", StringUtility.alignRight(13l, 20));
357  1 try {
358  1 StringUtility.alignRight(13l, 21);
359  0 fail("Exception expected");
360    } catch (RuntimeException e) {
361    // expected
362    }
363    }
364   
 
365  1 toggle public void testAlignRightStringInt() throws Exception {
366  1 try {
367  1 StringUtility.alignRight("13", 0);
368  0 fail("Exception expected");
369    } catch (RuntimeException e) {
370    // expected
371    }
372  1 try {
373  1 StringUtility.alignRight("13", -10);
374  0 fail("Exception expected");
375    } catch (RuntimeException e) {
376    // expected
377    }
378  1 assertEquals("13", StringUtility.alignRight("13", 2));
379  1 assertEquals("13", StringUtility.alignRight("13", 1));
380  1 assertEquals(" 13", StringUtility.alignRight("13", 20));
381  1 try {
382  1 StringUtility.alignRight("13", 21);
383  0 fail("Exception expected");
384    } catch (RuntimeException e) {
385    // expected
386    }
387    }
388   
389    /**
390    * Test {@link StringUtility#decodeXmlMarkup(StringBuffer)}.
391    *
392    * @throws Exception Test failed.
393    */
 
394  1 toggle public void testDecodeXmlMarkup() throws Exception {
395  1 final String test1 = "&amp;&lt;&gt;";
396  1 final StringBuffer buffer = new StringBuffer(test1);
397  1 assertEquals("&<>", StringUtility.unescapeXml(buffer.toString()));
398  1 final String test2 = "&#x3C;";
399  1 final StringBuffer buffer2 = new StringBuffer(test2);
400  1 assertEquals("<", StringUtility.unescapeXml(buffer2.toString()));
401    }
402   
403    /**
404    * Test {@link StringUtility#hex2String(String).
405    *
406    * @throws Exception Test failed.
407    */
 
408  1 toggle public void testHex2String() throws Exception {
409  1 assertEquals("A", StringUtility.hex2String("41"));
410  1 assertEquals(" ", StringUtility.hex2String("20"));
411  1 assertEquals("0", StringUtility.hex2String("30"));
412  1 assertEquals("\015\012", StringUtility.hex2String("0D 0A"));
413  1 assertEquals("A", StringUtility.hex2String("41 "));
414  1 assertEquals(" ", StringUtility.hex2String("20 "));
415  1 assertEquals("0", StringUtility.hex2String("30 "));
416  1 assertEquals("\015\012", StringUtility.hex2String("0D 0A "));
417  1 assertEquals("A", StringUtility.hex2String(" 41"));
418  1 assertEquals(" ", StringUtility.hex2String(" 20"));
419  1 assertEquals("0", StringUtility.hex2String(" 30"));
420  1 assertEquals("\015\012", StringUtility.hex2String(" 0D 0A"));
421  1 assertEquals("A", StringUtility.hex2String("4 1"));
422  1 assertEquals(" ", StringUtility.hex2String("2 0"));
423  1 assertEquals("0", StringUtility.hex2String("3 0"));
424  1 assertEquals("\015\012", StringUtility.hex2String("0 D 0 A"));
425  1 assertEquals("A", StringUtility.hex2String("4\n1"));
426  1 assertEquals(" ", StringUtility.hex2String("\t2 \r0\n"));
427  1 assertEquals("0", StringUtility.hex2String("3 0"));
428  1 assertEquals("\015\012", StringUtility.hex2String(" 0 D0A"));
429  1 assertEquals("\352", StringUtility.hex2String("EA", "ISO-8859-1"));
430  1 assertEquals("\352", StringUtility.hex2String("ea", "ISO-8859-1"));
431  1 assertEquals("\352", StringUtility.hex2String("e A ", "ISO-8859-1"));
432  1 assertEquals("\000", StringUtility.hex2String("00"));
433   
434    // wrong padding
435  1 try {
436  1 StringUtility.hex2String("0");
437  0 fail("wrong number format exception expected");
438    } catch (IllegalArgumentException e) {
439    // expected
440    }
441  1 try {
442  1 StringUtility.hex2String("A01");
443  0 fail("wrong number format exception expected");
444    } catch (IllegalArgumentException e) {
445    // expected
446    }
447   
448    // wrong character
449  1 try {
450  1 StringUtility.hex2String("a+");
451  0 fail("wrong number format exception expected");
452    } catch (IllegalArgumentException e) {
453    // expected
454    }
455  1 try {
456  1 StringUtility.hex2String("!+");
457  0 fail("wrong number format exception expected");
458    } catch (IllegalArgumentException e) {
459    // expected
460    }
461  1 try {
462  1 StringUtility.hex2String("ZZ");
463  0 fail("wrong number format exception expected");
464    } catch (IllegalArgumentException e) {
465    // expected
466    }
467    }
468   
469    /**
470    * Test {@link StringUtility#string2Hex(String).
471    *
472    * @throws Exception Test failed.
473    */
 
474  1 toggle public void testString2Hex() throws Exception {
475  1 assertEquals("41", StringUtility.string2Hex("A"));
476  1 assertEquals("20", StringUtility.string2Hex(" "));
477  1 assertEquals("0D 0A", StringUtility.string2Hex("\015\012"));
478  1 assertEquals("EA", StringUtility.string2Hex("\352", "ISO-8859-1"));
479  1 assertEquals("0D 0A 0D 0A 0D 0A 0D 0A 0D 0A",
480    StringUtility.string2Hex("\015\012\015\012\015\012\015\012\015\012"));
481  1 assertEquals("0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 20 20 20 20 20 20",
482    StringUtility.string2Hex(
483    "\015\012\015\012\015\012\015\012\015\012\040\040\040\040\040\040"));
484  1 assertEquals("0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 20 20 20 20 20 20\n41",
485    StringUtility.string2Hex(
486    "\015\012\015\012\015\012\015\012\015\012\040\040\040\040\040\040A"));
487    }
488   
489    /**
490    * Test {@link StringUtility#string2Hex(String) and {@link StringUtility#hex2String(String)}.
491    *
492    * @throws Exception Test failed.
493    */
 
494  1 toggle public void testString2HexAndBack() throws Exception {
495  1 final String first = "All my Ducks in a Row!!!$%&<>/\\?";
496  1 assertEquals(first, StringUtility.hex2String(StringUtility.string2Hex(first)));
497  1 final String second = "41 42 43";
498  1 assertEquals(second, StringUtility.string2Hex(StringUtility.hex2String(second)));
499  1 StringBuffer third = new StringBuffer(256);
500  257 for (int i = 0; i < 256; i++) {
501  256 third.append((char) i);
502    }
503  1 assertEquals(third.toString(),
504    StringUtility.hex2String(StringUtility.string2Hex(third.toString())));
505  1 assertEquals(StringUtility.string2Hex(third.toString()),
506    StringUtility.string2Hex(StringUtility.hex2String(StringUtility.string2Hex(
507    third.toString()))));
508    }
509   
510    /**
511    * Test {@link StringUtility#format(long, int)}.
512    *
513    * @throws Exception Test failed.
514    */
 
515  1 toggle public void testFormat() throws Exception {
516  1 assertEquals("0", StringUtility.format(0, 1));
517  1 try {
518  1 assertEquals("0", StringUtility.format(0, 0));
519  0 fail("IllegalArgumentException expected");
520    } catch (IllegalArgumentException e) {
521    // expected
522    }
523  1 assertEquals("00123456", StringUtility.format(123456, 8));
524  1 assertEquals("123456", StringUtility.format(123456, 6));
525  1 assertEquals("123456", StringUtility.format(123456, 5));
526  1 assertEquals("00000000000000123456", StringUtility.format(123456, 20));
527  1 try {
528  1 assertEquals("00000000000000000000000123456", StringUtility.format(123456, 30));
529  0 fail("IllegalArgumentException expected");
530    } catch (IllegalArgumentException e) {
531    // expected
532    }
533  1 try {
534  1 assertEquals("000000000000000123456", StringUtility.format(123456, 21));
535  0 fail("IllegalArgumentException expected");
536    } catch (IllegalArgumentException e) {
537    // expected
538    }
539    }
540   
541    /**
542    * Test {@link StringUtility#getSystemLineSeparator()}.
543    *
544    * @throws Exception Test failed.
545    */
 
546  1 toggle public void testGetSystemLineSeparator() throws Exception {
547  1 assertEquals(System.getProperty("line.separator"),
548    StringUtility.getSystemLineSeparator());
549    }
550   
551    /**
552    * Test {@link StringUtility#useSystemLineSeparator(String)}.
553    *
554    * @throws Exception Test failed.
555    */
 
556  1 toggle public void testAddSystemLineSeparator() throws Exception {
557  1 assertEquals(null, StringUtility.useSystemLineSeparator(null));
558  1 assertEquals("" + System.getProperty("line.separator"),
559    StringUtility.useSystemLineSeparator("\015\012"));
560  1 assertEquals("asdfghj" + System.getProperty("line.separator"),
561    StringUtility.useSystemLineSeparator("asdfghj\015"));
562    }
563   
564    /**
565    * Test {@link StringUtility#substring(String, int, int)}.
566    *
567    * @throws Exception Test failed.
568    */
 
569  1 toggle public void testSubstring() throws Exception {
570  1 assertEquals("", StringUtility.substring("1234", 0, 0));
571  1 assertEquals("", StringUtility.substring("1234", -3, 3));
572  1 assertEquals("1", StringUtility.substring("1234", -3, 4));
573  1 assertEquals("", StringUtility.substring("1234", 4, 1));
574  1 assertEquals("4", StringUtility.substring("1234", 3, 1));
575  1 assertEquals("4", StringUtility.substring("1234", 3, 100));
576  1 assertEquals("1234", StringUtility.substring("1234", 0, 100));
577  1 assertEquals("1234", StringUtility.substring("1234", -50, 100));
578  1 assertEquals("", StringUtility.substring("1234", -500, 100));
579  1 assertEquals("23", StringUtility.substring("1234", 1, 2));
580  1 assertEquals("34", StringUtility.substring("1234", 2, 2));
581  1 assertEquals("34", StringUtility.substring("1234", 2, 3));
582    }
583   
 
584  1 toggle public void testEscapeXml() throws Exception {
585  1 assertEquals("&quot;bread&quot; &amp; &quot;butter&quot;",
586    StringUtility.escapeXml("\"bread\" & \"butter\""));
587    }
588   
 
589  1 toggle public void testUnEscapeXml() throws Exception {
590  1 assertEquals("\"bread\" & \"butter\"",
591    StringUtility.unescapeXml("&quot;bread&quot; &amp; &quot;butter&quot;"));
592    }
593   
 
594  1 toggle public void testIsNotIn() throws Exception {
595  1 assertTrue(StringUtility.isNotIn("12", new String[] { "11", "14", "13"}));
596  1 assertTrue(StringUtility.isNotIn("12", new String[] { "11", "14", null}));
597  1 assertFalse(StringUtility.isNotIn("12", new String[] { "12", "14", null}));
598  1 assertFalse(StringUtility.isNotIn("12", new String[] { null, "14", "12"}));
599  1 assertFalse(StringUtility.isNotIn("", new String[] { null, "14", "12"}));
600  1 assertFalse(StringUtility.isNotIn("", new String[] { "12", "14", "12"}));
601  1 assertFalse(StringUtility.isNotIn("", new String[] { "12", "", "12"}));
602  1 assertFalse(StringUtility.isNotIn(null, new String[] { null, "14", "12"}));
603  1 assertFalse(StringUtility.isNotIn(null, new String[] { "12", "14", "12"}));
604    }
605   
 
606  1 toggle public void testIsIn() throws Exception {
607  1 assertFalse(StringUtility.isIn("12", new String[] { "11", "14", "13"}));
608  1 assertFalse(StringUtility.isIn("12", new String[] { "11", "14", null}));
609  1 assertTrue(StringUtility.isIn("12", new String[] { "12", "14", null}));
610  1 assertTrue(StringUtility.isIn("12", new String[] { null, "14", "12"}));
611  1 assertTrue(StringUtility.isIn("", new String[] { null, "14", "12"}));
612  1 assertTrue(StringUtility.isIn("", new String[] { "12", "14", "12"}));
613  1 assertTrue(StringUtility.isIn("", new String[] { "12", "", "12"}));
614  1 assertTrue(StringUtility.isIn(null, new String[] { null, "14", "12"}));
615  1 assertTrue(StringUtility.isIn(null, new String[] { "12", "14", "12"}));
616    }
617   
618    }