aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java
diff options
context:
space:
mode:
authorEvgeniy Berlog <berlog@apache.org>2012-07-01 09:38:08 +0000
committerEvgeniy Berlog <berlog@apache.org>2012-07-01 09:38:08 +0000
commit5f543636b754773ab470a104ae6acd36102c7cb9 (patch)
treec56d66d54fdd78720f2627bdd46c311b92021b88 /src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java
parentfe51bb989b1e85d48d22a52974d40946eac325eb (diff)
downloadpoi-5f543636b754773ab470a104ae6acd36102c7cb9.tar.gz
poi-5f543636b754773ab470a104ae6acd36102c7cb9.zip
implemented work with existing shape groups and polygons
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1355866 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java b/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java
new file mode 100644
index 0000000000..bdbf25fc4d
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java
@@ -0,0 +1,225 @@
+package org.apache.poi.hssf.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.record.ObjRecord;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Evgeniy Berlog
+ * @date 29.06.12
+ */
+public class TestShapeGroup extends TestCase{
+
+ public void testResultEqualsToAbstractShape() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+ HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
+
+ EscherContainerRecord container = new EscherContainerRecord();
+ Map shapeToObj = new HashMap();
+ HSSFTestHelper.convertHSSFGroup(group, container, shapeToObj);
+
+ byte [] actual = group.getEscherContainer().serialize();
+ byte [] expected = container.getChild(0).serialize();
+
+ assertEquals(actual.length, expected.length);
+ assertTrue(Arrays.equals(actual, expected));
+
+ actual = group.getObjRecord().serialize();
+ expected = ((ObjRecord)shapeToObj.values().toArray()[0]).serialize();
+
+ assertEquals(actual.length, expected.length);
+ assertTrue(Arrays.equals(actual, expected));
+ }
+
+ public void testSetGetCoordinates(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+ HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
+ assertEquals(group.getX1(), 0);
+ assertEquals(group.getY1(), 0);
+ assertEquals(group.getX2(), 1023);
+ assertEquals(group.getY2(), 255);
+
+ group.setCoordinates(1,2,3,4);
+
+ assertEquals(group.getX1(), 1);
+ assertEquals(group.getY1(), 2);
+ assertEquals(group.getX2(), 3);
+ assertEquals(group.getY2(), 4);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ group = (HSSFShapeGroup) patriarch.getChildren().get(0);
+ assertEquals(group.getX1(), 1);
+ assertEquals(group.getY1(), 2);
+ assertEquals(group.getX2(), 3);
+ assertEquals(group.getY2(), 4);
+ }
+
+ public void testAddToExistingFile(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+ HSSFShapeGroup group1 = patriarch.createGroup(new HSSFClientAnchor());
+ HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
+
+ group1.setCoordinates(1,2,3,4);
+ group2.setCoordinates(5,6,7,8);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 2);
+
+ HSSFShapeGroup group3 = patriarch.createGroup(new HSSFClientAnchor());
+ group3.setCoordinates(9,10,11,12);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 3);
+ }
+
+ public void testModify() throws Exception {
+ HSSFWorkbook wb = new HSSFWorkbook();
+
+ // create a sheet with a text box
+ HSSFSheet sheet = wb.createSheet();
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+ HSSFShapeGroup group1 = patriarch.createGroup(new
+ HSSFClientAnchor(0,0,0,0,
+ (short)0, 0, (short)15, 25));
+ group1.setCoordinates(0, 0, 792, 612);
+
+ HSSFTextbox textbox1 = group1.createTextbox(new
+ HSSFChildAnchor(100, 100, 300, 300));
+ HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!");
+ textbox1.setString(rt1);
+
+ // write, read back and check that our text box is there
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ patriarch = sheet.getDrawingPatriarch();
+ assertEquals(1, patriarch.getChildren().size());
+
+ group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
+ assertEquals(1, group1.getChildren().size());
+ textbox1 = (HSSFTextbox)group1.getChildren().get(0);
+ assertEquals("Hello, World!", textbox1.getString().getString());
+
+ // modify anchor
+ assertEquals(new HSSFChildAnchor(100, 100, 300, 300),
+ textbox1.getAnchor());
+ HSSFChildAnchor newAnchor = new HSSFChildAnchor(200,200, 400, 400);
+ textbox1.setAnchor(newAnchor);
+ // modify text
+ textbox1.setString(new HSSFRichTextString("Hello, World! (modified)"));
+
+ // add a new text box
+ HSSFTextbox textbox2 = group1.createTextbox(new
+ HSSFChildAnchor(400, 400, 600, 600));
+ HSSFRichTextString rt2 = new HSSFRichTextString("Hello, World-2");
+ textbox2.setString(rt2);
+ assertEquals(2, group1.getChildren().size());
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ patriarch = sheet.getDrawingPatriarch();
+ assertEquals(1, patriarch.getChildren().size());
+
+ group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
+ assertEquals(2, group1.getChildren().size());
+ textbox1 = (HSSFTextbox)group1.getChildren().get(0);
+ assertEquals("Hello, World! (modified)",
+ textbox1.getString().getString());
+ assertEquals(new HSSFChildAnchor(200,200, 400, 400),
+ textbox1.getAnchor());
+
+ textbox2 = (HSSFTextbox)group1.getChildren().get(1);
+ assertEquals("Hello, World-2", textbox2.getString().getString());
+ assertEquals(new HSSFChildAnchor(400, 400, 600, 600),
+ textbox2.getAnchor());
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ patriarch = sheet.getDrawingPatriarch();
+ group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
+ textbox1 = (HSSFTextbox)group1.getChildren().get(0);
+ textbox2 = (HSSFTextbox)group1.getChildren().get(1);
+ HSSFTextbox textbox3 = group1.createTextbox(new
+ HSSFChildAnchor(400,200, 600, 400));
+ HSSFRichTextString rt3 = new HSSFRichTextString("Hello, World-3");
+ textbox3.setString(rt3);
+ }
+
+ public void testAddShapesToGroup(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+
+ // create a sheet with a text box
+ HSSFSheet sheet = wb.createSheet();
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+ HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
+ int index = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ group.createPicture(new HSSFChildAnchor(), index);
+ HSSFPolygon polygon = group.createPolygon(new HSSFChildAnchor());
+ polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
+ group.createTextbox(new HSSFChildAnchor());
+ group.createShape(new HSSFChildAnchor());
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ patriarch = sheet.getDrawingPatriarch();
+ assertEquals(1, patriarch.getChildren().size());
+
+ assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
+ group = (HSSFShapeGroup) patriarch.getChildren().get(0);
+
+ assertEquals(group.getChildren().size(), 4);
+
+ assertTrue(group.getChildren().get(0) instanceof HSSFPicture);
+ assertTrue(group.getChildren().get(1) instanceof HSSFPolygon);
+ assertTrue(group.getChildren().get(2) instanceof HSSFTextbox);
+ assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape);
+
+ HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
+
+ index = wb.addPicture(new byte[]{2,2,2}, HSSFWorkbook.PICTURE_TYPE_JPEG);
+ group2.createPicture(new HSSFChildAnchor(), index);
+ polygon = group2.createPolygon(new HSSFChildAnchor());
+ polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
+ group2.createTextbox(new HSSFChildAnchor());
+ group2.createShape(new HSSFChildAnchor());
+ group2.createShape(new HSSFChildAnchor());
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ patriarch = sheet.getDrawingPatriarch();
+ assertEquals(2, patriarch.getChildren().size());
+
+ group = (HSSFShapeGroup) patriarch.getChildren().get(1);
+
+ assertEquals(group.getChildren().size(), 5);
+
+ assertTrue(group.getChildren().get(0) instanceof HSSFPicture);
+ assertTrue(group.getChildren().get(1) instanceof HSSFPolygon);
+ assertTrue(group.getChildren().get(2) instanceof HSSFTextbox);
+ assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape);
+ assertTrue(group.getChildren().get(4) instanceof HSSFSimpleShape);
+
+ group.getShapeId();
+ }
+}