KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > util > test > StringUtilTestCase


1 /*
2  * Copyright The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.util.test;
9
10 import junit.framework.TestCase;
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 import org.apache.avalon.excalibur.util.StringUtil;
14
15 /**
16  * This is used to test StringUtil for correctness.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public final class StringUtilTestCase
21     extends TestCase
22 {
23     private final static String JavaDoc S1 = "TestMe";
24     private final static String JavaDoc S2 = "TestMeTestMe";
25     private final static String JavaDoc S3 = "TestMeTestMeTestMe";
26     private final static String JavaDoc S4 = "MeeeMer";
27     private final static String JavaDoc S5 = "MeeeeMer";
28
29     private final static String JavaDoc P11 = "eT";
30     private final static String JavaDoc P21 = "eT";
31     private final static String JavaDoc P31 = "eT";
32     private final static String JavaDoc P41 = "ee";
33     private final static String JavaDoc P51 = "ee";
34
35     private final static String JavaDoc P12 = "Te";
36     private final static String JavaDoc P22 = "Te";
37     private final static String JavaDoc P32 = "Te";
38     private final static String JavaDoc P42 = "ff";
39     private final static String JavaDoc P52 = "ff";
40
41     private final static String JavaDoc SR1 = "TestMe";
42     private final static String JavaDoc SR2 = "TestMTeestMe";
43     private final static String JavaDoc SR3 = "TestMTeestMTeestMe";
44     private final static String JavaDoc SR4 = "MffeMer";
45     private final static String JavaDoc SR5 = "MffffMer";
46
47     private final static String JavaDoc ST1 = "TestMe";
48     private final static String JavaDoc ST2 = "TestMeT";
49     private final static String JavaDoc ST3 = "TestMeT";
50     private final static String JavaDoc ST4 = "MeeeMer";
51     private final static String JavaDoc ST5 = "MeeeeMe";
52
53     private final static String JavaDoc STN1 = "TestMe";
54     private final static String JavaDoc STN2 = "Test...";
55     private final static String JavaDoc STN3 = "Test...";
56     private final static String JavaDoc STN4 = "MeeeMer";
57     private final static String JavaDoc STN5 = "Meee...";
58
59     private final static String JavaDoc[] SS1 = new String JavaDoc[] { "T", "stM" };
60     private final static String JavaDoc[] SS2 = new String JavaDoc[] { "T", "stM", "T", "stM" };
61     private final static String JavaDoc[] SS3 = new String JavaDoc[] { "T", "stM", "T", "stM", "T", "stM" };
62     private final static String JavaDoc[] SS4 = new String JavaDoc[] { "M", "M", "r" };
63     private final static String JavaDoc[] SS5 = new String JavaDoc[] { "M", "M", "r" };
64
65     private final static String JavaDoc SP = " ";
66
67     private final static String JavaDoc WU1 = S1 + SP + S2 + SP + S3 + SP + S4 + SP + S5;
68     private final static String JavaDoc WW1 =
69         "TestMe\n" +
70         "TestMeTe\n" +
71         "stMe\n" +
72         "TestMeTe\n" +
73         "stMeTest\n" +
74         "Me\n" +
75         "MeeeMer\n" +
76         "MeeeeMer";
77     private final static String JavaDoc WW2 =
78         "TestMe\n" +
79         "TestMeT\n" +
80         "estMe\n" +
81         "TestMeT\n" +
82         "estMeTe\n" +
83         "stMe\n" +
84         "MeeeMer\n" +
85         "MeeeeMe\n" +
86         "r";
87
88     public StringUtilTestCase( final String JavaDoc name )
89     {
90         super( name );
91     }
92
93     public void testReplaceSubString()
94         throws Exception JavaDoc
95     {
96         final String JavaDoc result1 = StringUtil.replaceSubString( S1, P11, P12 );
97         final String JavaDoc result2 = StringUtil.replaceSubString( S2, P21, P22 );
98         final String JavaDoc result3 = StringUtil.replaceSubString( S3, P31, P32 );
99         final String JavaDoc result4 = StringUtil.replaceSubString( S4, P41, P42 );
100         final String JavaDoc result5 = StringUtil.replaceSubString( S5, P51, P52 );
101
102         assertEquals( "ReplaceSubString SR1", SR1, result1 );
103         assertEquals( "ReplaceSubString SR2", SR2, result2 );
104         assertEquals( "ReplaceSubString SR3", SR3, result3 );
105         assertEquals( "ReplaceSubString SR4", SR4, result4 );
106         assertEquals( "ReplaceSubString SR5", SR5, result5 );
107     }
108
109     public void testWordWrap()
110         throws Exception JavaDoc
111     {
112         assertEquals( "WordWrap S1", WW1, StringUtil.wordWrap( WU1, 8, true ) );
113         assertEquals( "WordWrap S1", WW2, StringUtil.wordWrap( WU1, 7, true ) );
114     }
115
116     public void testTruncate()
117         throws Exception JavaDoc
118     {
119         assertEquals( "Truncate S1", ST1, StringUtil.truncate( S1, 7 ) );
120         assertEquals( "Truncate S2", ST2, StringUtil.truncate( S2, 7 ) );
121         assertEquals( "Truncate S3", ST3, StringUtil.truncate( S3, 7 ) );
122         assertEquals( "Truncate S4", ST4, StringUtil.truncate( S4, 7 ) );
123         assertEquals( "Truncate S5", ST5, StringUtil.truncate( S5, 7 ) );
124     }
125
126     public void testTruncateNicely()
127         throws Exception JavaDoc
128     {
129         assertEquals( "Truncate Nicely S1", STN1, StringUtil.truncateNicely( S1, 7 ) );
130         assertEquals( "Truncate Nicely S2", STN2, StringUtil.truncateNicely( S2, 7 ) );
131         assertEquals( "Truncate Nicely S3", STN3, StringUtil.truncateNicely( S3, 7 ) );
132         assertEquals( "Truncate Nicely S4", STN4, StringUtil.truncateNicely( S4, 7 ) );
133         assertEquals( "Truncate Nicely S5", STN5, StringUtil.truncateNicely( S5, 7 ) );
134
135         assertEquals( "Truncate Nicely 1", ".", StringUtil.truncateNicely( S5, 1 ) );
136         assertEquals( "Truncate Nicely 2", "..", StringUtil.truncateNicely( S5, 2 ) );
137         assertEquals( "Truncate Nicely 3", "...", StringUtil.truncateNicely( S5, 3 ) );
138     }
139
140     public void testSplitString()
141         throws Exception JavaDoc
142     {
143         assertEqualArrays( SS1, StringUtil.split( S1, "e" ) );
144         assertEqualArrays( SS2, StringUtil.split( S2, "e" ) );
145         assertEqualArrays( SS3, StringUtil.split( S3, "e" ) );
146         assertEqualArrays( SS4, StringUtil.split( S4, "e" ) );
147         assertEqualArrays( SS5, StringUtil.split( S5, "e" ) );
148     }
149
150     public void testConcatStrings()
151         throws Exception JavaDoc
152     {
153         assertEquals( "TestMeTruly", StringUtil.concat("Test", "Me", "Truly"));
154         assertEquals( "<prefix:name/>", StringUtil.concat(new String JavaDoc[] {
155                                                             "<",
156                                                             "prefix",
157                                                             ":",
158                                                             "name",
159                                                             "/>"
160                                                           }));
161     }
162
163     private void assertEqualArrays( final String JavaDoc[] s1, final String JavaDoc[] s2 )
164     {
165         assertEquals( "Array Length Equality", s1.length, s2.length );
166         assertEquals( "Array Type Equality",
167                       s1.getClass().getComponentType(),
168                       s2.getClass().getComponentType() );
169
170         for( int i = 0; i < s1.length; i++ )
171         {
172             assertEquals( "Array Element " + i, s1[ i ], s2[ i ] );
173         }
174     }
175 }
176
Popular Tags