diff options
author | Evgeniy Berlog <berlog@apache.org> | 2012-07-01 09:38:08 +0000 |
---|---|---|
committer | Evgeniy Berlog <berlog@apache.org> | 2012-07-01 09:38:08 +0000 |
commit | 5f543636b754773ab470a104ae6acd36102c7cb9 (patch) | |
tree | c56d66d54fdd78720f2627bdd46c311b92021b88 /src/testcases/org/apache/poi | |
parent | fe51bb989b1e85d48d22a52974d40946eac325eb (diff) | |
download | poi-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')
5 files changed, 516 insertions, 15 deletions
diff --git a/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java b/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java index 507561e7e4..bc22d17a97 100644 --- a/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java +++ b/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java @@ -1,6 +1,7 @@ package org.apache.poi.hssf.model;
import org.apache.poi.hssf.usermodel.HSSFComment;
+import org.apache.poi.hssf.usermodel.HSSFPolygon;
import org.apache.poi.hssf.usermodel.HSSFTextbox;
/**
@@ -15,4 +16,8 @@ public class HSSFTestModelHelper { public static CommentShape createCommentShape(int shapeId, HSSFComment comment){
return new CommentShape(comment, shapeId);
}
+
+ public static PolygonShape createPolygonShape(int shapeId, HSSFPolygon polygon){
+ return new PolygonShape(polygon, shapeId);
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java b/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java index 18572d6ec1..1f058c06da 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java +++ b/src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java @@ -201,7 +201,7 @@ public class TestDrawingAggregate extends TestCase { HSSFPatriarch drawing = sheet.createDrawingPatriarch();
EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(drawing);
- callConvertPatriarch(agg1);
+ HSSFTestHelper.callConvertPatriarch(agg1);
agg1.setPatriarch(null);
agg.setPatriarch(null);
@@ -213,20 +213,7 @@ public class TestDrawingAggregate extends TestCase { 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
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java index 9b2273c590..214121d940 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java @@ -16,17 +16,40 @@ ==================================================================== */ package org.apache.poi.hssf.usermodel; +import org.apache.poi.ddf.EscherContainerRecord; +import org.apache.poi.ddf.EscherDggRecord; import org.apache.poi.ddf.EscherOptRecord; +import org.apache.poi.hssf.model.DrawingManager2; import org.apache.poi.hssf.model.InternalSheet; import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.hssf.record.EscherAggregate; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + /** * Helper class for HSSF tests that aren't within the * HSSF UserModel package, but need to do internal * UserModel things. */ public class HSSFTestHelper { + + private static class MockDrawingManager extends DrawingManager2 { +// +// public MockDrawingManager(EscherDggRecord dgg) { +// super(dgg); +// } + + public MockDrawingManager (){ + super(null); + } + + @Override + public int allocateShapeId(short drawingGroupId) { + return 0; //Mock value + } + } /** * Lets non UserModel tests at the low level Workbook */ @@ -52,4 +75,34 @@ public class HSSFTestHelper { public static EscherOptRecord getOptRecord(HSSFShape shape){ return shape._optRecord; } + + public static void convertHSSFGroup(HSSFShapeGroup shape, EscherContainerRecord escherParent, Map shapeToObj){ + Class clazz = EscherAggregate.class; + try { + Method method = clazz.getDeclaredMethod("convertGroup", HSSFShapeGroup.class, EscherContainerRecord.class, Map.class); + method.setAccessible(true); + method.invoke(new EscherAggregate(new MockDrawingManager()), shape, escherParent, shapeToObj); + } 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. + } catch (IllegalAccessException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + public 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. + } + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestPolygon.java b/src/testcases/org/apache/poi/hssf/usermodel/TestPolygon.java new file mode 100644 index 0000000000..7c83a0d25f --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestPolygon.java @@ -0,0 +1,231 @@ +package org.apache.poi.hssf.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ddf.EscherArrayProperty;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherSpRecord;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.model.HSSFTestModelHelper;
+import org.apache.poi.hssf.model.PolygonShape;
+import org.apache.poi.hssf.record.ObjRecord;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * @author Evgeniy Berlog
+ * @date 28.06.12
+ */
+public class TestPolygon extends TestCase{
+
+ public void testResultEqualsToAbstractShape() throws IOException {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+
+ HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon.setPolygonDrawArea( 100, 100 );
+ polygon.setPoints( new int[]{0, 90, 50}, new int[]{5, 5, 44} );
+ PolygonShape polygonShape = HSSFTestModelHelper.createPolygonShape(1024, polygon);
+ polygon.setShapeId(1024);
+
+ assertEquals(polygon.getEscherContainer().getChildRecords().size(), 4);
+ assertEquals(polygonShape.getSpContainer().getChildRecords().size(), 4);
+
+ //sp record
+ byte[] expected = polygonShape.getSpContainer().getChild(0).serialize();
+ byte[] actual = polygon.getEscherContainer().getChild(0).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = polygonShape.getSpContainer().getChild(2).serialize();
+ actual = polygon.getEscherContainer().getChild(2).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = polygonShape.getSpContainer().getChild(3).serialize();
+ actual = polygon.getEscherContainer().getChild(3).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ ObjRecord obj = polygon.getObjRecord();
+ ObjRecord objShape = polygonShape.getObjRecord();
+
+ expected = obj.serialize();
+ actual = objShape.serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+ }
+
+ public void testPolygonPoints(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+
+ HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon.setPolygonDrawArea( 100, 100 );
+ polygon.setPoints( new int[]{0, 90, 50, 90}, new int[]{5, 5, 44, 88} );
+
+ PolygonShape polygonShape = HSSFTestModelHelper.createPolygonShape(0, polygon);
+
+ EscherArrayProperty verticesProp1 = polygon._optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
+ EscherArrayProperty verticesProp2 = ((EscherOptRecord)polygonShape.getSpContainer().getChildById(EscherOptRecord.RECORD_ID))
+ .lookup(EscherProperties.GEOMETRY__VERTICES);
+
+ assertEquals(verticesProp1.getNumberOfElementsInArray(), verticesProp2.getNumberOfElementsInArray());
+ assertEquals(verticesProp1.toXml(""), verticesProp2.toXml(""));
+
+ polygon.setPoints(new int[]{1,2,3}, new int[] {4,5,6});
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{1, 2, 3}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{4, 5, 6}));
+
+ polygonShape = HSSFTestModelHelper.createPolygonShape(0, polygon);
+ verticesProp1 = polygon._optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
+ verticesProp2 = ((EscherOptRecord)polygonShape.getSpContainer().getChildById(EscherOptRecord.RECORD_ID))
+ .lookup(EscherProperties.GEOMETRY__VERTICES);
+
+ assertEquals(verticesProp1.getNumberOfElementsInArray(), verticesProp2.getNumberOfElementsInArray());
+ assertEquals(verticesProp1.toXml(""), verticesProp2.toXml(""));
+ }
+
+ public void testSetGetProperties(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+
+ HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon.setPolygonDrawArea( 102, 101 );
+ polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} );
+
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{1,2,3}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{4, 5, 6}));
+ assertEquals(polygon.getDrawAreaHeight(), 101);
+ assertEquals(polygon.getDrawAreaWidth(), 102);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ polygon = (HSSFPolygon) patriarch.getChildren().get(0);
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{1, 2, 3}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{4, 5, 6}));
+ assertEquals(polygon.getDrawAreaHeight(), 101);
+ assertEquals(polygon.getDrawAreaWidth(), 102);
+
+ polygon.setPolygonDrawArea( 1021, 1011 );
+ polygon.setPoints( new int[]{11,21,31}, new int[]{41,51,61} );
+
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{11, 21, 31}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{41, 51, 61}));
+ assertEquals(polygon.getDrawAreaHeight(), 1011);
+ assertEquals(polygon.getDrawAreaWidth(), 1021);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ polygon = (HSSFPolygon) patriarch.getChildren().get(0);
+
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{11, 21, 31}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{41, 51, 61}));
+ assertEquals(polygon.getDrawAreaHeight(), 1011);
+ assertEquals(polygon.getDrawAreaWidth(), 1021);
+ }
+
+ public void testAddToExistingFile(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+
+ HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon.setPolygonDrawArea( 102, 101 );
+ polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} );
+
+ HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon1.setPolygonDrawArea( 103, 104 );
+ polygon1.setPoints( new int[]{11,12,13}, new int[]{14,15,16} );
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 2);
+
+ HSSFPolygon polygon2 = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon2.setPolygonDrawArea( 203, 204 );
+ polygon2.setPoints( new int[]{21,22,23}, new int[]{24,25,26} );
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 3);
+
+ polygon = (HSSFPolygon) patriarch.getChildren().get(0);
+ polygon1 = (HSSFPolygon) patriarch.getChildren().get(1);
+ polygon2 = (HSSFPolygon) patriarch.getChildren().get(2);
+
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{1, 2, 3}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{4,5,6}));
+ assertEquals(polygon.getDrawAreaHeight(), 101);
+ assertEquals(polygon.getDrawAreaWidth(), 102);
+
+ assertTrue(Arrays.equals(polygon1.getXPoints(), new int[]{11,12,13}));
+ assertTrue(Arrays.equals(polygon1.getYPoints(), new int[]{14,15,16}));
+ assertEquals(polygon1.getDrawAreaHeight(), 104);
+ assertEquals(polygon1.getDrawAreaWidth(), 103);
+
+ assertTrue(Arrays.equals(polygon2.getXPoints(), new int[]{21,22,23}));
+ assertTrue(Arrays.equals(polygon2.getYPoints(), new int[]{24,25,26}));
+ assertEquals(polygon2.getDrawAreaHeight(), 204);
+ assertEquals(polygon2.getDrawAreaWidth(), 203);
+ }
+
+ public void testExistingFile() throws IOException {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
+ HSSFSheet sheet = wb.getSheet("polygon");
+ HSSFPatriarch drawing = sheet.getDrawingPatriarch();
+ assertEquals(1, drawing.getChildren().size());
+
+ HSSFPolygon polygon = (HSSFPolygon) drawing.getChildren().get(0);
+ assertEquals(polygon.getDrawAreaHeight(), 2466975);
+ assertEquals(polygon.getDrawAreaWidth(), 3686175);
+ assertTrue(Arrays.equals(polygon.getXPoints(), new int[]{0, 0, 31479, 16159, 19676, 20502}));
+ assertTrue(Arrays.equals(polygon.getYPoints(), new int[]{0, 0, 36, 56, 34, 18}));
+ }
+
+ public void testPolygonType(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+
+ HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon.setPolygonDrawArea( 102, 101 );
+ polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} );
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor());
+ polygon1.setPolygonDrawArea( 102, 101 );
+ polygon1.setPoints( new int[]{1,2,3}, new int[]{4,5,6} );
+
+ EscherSpRecord spRecord = polygon1.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
+
+ spRecord.setShapeType((short)77/**RANDOM**/);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 2);
+ assertTrue(patriarch.getChildren().get(0) instanceof HSSFPolygon);
+ assertTrue(patriarch.getChildren().get(1) instanceof HSSFPolygon);
+ }
+}
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();
+ }
+}
|