poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r690732 [2/2] - in /poi/branches/ooxml: ./ src/documentation/content/xdocs/ src/java/org/apache/poi/hssf/model/ src/java/org/apache/poi/hssf/record/ src/java/org/apache/poi/hssf/record/aggregates/ src/java/org/apache/poi/hssf/usermodel/ src...
Date Sun, 31 Aug 2008 17:08:53 GMT
Modified: poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java
(original)
+++ poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hpbf/model/TestQuillContents.java
Sun Aug 31 10:08:51 2008
@@ -21,6 +21,10 @@
 
 import org.apache.poi.hpbf.HPBFDocument;
 import org.apache.poi.hpbf.model.qcbits.QCTextBit;
+import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type12;
+import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type0;
+import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type4;
+import org.apache.poi.hpbf.model.qcbits.QCPLCBit.Type8;
 
 import junit.framework.TestCase;
 
@@ -77,4 +81,354 @@
 		assertTrue(t.startsWith("This is some text on the first page"));
 		assertTrue(t.endsWith("Within doc to page 1\r"));
 	}
+	
+	public void testPLC() throws Exception {
+		File f = new File(dir, "Simple.pub");
+		HPBFDocument doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		
+		QuillContents qc = doc.getQuillContents();
+		assertEquals(20, qc.getBits().length);
+		
+		assertTrue(qc.getBits()[9] instanceof Type4);
+		assertTrue(qc.getBits()[10] instanceof Type4);
+		assertTrue(qc.getBits()[12] instanceof Type8);
+		
+		Type4 plc9 = (Type4)qc.getBits()[9];
+		Type4 plc10 = (Type4)qc.getBits()[10];
+		Type8 plc12 = (Type8)qc.getBits()[12];
+		
+		
+		assertEquals(1, plc9.getNumberOfPLCs());
+		assertEquals(4, plc9.getPreData().length);
+		assertEquals(1, plc9.getPlcValA().length);
+		assertEquals(1, plc9.getPlcValB().length);
+		
+		assertEquals(0, plc9.getPreData()[0]);
+		assertEquals(0, plc9.getPreData()[1]);
+		assertEquals(0, plc9.getPreData()[2]);
+		assertEquals(0, plc9.getPreData()[3]);
+		assertEquals(0x356, plc9.getPlcValA()[0]);
+		assertEquals(0x600, plc9.getPlcValB()[0]);
+		
+		
+		assertEquals(1, plc10.getNumberOfPLCs());
+		assertEquals(4, plc10.getPreData().length);
+		assertEquals(1, plc10.getPlcValA().length);
+		assertEquals(1, plc10.getPlcValB().length);
+		
+		assertEquals(0, plc10.getPreData()[0]);
+		assertEquals(0, plc10.getPreData()[1]);
+		assertEquals(0, plc10.getPreData()[2]);
+		assertEquals(0, plc10.getPreData()[3]);
+		assertEquals(0x356, plc10.getPlcValA()[0]);
+		assertEquals(0x800, plc10.getPlcValB()[0]);
+		
+		assertEquals(2, plc12.getNumberOfPLCs());
+		assertEquals(7, plc12.getPreData().length);
+		assertEquals(2, plc12.getPlcValA().length);
+		assertEquals(2, plc12.getPlcValB().length);
+		
+		assertEquals(0xff, plc12.getPreData()[0]);
+		assertEquals(0, plc12.getPreData()[1]);
+		assertEquals(0x3d, plc12.getPreData()[2]);
+		assertEquals(0, plc12.getPreData()[3]);
+		assertEquals(0x6e, plc12.getPreData()[4]);
+		assertEquals(0, plc12.getPreData()[5]);
+		assertEquals(0, plc12.getPreData()[6]);
+		assertEquals(0xa0000, plc12.getPlcValA()[0]);
+		assertEquals(0x22000000, plc12.getPlcValB()[0]);
+		assertEquals(0x05, plc12.getPlcValA()[1]);
+		assertEquals(0x04, plc12.getPlcValB()[1]);
+	}
+	
+	public void testComplexPLC() throws Exception {
+		File f = new File(dir, "Sample.pub");
+		HPBFDocument doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		
+		QuillContents qc = doc.getQuillContents();
+		assertEquals(20, qc.getBits().length);
+		
+		assertTrue(qc.getBits()[10] instanceof Type4);
+		assertTrue(qc.getBits()[11] instanceof Type4);
+		assertTrue(qc.getBits()[13] instanceof Type0);
+		assertTrue(qc.getBits()[14] instanceof Type12);
+		assertTrue(qc.getBits()[15] instanceof Type12);
+		assertTrue(qc.getBits()[16] instanceof Type8);
+		
+		Type4 plc10 = (Type4)qc.getBits()[10];
+		Type4 plc11 = (Type4)qc.getBits()[11];
+		Type0 plc13 = (Type0)qc.getBits()[13];
+		Type12 plc14 = (Type12)qc.getBits()[14];
+		Type12 plc15 = (Type12)qc.getBits()[15];
+		Type8 plc16 = (Type8)qc.getBits()[16];
+		
+		
+		assertEquals(1, plc10.getNumberOfPLCs());
+		assertEquals(4, plc10.getPreData().length);
+		assertEquals(1, plc10.getPlcValA().length);
+		assertEquals(1, plc10.getPlcValB().length);
+		
+		assertEquals(0, plc10.getPreData()[0]);
+		assertEquals(0, plc10.getPreData()[1]);
+		assertEquals(0, plc10.getPreData()[2]);
+		assertEquals(0, plc10.getPreData()[3]);
+		assertEquals(0x5d0, plc10.getPlcValA()[0]);
+		assertEquals(0x800, plc10.getPlcValB()[0]);
+		
+		
+		assertEquals(2, plc11.getNumberOfPLCs());
+		assertEquals(4, plc11.getPreData().length);
+		assertEquals(2, plc11.getPlcValA().length);
+		assertEquals(2, plc11.getPlcValB().length);
+		
+		assertEquals(0, plc11.getPreData()[0]);
+		assertEquals(0, plc11.getPreData()[1]);
+		assertEquals(0, plc11.getPreData()[2]);
+		assertEquals(0, plc11.getPreData()[3]);
+		assertEquals(0x53a, plc11.getPlcValA()[0]);
+		assertEquals(0x5d0, plc11.getPlcValB()[0]);
+		assertEquals(0xa00, plc11.getPlcValA()[1]);
+		assertEquals(0xc00, plc11.getPlcValB()[1]);
+		
+		
+		assertEquals(5, plc13.getNumberOfPLCs());
+		assertEquals(4, plc13.getPreData().length);
+		assertEquals(5, plc13.getPlcValA().length);
+		assertEquals(5, plc13.getPlcValB().length);
+		
+		assertEquals(0xff00, plc13.getPreData()[0]);
+		assertEquals(0, plc13.getPreData()[1]);
+		assertEquals(0xf, plc13.getPreData()[2]);
+		assertEquals(0, plc13.getPreData()[3]);
+		assertEquals(0x19, plc13.getPlcValA()[0]);
+		assertEquals(0x00, plc13.getPlcValB()[0]);
+		assertEquals(0x27, plc13.getPlcValA()[1]);
+		assertEquals(0x00, plc13.getPlcValB()[1]);
+		assertEquals(0x36, plc13.getPlcValA()[2]);
+		assertEquals(0x00, plc13.getPlcValB()[2]);
+		assertEquals(0x42, plc13.getPlcValA()[3]);
+		assertEquals(0x00, plc13.getPlcValB()[3]);
+		assertEquals(0x50, plc13.getPlcValA()[4]);
+		assertEquals(0x00, plc13.getPlcValB()[4]);
+		
+		
+		// TODO - test the type 12s
+		
+		
+		assertEquals(6, plc16.getNumberOfPLCs());
+		assertEquals(7, plc16.getPreData().length);
+		assertEquals(6, plc16.getPlcValA().length);
+		assertEquals(6, plc16.getPlcValB().length);
+		
+		assertEquals(0xff, plc16.getPreData()[0]);
+		assertEquals(0, plc16.getPreData()[1]);
+		assertEquals(0x56, plc16.getPreData()[2]);
+		assertEquals(0, plc16.getPreData()[3]);
+		assertEquals(0x62, plc16.getPreData()[4]);
+		assertEquals(0, plc16.getPreData()[5]);
+		assertEquals(0x3e, plc16.getPreData()[6]);
+		assertEquals(0x500000, plc16.getPlcValA()[0]);
+		assertEquals(0x570000, plc16.getPlcValB()[0]);
+		assertEquals(0x4b0000, plc16.getPlcValA()[1]);
+		assertEquals(0x000000, plc16.getPlcValB()[1]);
+		assertEquals(0x0a0000, plc16.getPlcValA()[2]);
+		assertEquals(0x22000000, plc16.getPlcValB()[2]);
+		assertEquals(0x000005, plc16.getPlcValA()[3]);
+		assertEquals(0x000004, plc16.getPlcValB()[3]);
+		assertEquals(0x000004, plc16.getPlcValA()[4]);
+		assertEquals(0x000004, plc16.getPlcValB()[4]);
+		assertEquals(0x000004, plc16.getPlcValA()[5]);
+		assertEquals(0x000004, plc16.getPlcValB()[5]);
+	}
+	
+	public void testNoHyperlinks() throws Exception {
+		File f = new File(dir, "SampleNewsletter.pub");
+		HPBFDocument doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		
+		QuillContents qc = doc.getQuillContents();
+		assertEquals(20, qc.getBits().length);
+		
+		Type12 plc18 = (Type12)qc.getBits()[18];
+		
+		assertEquals(1, plc18.getNumberOfPLCs());
+		assertEquals(0, plc18.getNumberOfHyperlinks());
+		assertEquals(0, plc18.getTextStartAt(0));
+		assertEquals(601, plc18.getAllTextEndAt());
+	}
+	
+	public void testSimpleHyperlink() throws Exception {
+		File f;
+		HPBFDocument doc;
+		QuillContents qc;
+		Type12 hlBit;
+		
+		// Link at 10
+		f = new File(dir, "LinkAt10.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBit = (Type12)qc.getBits()[12];
+		assertEquals(1, hlBit.getNumberOfPLCs());
+		assertEquals(1, hlBit.getNumberOfHyperlinks());
+		
+		assertEquals(10, hlBit.getTextStartAt(0));
+		assertEquals(15, hlBit.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0));
+		
+		// Longer link at 10
+		f = new File(dir, "LinkAt10Longer.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBit = (Type12)qc.getBits()[12];
+		assertEquals(1, hlBit.getNumberOfPLCs());
+		assertEquals(1, hlBit.getNumberOfHyperlinks());
+		
+		assertEquals(10, hlBit.getTextStartAt(0));
+		assertEquals(15, hlBit.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/hpbf/", hlBit.getHyperlink(0));
+		
+		// Link at 20
+		f = new File(dir, "LinkAt20.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBit = (Type12)qc.getBits()[12];
+		assertEquals(1, hlBit.getNumberOfPLCs());
+		assertEquals(1, hlBit.getNumberOfHyperlinks());
+		
+		assertEquals(20, hlBit.getTextStartAt(0));
+		assertEquals(25, hlBit.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0));
+	}
+	
+	public void testManyHyperlinks() throws Exception {
+		File f;
+		HPBFDocument doc;
+		QuillContents qc;
+		Type12 hlBit;
+		
+		// Link at 10
+		f = new File(dir, "LinkAt10.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBit = (Type12)qc.getBits()[12];
+		assertEquals(1, hlBit.getNumberOfPLCs());
+		assertEquals(1, hlBit.getNumberOfHyperlinks());
+		
+		assertEquals(10, hlBit.getTextStartAt(0));
+		assertEquals(15, hlBit.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBit.getHyperlink(0));
+		
+	}
+	
+	public void testHyperlinkDifferentVersions() throws Exception {
+		File f;
+		HPBFDocument doc;
+		QuillContents qc;
+		Type12 hlBitA;
+		Type12 hlBitB;
+		
+		// Latest version
+		f = new File(dir, "Sample.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBitA = (Type12)qc.getBits()[14];
+		assertEquals(2, hlBitA.getNumberOfPLCs());
+		assertEquals(2, hlBitA.getNumberOfHyperlinks());
+		
+		assertEquals(25, hlBitA.getTextStartAt(0));
+		assertEquals(72, hlBitA.getTextStartAt(1));
+		assertEquals(87, hlBitA.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBitA.getHyperlink(0));
+		assertEquals("C:\\Documents and Settings\\Nick\\My Documents\\Booleans.xlsx", hlBitA.getHyperlink(1));
+		
+		hlBitB = (Type12)qc.getBits()[15];
+		assertEquals(3, hlBitB.getNumberOfPLCs());
+		assertEquals(3, hlBitB.getNumberOfHyperlinks());
+		
+		assertEquals(27, hlBitB.getTextStartAt(0));
+		assertEquals(37, hlBitB.getTextStartAt(1));
+		assertEquals(54, hlBitB.getTextStartAt(2));
+		assertEquals(75, hlBitB.getAllTextEndAt());
+		assertEquals("", hlBitB.getHyperlink(0));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2));
+		
+		// 2000 version
+		f = new File(dir, "Sample2000.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBitA = (Type12)qc.getBits()[13];
+		assertEquals(2, hlBitA.getNumberOfPLCs());
+		assertEquals(2, hlBitA.getNumberOfHyperlinks());
+		
+		assertEquals(25, hlBitA.getTextStartAt(0));
+		assertEquals(72, hlBitA.getTextStartAt(1));
+		assertEquals(87, hlBitA.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBitA.getHyperlink(0));
+		assertEquals("C:\\Documents and Settings\\Nick\\My Documents\\Booleans.xlsx", hlBitA.getHyperlink(1));
+		
+		hlBitB = (Type12)qc.getBits()[14];
+		assertEquals(3, hlBitB.getNumberOfPLCs());
+		assertEquals(3, hlBitB.getNumberOfHyperlinks());
+		
+		assertEquals(27, hlBitB.getTextStartAt(0));
+		assertEquals(37, hlBitB.getTextStartAt(1));
+		assertEquals(54, hlBitB.getTextStartAt(2));
+		assertEquals(75, hlBitB.getAllTextEndAt());
+		assertEquals("", hlBitB.getHyperlink(0));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2));
+		
+		// 98 version
+		f = new File(dir, "Sample98.pub");
+		doc = new HPBFDocument(
+				new FileInputStream(f)
+		);
+		qc = doc.getQuillContents();
+		
+		hlBitA = (Type12)qc.getBits()[13];
+		assertEquals(2, hlBitA.getNumberOfPLCs());
+		assertEquals(2, hlBitA.getNumberOfHyperlinks());
+		
+		assertEquals(25, hlBitA.getTextStartAt(0));
+		assertEquals(72, hlBitA.getTextStartAt(1));
+		assertEquals(87, hlBitA.getAllTextEndAt());
+		assertEquals("http://poi.apache.org/", hlBitA.getHyperlink(0));
+		assertEquals("C:\\Documents and Settings\\Nick\\My Documents\\Booleans.xlsx", hlBitA.getHyperlink(1));
+		
+		hlBitB = (Type12)qc.getBits()[14];
+		assertEquals(3, hlBitB.getNumberOfPLCs());
+		assertEquals(3, hlBitB.getNumberOfHyperlinks());
+		
+		assertEquals(27, hlBitB.getTextStartAt(0));
+		assertEquals(37, hlBitB.getTextStartAt(1));
+		assertEquals(54, hlBitB.getTextStartAt(2));
+		assertEquals(75, hlBitB.getAllTextEndAt());
+		assertEquals("", hlBitB.getHyperlink(0));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(1));
+		assertEquals("mailto:dev@poi.apache.org?subject=HPBF", hlBitB.getHyperlink(2));
+	}
 }

Modified: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/model/TestSheet.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/model/TestSheet.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/testcases/org/apache/poi/hssf/model/TestSheet.java (original)
+++ poi/branches/ooxml/src/testcases/org/apache/poi/hssf/model/TestSheet.java Sun Aug 31 10:08:51
2008
@@ -17,7 +17,6 @@
 
 package org.apache.poi.hssf.model;
 
-import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -25,8 +24,6 @@
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.eventmodel.ERFListener;
-import org.apache.poi.hssf.eventmodel.EventRecordFactory;
 import org.apache.poi.hssf.record.BOFRecord;
 import org.apache.poi.hssf.record.BlankRecord;
 import org.apache.poi.hssf.record.CellValueRecordInterface;
@@ -46,6 +43,7 @@
 import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
 import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
 import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
+import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
@@ -88,15 +86,16 @@
         return result;
     }
 
-    private static final class MergedCellListener implements ERFListener {
+    private static final class MergedCellListener implements RecordVisitor {
 
         private int _count;
         public MergedCellListener() {
             _count = 0;
         }
-        public boolean processRecord(Record rec) {
-            _count++;
-            return true;
+        public void visitRecord(Record r) {
+            if (r instanceof MergeCellsRecord) {
+                _count++;
+            }
         }
         public int getCount() {
             return _count;
@@ -118,12 +117,8 @@
         assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
 
         //test that the regions were spread out over the appropriate number of records
-        byte[] sheetData = new byte[sheet.getSize()];
-        sheet.serialize(0, sheetData);
         MergedCellListener mcListener = new MergedCellListener();
-        EventRecordFactory erf = new EventRecordFactory(mcListener, new short[] { MergeCellsRecord.sid,
});
-//        POIFSFileSystem poifs = new POIFSFileSystem(new ByteArrayInputStream(sheetData));
-        erf.processRecords(new ByteArrayInputStream(sheetData));
+        sheet.visitContainedRecords(mcListener, 0);
         int recordsAdded    = mcListener.getCount();
         int recordsExpected = regionsToAdd/1027;
         if ((regionsToAdd % 1027) != 0)
@@ -416,6 +411,27 @@
         assertEquals(DEFAULT_IDX, xfindex);
     }
 
+    private static final class SizeCheckingRecordVisitor implements RecordVisitor {
+
+        private int _totalSize;
+        public SizeCheckingRecordVisitor() {
+            _totalSize = 0;
+        }
+        public void visitRecord(Record r) {
+
+            int estimatedSize=r.getRecordSize();
+            byte[] buf = new byte[estimatedSize];
+            int serializedSize = r.serialize(0, buf);
+            if (estimatedSize != serializedSize) {
+                throw new AssertionFailedError("serialized size mismatch for record (" 
+                        + r.getClass().getName() + ")");
+            }
+            _totalSize += estimatedSize;
+        }
+        public int getTotalSize() {
+            return _totalSize;
+        }
+    }
     /**
      * Prior to bug 45066, POI would get the estimated sheet size wrong
      * when an <tt>UncalcedRecord</tt> was present.<p/>
@@ -429,13 +445,13 @@
         records.add(createWindow2Record());
         records.add(EOFRecord.instance);
         Sheet sheet = Sheet.createSheet(records, 0, 0);
-
-        int estimatedSize = sheet.getSize();
-        int serializedSize = sheet.serialize(0, new byte[estimatedSize]);
-        if (serializedSize != estimatedSize) {
-            throw new AssertionFailedError("Identified bug 45066 b");
-        }
-        assertEquals(90, serializedSize);
+        
+        // The original bug was due to different logic for collecting records for sizing
and 
+        // serialization. The code has since been refactored into a single method for visiting
+        // all contained records.  Now this test is much less interesting
+        SizeCheckingRecordVisitor scrv = new SizeCheckingRecordVisitor();
+        sheet.visitContainedRecords(scrv, 0);
+        assertEquals(90, scrv.getTotalSize());
     }
 
     /**
@@ -479,31 +495,31 @@
      * That value is found on the IndexRecord.
      */
     private static int getDbCellRecordPos(Sheet sheet) {
-        int size = sheet.getSize();
-        byte[] data = new byte[size];
-        sheet.serialize(0, data);
 
         MyIndexRecordListener myIndexListener = new MyIndexRecordListener();
-        EventRecordFactory erf = new EventRecordFactory(myIndexListener, new short[] { IndexRecord.sid,
});
-        erf.processRecords(new ByteArrayInputStream(data));
+        sheet.visitContainedRecords(myIndexListener, 0);
         IndexRecord indexRecord = myIndexListener.getIndexRecord();
         int dbCellRecordPos = indexRecord.getDbcellAt(0);
         return dbCellRecordPos;
     }
 
-    private static final class MyIndexRecordListener implements ERFListener {
+    private static final class MyIndexRecordListener implements RecordVisitor {
 
         private IndexRecord _indexRecord;
         public MyIndexRecordListener() {
             // no-arg constructor
         }
-        public boolean processRecord(Record rec) {
-            _indexRecord = (IndexRecord)rec;
-            return true;
-        }
         public IndexRecord getIndexRecord() {
             return _indexRecord;
         }
+        public void visitRecord(Record r) {
+            if (r instanceof IndexRecord) {
+                if (_indexRecord != null) {
+                    throw new RuntimeException("too many index records");
+                }
+                _indexRecord = (IndexRecord)r;
+            }
+        }
     }
     
     /**
@@ -541,5 +557,23 @@
         }
         assertEquals("Informations", cell.getRichStringCellValue().getString());
     }
+    /**
+     * In 3.1, setting margins between creating first row and first cell caused an exception.
+     */
+    public void testSetMargins_bug45717() {
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet("Vorschauliste");
+        HSSFRow row = sheet.createRow(0);
+
+        sheet.setMargin(HSSFSheet.LeftMargin, 0.3);
+        try {
+            row.createCell((short) 0);
+        } catch (IllegalStateException e) {
+            if (e.getMessage().equals("Cannot create value records before row records exist"))
{
+                throw new AssertionFailedError("Identified bug 45717");
+            }
+            throw e;
+        }
+    }
 }
 

Modified: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
(original)
+++ poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
Sun Aug 31 10:08:51 2008
@@ -15,13 +15,10 @@
    limitations under the License.
 ==================================================================== */
 
-/*
- * TestFormulaRecordAggregate.java
- *
- * Created on March 21, 2003, 12:32 AM
- */
-
 package org.apache.poi.hssf.record.aggregates;
+
+import junit.framework.TestCase;
+
 import org.apache.poi.hssf.record.FormulaRecord;
 import org.apache.poi.hssf.record.StringRecord;
 
@@ -29,14 +26,13 @@
  *
  * @author  avik
  */
-public final class TestFormulaRecordAggregate extends junit.framework.TestCase {
+public final class TestFormulaRecordAggregate extends TestCase {
     
     public void testBasic() throws Exception {
         FormulaRecord f = new FormulaRecord();
         StringRecord s = new StringRecord();
         s.setString("abc");
-        FormulaRecordAggregate fagg = new FormulaRecordAggregate(f);
-        fagg.setStringRecord(s);
+        FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.EMPTY);
         assertEquals("abc", fagg.getStringValue());
     }
 }

Modified: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java
(original)
+++ poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java
Sun Aug 31 10:08:51 2008
@@ -17,25 +17,100 @@
 
 package org.apache.poi.hssf.record.aggregates;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.record.ArrayRecord;
+import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.RowRecord;
+import org.apache.poi.hssf.record.SharedFormulaRecord;
+import org.apache.poi.hssf.record.SharedValueRecordBase;
+import org.apache.poi.hssf.record.TableRecord;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.RecordInspector;
+import org.apache.poi.hssf.util.CellRangeAddress8Bit;
 
 /**
  * 
  */
 public final class TestRowRecordsAggregate extends TestCase {
-    
-    public void testRowGet() {
-        RowRecordsAggregate rra = new RowRecordsAggregate();
-        RowRecord rr = new RowRecord(4);
-        rra.insertRow(rr);
-        rra.insertRow(new RowRecord(1));
-        
-        RowRecord rr1 = rra.getRow(4);
-        
-        assertNotNull(rr1);
-        assertEquals("Row number is 1", 4, rr1.getRowNumber());
-        assertTrue("Row record retrieved is identical ", rr1 == rr);
-    }
+
+	public void testRowGet() {
+		RowRecordsAggregate rra = new RowRecordsAggregate();
+		RowRecord rr = new RowRecord(4);
+		rra.insertRow(rr);
+		rra.insertRow(new RowRecord(1));
+
+		RowRecord rr1 = rra.getRow(4);
+
+		assertNotNull(rr1);
+		assertEquals("Row number is 1", 4, rr1.getRowNumber());
+		assertTrue("Row record retrieved is identical ", rr1 == rr);
+	}
+
+	/**
+	 * Prior to Aug 2008, POI would re-serialize spreadsheets with {@link ArrayRecord}s or
+	 * {@link TableRecord}s with those records out of order.  Similar to 
+	 * {@link SharedFormulaRecord}s, these records should appear immediately after the first
+	 * {@link FormulaRecord}s that they apply to (and only once).<br/>
+	 */
+	public void testArraysAndTables() {
+		HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls");
+		Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0);
+		
+		int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class);
+		assertEquals(5, countArrayFormulas);
+		int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class);
+		assertEquals(3, countTableFormulas);
+
+		// Note - SharedFormulaRecords are currently not re-serialized by POI (each is extracted
+		// into many non-shared formulas), but if they ever were, the same rules would apply. 
+		int countSharedFormulas = verifySharedValues(sheetRecs, SharedFormulaRecord.class);
+		assertEquals(0, countSharedFormulas);
+		
+
+		if (false) { // set true to observe re-serialized file
+			File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls");
+			try {
+				OutputStream os = new FileOutputStream(f);
+				wb.write(os);
+				os.close();
+			} catch (IOException e) {
+				throw new RuntimeException(e);
+			}
+			System.out.println("Output file to " + f.getAbsolutePath());
+		}
+	}
+
+	private static int verifySharedValues(Record[] recs, Class shfClass) {
+		
+		int result =0;
+		for(int i=0; i<recs.length; i++) {
+			Record rec = recs[i];
+			if (rec.getClass() == shfClass) {
+				result++;
+				Record prevRec = recs[i-1];
+				if (!(prevRec instanceof FormulaRecord)) {
+					throw new AssertionFailedError("Bad record order at index "
+							+ i + ": Formula record expected but got (" 
+							+ prevRec.getClass().getName() + ")");
+				}
+				verifySharedFormula((FormulaRecord) prevRec, rec);
+			}
+		}
+		return result;
+	}
+
+	private static void verifySharedFormula(FormulaRecord firstFormula, Record rec) {
+		CellRangeAddress8Bit range = ((SharedValueRecordBase)rec).getRange();
+		assertEquals(range.getFirstRow(), firstFormula.getRow());
+		assertEquals(range.getFirstColumn(), firstFormula.getColumn());
+	}
 }

Modified: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestValueRecordsAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestValueRecordsAggregate.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestValueRecordsAggregate.java
(original)
+++ poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestValueRecordsAggregate.java
Sun Aug 31 10:08:51 2008
@@ -28,10 +28,15 @@
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.model.RecordStream;
+import org.apache.poi.hssf.model.RowBlocksReader;
 import org.apache.poi.hssf.record.BlankRecord;
+import org.apache.poi.hssf.record.CellValueRecordInterface;
 import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.RecordBase;
 import org.apache.poi.hssf.record.SharedFormulaRecord;
+import org.apache.poi.hssf.record.WindowTwoRecord;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
@@ -47,6 +52,7 @@
         List records = new ArrayList();
         records.add( new FormulaRecord() );
         records.add( new SharedFormulaRecord() );
+        records.add(new WindowTwoRecord());
 
         constructValueRecord(records);
         Iterator iterator = valueRecord.getIterator();
@@ -59,8 +65,13 @@
     }
 
     private void constructValueRecord(List records) {
-        SharedFormulaHolder sfrh = SharedFormulaHolder.create(records, 0, records.size());
-        valueRecord.construct(records, 0, records.size(), sfrh );
+        RowBlocksReader rbr = new RowBlocksReader(records, 0);
+        SharedValueManager sfrh = rbr.getSharedFormulaManager();
+        RecordStream rs = rbr.getPlainRecordStream();
+        while(rs.hasNext()) {
+            Record rec = rs.getNext();
+            valueRecord.construct((CellValueRecordInterface)rec, rs, sfrh);
+        }
     }
 
     private static List testData() {
@@ -73,6 +84,7 @@
         blankRecord.setColumn( (short) 2 );
         records.add( formulaRecord );
         records.add( blankRecord );
+        records.add(new WindowTwoRecord());
         return records;
     }
 

Modified: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java?rev=690732&r1=690731&r2=690732&view=diff
==============================================================================
--- poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java (original)
+++ poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java Sun Aug
31 10:08:51 2008
@@ -17,23 +17,19 @@
 
 package org.apache.poi.hssf.usermodel;
 
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.Iterator;
 
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.eventmodel.ERFListener;
-import org.apache.poi.hssf.eventmodel.EventRecordFactory;
 import org.apache.poi.hssf.model.Workbook;
 import org.apache.poi.hssf.record.BackupRecord;
 import org.apache.poi.hssf.record.LabelSSTRecord;
 import org.apache.poi.hssf.record.Record;
-import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
+import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.Region;
@@ -73,10 +69,7 @@
      *             HSSFSheet last row or first row is incorrect.             <P>
      *
      */
-
-    public void testWriteSheetSimple()
-        throws IOException
-    {
+    public void testWriteSheetSimple() throws IOException {
         File             file = TempFile.createTempFile("testWriteSheetSimple",
                                                     ".xls");
         FileOutputStream out  = new FileOutputStream(file);
@@ -85,13 +78,10 @@
         HSSFRow          r    = null;
         HSSFCell         c    = null;
 
-        for (short rownum = ( short ) 0; rownum < 100; rownum++)
-        {
+        for (int rownum = 0; rownum < 100; rownum++) {
             r = s.createRow(rownum);
 
-            // r.setRowNum(( short ) rownum);
-            for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
-            {
+            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                 c = r.createCell(cellnum);
                 c.setCellValue(rownum * 10000 + cellnum
                                + ((( double ) rownum / 1000)
@@ -105,8 +95,6 @@
         sanityChecker.checkHSSFWorkbook(wb);
         assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
         assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
-
-        // assert((s.getLastRowNum() == 99));
     }
 
     /**
@@ -131,13 +119,10 @@
         HSSFRow          r    = null;
         HSSFCell         c    = null;
 
-        for (short rownum = ( short ) 0; rownum < 100; rownum++)
-        {
+        for (int rownum = 0; rownum < 100; rownum++) {
             r = s.createRow(rownum);
 
-            // r.setRowNum(( short ) rownum);
-            for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
-            {
+            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                 c = r.createCell(cellnum);
                 c.setCellValue(rownum * 10000 + cellnum
                                + ((( double ) rownum / 1000)
@@ -146,13 +131,11 @@
                 c.setCellValue(new HSSFRichTextString("TEST"));
             }
         }
-        for (short rownum = ( short ) 0; rownum < 25; rownum++)
-        {
+        for (int rownum = 0; rownum < 25; rownum++) {
             r = s.getRow(rownum);
             s.removeRow(r);
         }
-        for (short rownum = ( short ) 75; rownum < 100; rownum++)
-        {
+        for (int rownum = 75; rownum < 100; rownum++) {
             r = s.getRow(rownum);
             s.removeRow(r);
         }
@@ -429,12 +412,10 @@
         HSSFWorkbook     wb   = new HSSFWorkbook();
         HSSFSheet        s    = wb.createSheet();
 
-        for (short rownum = ( short ) 0; rownum < 100; rownum++)
-        {
+        for (int rownum = 0; rownum < 100; rownum++) {
             HSSFRow r = s.createRow(rownum);
 
-            for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)
-            {
+            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                 HSSFCell c = r.createCell(cellnum);
                 c.setCellValue(rownum * 10000 + cellnum
                                + ((( double ) rownum / 1000)
@@ -466,13 +447,10 @@
     /**
      * Test the backup field gets set as expected.
      */
-
-    public void testBackupRecord()
-        throws Exception
-    {
-        HSSFWorkbook wb       = new HSSFWorkbook();
-        wb.createSheet();
-        Workbook     workbook = wb.getWorkbook();
+    public void testBackupRecord() {
+        HSSFWorkbook wb = new HSSFWorkbook();
+		wb.createSheet();
+		Workbook workbook = wb.getWorkbook();
         BackupRecord record   = workbook.getBackupRecord();
 
         assertEquals(0, record.getBackup());
@@ -480,7 +458,7 @@
         assertEquals(1, record.getBackup());
     }
 
-    private static final class RecordCounter implements ERFListener {
+    private static final class RecordCounter implements RecordVisitor {
         private int _count;
 
         public RecordCounter() {
@@ -489,9 +467,10 @@
         public int getCount() {
             return _count;
         }
-        public boolean processRecord(Record rec) {
-            _count++;
-            return true;
+        public void visitRecord(Record r) {
+            if (r instanceof LabelSSTRecord) {
+                _count++;
+            }
         }
     }
     
@@ -500,9 +479,7 @@
      *
      * We need to make sure only one LabelSSTRecord is produced.
      */
-    public void testRepeatingBug()
-        throws Exception
-    {
+    public void testRepeatingBug() {
         HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet    sheet    = workbook.createSheet("Design Variants");
         HSSFRow      row      = sheet.createRow(2);
@@ -511,12 +488,8 @@
         cell.setCellValue(new HSSFRichTextString("Class"));
         cell = row.createCell(2);
 
-        byte[] data = new byte[sheet.getSheet().getSize()];
-        sheet.getSheet().serialize(0, data);
         RecordCounter rc = new RecordCounter();
-        EventRecordFactory erf = new EventRecordFactory(rc, new short[] { LabelSSTRecord.sid,
});
-        erf.processRecords(new ByteArrayInputStream(data));
-        
+        sheet.getSheet().visitContainedRecords(rc, 0);
         assertEquals(1, rc.getCount());
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message