001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hssf.usermodel;
018:
019: import junit.framework.TestCase;
020:
021: import java.io.*;
022:
023: /**
024: * Tests TestHSSFCellComment.
025: *
026: * @author Yegor Kozlov
027: */
028:
029: public class TestHSSFComment extends TestCase {
030:
031: /**
032: * Test that we can create cells and add comments to it.
033: */
034: public static void testWriteComments() throws Exception {
035: String cellText = "Hello, World";
036: String commentText = "We can set comments in POI";
037: String commentAuthor = "Apache Software Foundation";
038: int cellRow = 3;
039: short cellColumn = 1;
040:
041: HSSFWorkbook wb = new HSSFWorkbook();
042:
043: HSSFSheet sheet = wb.createSheet();
044:
045: HSSFCell cell = sheet.createRow(cellRow).createCell(cellColumn);
046: cell.setCellValue(new HSSFRichTextString(cellText));
047: assertNull(cell.getCellComment());
048:
049: HSSFPatriarch patr = sheet.createDrawingPatriarch();
050: HSSFClientAnchor anchor = new HSSFClientAnchor();
051: anchor.setAnchor((short) 4, 2, 0, 0, (short) 6, 5, 0, 0);
052: HSSFComment comment = patr.createComment(anchor);
053: HSSFRichTextString string1 = new HSSFRichTextString(commentText);
054: comment.setString(string1);
055: comment.setAuthor(commentAuthor);
056: cell.setCellComment(comment);
057:
058: //verify our settings
059: assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment
060: .getShapeType());
061: assertEquals(commentAuthor, comment.getAuthor());
062: assertEquals(commentText, comment.getString().getString());
063: assertEquals(cellRow, comment.getRow());
064: assertEquals(cellColumn, comment.getColumn());
065:
066: //serialize the workbook and read it again
067: ByteArrayOutputStream out = new ByteArrayOutputStream();
068: wb.write(out);
069: out.close();
070:
071: wb = new HSSFWorkbook(new ByteArrayInputStream(out
072: .toByteArray()));
073: sheet = wb.getSheetAt(0);
074: cell = sheet.getRow(cellRow).getCell(cellColumn);
075: comment = cell.getCellComment();
076:
077: assertNotNull(comment);
078: assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment
079: .getShapeType());
080: assertEquals(commentAuthor, comment.getAuthor());
081: assertEquals(commentText, comment.getString().getString());
082: assertEquals(cellRow, comment.getRow());
083: assertEquals(cellColumn, comment.getColumn());
084: }
085:
086: /**
087: * test that we can read cell comments from an existing workbook.
088: */
089: public static void testReadComments() throws Exception {
090:
091: String dir = System.getProperty("HSSF.testdata.path");
092: FileInputStream is = new FileInputStream(new File(dir,
093: "SimpleWithComments.xls"));
094: HSSFWorkbook wb = new HSSFWorkbook(is);
095: is.close();
096:
097: HSSFSheet sheet = wb.getSheetAt(0);
098:
099: HSSFCell cell;
100: HSSFRow row;
101: HSSFComment comment;
102:
103: for (int rownum = 0; rownum < 3; rownum++) {
104: row = sheet.getRow(rownum);
105: cell = row.getCell((short) 0);
106: comment = cell.getCellComment();
107: assertNull("Cells in the first column are not commented",
108: comment);
109: assertNull(sheet.getCellComment(rownum, 0));
110: }
111:
112: for (int rownum = 0; rownum < 3; rownum++) {
113: row = sheet.getRow(rownum);
114: cell = row.getCell((short) 1);
115: comment = cell.getCellComment();
116: assertNotNull("Cells in the second column have comments",
117: comment);
118: assertNotNull("Cells in the second column have comments",
119: sheet.getCellComment(rownum, 1));
120:
121: assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment
122: .getShapeType());
123: assertEquals("Yegor Kozlov", comment.getAuthor());
124: assertFalse(
125: "cells in the second column have not empyy notes",
126: "".equals(comment.getString().getString()));
127: assertEquals(rownum, comment.getRow());
128: assertEquals(cell.getCellNum(), comment.getColumn());
129: }
130: }
131:
132: /**
133: * test that we can modify existing cell comments
134: */
135: public static void testModifyComments() throws Exception {
136:
137: String dir = System.getProperty("HSSF.testdata.path");
138: FileInputStream is = new FileInputStream(new File(dir,
139: "SimpleWithComments.xls"));
140: HSSFWorkbook wb = new HSSFWorkbook(is);
141: is.close();
142:
143: HSSFSheet sheet = wb.getSheetAt(0);
144:
145: HSSFCell cell;
146: HSSFRow row;
147: HSSFComment comment;
148:
149: for (int rownum = 0; rownum < 3; rownum++) {
150: row = sheet.getRow(rownum);
151: cell = row.getCell((short) 1);
152: comment = cell.getCellComment();
153: comment.setAuthor("Mofified[" + rownum + "] by Yegor");
154: comment.setString(new HSSFRichTextString(
155: "Modified comment at row " + rownum));
156: }
157:
158: ByteArrayOutputStream out = new ByteArrayOutputStream();
159: wb.write(out);
160: out.close();
161:
162: wb = new HSSFWorkbook(new ByteArrayInputStream(out
163: .toByteArray()));
164: sheet = wb.getSheetAt(0);
165:
166: for (int rownum = 0; rownum < 3; rownum++) {
167: row = sheet.getRow(rownum);
168: cell = row.getCell((short) 1);
169: comment = cell.getCellComment();
170:
171: assertEquals("Mofified[" + rownum + "] by Yegor", comment
172: .getAuthor());
173: assertEquals("Modified comment at row " + rownum, comment
174: .getString().getString());
175: }
176:
177: }
178: }
|