KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > StringUtilsTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.util;
17
18 import scriptella.AbstractTestCase;
19
20 import java.util.Random JavaDoc;
21
22 /**
23  * Tests for{@link StringUtils}.
24  *
25  * @author Fyodor Kupolov
26  * @version 1.0
27  */

28 public class StringUtilsTest extends AbstractTestCase {
29     public void testIsEmpty() {
30         assertTrue(StringUtils.isEmpty(null));
31         assertTrue(StringUtils.isEmpty(""));
32         assertFalse(StringUtils.isEmpty(" test"));
33     }
34
35     public void testIsAsciiWhitespacesOnly() {
36         assertTrue(StringUtils.isAsciiWhitespacesOnly(""));
37         assertTrue(StringUtils.isAsciiWhitespacesOnly(null));
38         assertTrue(StringUtils.isAsciiWhitespacesOnly(" "));
39         assertTrue(StringUtils.isAsciiWhitespacesOnly(" \t\r \n "));
40         assertTrue(StringUtils.isAsciiWhitespacesOnly("\n"));
41         assertFalse(StringUtils.isAsciiWhitespacesOnly(" 1 "));
42         assertFalse(StringUtils.isAsciiWhitespacesOnly(" abc "));
43         assertFalse(StringUtils.isAsciiWhitespacesOnly("-----"));
44     }
45
46     public void testIsDecimalInteger() {
47         assertTrue(StringUtils.isDecimalInt("123"));
48         assertFalse(StringUtils.isDecimalInt("")); //empty string is not a number
49
assertFalse(StringUtils.isDecimalInt(null)); //nulls also
50
assertTrue(StringUtils.isDecimalInt("0"));
51         assertTrue(StringUtils.isDecimalInt("01"));
52         assertFalse(StringUtils.isDecimalInt("01a"));
53         assertFalse(StringUtils.isDecimalInt("-1")); //negatives are not supported
54

55         for (int i=0;i<1000;i++) {
56             assertTrue("i="+i, StringUtils.isDecimalInt(String.valueOf(i)));
57         }
58         Random JavaDoc rnd = new Random JavaDoc(0); //use constant seed to simplify reproducing
59
for (int i=0;i<1000;i++) {
60             int n = rnd.nextInt(Integer.MAX_VALUE);
61             assertTrue("n="+n, StringUtils.isDecimalInt(String.valueOf(n)));
62         }
63
64     }
65
66     public void testConsoleFormat() {
67         String JavaDoc sep = System.getProperty("line.separator");
68         String JavaDoc test = " \u0000 test\r\n line2\r line3 ";
69         assertEquals("test"+sep+" line2"+sep+" line3", StringUtils.consoleFormat(test));
70     }
71 }
72
Popular Tags