1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.table;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.event.TableModelEvent;
22  import javax.swing.event.TableModelListener;
23  
24  import junit.framework.Assert;
25  
26  import org.easymock.EasyMock;
27  
28  
29  /**
30   * TODO finish comment
31   *
32   * @author Kevin Stembridge
33   * @since 0.3
34   *
35   */
36  public abstract class AbstractBaseTableModelTests extends AbstractMutableTableModelTests {
37      
38      /**
39       * {@inheritDoc}
40       */
41      protected MutableTableModel getTableModel() {
42          return getBaseTableModel();
43      }
44  
45      /**
46       * Subclasses must implement this method to return the implementation to be tested.
47       *
48       * @return The table model implementation to be tested. Never null.
49       */
50      protected abstract BaseTableModel getBaseTableModel();
51  
52      /**
53       * Creates a new uninitialized {@code AbstractBaseTableModelTests}.
54       */
55      public AbstractBaseTableModelTests() {
56          super();
57      }
58  
59      /**
60       * Test method for {@link BaseTableModel#setRows(java.util.List)}.
61       */
62      public final void testSetRows() {
63          
64          BaseTableModel model = getBaseTableModel();
65          
66          List rows = new ArrayList();
67          rows.add(new Object());
68          rows.add(new Object());
69          
70          //create the mock listeners and add them to the model
71          TableModelListener listener1 = (TableModelListener) EasyMock.createMock(TableModelListener.class);
72          TableModelListener listener2 = (TableModelListener) EasyMock.createMock(TableModelListener.class);
73          model.addTableModelListener(listener1);
74          model.addTableModelListener(listener2);
75          
76          //set the expectations on the mock listeners
77          TableModelEvent expectedEvent = new TableModelEvent(model);
78          listener1.tableChanged(matchEvent(expectedEvent));
79          listener2.tableChanged(matchEvent(expectedEvent));
80          
81          //switch the mocks to replay mode
82          EasyMock.replay(listener1);
83          EasyMock.replay(listener2);
84          
85          //...and execute the test
86          model.setRows(rows);
87          
88          Assert.assertEquals(2, model.getRowCount());
89          EasyMock.verify(listener1);
90          EasyMock.verify(listener2);
91          
92          //Create a new list of rows and confirm that it overwrites the existing rows
93          List rows2 = new ArrayList(3);
94          rows2.add(new Object());
95          rows2.add(new Object());
96          rows2.add(new Object());
97          
98          //reset the mocks
99          EasyMock.reset(listener1);
100         EasyMock.reset(listener2);
101         
102         //set the expectations on the mock listeners
103         listener1.tableChanged(matchEvent(expectedEvent));
104         listener2.tableChanged(matchEvent(expectedEvent));
105         
106         //switch the mocks to replay mode
107         EasyMock.replay(listener1);
108         EasyMock.replay(listener2);
109         
110         //...and execute the test
111         model.setRows(rows2);
112         
113         Assert.assertEquals(3, model.getRowCount());
114         EasyMock.verify(listener1);
115         EasyMock.verify(listener2);
116 
117        
118     }
119 
120     /**
121      * Test method for {@link org.springframework.richclient.table.BaseTableModel#hasRowNumbers()}.
122      */
123     public final void testRowNumbersFlag() {
124         
125         BaseTableModel model = getBaseTableModel();
126         
127         Assert.assertTrue("Assert default rowNumbers flag is true", model.hasRowNumbers());
128         
129         model.setRowNumbers(false);
130         
131         Assert.assertFalse("Assert rowNumbers flag is false", model.hasRowNumbers());
132         
133     }
134 
135     /**
136      * Test method for {@link org.springframework.richclient.table.BaseTableModel#getRow(int)}.
137      */
138     public final void testGetRow() {
139         
140         BaseTableModel model = getBaseTableModel();
141         
142         Object row1 = new Object();
143         Object row2 = new Object();
144         Object row3 = new Object();
145         
146         List rows = new ArrayList(3);
147         rows.add(row1);
148         rows.add(row2);
149         rows.add(row3);
150         
151         model.setRows(rows);
152         
153         Assert.assertEquals(row1, model.getRow(0));
154         Assert.assertEquals(row2, model.getRow(1));
155         Assert.assertEquals(row3, model.getRow(2));
156         
157         try {
158             model.getRow(-1);
159             Assert.fail("Should have thrown an IndexOutOfBoundsException");
160         }
161         catch (IndexOutOfBoundsException e) {
162             //test passes
163         }
164         
165         try {
166             model.getRow(3);
167             Assert.fail("Should have thrown an IndexOutOfBoundsException");
168         }
169         catch (IndexOutOfBoundsException e) {
170             //test passes
171         }
172         
173     }
174 
175     /**
176      * Test method for {@link BaseTableModel#getRows()}.
177      */
178     public final void testGetRows() {
179         
180         BaseTableModel model = getBaseTableModel();
181         
182         Assert.assertNotNull("Assert model.getRows is not null", model.getRows());
183         Assert.assertTrue("Assert model.getRows is an empty list", model.getRows().isEmpty());
184         
185         List rows = new ArrayList(3);
186         rows.add(new Object());
187         rows.add(new Object());
188         rows.add(new Object());
189         model.setRows(rows);
190         
191         Assert.assertEquals(rows, model.getRows());
192         
193     }
194 
195     /**
196      * Test method for {@link BaseTableModel#rowOf(java.lang.Object)}.
197      */
198     public final void testRowOf() {
199         
200         BaseTableModel model = getBaseTableModel();
201         Object expectedRow = new Object();
202         
203         //confirm that -1 is returned if the model does not contain the element
204         Assert.assertEquals(-1, model.rowOf(expectedRow));
205         Assert.assertEquals(-1, model.rowOf(expectedRow));
206         
207         //create a list of rows with the expected row at the first and third positions, and add 
208         //them to the model
209         List rows = new ArrayList();
210         rows.add(expectedRow);
211         rows.add(new Object());
212         rows.add(expectedRow);
213         model.setRows(rows);
214         
215         //confirm that the expected row is at index 0
216         Assert.assertEquals(0, model.rowOf(expectedRow));
217         
218     }
219 
220 }