aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcases/org/apache/poi/hssf')
-rw-r--r--src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java13
-rw-r--r--src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java37
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java4
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestText.java186
4 files changed, 240 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java b/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java
new file mode 100644
index 0000000000..cca8f2d635
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java
@@ -0,0 +1,13 @@
+package org.apache.poi.hssf.model;
+
+import org.apache.poi.hssf.usermodel.HSSFTextbox;
+
+/**
+ * @author Evgeniy Berlog
+ * @date 25.06.12
+ */
+public class HSSFTestModelHelper {
+ public static TextboxShape createTextboxShape(int shapeId, HSSFTextbox textbox){
+ return new TextboxShape(textbox, shapeId);
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
index 097d5d01ee..1beeabe94b 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
@@ -251,4 +251,41 @@ public class TestDrawingShapes extends TestCase{
((EscherContainerRecord)spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);
assertEquals(1026, sp2.getShapeId());
}
+
+ /**
+ * Test get new id for shapes from existing file
+ * File already have for 1 shape on each sheet, because document must contain EscherDgRecord for each sheet
+ */
+ public void testAllocateNewIds(){
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("empty.xls");
+ HSSFSheet sheet = wb.getSheetAt(0);
+ HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
+
+ /**
+ * 2048 - main SpContainer id
+ * 2049 - existing shape id
+ */
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2050);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2051);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2052);
+
+ sheet = wb.getSheetAt(1);
+ patriarch = sheet.getDrawingPatriarch();
+
+ /**
+ * 3072 - main SpContainer id
+ * 3073 - existing shape id
+ */
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3074);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3075);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3076);
+
+
+ sheet = wb.getSheetAt(2);
+ patriarch = sheet.getDrawingPatriarch();
+
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1026);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1027);
+ assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1028);
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java
index 2d6b14b79a..496139eaab 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java
@@ -43,4 +43,8 @@ public class HSSFTestHelper {
public static EscherAggregate getEscherAggregate(HSSFPatriarch patriarch){
return patriarch._getBoundAggregate();
}
+
+ public static int allocateNewShapeId(HSSFPatriarch patriarch){
+ return patriarch.newShapeId();
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestText.java b/src/testcases/org/apache/poi/hssf/usermodel/TestText.java
new file mode 100644
index 0000000000..213c5be674
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestText.java
@@ -0,0 +1,186 @@
+package org.apache.poi.hssf.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.model.HSSFTestModelHelper;
+import org.apache.poi.hssf.model.TextboxShape;
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
+import org.apache.poi.hssf.record.ObjRecord;
+import org.apache.poi.hssf.record.TextObjectRecord;
+
+import java.util.Arrays;
+
+/**
+ * @author Evgeniy Berlog
+ * @date 25.06.12
+ */
+public class TestText extends TestCase {
+
+ public void testResultEqualsToAbstractShape() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());
+ TextboxShape textboxShape = HSSFTestModelHelper.createTextboxShape(0, textbox);
+
+ assertEquals(textbox.getEscherContainer().getChildRecords().size(), 5);
+ assertEquals(textboxShape.getSpContainer().getChildRecords().size(), 5);
+
+ //sp record
+ byte[] expected = textboxShape.getSpContainer().getChild(0).serialize();
+ byte[] actual = textbox.getEscherContainer().getChild(0).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = textboxShape.getSpContainer().getChild(2).serialize();
+ actual = textbox.getEscherContainer().getChild(2).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = textboxShape.getSpContainer().getChild(3).serialize();
+ actual = textbox.getEscherContainer().getChild(3).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = textboxShape.getSpContainer().getChild(4).serialize();
+ actual = textbox.getEscherContainer().getChild(4).serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ ObjRecord obj = textbox.getObjRecord();
+ ((CommonObjectDataSubRecord) obj.getSubRecords().get(0)).setObjectId(-1024);
+ ObjRecord objShape = textboxShape.getObjRecord();
+
+ expected = obj.serialize();
+ actual = objShape.serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+
+ TextObjectRecord tor = textbox.getTextObjectRecord();
+ TextObjectRecord torShape = textboxShape.getTextObjectRecord();
+
+ expected = tor.serialize();
+ actual = torShape.serialize();
+
+ assertEquals(expected.length, actual.length);
+ assertTrue(Arrays.equals(expected, actual));
+ }
+
+ public void testAddTextToExistingFile() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());
+ textbox.setString(new HSSFRichTextString("just for test"));
+ HSSFTextbox textbox2 = patriarch.createTextbox(new HSSFClientAnchor());
+ textbox2.setString(new HSSFRichTextString("just for test2"));
+
+ assertEquals(patriarch.getChildren().size(), 2);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 2);
+ HSSFTextbox text3 = patriarch.createTextbox(new HSSFClientAnchor());
+ text3.setString(new HSSFRichTextString("text3"));
+ assertEquals(patriarch.getChildren().size(), 3);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+
+ assertEquals(patriarch.getChildren().size(), 3);
+ assertEquals(((HSSFTextbox) patriarch.getChildren().get(0)).getString().getString(), "just for test");
+ assertEquals(((HSSFTextbox) patriarch.getChildren().get(1)).getString().getString(), "just for test2");
+ assertEquals(((HSSFTextbox) patriarch.getChildren().get(2)).getString().getString(), "text3");
+ }
+
+ public void testSetGetProperties() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sh = wb.createSheet();
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());
+ textbox.setString(new HSSFRichTextString("test"));
+ assertEquals(textbox.getString().getString(), "test");
+
+ textbox.setHorizontalAlignment((short) 5);
+ assertEquals(textbox.getHorizontalAlignment(), 5);
+
+ textbox.setVerticalAlignment((short) 6);
+ assertEquals(textbox.getVerticalAlignment(), (short) 6);
+
+ textbox.setMarginBottom(7);
+ assertEquals(textbox.getMarginBottom(), 7);
+
+ textbox.setMarginLeft(8);
+ assertEquals(textbox.getMarginLeft(), 8);
+
+ textbox.setMarginRight(9);
+ assertEquals(textbox.getMarginRight(), 9);
+
+ textbox.setMarginTop(10);
+ assertEquals(textbox.getMarginTop(), 10);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+ textbox = (HSSFTextbox) patriarch.getChildren().get(0);
+ assertEquals(textbox.getString().getString(), "test");
+ assertEquals(textbox.getHorizontalAlignment(), 5);
+ assertEquals(textbox.getVerticalAlignment(), (short) 6);
+ assertEquals(textbox.getMarginBottom(), 7);
+ assertEquals(textbox.getMarginLeft(), 8);
+ assertEquals(textbox.getMarginRight(), 9);
+ assertEquals(textbox.getMarginTop(), 10);
+
+ textbox.setString(new HSSFRichTextString("test1"));
+ textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);
+ textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_TOP);
+ textbox.setMarginBottom(71);
+ textbox.setMarginLeft(81);
+ textbox.setMarginRight(91);
+ textbox.setMarginTop(101);
+
+ assertEquals(textbox.getString().getString(), "test1");
+ assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);
+ assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);
+ assertEquals(textbox.getMarginBottom(), 71);
+ assertEquals(textbox.getMarginLeft(), 81);
+ assertEquals(textbox.getMarginRight(), 91);
+ assertEquals(textbox.getMarginTop(), 101);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sh = wb.getSheetAt(0);
+ patriarch = sh.getDrawingPatriarch();
+ textbox = (HSSFTextbox) patriarch.getChildren().get(0);
+
+ assertEquals(textbox.getString().getString(), "test1");
+ assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);
+ assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);
+ assertEquals(textbox.getMarginBottom(), 71);
+ assertEquals(textbox.getMarginLeft(), 81);
+ assertEquals(textbox.getMarginRight(), 91);
+ assertEquals(textbox.getMarginTop(), 101);
+ }
+
+ public void testExistingFileWithText(){
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
+ HSSFSheet sheet = wb.getSheet("text");
+ HSSFPatriarch drawing = sheet.getDrawingPatriarch();
+ assertEquals(1, drawing.getChildren().size());
+ HSSFTextbox textbox = (HSSFTextbox) drawing.getChildren().get(0);
+ assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_LEFT);
+ assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);
+ assertEquals(textbox.getMarginTop(), 0);
+ assertEquals(textbox.getMarginBottom(), 3600000);
+ assertEquals(textbox.getMarginLeft(), 3600000);
+ assertEquals(textbox.getMarginRight(), 0);
+ assertEquals(textbox.getString().getString(), "teeeeesssstttt");
+ }
+}