FileDocCategorySizeDatePackage
TestPropertySorter.javaAPI DocApache Poi 3.0.15387Mon Jan 01 12:39:44 GMT 2007org.apache.poi.poifs.filesystem

TestPropertySorter

public class TestPropertySorter extends TestCase
Verify the order of entries DirectoryProperty .

In particular it is important to serialize ROOT._VBA_PROJECT_CUR.VBA node. See bug 39234 in bugzilla. Thanks to Bill Seddon for providing the solution.

author
Yegor Kozlov

Fields Summary
protected static final String[]
_entries
protected File
testFile
Constructors Summary
Methods Summary
protected org.apache.poi.poifs.property.Property[]getVBAProperties(org.apache.poi.poifs.filesystem.POIFSFileSystem fs)

return
array of properties read from ROOT._VBA_PROJECT_CUR.VBA node

        String _VBA_PROJECT_CUR = "_VBA_PROJECT_CUR";
        String VBA = "VBA";

        DirectoryEntry root = fs.getRoot();
        DirectoryEntry vba_project = (DirectoryEntry)root.getEntry(_VBA_PROJECT_CUR);

        DirectoryNode vba = (DirectoryNode)vba_project.getEntry(VBA);
        DirectoryProperty  p = (DirectoryProperty)vba.getProperty();

        ArrayList lst = new ArrayList();
        for (Iterator it = p.getChildren(); it.hasNext();){
            Property ch = (Property)it.next();
            lst.add(ch);
        }
        return (Property [])lst.toArray(new Property[ 0 ]);
    
public voidsetUp()


      
        String home = System.getProperty("HSSF.testdata.path");
        testFile = new File(home + "/39234.xls");
    
public voidtestSerialization()
Serialize file system and verify that the order of properties is the same as in the original file.

        InputStream is = new FileInputStream(testFile);
        POIFSFileSystem fs = new POIFSFileSystem(is);
        is.close();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fs.writeFilesystem(out);
        out.close();
        is = new ByteArrayInputStream(out.toByteArray());
        fs = new POIFSFileSystem(is);
        is.close();
        Property[] props = getVBAProperties(fs);
        Arrays.sort(props, new DirectoryProperty.PropertyComparator());

        assertEquals(_entries.length, props.length);
        for (int i = 0; i < props.length; i++) {
            assertEquals(_entries[i], props[i].getName());
        }
    
public voidtestSortProperties()
Test sorting of properties in DirectoryProperty

        InputStream is = new FileInputStream(testFile);
        POIFSFileSystem fs = new POIFSFileSystem(is);
        is.close();
        Property[] props = getVBAProperties(fs);

        assertEquals(_entries.length, props.length);

        // (1). See that there is a problem with the old case-sensitive property comparartor
        Arrays.sort(props, new CaseSensitivePropertyComparator());
        try {
            for (int i = 0; i < props.length; i++) {
                assertEquals(_entries[i], props[i].getName());
            }
            fail("case-sensitive property comparator returns properties in wrong order");
        } catch (ComparisonFailure e){
            ; // as expected
        }

        // (2) Verify that the fixed proeprty comparator works right
        Arrays.sort(props, new DirectoryProperty.PropertyComparator());
        for (int i = 0; i < props.length; i++) {
            assertEquals(_entries[i], props[i].getName());
        }