KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > CharSequenceUtilitiesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import java.util.Random JavaDoc;
23 import junit.framework.TestCase;
24
25 public class CharSequenceUtilitiesTest extends TestCase {
26
27     private static final int CHARS_LENGTH = 1000;
28     private static final int SUBSTR_LENGTH = 100;
29     private static final Random JavaDoc rnd = new Random JavaDoc(0);
30
31     public CharSequenceUtilitiesTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     public void testCharSequence() {
36         char[] chars = new char[CHARS_LENGTH];
37         char[] chars_2 = new char[CHARS_LENGTH];
38         generateChars(chars);
39         generateChars(chars_2);
40         String JavaDoc string = new String JavaDoc(chars);
41         String JavaDoc string_2 = new String JavaDoc(chars_2);
42         
43         // textEquals
44
assertTrue(CharSequenceUtilities.textEquals(string, string));
45         String JavaDoc s = new String JavaDoc(chars);
46         assertTrue(CharSequenceUtilities.textEquals(string, s));
47         assertTrue(CharSequenceUtilities.textEquals(string, string_2) == string.equals(string_2));
48         
49         // toString
50
assertTrue(CharSequenceUtilities.toString(string).equals(string));
51         
52         try {
53             CharSequenceUtilities.toString(string, -1, 0);
54             assertTrue(false);
55         } catch (IndexOutOfBoundsException JavaDoc e) {
56         }
57         try {
58             CharSequenceUtilities.toString(string, 0, CHARS_LENGTH + 1);
59             assertTrue(false);
60         } catch (IndexOutOfBoundsException JavaDoc e) {
61         }
62         try {
63             CharSequenceUtilities.toString(string, CHARS_LENGTH, 0);
64             assertTrue(false);
65         } catch (IndexOutOfBoundsException JavaDoc e) {
66         }
67         int start = CHARS_LENGTH / 5;
68         int end = CHARS_LENGTH - start;
69         assertTrue(CharSequenceUtilities.toString(string, start, end).equals(string.substring(start, end)));
70         
71         // append
72
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
73         CharSequenceUtilities.append(buf, string);
74         CharSequenceUtilities.append(buf, string_2);
75         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
76         buff.append(string);
77         buff.append(string_2);
78         assertTrue(buff.toString().equals(buf.toString()));
79         
80         buf = new StringBuffer JavaDoc();
81         CharSequenceUtilities.append(buf, string, start, end);
82         assertTrue(buf.toString().equals(string.substring(start, end)));
83         
84         // indexOf
85
char ch = string.charAt(start);
86         assertTrue(string.indexOf(ch) == CharSequenceUtilities.indexOf(string, ch));
87         assertTrue(string.indexOf(ch, 2 * start) == CharSequenceUtilities.indexOf(string, ch, 2 * start));
88         
89         String JavaDoc eta = string.substring(start, start + SUBSTR_LENGTH);
90         assertTrue(string.indexOf(eta) == CharSequenceUtilities.indexOf(string, eta));
91         eta = string.substring(2 * start, 2 * start + SUBSTR_LENGTH);
92         assertTrue(string.indexOf(eta, start) == CharSequenceUtilities.indexOf(string, eta, start));
93         
94         // lastIndexOf
95
assertTrue(string.lastIndexOf(ch) == CharSequenceUtilities.lastIndexOf(string, ch));
96         assertTrue(string.lastIndexOf(ch, 2 * start) == CharSequenceUtilities.lastIndexOf(string, ch, 2 * start));
97         
98         eta = string.substring(start, start + SUBSTR_LENGTH);
99         assertTrue(string.lastIndexOf(eta) == CharSequenceUtilities.lastIndexOf(string, eta));
100         eta = string.substring(2 * start, 2 * start + SUBSTR_LENGTH);
101         assertTrue(string.lastIndexOf(eta, CHARS_LENGTH) == CharSequenceUtilities.lastIndexOf(string, eta, CHARS_LENGTH));
102         
103         // trim
104
buf = new StringBuffer JavaDoc();
105         for (int x = 0; x < SUBSTR_LENGTH; x++) {
106             buf.append((char)rnd.nextInt(' ' + 1));
107         }
108         buf.append(string);
109         for (int x = 0; x < SUBSTR_LENGTH; x++) {
110             buf.append((char)rnd.nextInt(' ' + 1));
111         }
112         assertTrue(CharSequenceUtilities.textEquals(string, CharSequenceUtilities.trim(buf.toString())));
113         
114         // startsWith
115
assertTrue(CharSequenceUtilities.startsWith(string, string.substring(0, SUBSTR_LENGTH)));
116         
117         // endsWith
118
assertTrue(CharSequenceUtilities.endsWith(string, string.substring(CHARS_LENGTH - SUBSTR_LENGTH)));
119     }
120     
121     public void generateChars(char[] chars) {
122         for (int x = 0; x < chars.length; x++) {
123             chars[x] = (char) rnd.nextInt();
124         }
125     }
126     
127 }
128
Popular Tags