FileDocCategorySizeDatePackage
TestHSSFComment.javaAPI DocApache Poi 3.0.16346Thu May 31 18:46:28 BST 2007org.apache.poi.hssf.usermodel

TestHSSFComment

public class TestHSSFComment extends TestCase
Tests TestHSSFCellComment.
author
Yegor Kozlov

Fields Summary
Constructors Summary
Methods Summary
public static voidtestModifyComments()
test that we can modify existing cell comments


         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)1);
             comment = cell.getCellComment();
             comment.setAuthor("Mofified["+rownum+"] by Yegor");
             comment.setString(new HSSFRichTextString("Modified comment at row " + rownum));
         }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        wb.write(out);
        out.close();

        wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
        sheet = wb.getSheetAt(0);

        for (int rownum = 0; rownum < 3; rownum++) {
            row = sheet.getRow(rownum);
            cell = row.getCell((short)1);
            comment = cell.getCellComment();

            assertEquals("Mofified["+rownum+"] by Yegor", comment.getAuthor());
            assertEquals("Modified comment at row " + rownum, comment.getString().getString());
        }

     
public static voidtestReadComments()
test that we can read cell comments from an existing workbook.


         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);
             assertNull(sheet.getCellComment(rownum, 0));
         }

         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);
             assertNotNull("Cells in the second column have comments", sheet.getCellComment(rownum, 1));

             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());
         }
     
public static voidtestWriteComments()
Test that we can create cells and add comments to it.

        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());