001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.table;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import javax.swing.event.TableModelEvent;
022 import javax.swing.event.TableModelListener;
023
024 import junit.framework.Assert;
025
026 import org.easymock.EasyMock;
027
028
029 /**
030 * TODO finish comment
031 *
032 * @author Kevin Stembridge
033 * @since 0.3
034 *
035 */
036 public abstract class AbstractBaseTableModelTests extends AbstractMutableTableModelTests {
037
038 /**
039 * {@inheritDoc}
040 */
041 protected MutableTableModel getTableModel() {
042 return getBaseTableModel();
043 }
044
045 /**
046 * Subclasses must implement this method to return the implementation to be tested.
047 *
048 * @return The table model implementation to be tested. Never null.
049 */
050 protected abstract BaseTableModel getBaseTableModel();
051
052 /**
053 * Creates a new uninitialized {@code AbstractBaseTableModelTests}.
054 */
055 public AbstractBaseTableModelTests() {
056 super();
057 }
058
059 /**
060 * Test method for {@link BaseTableModel#setRows(java.util.List)}.
061 */
062 public final void testSetRows() {
063
064 BaseTableModel model = getBaseTableModel();
065
066 List rows = new ArrayList();
067 rows.add(new Object());
068 rows.add(new Object());
069
070 //create the mock listeners and add them to the model
071 TableModelListener listener1 = (TableModelListener) EasyMock.createMock(TableModelListener.class);
072 TableModelListener listener2 = (TableModelListener) EasyMock.createMock(TableModelListener.class);
073 model.addTableModelListener(listener1);
074 model.addTableModelListener(listener2);
075
076 //set the expectations on the mock listeners
077 TableModelEvent expectedEvent = new TableModelEvent(model);
078 listener1.tableChanged(matchEvent(expectedEvent));
079 listener2.tableChanged(matchEvent(expectedEvent));
080
081 //switch the mocks to replay mode
082 EasyMock.replay(listener1);
083 EasyMock.replay(listener2);
084
085 //...and execute the test
086 model.setRows(rows);
087
088 Assert.assertEquals(2, model.getRowCount());
089 EasyMock.verify(listener1);
090 EasyMock.verify(listener2);
091
092 //Create a new list of rows and confirm that it overwrites the existing rows
093 List rows2 = new ArrayList(3);
094 rows2.add(new Object());
095 rows2.add(new Object());
096 rows2.add(new Object());
097
098 //reset the mocks
099 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 }