2004-04-09 13:05:39 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/* ====================================================================
|
2006-12-22 19:18:16 +00:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-04-09 13:05:39 +00:00
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
package org.apache.poi.util;
|
|
|
|
|
|
|
|
|
|
import junit.framework.*;
|
|
|
|
|
|
|
|
|
|
import java.text.NumberFormat;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unit test for StringUtil
|
|
|
|
|
*
|
|
|
|
|
* @author Marc Johnson (mjohnson at apache dot org
|
2002-02-11 04:23:11 +00:00
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
2002-09-02 02:11:16 +00:00
|
|
|
* @author Sergei Kozello (sergeikozello at mail.ru)
|
2002-01-31 02:22:28 +00:00
|
|
|
*/
|
|
|
|
|
public class TestStringUtil
|
2002-11-14 11:57:13 +00:00
|
|
|
extends TestCase
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Creates new TestStringUtil
|
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
*/
|
2002-11-14 11:57:13 +00:00
|
|
|
public TestStringUtil( String name )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
super( name );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-09-02 02:11:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test getFromUnicodeHigh for symbols with code below and more 127
|
|
|
|
|
*/
|
|
|
|
|
public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127()
|
|
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
byte[] test_data = new byte[]{0x22, 0x04,
|
|
|
|
|
0x35, 0x04,
|
|
|
|
|
0x41, 0x04,
|
|
|
|
|
0x42, 0x04,
|
|
|
|
|
0x20, 0x00,
|
|
|
|
|
0x74, 0x00,
|
|
|
|
|
0x65, 0x00,
|
|
|
|
|
0x73, 0x00,
|
|
|
|
|
0x74, 0x00,
|
|
|
|
|
};
|
|
|
|
|
|
2002-09-02 02:11:16 +00:00
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "\u0422\u0435\u0441\u0442 test",
|
2003-08-19 14:07:40 +00:00
|
|
|
StringUtil.getFromUnicodeLE( test_data ) );
|
2002-09-02 02:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test putCompressedUnicode
|
|
|
|
|
*/
|
2002-11-08 02:34:08 +00:00
|
|
|
public void testPutCompressedUnicode() throws Exception
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
byte[] output = new byte[100];
|
2002-01-31 02:22:28 +00:00
|
|
|
byte[] expected_output =
|
2002-11-14 11:57:13 +00:00
|
|
|
{
|
|
|
|
|
(byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l',
|
|
|
|
|
(byte) 'o', (byte) ' ', (byte) 'W', (byte) 'o',
|
|
|
|
|
(byte) 'r', (byte) 'l', (byte) 'd', (byte) 0xAE
|
|
|
|
|
};
|
|
|
|
|
String input = new String( expected_output, StringUtil.getPreferredEncoding() );
|
|
|
|
|
|
|
|
|
|
StringUtil.putCompressedUnicode( input, output, 0 );
|
|
|
|
|
for ( int j = 0; j < expected_output.length; j++ )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "testing offset " + j, expected_output[j],
|
|
|
|
|
output[j] );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
StringUtil.putCompressedUnicode( input, output,
|
|
|
|
|
100 - expected_output.length );
|
|
|
|
|
for ( int j = 0; j < expected_output.length; j++ )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "testing offset " + j, expected_output[j],
|
|
|
|
|
output[100 + j - expected_output.length] );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
StringUtil.putCompressedUnicode( input, output,
|
|
|
|
|
101 - expected_output.length );
|
|
|
|
|
fail( "Should have caught ArrayIndexOutOfBoundsException" );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
catch ( ArrayIndexOutOfBoundsException ignored )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
|
|
|
|
// as expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test putUncompressedUnicode
|
|
|
|
|
*/
|
|
|
|
|
public void testPutUncompressedUnicode()
|
|
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
byte[] output = new byte[100];
|
|
|
|
|
String input = "Hello World";
|
2002-01-31 02:22:28 +00:00
|
|
|
byte[] expected_output =
|
2002-11-14 11:57:13 +00:00
|
|
|
{
|
|
|
|
|
(byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l',
|
|
|
|
|
(byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0,
|
|
|
|
|
(byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o',
|
|
|
|
|
(byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0,
|
|
|
|
|
(byte) 'd', (byte) 0
|
|
|
|
|
};
|
|
|
|
|
|
2003-08-19 14:07:40 +00:00
|
|
|
StringUtil.putUnicodeLE( input, output, 0 );
|
2002-11-14 11:57:13 +00:00
|
|
|
for ( int j = 0; j < expected_output.length; j++ )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "testing offset " + j, expected_output[j],
|
|
|
|
|
output[j] );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2003-08-19 14:07:40 +00:00
|
|
|
StringUtil.putUnicodeLE( input, output,
|
2002-11-14 11:57:13 +00:00
|
|
|
100 - expected_output.length );
|
|
|
|
|
for ( int j = 0; j < expected_output.length; j++ )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "testing offset " + j, expected_output[j],
|
|
|
|
|
output[100 + j - expected_output.length] );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
2003-08-19 14:07:40 +00:00
|
|
|
StringUtil.putUnicodeLE( input, output,
|
2002-11-14 11:57:13 +00:00
|
|
|
101 - expected_output.length );
|
|
|
|
|
fail( "Should have caught ArrayIndexOutOfBoundsException" );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
catch ( ArrayIndexOutOfBoundsException ignored )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
|
|
|
|
// as expected
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
public void testFormat()
|
2002-11-14 11:57:13 +00:00
|
|
|
throws Exception
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
assertEquals( "This is a test " + fmt( 1.2345, 2, 2 ),
|
|
|
|
|
StringUtil.format( "This is a test %2.2", new Object[]
|
|
|
|
|
{
|
|
|
|
|
new Double( 1.2345 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test " + fmt( 1.2345, -1, 3 ),
|
|
|
|
|
StringUtil.format( "This is a test %.3", new Object[]
|
|
|
|
|
{
|
|
|
|
|
new Double( 1.2345 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a great test " + fmt( 1.2345, -1, 3 ),
|
|
|
|
|
StringUtil.format( "This is a % test %.3", new Object[]
|
|
|
|
|
{
|
|
|
|
|
"great", new Double( 1.2345 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test 1",
|
|
|
|
|
StringUtil.format( "This is a test %", new Object[]
|
|
|
|
|
{
|
|
|
|
|
new Integer( 1 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test 1",
|
|
|
|
|
StringUtil.format( "This is a test %", new Object[]
|
|
|
|
|
{
|
|
|
|
|
new Integer( 1 ), new Integer( 1 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test 1.x",
|
|
|
|
|
StringUtil.format( "This is a test %1.x", new Object[]
|
|
|
|
|
{
|
|
|
|
|
new Integer( 1 )
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test ?missing data?1.x",
|
|
|
|
|
StringUtil.format( "This is a test %1.x", new Object[]
|
|
|
|
|
{
|
|
|
|
|
} ) );
|
|
|
|
|
assertEquals( "This is a test %1.x",
|
|
|
|
|
StringUtil.format( "This is a test \\%1.x", new Object[]
|
|
|
|
|
{
|
|
|
|
|
} ) );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
|
|
|
|
|
private String fmt( double num, int minIntDigits, int maxFracDigitis )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
|
|
|
|
NumberFormat nf = NumberFormat.getInstance();
|
|
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
if ( minIntDigits != -1 )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
nf.setMinimumIntegerDigits( minIntDigits );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
if ( maxFracDigitis != -1 )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
nf.setMaximumFractionDigits( maxFracDigitis );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
|
|
|
|
|
return nf.format( num );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-14 11:57:13 +00:00
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
/**
|
|
|
|
|
* main
|
|
|
|
|
*
|
|
|
|
|
* @param ignored_args
|
|
|
|
|
*/
|
2002-11-14 11:57:13 +00:00
|
|
|
public static void main( String[] ignored_args )
|
2002-01-31 02:22:28 +00:00
|
|
|
{
|
2002-11-14 11:57:13 +00:00
|
|
|
System.out.println( "Testing util.StringUtil functionality" );
|
|
|
|
|
junit.textui.TestRunner.run( TestStringUtil.class );
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
|
2002-11-08 02:34:08 +00:00
|
|
|
/**
|
|
|
|
|
* @see junit.framework.TestCase#setUp()
|
|
|
|
|
*/
|
2002-11-14 11:57:13 +00:00
|
|
|
protected void setUp() throws Exception
|
|
|
|
|
{
|
2002-11-08 02:34:08 +00:00
|
|
|
super.setUp();
|
2002-11-14 11:57:13 +00:00
|
|
|
|
|
|
|
|
// System.setProperty()
|
2002-11-08 02:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
2002-01-31 02:22:28 +00:00
|
|
|
}
|
2002-11-14 11:57:13 +00:00
|
|
|
|