KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestStringUtil


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

17         
18 package org.apache.poi.util;
19
20 import junit.framework.*;
21
22 import java.text.NumberFormat JavaDoc;
23
24 /**
25  * Unit test for StringUtil
26  *
27  * @author Marc Johnson (mjohnson at apache dot org
28  * @author Glen Stampoultzis (glens at apache.org)
29  * @author Sergei Kozello (sergeikozello at mail.ru)
30  */

31 public class TestStringUtil
32         extends TestCase
33 {
34     /**
35      * Creates new TestStringUtil
36      *
37      * @param name
38      */

39     public TestStringUtil( String JavaDoc name )
40     {
41         super( name );
42     }
43
44     /**
45      * test simple form of getFromUnicode
46      */

47     public void testSimpleGetFromUnicode()
48     {
49         byte[] test_data = new byte[32];
50         int index = 0;
51
52         for ( int k = 0; k < 16; k++ )
53         {
54             test_data[index++] = (byte) 0;
55             test_data[index++] = (byte) ( 'a' + k );
56         }
57
58         assertEquals( "abcdefghijklmnop",
59                 StringUtil.getFromUnicodeBE( test_data ) );
60     }
61
62     /**
63      * test simple form of getFromUnicode with symbols with code below and more 127
64      */

65     public void testGetFromUnicodeSymbolsWithCodesMoreThan127()
66     {
67         byte[] test_data = new byte[]{0x04, 0x22,
68                                       0x04, 0x35,
69                                       0x04, 0x41,
70                                       0x04, 0x42,
71                                       0x00, 0x20,
72                                       0x00, 0x74,
73                                       0x00, 0x65,
74                                       0x00, 0x73,
75                                       0x00, 0x74,
76         };
77
78         assertEquals( "\u0422\u0435\u0441\u0442 test",
79                 StringUtil.getFromUnicodeBE( test_data ) );
80     }
81
82     /**
83      * test getFromUnicodeHigh for symbols with code below and more 127
84      */

85     public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127()
86     {
87         byte[] test_data = new byte[]{0x22, 0x04,
88                                       0x35, 0x04,
89                                       0x41, 0x04,
90                                       0x42, 0x04,
91                                       0x20, 0x00,
92                                       0x74, 0x00,
93                                       0x65, 0x00,
94                                       0x73, 0x00,
95                                       0x74, 0x00,
96         };
97
98
99         assertEquals( "\u0422\u0435\u0441\u0442 test",
100                 StringUtil.getFromUnicodeLE( test_data ) );
101     }
102
103     /**
104      * Test more complex form of getFromUnicode
105      */

106     public void testComplexGetFromUnicode()
107     {
108         byte[] test_data = new byte[32];
109         int index = 0;
110         for ( int k = 0; k < 16; k++ )
111         {
112             test_data[index++] = (byte) 0;
113             test_data[index++] = (byte) ( 'a' + k );
114         }
115         assertEquals( "abcdefghijklmno",
116                 StringUtil.getFromUnicodeBE( test_data, 0, 15 ) );
117         assertEquals( "bcdefghijklmnop",
118                 StringUtil.getFromUnicodeBE( test_data, 2, 15 ) );
119         try
120         {
121             StringUtil.getFromUnicodeBE( test_data, -1, 16 );
122             fail( "Should have caught ArrayIndexOutOfBoundsException" );
123         }
124         catch ( ArrayIndexOutOfBoundsException JavaDoc ignored )
125         {
126             // as expected
127
}
128
129         try
130         {
131             StringUtil.getFromUnicodeBE( test_data, 32, 16 );
132             fail( "Should have caught ArrayIndexOutOfBoundsException" );
133         }
134         catch ( ArrayIndexOutOfBoundsException JavaDoc ignored )
135         {
136             // as expected
137
}
138
139         try
140         {
141             StringUtil.getFromUnicodeBE( test_data, 1, 16 );
142             fail( "Should have caught IllegalArgumentException" );
143         }
144         catch ( IllegalArgumentException JavaDoc ignored )
145         {
146             // as expected
147
}
148
149         try
150         {
151             StringUtil.getFromUnicodeBE( test_data, 1, -1 );
152             fail( "Should have caught IllegalArgumentException" );
153         }
154         catch ( IllegalArgumentException JavaDoc ignored )
155         {
156             // as expected
157
}
158     }
159
160     /**
161      * Test putCompressedUnicode
162      */

163     public void testPutCompressedUnicode() throws Exception JavaDoc
164     {
165         byte[] output = new byte[100];
166         byte[] expected_output =
167                 {
168                     (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l',
169                     (byte) 'o', (byte) ' ', (byte) 'W', (byte) 'o',
170                     (byte) 'r', (byte) 'l', (byte) 'd', (byte) 0xAE
171                 };
172         String JavaDoc input = new String JavaDoc( expected_output, StringUtil.getPreferredEncoding() );
173
174         StringUtil.putCompressedUnicode( input, output, 0 );
175         for ( int j = 0; j < expected_output.length; j++ )
176         {
177             assertEquals( "testing offset " + j, expected_output[j],
178                     output[j] );
179         }
180         StringUtil.putCompressedUnicode( input, output,
181                 100 - expected_output.length );
182         for ( int j = 0; j < expected_output.length; j++ )
183         {
184             assertEquals( "testing offset " + j, expected_output[j],
185                     output[100 + j - expected_output.length] );
186         }
187         try
188         {
189             StringUtil.putCompressedUnicode( input, output,
190                     101 - expected_output.length );
191             fail( "Should have caught ArrayIndexOutOfBoundsException" );
192         }
193         catch ( ArrayIndexOutOfBoundsException JavaDoc ignored )
194         {
195             // as expected
196
}
197     }
198
199     /**
200      * Test putUncompressedUnicode
201      */

202     public void testPutUncompressedUnicode()
203     {
204         byte[] output = new byte[100];
205         String JavaDoc input = "Hello World";
206         byte[] expected_output =
207                 {
208                     (byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l',
209                     (byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0,
210                     (byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o',
211                     (byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0,
212                     (byte) 'd', (byte) 0
213                 };
214
215         StringUtil.putUnicodeLE( input, output, 0 );
216         for ( int j = 0; j < expected_output.length; j++ )
217         {
218             assertEquals( "testing offset " + j, expected_output[j],
219                     output[j] );
220         }
221         StringUtil.putUnicodeLE( input, output,
222                 100 - expected_output.length );
223         for ( int j = 0; j < expected_output.length; j++ )
224         {
225             assertEquals( "testing offset " + j, expected_output[j],
226                     output[100 + j - expected_output.length] );
227         }
228         try
229         {
230             StringUtil.putUnicodeLE( input, output,
231                     101 - expected_output.length );
232             fail( "Should have caught ArrayIndexOutOfBoundsException" );
233         }
234         catch ( ArrayIndexOutOfBoundsException JavaDoc ignored )
235         {
236             // as expected
237
}
238     }
239
240
241     public void testFormat()
242             throws Exception JavaDoc
243     {
244         assertEquals( "This is a test " + fmt( 1.2345, 2, 2 ),
245                 StringUtil.format( "This is a test %2.2", new Object JavaDoc[]
246                 {
247                     new Double JavaDoc( 1.2345 )
248                 } ) );
249         assertEquals( "This is a test " + fmt( 1.2345, -1, 3 ),
250                 StringUtil.format( "This is a test %.3", new Object JavaDoc[]
251                 {
252                     new Double JavaDoc( 1.2345 )
253                 } ) );
254         assertEquals( "This is a great test " + fmt( 1.2345, -1, 3 ),
255                 StringUtil.format( "This is a % test %.3", new Object JavaDoc[]
256                 {
257                     "great", new Double JavaDoc( 1.2345 )
258                 } ) );
259         assertEquals( "This is a test 1",
260                 StringUtil.format( "This is a test %", new Object JavaDoc[]
261                 {
262                     new Integer JavaDoc( 1 )
263                 } ) );
264         assertEquals( "This is a test 1",
265                 StringUtil.format( "This is a test %", new Object JavaDoc[]
266                 {
267                     new Integer JavaDoc( 1 ), new Integer JavaDoc( 1 )
268                 } ) );
269         assertEquals( "This is a test 1.x",
270                 StringUtil.format( "This is a test %1.x", new Object JavaDoc[]
271                 {
272                     new Integer JavaDoc( 1 )
273                 } ) );
274         assertEquals( "This is a test ?missing data?1.x",
275                 StringUtil.format( "This is a test %1.x", new Object JavaDoc[]
276                 {
277                 } ) );
278         assertEquals( "This is a test %1.x",
279                 StringUtil.format( "This is a test \\%1.x", new Object JavaDoc[]
280                 {
281                 } ) );
282     }
283
284
285     private String JavaDoc fmt( double num, int minIntDigits, int maxFracDigitis )
286     {
287         NumberFormat JavaDoc nf = NumberFormat.getInstance();
288
289         if ( minIntDigits != -1 )
290         {
291             nf.setMinimumIntegerDigits( minIntDigits );
292         }
293         if ( maxFracDigitis != -1 )
294         {
295             nf.setMaximumFractionDigits( maxFracDigitis );
296         }
297
298         return nf.format( num );
299     }
300
301
302     /**
303      * main
304      *
305      * @param ignored_args
306      */

307     public static void main( String JavaDoc[] ignored_args )
308     {
309         System.out.println( "Testing util.StringUtil functionality" );
310         junit.textui.TestRunner.run( TestStringUtil.class );
311     }
312
313     /**
314      * @see junit.framework.TestCase#setUp()
315      */

316     protected void setUp() throws Exception JavaDoc
317     {
318         super.setUp();
319
320         // System.setProperty()
321
}
322
323 }
324
325
Popular Tags