2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
19 package org
.apache
.poi
.util
;
21 import junit
.framework
.*;
23 import java
.text
.NumberFormat
;
26 * Unit test for StringUtil
28 * @author Marc Johnson (mjohnson at apache dot org
29 * @author Glen Stampoultzis (glens at apache.org)
30 * @author Sergei Kozello (sergeikozello at mail.ru)
32 public class TestStringUtil
36 * Creates new TestStringUtil
40 public TestStringUtil( String name
)
46 * test simple form of getFromUnicode
48 public void testSimpleGetFromUnicode()
50 byte[] test_data
= new byte[32];
53 for ( int k
= 0; k
< 16; k
++ )
55 test_data
[index
++] = (byte) 0;
56 test_data
[index
++] = (byte) ( 'a' + k
);
59 assertEquals( "abcdefghijklmnop",
60 StringUtil
.getFromUnicodeBE( test_data
) );
64 * test simple form of getFromUnicode with symbols with code below and more 127
66 public void testGetFromUnicodeSymbolsWithCodesMoreThan127()
68 byte[] test_data
= new byte[]{0x04, 0x22,
79 assertEquals( "\u0422\u0435\u0441\u0442 test",
80 StringUtil
.getFromUnicodeBE( test_data
) );
84 * test getFromUnicodeHigh for symbols with code below and more 127
86 public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127()
88 byte[] test_data
= new byte[]{0x22, 0x04,
100 assertEquals( "\u0422\u0435\u0441\u0442 test",
101 StringUtil
.getFromUnicodeLE( test_data
) );
105 * Test more complex form of getFromUnicode
107 public void testComplexGetFromUnicode()
109 byte[] test_data
= new byte[32];
111 for ( int k
= 0; k
< 16; k
++ )
113 test_data
[index
++] = (byte) 0;
114 test_data
[index
++] = (byte) ( 'a' + k
);
116 assertEquals( "abcdefghijklmno",
117 StringUtil
.getFromUnicodeBE( test_data
, 0, 15 ) );
118 assertEquals( "bcdefghijklmnop",
119 StringUtil
.getFromUnicodeBE( test_data
, 2, 15 ) );
122 StringUtil
.getFromUnicodeBE( test_data
, -1, 16 );
123 fail( "Should have caught ArrayIndexOutOfBoundsException" );
125 catch ( ArrayIndexOutOfBoundsException ignored
)
132 StringUtil
.getFromUnicodeBE( test_data
, 32, 16 );
133 fail( "Should have caught ArrayIndexOutOfBoundsException" );
135 catch ( ArrayIndexOutOfBoundsException ignored
)
142 StringUtil
.getFromUnicodeBE( test_data
, 1, 16 );
143 fail( "Should have caught IllegalArgumentException" );
145 catch ( IllegalArgumentException ignored
)
152 StringUtil
.getFromUnicodeBE( test_data
, 1, -1 );
153 fail( "Should have caught IllegalArgumentException" );
155 catch ( IllegalArgumentException ignored
)
162 * Test putCompressedUnicode
164 public void testPutCompressedUnicode() throws Exception
166 byte[] output
= new byte[100];
167 byte[] expected_output
=
169 (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l',
170 (byte) 'o', (byte) ' ', (byte) 'W', (byte) 'o',
171 (byte) 'r', (byte) 'l', (byte) 'd', (byte) 0xAE
173 String input
= new String( expected_output
, StringUtil
.getPreferredEncoding() );
175 StringUtil
.putCompressedUnicode( input
, output
, 0 );
176 for ( int j
= 0; j
< expected_output
.length
; j
++ )
178 assertEquals( "testing offset " + j
, expected_output
[j
],
181 StringUtil
.putCompressedUnicode( input
, output
,
182 100 - expected_output
.length
);
183 for ( int j
= 0; j
< expected_output
.length
; j
++ )
185 assertEquals( "testing offset " + j
, expected_output
[j
],
186 output
[100 + j
- expected_output
.length
] );
190 StringUtil
.putCompressedUnicode( input
, output
,
191 101 - expected_output
.length
);
192 fail( "Should have caught ArrayIndexOutOfBoundsException" );
194 catch ( ArrayIndexOutOfBoundsException ignored
)
201 * Test putUncompressedUnicode
203 public void testPutUncompressedUnicode()
205 byte[] output
= new byte[100];
206 String input
= "Hello World";
207 byte[] expected_output
=
209 (byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l',
210 (byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0,
211 (byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o',
212 (byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0,
216 StringUtil
.putUnicodeLE( input
, output
, 0 );
217 for ( int j
= 0; j
< expected_output
.length
; j
++ )
219 assertEquals( "testing offset " + j
, expected_output
[j
],
222 StringUtil
.putUnicodeLE( input
, output
,
223 100 - expected_output
.length
);
224 for ( int j
= 0; j
< expected_output
.length
; j
++ )
226 assertEquals( "testing offset " + j
, expected_output
[j
],
227 output
[100 + j
- expected_output
.length
] );
231 StringUtil
.putUnicodeLE( input
, output
,
232 101 - expected_output
.length
);
233 fail( "Should have caught ArrayIndexOutOfBoundsException" );
235 catch ( ArrayIndexOutOfBoundsException ignored
)
242 public void testFormat()
245 assertEquals( "This is a test " + fmt( 1.2345, 2, 2 ),
246 StringUtil
.format( "This is a test %2.2", new Object
[]
250 assertEquals( "This is a test " + fmt( 1.2345, -1, 3 ),
251 StringUtil
.format( "This is a test %.3", new Object
[]
255 assertEquals( "This is a great test " + fmt( 1.2345, -1, 3 ),
256 StringUtil
.format( "This is a % test %.3", new Object
[]
258 "great", new Double( 1.2345 )
260 assertEquals( "This is a test 1",
261 StringUtil
.format( "This is a test %", new Object
[]
265 assertEquals( "This is a test 1",
266 StringUtil
.format( "This is a test %", new Object
[]
268 new Integer( 1 ), new Integer( 1 )
270 assertEquals( "This is a test 1.x",
271 StringUtil
.format( "This is a test %1.x", new Object
[]
275 assertEquals( "This is a test ?missing data?1.x",
276 StringUtil
.format( "This is a test %1.x", new Object
[]
279 assertEquals( "This is a test %1.x",
280 StringUtil
.format( "This is a test \\%1.x", new Object
[]
286 private String
fmt( double num
, int minIntDigits
, int maxFracDigitis
)
288 NumberFormat nf
= NumberFormat
.getInstance();
290 if ( minIntDigits
!= -1 )
292 nf
.setMinimumIntegerDigits( minIntDigits
);
294 if ( maxFracDigitis
!= -1 )
296 nf
.setMaximumFractionDigits( maxFracDigitis
);
299 return nf
.format( num
);
306 * @param ignored_args
308 public static void main( String
[] ignored_args
)
310 System
.out
.println( "Testing util.StringUtil functionality" );
311 junit
.textui
.TestRunner
.run( TestStringUtil
.class );
315 * @see junit.framework.TestCase#setUp()
317 protected void setUp() throws Exception
321 // System.setProperty()