aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache
diff options
context:
space:
mode:
authorEvgeniy Berlog <berlog@apache.org>2012-06-22 09:37:17 +0000
committerEvgeniy Berlog <berlog@apache.org>2012-06-22 09:37:17 +0000
commit7efc80c3e8945546bb7fa4fa019e9f6449e5b55b (patch)
tree0ce321aab8a9b4c3e41587a76032b89c5c33d144 /src/testcases/org/apache
parent93b0bb98bfcd57fd0bbc3acc663296c77aea085b (diff)
downloadpoi-7efc80c3e8945546bb7fa4fa019e9f6449e5b55b.tar.gz
poi-7efc80c3e8945546bb7fa4fa019e9f6449e5b55b.zip
added support for HSSFPicture in HSSFShapeFactory, fixed bugs, added tests
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1352818 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache')
-rw-r--r--src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java36
-rw-r--r--src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java94
-rw-r--r--src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java175
3 files changed, 302 insertions, 3 deletions
diff --git a/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java b/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java
index 43cdd8b856..18572d6ec1 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java
@@ -43,6 +43,8 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -192,6 +194,40 @@ public class TestDrawingAggregate extends TestCase {
}
}
+ public void testBuildBaseTree(){
+ EscherAggregate agg = new EscherAggregate();
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+
+ HSSFPatriarch drawing = sheet.createDrawingPatriarch();
+ EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(drawing);
+ callConvertPatriarch(agg1);
+ agg1.setPatriarch(null);
+
+ agg.setPatriarch(null);
+
+ byte[] aggS = agg.serialize();
+ byte []agg1S = agg1.serialize();
+
+ assertEquals(aggS.length, agg1S.length);
+ assertTrue(Arrays.equals(aggS, agg1S));
+ }
+
+ private static void callConvertPatriarch(EscherAggregate agg) {
+ Method method = null;
+ try {
+ method = agg.getClass().getDeclaredMethod("convertPatriarch", HSSFPatriarch.class);
+ method.setAccessible(true);
+ method.invoke(agg, agg.getPatriarch());
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ } catch (InvocationTargetException e) {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+
/**
* when reading incomplete data ensure that the serialized bytes
match the source
diff --git a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
index f276cba391..097d5d01ee 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
@@ -3,6 +3,9 @@ package org.apache.poi.hssf.model;
import junit.framework.TestCase;
import org.apache.poi.ddf.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
+import org.apache.poi.hssf.record.EscherAggregate;
+import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.util.HexDump;
@@ -72,6 +75,38 @@ public class TestDrawingShapes extends TestCase{
assertEquals(true,
((EscherBoolProperty)opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());
}
+
+ public void testDefaultPictureSettings(){
+ HSSFPicture picture = new HSSFPicture(null, new HSSFClientAnchor());
+ assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
+ assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
+ assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_SOLID);
+ assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
+ assertEquals(picture.isNoFill(), false);
+ assertEquals(picture.getPictureIndex(), -1);//not set yet
+ }
+
+ /**
+ * No NullPointerException should appear
+ */
+ public void testDefaultSettingsWithEmptyContainer(){
+ EscherContainerRecord container = new EscherContainerRecord();
+ EscherOptRecord opt = new EscherOptRecord();
+ opt.setRecordId(EscherOptRecord.RECORD_ID);
+ container.addChildRecord(opt);
+ ObjRecord obj = new ObjRecord();
+ CommonObjectDataSubRecord cod = new CommonObjectDataSubRecord();
+ cod.setObjectType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);
+ obj.addSubRecord(cod);
+ HSSFPicture picture = new HSSFPicture(container, obj);
+
+ assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
+ assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
+ assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
+ assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
+ assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);
+ assertEquals(picture.getPictureIndex(), -1);//not set yet
+ }
/**
* create a rectangle, save the workbook, read back and verify that all shape properties are there
*/
@@ -139,6 +174,28 @@ public class TestDrawingShapes extends TestCase{
assertEquals(rectangle2.getAnchor().getDy1(), 4);
assertEquals(rectangle2.getAnchor().getDy2(), 5);
assertEquals(rectangle2.isNoFill(), false);
+
+ HSSFSimpleShape rect3 = drawing.createSimpleShape(new HSSFClientAnchor());
+ rect3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ drawing = wb.getSheetAt(0).getDrawingPatriarch();
+ assertEquals(drawing.getChildren().size(), 2);
+ }
+
+ public void testReadExistingImage(){
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
+ HSSFSheet sheet = wb.getSheet("pictures");
+ HSSFPatriarch drawing = sheet.getDrawingPatriarch();
+ assertEquals(1, drawing.getChildren().size());
+ HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0);
+
+ assertEquals(picture.getPictureIndex(), 1);
+ assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
+ assertEquals(picture.getFillColor(), 0x5DC943);
+ assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
+ assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
+ assertEquals(picture.isNoFill(), true);
}
@@ -157,4 +214,41 @@ public class TestDrawingShapes extends TestCase{
assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT*2);
}
}
+
+ public void testShapeIds() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet1 = wb.createSheet();
+ HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch();
+ for(int i = 0; i < 2; i++) {
+ patriarch1.createSimpleShape(new HSSFClientAnchor());
+ }
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet1 = wb.getSheetAt(0);
+ patriarch1 = sheet1.getDrawingPatriarch();
+
+ EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1);
+ // last shape ID cached in EscherDgRecord
+ EscherDgRecord dg1 =
+ agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
+ assertEquals(1026, dg1.getLastMSOSPID());
+
+ // iterate over shapes and check shapeId
+ EscherContainerRecord spgrContainer =
+ agg1.getEscherContainer().getChildContainers().get(0);
+ // root spContainer + 2 spContainers for shapes
+ assertEquals(3, spgrContainer.getChildRecords().size());
+
+ EscherSpRecord sp0 =
+ ((EscherContainerRecord)spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);
+ assertEquals(1024, sp0.getShapeId());
+
+ EscherSpRecord sp1 =
+ ((EscherContainerRecord)spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);
+ assertEquals(1025, sp1.getShapeId());
+
+ EscherSpRecord sp2 =
+ ((EscherContainerRecord)spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);
+ assertEquals(1026, sp2.getShapeId());
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java b/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
index ef90f01f40..248247266a 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
@@ -1,10 +1,10 @@
package org.apache.poi.hssf.model;
import junit.framework.TestCase;
-import org.apache.poi.ddf.EscherChildAnchorRecord;
-import org.apache.poi.ddf.EscherClientAnchorRecord;
-import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.util.HexDump;
/**
* @author Evgeniy Berlog
@@ -12,6 +12,63 @@ import org.apache.poi.hssf.usermodel.*;
*/
public class TestHSSFAnchor extends TestCase {
+ public void testDefaultValues(){
+ HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
+ assertEquals(clientAnchor.getAnchorType(), 0);
+ assertEquals(clientAnchor.getCol1(), 0);
+ assertEquals(clientAnchor.getCol2(), 0);
+ assertEquals(clientAnchor.getDx1(), 0);
+ assertEquals(clientAnchor.getDx2(), 0);
+ assertEquals(clientAnchor.getDy1(), 0);
+ assertEquals(clientAnchor.getDy2(), 0);
+ assertEquals(clientAnchor.getRow1(), 0);
+ assertEquals(clientAnchor.getRow2(), 0);
+
+ clientAnchor = new HSSFClientAnchor(new EscherClientAnchorRecord());
+ assertEquals(clientAnchor.getAnchorType(), 0);
+ assertEquals(clientAnchor.getCol1(), 0);
+ assertEquals(clientAnchor.getCol2(), 0);
+ assertEquals(clientAnchor.getDx1(), 0);
+ assertEquals(clientAnchor.getDx2(), 0);
+ assertEquals(clientAnchor.getDy1(), 0);
+ assertEquals(clientAnchor.getDy2(), 0);
+ assertEquals(clientAnchor.getRow1(), 0);
+ assertEquals(clientAnchor.getRow2(), 0);
+
+ HSSFChildAnchor childAnchor = new HSSFChildAnchor();
+ assertEquals(childAnchor.getDx1(), 0);
+ assertEquals(childAnchor.getDx2(), 0);
+ assertEquals(childAnchor.getDy1(), 0);
+ assertEquals(childAnchor.getDy2(), 0);
+
+ childAnchor = new HSSFChildAnchor(new EscherChildAnchorRecord());
+ assertEquals(childAnchor.getDx1(), 0);
+ assertEquals(childAnchor.getDx2(), 0);
+ assertEquals(childAnchor.getDy1(), 0);
+ assertEquals(childAnchor.getDy2(), 0);
+ }
+
+ public void testCorrectOrderInSpContainer(){
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
+ HSSFSheet sheet = wb.getSheet("pictures");
+ HSSFPatriarch drawing = sheet.getDrawingPatriarch();
+
+ HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);
+ rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
+
+ assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);
+ assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);
+ assertEquals(" " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + " ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
+ assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);
+
+ rectangle.setAnchor(new HSSFClientAnchor());
+
+ assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);
+ assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);
+ assertEquals(" " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + " ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
+ assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);
+ }
+
public void testCreateClientAnchorFromContainer(){
EscherContainerRecord container = new EscherContainerRecord();
EscherClientAnchorRecord escher = new EscherClientAnchorRecord();
@@ -206,4 +263,116 @@ public class TestHSSFAnchor extends TestCase {
assertEquals(anchor.getDy2(), 118);
assertEquals(escher.getDy2(), 118);
}
+
+ public void testEqualsToSelf(){
+ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ assertEquals(clientAnchor, clientAnchor);
+
+ HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
+ assertEquals(childAnchor, childAnchor);
+ }
+
+ public void testPassIncompatibleTypeIsFalse(){
+ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ assertNotSame(clientAnchor, "wrongType");
+
+ HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
+ assertNotSame(childAnchor, "wrongType");
+ }
+
+ public void testNullReferenceIsFalse() {
+ HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ assertFalse("Passing null to equals should return false", clientAnchor.equals(null));
+
+ HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);
+ assertFalse("Passing null to equals should return false", childAnchor.equals(null));
+ }
+
+ public void testEqualsIsReflexiveIsSymmetric() {
+ HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+
+ assertTrue(clientAnchor1.equals(clientAnchor2));
+ assertTrue(clientAnchor1.equals(clientAnchor2));
+
+ HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);
+ HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);
+
+ assertTrue(childAnchor1.equals(childAnchor2));
+ assertTrue(childAnchor2.equals(childAnchor1));
+ }
+
+ public void testEqualsValues(){
+ HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setDx1(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setDx1(0);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setDy1(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setDy1(1);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setDx2(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setDx2(2);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setDy2(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setDy2(3);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setCol1(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setCol1(4);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setRow1(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setRow1(5);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setCol2(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setCol2(6);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setRow2(10);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setRow2(7);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ clientAnchor2.setAnchorType(3);
+ assertNotSame(clientAnchor1, clientAnchor2);
+ clientAnchor2.setAnchorType(0);
+ assertEquals(clientAnchor1, clientAnchor2);
+
+ HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);
+ HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);
+
+ childAnchor1.setDx1(10);
+ assertNotSame(childAnchor1, childAnchor2);
+ childAnchor1.setDx1(0);
+ assertEquals(childAnchor1, childAnchor2);
+
+ childAnchor2.setDy1(10);
+ assertNotSame(childAnchor1, childAnchor2);
+ childAnchor2.setDy1(1);
+ assertEquals(childAnchor1, childAnchor2);
+
+ childAnchor2.setDx2(10);
+ assertNotSame(childAnchor1, childAnchor2);
+ childAnchor2.setDx2(2);
+ assertEquals(childAnchor1, childAnchor2);
+
+ childAnchor2.setDy2(10);
+ assertNotSame(childAnchor1, childAnchor2);
+ childAnchor2.setDy2(3);
+ assertEquals(childAnchor1, childAnchor2);
+ }
}