Fixed non-use of 10 sample files (named 'BigSSTRecord*'). Resurrected SSTRecord test (commented out in r353769).

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@780874 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-06-01 23:13:29 +00:00
parent e6149cca3a
commit dd7e611bda
2 changed files with 105 additions and 157 deletions

View File

@ -176,28 +176,6 @@ public final class SSTRecord extends ContinuableRecord {
return sid; return sid;
} }
/**
* @return hashcode
*/
public int hashCode()
{
return field_2_num_unique_strings;
}
public boolean equals( Object o )
{
if ( ( o == null ) || ( o.getClass() != this.getClass() ) )
{
return false;
}
SSTRecord other = (SSTRecord) o;
return ( ( field_1_num_strings == other
.field_1_num_strings ) && ( field_2_num_unique_strings == other
.field_2_num_unique_strings ) && field_3_strings
.equals( other.field_3_strings ) );
}
/** /**
* Fill the fields from the data * Fill the fields from the data
* <P> * <P>

View File

@ -17,141 +17,127 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
/** /**
* @author Marc Johnson (mjohnson at apache dot org) * @author Marc Johnson (mjohnson at apache dot org)
* @author Glen Stampoultzis (glens at apache.org) * @author Glen Stampoultzis (glens at apache.org)
*/ */
public final class TestSSTRecord extends TestCase { public final class TestSSTRecord extends TestCase {
/** /**
* test processContinueRecord * decodes hexdump files and concatenates the results
* @param hexDumpFileNames names of sample files in the hssf test data directory
*/ */
public void testProcessContinueRecord() { private static byte[] concatHexDumps(String... hexDumpFileNames) {
//jmh byte[] testdata = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord" ); int nFiles = hexDumpFileNames.length;
//jmh byte[] input = new byte[testdata.length - 4]; ByteArrayOutputStream baos = new ByteArrayOutputStream(nFiles * 8228);
//jmh for (int i = 0; i < nFiles; i++) {
//jmh System.arraycopy( testdata, 4, input, 0, input.length ); String sampleFileName = hexDumpFileNames[i];
//jmh SSTRecord record = InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
//jmh new SSTRecord( LittleEndian.getShort( testdata, 0 ), BufferedReader br = new BufferedReader(new InputStreamReader(is));
//jmh LittleEndian.getShort( testdata, 2 ), input ); try {
//jmh byte[] continueRecord = HexRead.readData( _test_file_path + File.separator + "BigSSTRecordCR" ); while (true) {
//jmh String line = br.readLine();
//jmh input = new byte[continueRecord.length - 4]; if (line == null) {
//jmh System.arraycopy( continueRecord, 4, input, 0, input.length ); break;
//jmh record.processContinueRecord( input ); }
//jmh assertEquals( 1464, record.getNumStrings() ); baos.write(HexRead.readFromString(line));
//jmh assertEquals( 688, record.getNumUniqueStrings() ); }
//jmh assertEquals( 688, record.countStrings() ); is.close();
//jmh byte[] ser_output = record.serialize(); } catch (IOException e) {
//jmh int offset = 0; throw new RuntimeException(e);
//jmh short type = LittleEndian.getShort( ser_output, offset ); }
//jmh }
//jmh offset += LittleEndianConsts.SHORT_SIZE;
//jmh short length = LittleEndian.getShort( ser_output, offset ); return baos.toByteArray();
//jmh }
//jmh offset += LittleEndianConsts.SHORT_SIZE;
//jmh byte[] recordData = new byte[length]; /**
//jmh * @param rawData serialization of one {@link SSTRecord} and zero or more {@link ContinueRecord}s
//jmh System.arraycopy( ser_output, offset, recordData, 0, length ); */
//jmh offset += length; private static SSTRecord createSSTFromRawData(byte[] rawData) {
//jmh SSTRecord testRecord = new SSTRecord( type, length, recordData ); RecordInputStream in = new RecordInputStream(new ByteArrayInputStream(rawData));
//jmh in.nextRecord();
//jmh assertEquals( ContinueRecord.sid, SSTRecord result = new SSTRecord(in);
//jmh LittleEndian.getShort( ser_output, offset ) ); assertEquals(0, in.remaining());
//jmh offset += LittleEndianConsts.SHORT_SIZE; assertTrue(!in.hasNextRecord());
//jmh length = LittleEndian.getShort( ser_output, offset ); return result;
//jmh offset += LittleEndianConsts.SHORT_SIZE; }
//jmh byte[] cr = new byte[length];
//jmh /**
//jmh System.arraycopy( ser_output, offset, cr, 0, length ); * SST is often split over several {@link ContinueRecord}s
//jmh offset += length; */
//jmh assertEquals( offset, ser_output.length ); public void testContinuedRecord() {
//jmh testRecord.processContinueRecord( cr ); byte[] origData;
//jmh assertEquals( record, testRecord ); SSTRecord record;
//jmh byte[] ser_output;
//jmh // testing based on new bug report
//jmh testdata = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2" ); origData = concatHexDumps("BigSSTRecord", "BigSSTRecordCR");
//jmh input = new byte[testdata.length - 4]; record = createSSTFromRawData(origData);
//jmh System.arraycopy( testdata, 4, input, 0, input.length ); assertEquals( 1464, record.getNumStrings() );
//jmh record = new SSTRecord( LittleEndian.getShort( testdata, 0 ), assertEquals( 688, record.getNumUniqueStrings() );
//jmh LittleEndian.getShort( testdata, 2 ), input ); assertEquals( 688, record.countStrings() );
//jmh byte[] continueRecord1 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR1" ); ser_output = record.serialize();
//jmh assertTrue(Arrays.equals(origData, ser_output));
//jmh input = new byte[continueRecord1.length - 4];
//jmh System.arraycopy( continueRecord1, 4, input, 0, input.length ); // testing based on new bug report
//jmh record.processContinueRecord( input ); origData = concatHexDumps("BigSSTRecord2", "BigSSTRecord2CR1", "BigSSTRecord2CR2", "BigSSTRecord2CR3",
//jmh byte[] continueRecord2 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR2" ); "BigSSTRecord2CR4", "BigSSTRecord2CR5", "BigSSTRecord2CR6", "BigSSTRecord2CR7");
//jmh record = createSSTFromRawData(origData);
//jmh input = new byte[continueRecord2.length - 4];
//jmh System.arraycopy( continueRecord2, 4, input, 0, input.length );
//jmh record.processContinueRecord( input ); assertEquals( 158642, record.getNumStrings() );
//jmh byte[] continueRecord3 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR3" ); assertEquals( 5249, record.getNumUniqueStrings() );
//jmh assertEquals( 5249, record.countStrings() );
//jmh input = new byte[continueRecord3.length - 4]; ser_output = record.serialize();
//jmh System.arraycopy( continueRecord3, 4, input, 0, input.length ); if (false) { // set true to observe make sure areSameSSTs() is working
//jmh record.processContinueRecord( input ); ser_output[11000] = 'X';
//jmh byte[] continueRecord4 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR4" ); }
//jmh
//jmh input = new byte[continueRecord4.length - 4]; SSTRecord rec2 = createSSTFromRawData(ser_output);
//jmh System.arraycopy( continueRecord4, 4, input, 0, input.length ); if (!areSameSSTs(record, rec2)) {
//jmh record.processContinueRecord( input ); throw new AssertionFailedError("large SST re-serialized incorrectly");
//jmh byte[] continueRecord5 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR5" ); }
//jmh if (false) {
//jmh input = new byte[continueRecord5.length - 4]; // TODO - trivial differences in ContinueRecord break locations
//jmh System.arraycopy( continueRecord5, 4, input, 0, input.length ); // Sample data should be checked against what most recent Excel version produces.
//jmh record.processContinueRecord( input ); // maybe tweaks are required in ContinuableRecordOutput
//jmh byte[] continueRecord6 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR6" ); assertTrue(Arrays.equals(origData, ser_output));
//jmh }
//jmh input = new byte[continueRecord6.length - 4]; }
//jmh System.arraycopy( continueRecord6, 4, input, 0, input.length );
//jmh record.processContinueRecord( input ); private boolean areSameSSTs(SSTRecord a, SSTRecord b) {
//jmh byte[] continueRecord7 = HexRead.readData( _test_file_path + File.separator + "BigSSTRecord2CR7" );
//jmh if (a.getNumStrings() != b.getNumStrings()) {
//jmh input = new byte[continueRecord7.length - 4]; return false;
//jmh System.arraycopy( continueRecord7, 4, input, 0, input.length ); }
//jmh record.processContinueRecord( input ); int nElems = a.getNumUniqueStrings();
//jmh assertEquals( 158642, record.getNumStrings() ); if (nElems != b.getNumUniqueStrings()) {
//jmh assertEquals( 5249, record.getNumUniqueStrings() ); return false;
//jmh assertEquals( 5249, record.countStrings() ); }
//jmh ser_output = record.serialize(); for(int i=0; i<nElems; i++) {
//jmh offset = 0; if (!a.getString(i).equals(b.getString(i))) {
//jmh type = LittleEndian.getShort( ser_output, offset ); return false;
//jmh offset += LittleEndianConsts.SHORT_SIZE; }
//jmh length = LittleEndian.getShort( ser_output, offset ); }
//jmh offset += LittleEndianConsts.SHORT_SIZE; return true;
//jmh recordData = new byte[length];
//jmh System.arraycopy( ser_output, offset, recordData, 0, length );
//jmh offset += length;
//jmh testRecord = new SSTRecord( type, length, recordData );
//jmh for ( int count = 0; count < 7; count++ )
//jmh {
//jmh assertEquals( ContinueRecord.sid,
//jmh LittleEndian.getShort( ser_output, offset ) );
//jmh offset += LittleEndianConsts.SHORT_SIZE;
//jmh length = LittleEndian.getShort( ser_output, offset );
//jmh offset += LittleEndianConsts.SHORT_SIZE;
//jmh cr = new byte[length];
//jmh System.arraycopy( ser_output, offset, cr, 0, length );
//jmh testRecord.processContinueRecord( cr );
//jmh offset += length;
//jmh }
//jmh assertEquals( offset, ser_output.length );
//jmh assertEquals( record, testRecord );
//jmh assertEquals( record.countStrings(), testRecord.countStrings() );
} }
/** /**
@ -277,8 +263,7 @@ public final class TestSSTRecord extends TestCase {
/** /**
* test simple addString * test simple addString
*/ */
public void testSimpleAddString() public void testSimpleAddString() {
{
SSTRecord record = new SSTRecord(); SSTRecord record = new SSTRecord();
UnicodeString s1 = new UnicodeString("Hello world"); UnicodeString s1 = new UnicodeString("Hello world");
@ -324,9 +309,7 @@ public final class TestSSTRecord extends TestCase {
/** /**
* test simple constructor * test simple constructor
*/ */
public void testSimpleConstructor() {
public void testSimpleConstructor()
{
SSTRecord record = new SSTRecord(); SSTRecord record = new SSTRecord();
assertEquals( 0, record.getNumStrings() ); assertEquals( 0, record.getNumStrings() );
@ -347,29 +330,16 @@ public final class TestSSTRecord extends TestCase {
} }
} }
/**
* 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. * Tests that workbooks with rich text that duplicates a non rich text cell can be read and written.
*/ */
public void testReadWriteDuplicatedRichText1() public void testReadWriteDuplicatedRichText1() {
throws Exception
{
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("duprich1.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("duprich1.xls");
HSSFSheet sheet = wb.getSheetAt( 1 ); HSSFSheet sheet = wb.getSheetAt( 1 );
assertEquals( "01/05 (Wed)", sheet.getRow( 0 ).getCell(8 ).getStringCellValue() ); assertEquals( "01/05 (Wed)", sheet.getRow( 0 ).getCell(8 ).getStringCellValue() );
assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell(8 ).getStringCellValue() ); assertEquals( "01/05 (Wed)", sheet.getRow( 1 ).getCell(8 ).getStringCellValue() );
ByteArrayOutputStream baos = new ByteArrayOutputStream(); HSSFTestDataSamples.writeOutAndReadBack(wb);
wb.write( baos );
// test the second file. // test the second file.
wb = HSSFTestDataSamples.openSampleWorkbook("duprich2.xls"); wb = HSSFTestDataSamples.openSampleWorkbook("duprich2.xls");
@ -382,6 +352,6 @@ public final class TestSSTRecord extends TestCase {
assertEquals( "Testing", sheet.getRow( row++ ).getCell(0 ).getStringCellValue() ); assertEquals( "Testing", 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 ); HSSFTestDataSamples.writeOutAndReadBack(wb);
} }
} }