1 2 17 18 package org.apache.poi.util; 19 20 import junit.framework.*; 21 22 import java.text.NumberFormat ; 23 24 31 public class TestStringUtil 32 extends TestCase 33 { 34 39 public TestStringUtil( String name ) 40 { 41 super( name ); 42 } 43 44 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 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 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 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 ignored ) 125 { 126 } 128 129 try 130 { 131 StringUtil.getFromUnicodeBE( test_data, 32, 16 ); 132 fail( "Should have caught ArrayIndexOutOfBoundsException" ); 133 } 134 catch ( ArrayIndexOutOfBoundsException ignored ) 135 { 136 } 138 139 try 140 { 141 StringUtil.getFromUnicodeBE( test_data, 1, 16 ); 142 fail( "Should have caught IllegalArgumentException" ); 143 } 144 catch ( IllegalArgumentException ignored ) 145 { 146 } 148 149 try 150 { 151 StringUtil.getFromUnicodeBE( test_data, 1, -1 ); 152 fail( "Should have caught IllegalArgumentException" ); 153 } 154 catch ( IllegalArgumentException ignored ) 155 { 156 } 158 } 159 160 163 public void testPutCompressedUnicode() throws Exception 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 input = new String ( 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 ignored ) 194 { 195 } 197 } 198 199 202 public void testPutUncompressedUnicode() 203 { 204 byte[] output = new byte[100]; 205 String 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 ignored ) 235 { 236 } 238 } 239 240 241 public void testFormat() 242 throws Exception 243 { 244 assertEquals( "This is a test " + fmt( 1.2345, 2, 2 ), 245 StringUtil.format( "This is a test %2.2", new Object [] 246 { 247 new Double ( 1.2345 ) 248 } ) ); 249 assertEquals( "This is a test " + fmt( 1.2345, -1, 3 ), 250 StringUtil.format( "This is a test %.3", new Object [] 251 { 252 new Double ( 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 [] 256 { 257 "great", new Double ( 1.2345 ) 258 } ) ); 259 assertEquals( "This is a test 1", 260 StringUtil.format( "This is a test %", new Object [] 261 { 262 new Integer ( 1 ) 263 } ) ); 264 assertEquals( "This is a test 1", 265 StringUtil.format( "This is a test %", new Object [] 266 { 267 new Integer ( 1 ), new Integer ( 1 ) 268 } ) ); 269 assertEquals( "This is a test 1.x", 270 StringUtil.format( "This is a test %1.x", new Object [] 271 { 272 new Integer ( 1 ) 273 } ) ); 274 assertEquals( "This is a test ?missing data?1.x", 275 StringUtil.format( "This is a test %1.x", new Object [] 276 { 277 } ) ); 278 assertEquals( "This is a test %1.x", 279 StringUtil.format( "This is a test \\%1.x", new Object [] 280 { 281 } ) ); 282 } 283 284 285 private String fmt( double num, int minIntDigits, int maxFracDigitis ) 286 { 287 NumberFormat 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 307 public static void main( String [] ignored_args ) 308 { 309 System.out.println( "Testing util.StringUtil functionality" ); 310 junit.textui.TestRunner.run( TestStringUtil.class ); 311 } 312 313 316 protected void setUp() throws Exception 317 { 318 super.setUp(); 319 320 } 322 323 } 324 325 | Popular Tags |