001package csheets.ext.comments;
002
003import static org.junit.Assert.assertTrue;
004
005import org.junit.Test;
006
007import csheets.core.Cell;
008import csheets.core.Spreadsheet;
009import csheets.core.Workbook;
010import csheets.ext.Extension;
011
012/**
013 * A Unit Test class to test CommentableCell.
014 * @see CommentableCell
015 * @author Alexandre Braganca
016 */
017public class CommentableCellTest {
018        
019        private boolean isNotified=false;
020
021        /**
022         * A method that tests the property hasComment.
023         */
024        @Test public void testHasComment() {
025                
026                // create a workbook with 2 sheets
027                Workbook wb=new Workbook(2);
028                Spreadsheet s=wb.getSpreadsheet(0);
029                // get the first cell
030                Cell c=s.getCell(0,0);
031                
032                // activate the comments on the first cell
033                CommentableCell cc=new CommentableCell(c);
034
035                boolean hasComment=cc.hasComment();
036                
037                assertTrue(hasComment==false);          
038                
039                cc.setUserComment("coment");
040
041                hasComment=cc.hasComment();
042                
043                assertTrue(hasComment);         
044        }
045
046        /**
047         * A method that tests the setter and getter of the user comment.
048         */
049        @Test public void testSetGetUserComment() {
050                
051                // create a workbook with 2 sheets
052                Workbook wb=new Workbook(2);
053                Spreadsheet s=wb.getSpreadsheet(0);
054                // get the first cell
055                Cell c=s.getCell(0,0);
056                
057                // activate the comments on the first cell
058                CommentableCell cc=new CommentableCell(c);
059
060                cc.setUserComment("Hello");
061                
062                assertTrue("Hello".compareTo(cc.getUserComment())==0);          
063        }
064        
065        /**
066         * A method that tests the notifications for commented cell listeners.
067         * @see CommentableCellListener
068         */     
069        @Test public void testCommentableCellListenner() {
070                
071                // create a workbook with 2 sheets
072                Workbook wb=new Workbook(2);
073                Spreadsheet s=wb.getSpreadsheet(0);
074                // get the first cell
075                Cell c=s.getCell(0,0);
076                
077                // activate the comments on the first cell
078                CommentableCell cc=new CommentableCell(c);
079                
080                CommentableCellListener listener=new CommentableCellListenerImpl();
081                
082                cc.addCommentableCellListener(listener);
083
084                // modify the cell... this should create an event
085                cc.setUserComment("Hello");
086                
087                assertTrue(isNotified);         
088        }
089
090        /**
091         * A inner utility class used by the method testCommentableCellListenner.
092         */     
093        class CommentableCellListenerImpl implements CommentableCellListener {
094
095                @Override
096                public void commentChanged(CommentableCell cell) {
097                        isNotified=true;
098                }
099                
100        }
101}