testsstrecord.java

来自「EXCEL read and write」· Java 代码 · 共 388 行 · 第 1/2 页

JAVA
388
字号
        RecordInputStream recStream = new RecordInputStream(new ByteArrayInputStream(content));        recStream.nextRecord();        record = new SSTRecord(recStream);        assertEquals( strings.length, record.getNumStrings() );        assertEquals( strings.length, record.getNumUniqueStrings() );        assertEquals( strings.length, record.countStrings() );        for ( int k = 0; k < strings.length; k++ )        {            assertEquals( strings[k], record.getString( k ) );        }        record = new SSTRecord();        bstrings[1] = new byte[bstrings[1].length - 1];        for ( int k = 0; k < bstrings.length; k++ )        {            if ( ( bstrings[k].length % 2 ) == 1 )            {                Arrays.fill( bstrings[k], (byte) ( 'a' + k ) );                strings[k] = new UnicodeString( new String(bstrings[k]) );            }            else            {                char[] data = new char[bstrings[k].length / 2];                Arrays.fill( data, (char) ( '\u2122' + k ) );                strings[k] = new UnicodeString(new String( data ));            }            record.addString( strings[k] );        }        content = new byte[record.getRecordSize()];        record.serialize( 0, content );        total_length--;        assertEquals( total_length, content.length );        recStream = new RecordInputStream(new ByteArrayInputStream(content));        recStream.nextRecord();        record = new SSTRecord(recStream);        assertEquals( strings.length, record.getNumStrings() );        assertEquals( strings.length, record.getNumUniqueStrings() );        assertEquals( strings.length, record.countStrings() );        for ( int k = 0; k < strings.length; k++ )        {            assertEquals( strings[k], record.getString( k ) );        }    }    /**     * test SSTRecord boundary conditions     */    public void testSSTRecordBug() {        // create an SSTRecord and write a certain pattern of strings        // to it ... then serialize it and verify the content        SSTRecord record = new SSTRecord();        // the record will start with two integers, then this string        // ... that will eat up 16 of the 8224 bytes that the record        // can hold        record.addString( new UnicodeString("Hello") );        // now we have an additional 8208 bytes, which is an exact        // multiple of 16 bytes        long testvalue = 1000000000000L;        for ( int k = 0; k < 2000; k++ )        {            record.addString( new UnicodeString(String.valueOf( testvalue++ )) );        }        byte[] content = new byte[record.getRecordSize()];        record.serialize( 0, content );        assertEquals(8224, LittleEndian.getShort(content, 2));        assertEquals(ContinueRecord.sid, LittleEndian.getShort(content, 8228));        assertEquals(8224, LittleEndian.getShort(content, 8228+2));        assertEquals( (byte) 13, content[4 + 8228] );        assertEquals(ContinueRecord.sid, LittleEndian.getShort(content, 2*8228));        assertEquals(8224, LittleEndian.getShort(content, 8228*2+2));        assertEquals( (byte) 13, content[4 + 8228 * 2] );        assertEquals(ContinueRecord.sid, LittleEndian.getShort(content, 3*8228));        assertEquals( (byte) 13, content[4 + 8228 * 3] );    }    /**     * test simple addString     */    public void testSimpleAddString()    {        SSTRecord record = new SSTRecord();        UnicodeString s1 = new UnicodeString("Hello world");        // \u2122 is the encoding of the trademark symbol ...        UnicodeString s2 = new UnicodeString("Hello world\u2122");        assertEquals( 0, record.addString( s1 ) );        assertEquals( s1, record.getString( 0 ) );        assertEquals( 1, record.countStrings() );        assertEquals( 1, record.getNumStrings() );        assertEquals( 1, record.getNumUniqueStrings() );        assertEquals( 0, record.addString( s1 ) );        assertEquals( s1, record.getString( 0 ) );        assertEquals( 1, record.countStrings() );        assertEquals( 2, record.getNumStrings() );        assertEquals( 1, record.getNumUniqueStrings() );        assertEquals( 1, record.addString( s2 ) );        assertEquals( s2, record.getString( 1 ) );        assertEquals( 2, record.countStrings() );        assertEquals( 3, record.getNumStrings() );        assertEquals( 2, record.getNumUniqueStrings() );        Iterator iter = record.getStrings();        while ( iter.hasNext() )        {            UnicodeString ucs = (UnicodeString) iter.next();            if ( ucs.equals( s1 ) )            {                assertEquals( (byte) 0, ucs.getOptionFlags() );            }            else if ( ucs.equals( s2 ) )            {                assertEquals( (byte) 1, ucs.getOptionFlags() );            }            else            {                fail( "cannot match string: " + ucs.getString() );            }        }    }    /**     * test simple constructor     */    public void testSimpleConstructor()    {        SSTRecord record = new SSTRecord();        assertEquals( 0, record.getNumStrings() );        assertEquals( 0, record.getNumUniqueStrings() );        assertEquals( 0, record.countStrings() );        byte[] output = record.serialize();        byte[] expected =                {                    (byte) record.getSid(), (byte) ( record.getSid() >> 8 ),                    (byte) 8, (byte) 0, (byte) 0, (byte) 0, (byte) 0,                    (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0                };        assertEquals( expected.length, output.length );        for ( int k = 0; k < expected.length; k++ )        {            assertEquals( String.valueOf( k ), expected[k], output[k] );        }    }    /**     * main method to run the unit tests     *     * @param ignored_args     */    public static void main( String[] ignored_args ) {        junit.textui.TestRunner.run( TestSSTRecord.class );    }    /**     * Tests that workbooks with rich text that duplicates a non rich text cell can be read and written.     */    public void testReadWriteDuplicatedRichText1()            throws Exception    {        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("duprich1.xls");        HSSFSheet sheet = wb.getSheetAt( 1 );        assertEquals( "01/05 (Wed)", sheet.getRow( 0 ).getCell(8 ).getStringCellValue() );        assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell(8 ).getStringCellValue() );        ByteArrayOutputStream baos = new ByteArrayOutputStream();        wb.write( baos );        // test the second file.        wb = HSSFTestDataSamples.openSampleWorkbook("duprich2.xls");        sheet = wb.getSheetAt( 0 );        int row = 0;        assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        assertEquals( "rich", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        assertEquals( "text", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        assertEquals( "strings", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() );        wb.write( baos );    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?