FileDocCategorySizeDatePackage
TestSmallDocumentBlock.javaAPI DocApache Poi 3.0.18642Mon Jan 01 12:39:44 GMT 2007org.apache.poi.poifs.storage

TestSmallDocumentBlock

public class TestSmallDocumentBlock extends TestCase
Class to test SmallDocumentBlock functionality
author
Marc Johnson

Fields Summary
private static final byte[]
_testdata
private static final int
_testdata_size
Constructors Summary
public TestSmallDocumentBlock(String name)
constructor

param
name


    
    
        _testdata = new byte[ _testdata_size ];
        for (int j = 0; j < _testdata.length; j++)
        {
            _testdata[ j ] = ( byte ) j;
        }
    
        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] ignored_args)
main method to run the unit tests

param
ignored_args

        System.out.println(
            "Testing org.apache.poi.poifs.storage.SmallDocumentBlock");
        junit.textui.TestRunner.run(TestSmallDocumentBlock.class);
    
public voidtestCalcSize()
test calcSize

        for (int j = 0; j < 10; j++)
        {
            assertEquals("testing " + j, j * 64,
                         SmallDocumentBlock.calcSize(j));
        }
    
public voidtestConvert1()
Test conversion from DocumentBlocks

exception
IOException

        ByteArrayInputStream stream    = new ByteArrayInputStream(_testdata);
        List                 documents = new ArrayList();

        while (true)
        {
            DocumentBlock block = new DocumentBlock(stream);

            documents.add(block);
            if (block.partiallyRead())
            {
                break;
            }
        }
        SmallDocumentBlock[] results =
            SmallDocumentBlock
                .convert(( BlockWritable [] ) documents
                    .toArray(new DocumentBlock[ 0 ]), _testdata_size);

        assertEquals("checking correct result size: ",
                     (_testdata_size + 63) / 64, results.length);
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        for (int j = 0; j < results.length; j++)
        {
            results[ j ].writeBlocks(output);
        }
        byte[] output_array = output.toByteArray();

        assertEquals("checking correct output size: ", 64 * results.length,
                     output_array.length);
        int index = 0;

        for (; index < _testdata_size; index++)
        {
            assertEquals("checking output " + index, _testdata[ index ],
                         output_array[ index ]);
        }
        for (; index < output_array.length; index++)
        {
            assertEquals("checking output " + index, ( byte ) 0xff,
                         output_array[ index ]);
        }
    
public voidtestConvert2()
Test conversion from byte array

exception
IOException;
exception
IOException

        for (int j = 0; j < 320; j++)
        {
            byte[] array = new byte[ j ];

            for (int k = 0; k < j; k++)
            {
                array[ k ] = ( byte ) k;
            }
            SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(array,
                                              319);

            assertEquals(5, blocks.length);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            for (int k = 0; k < blocks.length; k++)
            {
                blocks[ k ].writeBlocks(stream);
            }
            stream.close();
            byte[] output = stream.toByteArray();

            for (int k = 0; k < array.length; k++)
            {
                assertEquals(String.valueOf(k), array[ k ], output[ k ]);
            }
            for (int k = array.length; k < 320; k++)
            {
                assertEquals(String.valueOf(k), ( byte ) 0xFF, output[ k ]);
            }
        }
    
public voidtestExtract()
test extract method

exception
IOException

        byte[] data   = new byte[ 512 ];
        int    offset = 0;

        for (int j = 0; j < 8; j++)
        {
            for (int k = 0; k < 64; k++)
            {
                data[ offset++ ] = ( byte ) (k + j);
            }
        }
        RawDataBlock[] blocks =
        {
            new RawDataBlock(new ByteArrayInputStream(data))
        };
        List           output = SmallDocumentBlock.extract(blocks);
        Iterator       iter   = output.iterator();

        offset = 0;
        while (iter.hasNext())
        {
            byte[] out_data = (( SmallDocumentBlock ) iter.next()).getData();

            assertEquals("testing block at offset " + offset, 64,
                         out_data.length);
            for (int j = 0; j < out_data.length; j++)
            {
                assertEquals("testing byte at offset " + offset,
                             data[ offset ], out_data[ j ]);
                offset++;
            }
        }
    
public voidtestFill()
test fill

exception
IOException

        for (int j = 0; j <= 8; j++)
        {
            List foo = new ArrayList();

            for (int k = 0; k < j; k++)
            {
                foo.add(new Object());
            }
            int result = SmallDocumentBlock.fill(foo);

            assertEquals("correct big block count: ", (j + 7) / 8, result);
            assertEquals("correct small block count: ", 8 * result,
                         foo.size());
            for (int m = j; m < foo.size(); m++)
            {
                BlockWritable         block  = ( BlockWritable ) foo.get(m);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                block.writeBlocks(stream);
                byte[] output = stream.toByteArray();

                assertEquals("correct output size (block[ " + m + " ]): ",
                             64, output.length);
                for (int n = 0; n < 64; n++)
                {
                    assertEquals("correct value (block[ " + m + " ][ " + n
                                 + " ]): ", ( byte ) 0xff, output[ n ]);
                }
            }
        }
    
public voidtestRead()
Test read method

exception
IOException

        ByteArrayInputStream stream    = new ByteArrayInputStream(_testdata);
        List                 documents = new ArrayList();

        while (true)
        {
            DocumentBlock block = new DocumentBlock(stream);

            documents.add(block);
            if (block.partiallyRead())
            {
                break;
            }
        }
        SmallDocumentBlock[] blocks =
            SmallDocumentBlock
                .convert(( BlockWritable [] ) documents
                    .toArray(new DocumentBlock[ 0 ]), _testdata_size);

        for (int j = 1; j <= _testdata_size; j += 38)
        {
            byte[] buffer = new byte[ j ];
            int    offset = 0;

            for (int k = 0; k < (_testdata_size / j); k++)
            {
                SmallDocumentBlock.read(blocks, buffer, offset);
                for (int n = 0; n < buffer.length; n++)
                {
                    assertEquals("checking byte " + (k * j) + n,
                                 _testdata[ (k * j) + n ], buffer[ n ]);
                }
                offset += j;
            }
        }