aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2007-01-01 21:02:22 +0000
committerNick Burch <nick@apache.org>2007-01-01 21:02:22 +0000
commitc1d68a5e30a5fbc602480c7a66531c3624476d15 (patch)
tree5cc25e17550a9e32ce355f808604def261031405 /src/testcases
parent75d3621639886bac0532cf3e421cf9ff1fe2e250 (diff)
downloadpoi-c1d68a5e30a5fbc602480c7a66531c3624476d15.tar.gz
poi-c1d68a5e30a5fbc602480c7a66531c3624476d15.zip
Comment support from bug 41198, patch from Yegor
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@491629 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r--src/testcases/org/apache/poi/hssf/HSSFTests.java4
-rw-r--r--src/testcases/org/apache/poi/hssf/data/SimpleWithComments.xlsbin0 -> 13824 bytes
-rw-r--r--src/testcases/org/apache/poi/hssf/record/TestNoteRecord.java82
-rw-r--r--src/testcases/org/apache/poi/hssf/record/TestNoteStructureSubRecord.java68
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java120
5 files changed, 273 insertions, 1 deletions
diff --git a/src/testcases/org/apache/poi/hssf/HSSFTests.java b/src/testcases/org/apache/poi/hssf/HSSFTests.java
index d0fc8fe854..c26ba9beb3 100644
--- a/src/testcases/org/apache/poi/hssf/HSSFTests.java
+++ b/src/testcases/org/apache/poi/hssf/HSSFTests.java
@@ -110,6 +110,7 @@ import org.apache.poi.hssf.util.TestCellReference;
import org.apache.poi.hssf.util.TestRKUtil;
import org.apache.poi.hssf.util.TestRangeAddress;
import org.apache.poi.hssf.util.TestSheetReferences;
+import org.apache.poi.hssf.usermodel.TestHSSFComment;
/**
* Test Suite for running just HSSF tests. Mostly
@@ -227,7 +228,8 @@ public class HSSFTests
suite.addTest(new TestSuite(TestModelFactory.class));
suite.addTest(new TestSuite(TestDrawingManager.class));
suite.addTest(new TestSuite(TestSheet.class));
-
+
+ suite.addTest(new TestSuite(TestHSSFComment.class));
//$JUnit-END$
return suite;
}
diff --git a/src/testcases/org/apache/poi/hssf/data/SimpleWithComments.xls b/src/testcases/org/apache/poi/hssf/data/SimpleWithComments.xls
new file mode 100644
index 0000000000..0fbec16a63
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/data/SimpleWithComments.xls
Binary files differ
diff --git a/src/testcases/org/apache/poi/hssf/record/TestNoteRecord.java b/src/testcases/org/apache/poi/hssf/record/TestNoteRecord.java
new file mode 100644
index 0000000000..5d45094924
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/record/TestNoteRecord.java
@@ -0,0 +1,82 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record;
+
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+
+/**
+ * Tests the serialization and deserialization of the NoteRecord
+ * class works correctly. Test data taken directly from a real
+ * Excel file.
+ *
+ * @author Yegor Kozlov
+ */
+public class TestNoteRecord
+ extends TestCase
+{
+ private byte[] data = new byte[] {
+ 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02, 0x04, 0x1A, 0x00,
+ 0x00, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x20, 0x53, 0x6F,
+ 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x46, 0x6F, 0x75,
+ 0x6E, 0x64, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x00
+ };
+
+ public TestNoteRecord(String name)
+ {
+ super(name);
+ }
+
+ public void testRead()
+ throws Exception
+ {
+
+ NoteRecord record = new NoteRecord(new TestcaseRecordInputStream(NoteRecord.sid, (short)data.length, data));
+
+ assertEquals(NoteRecord.sid, record.getSid());
+ record.validateSid(NoteRecord.sid);
+ assertEquals(6, record.getRow());
+ assertEquals(1, record.getColumn());
+ assertEquals(NoteRecord.NOTE_VISIBLE, record.getFlags());
+ assertEquals(1026, record.getShapeId());
+ assertEquals("Apache Software Foundation", record.getAuthor());
+
+ }
+
+ public void testWrite()
+ {
+ NoteRecord record = new NoteRecord();
+ assertEquals(NoteRecord.sid, record.getSid());
+ record.validateSid(NoteRecord.sid);
+
+ record.setRow((short)6);
+ record.setColumn((short)1);
+ record.setFlags(NoteRecord.NOTE_VISIBLE);
+ record.setShapeId((short)1026);
+ record.setAuthor("Apache Software Foundation");
+
+ byte [] ser = record.serialize();
+ assertEquals(ser.length - 4, data.length);
+
+ byte[] recdata = new byte[ser.length - 4];
+ System.arraycopy(ser, 4, recdata, 0, recdata.length);
+ assertTrue(Arrays.equals(data, recdata));
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/record/TestNoteStructureSubRecord.java b/src/testcases/org/apache/poi/hssf/record/TestNoteStructureSubRecord.java
new file mode 100644
index 0000000000..833aa66128
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/record/TestNoteStructureSubRecord.java
@@ -0,0 +1,68 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.hssf.record;
+
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+
+/**
+ * Tests the serialization and deserialization of the NoteRecord
+ * class works correctly. Test data taken directly from a real
+ * Excel file.
+ *
+ * @author Yegor Kozlov
+ */
+public class TestNoteStructureSubRecord
+ extends TestCase
+{
+ private byte[] data = new byte[] {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80, 0x00, 0x00, 0x00,
+ 0x00, 0x00, (byte)0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x81, 0x01,
+ (byte)0xCC, (byte)0xEC
+ };
+
+ public TestNoteStructureSubRecord(String name)
+ {
+ super(name);
+ }
+
+ public void testRead()
+ throws Exception
+ {
+
+ NoteStructureSubRecord record = new NoteStructureSubRecord(new TestcaseRecordInputStream(NoteStructureSubRecord.sid, (short)data.length, data));
+
+ assertEquals(NoteStructureSubRecord.sid, record.getSid());
+ record.validateSid(NoteStructureSubRecord.sid);
+ assertEquals(data.length + 4, record.getRecordSize());
+
+ }
+
+ public void testWrite()
+ {
+ NoteStructureSubRecord record = new NoteStructureSubRecord();
+ assertEquals(NoteStructureSubRecord.sid, record.getSid());
+ record.validateSid(NoteStructureSubRecord.sid);
+ assertEquals(data.length + 4, record.getRecordSize());
+
+ byte [] ser = record.serialize();
+ assertEquals(ser.length - 4, data.length);
+
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
new file mode 100644
index 0000000000..3f38747b02
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
@@ -0,0 +1,120 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.hssf.usermodel;
+
+import junit.framework.TestCase;
+
+import java.io.*;
+
+/**
+ * Tests TestHSSFCellComment.
+ *
+ * @author Yegor Kozlov
+ */
+
+public class TestHSSFComment extends TestCase {
+
+ /**
+ * Test that we can create cells and add comments to it.
+ */
+ public static void testWriteComments() throws Exception {
+ String cellText = "Hello, World";
+ String commentText = "We can set comments in POI";
+ String commentAuthor = "Apache Software Foundation";
+ int cellRow = 3;
+ short cellColumn = 1;
+
+ HSSFWorkbook wb = new HSSFWorkbook();
+
+ HSSFSheet sheet = wb.createSheet();
+
+ HSSFCell cell = sheet.createRow(cellRow).createCell(cellColumn);
+ cell.setCellValue(new HSSFRichTextString(cellText));
+ assertNull(cell.getCellComment());
+
+ HSSFPatriarch patr = sheet.createDrawingPatriarch();
+ HSSFClientAnchor anchor = new HSSFClientAnchor();
+ anchor.setAnchor( (short)4, 2, 0, 0, (short) 6, 5, 0, 0);
+ HSSFComment comment = patr.createComment(anchor);
+ HSSFRichTextString string1 = new HSSFRichTextString(commentText);
+ comment.setString(string1);
+ comment.setAuthor(commentAuthor);
+ cell.setCellComment(comment);
+
+ //verify our settings
+ assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
+ assertEquals(commentAuthor, comment.getAuthor());
+ assertEquals(commentText, comment.getString().getString());
+ assertEquals(cellRow, comment.getRow());
+ assertEquals(cellColumn, comment.getColumn());
+
+ //serialize the workbook and read it again
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wb.write(out);
+ out.close();
+
+ wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+ sheet = wb.getSheetAt(0);
+ cell = sheet.getRow(cellRow).getCell(cellColumn);
+ comment = cell.getCellComment();
+
+ assertNotNull(comment);
+ assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
+ assertEquals(commentAuthor, comment.getAuthor());
+ assertEquals(commentText, comment.getString().getString());
+ assertEquals(cellRow, comment.getRow());
+ assertEquals(cellColumn, comment.getColumn());
+ }
+
+ /**
+ * test that we can read cell comments from an existing workbook.
+ */
+ public static void testReadComments() throws Exception {
+
+ String dir = System.getProperty("HSSF.testdata.path");
+ FileInputStream is = new FileInputStream(new File(dir, "SimpleWithComments.xls"));
+ HSSFWorkbook wb = new HSSFWorkbook(is);
+ is.close();
+
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ HSSFCell cell;
+ HSSFRow row;
+ HSSFComment comment;
+
+ for (int rownum = 0; rownum < 3; rownum++) {
+ row = sheet.getRow(rownum);
+ cell = row.getCell((short)0);
+ comment = cell.getCellComment();
+ assertNull("Cells in the first column are not commented", comment);
+ }
+
+ for (int rownum = 0; rownum < 3; rownum++) {
+ row = sheet.getRow(rownum);
+ cell = row.getCell((short)1);
+ comment = cell.getCellComment();
+ assertNotNull("Cells in the second column have comments", comment);
+ assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
+ assertEquals("Yegor Kozlov", comment.getAuthor());
+ assertFalse("cells in the second column have not empyy notes",
+ "".equals(comment.getString().getString()));
+ assertEquals(rownum, comment.getRow());
+ assertEquals(cell.getCellNum(), comment.getColumn());
+ }
+ }
+
+}