1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: TestSTable.java,v 1.10 2002/08/05 13:19:26 ludovicc Exp $
38 */
39
40
41 package test.view.swing;
42
43
44 import java.awt.Dimension;
45 import java.util.ArrayList;
46 import javax.swing.DefaultListSelectionModel;
47 import javax.swing.JScrollPane;
48 import junit.framework.TestCase;
49 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;
50 import org.scopemvc.core.Selector;
51 import org.scopemvc.view.swing.*;
52 import org.scopemvc.model.collection.ListModel;
53 import org.scopemvc.util.convertor.*;
54
55
56 /***
57 * <P>
58 *
59 * </P>
60 *
61 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>Steve Meyfroidt</A>
62 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
63 * @version $Revision: 1.10 $ $Date: 2002/08/05 13:19:26 $
64 */
65 public final class TestSTable extends TestCase {
66
67
68 private static final Log LOG = LogFactory.getLog(TestSTable.class);
69
70
71 private STable table;
72 private SwingDummyController controller;
73 private SPanel view;
74 private SwingDummyModel model;
75
76
77 public TestSTable(String inName) {
78 super(inName);
79 }
80
81
82 protected void setUp() throws Exception {
83
84 table = new STable();
85 table.setPreferredSize(new Dimension(100, 60));
86
87 view = new SPanel();
88 view.add(new JScrollPane(table));
89
90 controller = new SwingDummyController();
91 controller.setView(view);
92 controller.startup(); // does showView()
93
94 model = new SwingDummyModel();
95 model.initSubModels();
96 }
97
98
99 protected void tearDown() {
100 controller.shutdown();
101 }
102
103
104 public void testCreateSelectionModel1() throws Exception {
105 assertTrue(table.getSelectionModel() instanceof DefaultListSelectionModel);
106
107 table.setSelectionSelectorString("a");
108 assertTrue(table.getSelectionModel() instanceof SListSelectionModel);
109 }
110
111
112 public void testCreateSelectionModel2() throws Exception {
113 assertTrue(table.getSelectionModel() instanceof DefaultListSelectionModel);
114
115 table.setSelectionSelector(Selector.fromString("a"));
116 assertTrue(table.getSelectionModel() instanceof SListSelectionModel);
117 }
118
119
120 public void testCreateTableModel() throws Exception {
121 assertTrue(table.getModel() instanceof STableModel);
122 }
123
124
125 public void testSetup() throws Exception {
126 Selector s = Selector.fromString("xyz");
127
128 table.setSelectionSelector(s);
129 assertSame(s, ((SListSelectionModel)table.getSelectionModel()).getSelector());
130
131 assertEquals(0, ((STableModel)table.getModel()).getColumnCount());
132 table.setColumnSelectorStrings(new String[] {"a","b","c"});
133 assertEquals(3, ((STableModel)table.getModel()).getColumnCount());
134
135 table.setColumnSelectors(new Selector[] {s});
136 assertEquals(1, ((STableModel)table.getModel()).getColumnCount());
137
138 table.setColumnNames(new String[] {"a","b","c"});
139 assertEquals("a", ((STableModel)table.getModel()).getColumnName(0));
140 assertEquals("b", ((STableModel)table.getModel()).getColumnName(1));
141 assertEquals("c", ((STableModel)table.getModel()).getColumnName(2));
142 }
143
144
145 public void testFindIndexFor() throws Exception {
146 table.setSelectorString("stringNonIndexedProperty");
147 controller.setModel(model);
148 SuiteViewSwing.waitForAWT();
149
150 assertEquals(0, table.findIndexFor(model.getStringNonIndexedProperty().get(0)));
151 assertEquals(1, table.findIndexFor(model.getStringNonIndexedProperty().get(1)));
152 assertEquals(-1, table.findIndexFor(null));
153 assertEquals(-1, table.findIndexFor("x"));
154 }
155
156
157 public void testFindElementAt() throws Exception {
158 table.setSelectorString("stringNonIndexedProperty");
159 controller.setModel(model);
160 SuiteViewSwing.waitForAWT();
161
162 assertEquals(model.getStringNonIndexedProperty().get(0), table.findElementAt(0));
163 assertEquals(model.getStringNonIndexedProperty().get(1), table.findElementAt(1));
164 try {
165 assertNull(table.findElementAt(20));
166 } catch (IndexOutOfBoundsException okEx) {
167 // expected
168 }
169 try {
170 assertNull(table.findElementAt(-1));
171 } catch (IndexOutOfBoundsException okEx) {
172 // expected
173 }
174 }
175
176
177 public void testUnbound() throws Exception {
178 SuiteViewSwing.waitForAWT();
179 assertNull(table.getBoundModel());
180
181 table.setSelector(Selector.fromString("subModels"));
182 table.setColumnSelectorStrings(new String[]{"stringProperty","intProperty"});
183 table.setColumnNames(new String[]{"stringProperty","intProperty"});
184 SuiteViewSwing.waitForAWT();
185 assertEquals(Selector.fromString("subModels"), ((STableModel)table.getModel()).getSelector());
186 assertNull(((STableModel)table.getModel()).getShownModel());
187 assertEquals(0, ((STableModel)table.getModel()).getRowCount());
188 }
189
190
191 public void testBind1() throws Exception {
192 table.setSelector(Selector.fromString("subModels"));
193 table.setSelectionSelectorString("stringProperty");
194 table.setColumnSelectorStrings(new String[]{"stringProperty","intProperty"});
195 table.setColumnNames(new String[]{"stringProperty","intProperty"});
196 assertEquals(Selector.fromString("subModels"), ((STableModel)table.getModel()).getSelector());
197
198 controller.setModel(model);
199 SuiteViewSwing.waitForAWT();
200
201 assertSame(view.getBoundModel(), model);
202 assertSame(model, table.getBoundModel());
203 assertSame(model.getSubModels(), ((STableModel)table.getModel()).getShownModel());
204 assertSame(model, ((SListSelectionModel)table.getSelectionModel()).getBoundModel());
205
206 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getStringProperty(), table.getValueAt(0, 0));
207 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getStringProperty(), table.getValueAt(1, 0));
208 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getIntProperty(), ((Integer)table.getValueAt(0, 1)).intValue());
209 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getIntProperty(), ((Integer)table.getValueAt(1, 1)).intValue());
210 }
211
212
213 public void testBind2() throws Exception {
214 table.setSelector(Selector.fromString("subModels"));
215 table.setSelectionSelectorString("stringProperty");
216 table.setColumnSelectorStrings(new String[]{"stringProperty","intProperty"});
217 table.setColumnNames(new String[]{"stringProperty","intProperty"});
218 controller.setModel(model);
219 SuiteViewSwing.waitForAWT();
220
221 ListModel newModels = new ListModel();
222 newModels.add(new SwingDummyModel("aaa", 111));
223 newModels.add(new SwingDummyModel("bbb", 222));
224
225 model.setSubModels(newModels);
226 SuiteViewSwing.waitForAWT();
227
228 assertSame(view.getBoundModel(), model);
229 assertSame(model, table.getBoundModel());
230 assertSame(model.getSubModels(), ((STableModel)table.getModel()).getShownModel());
231 assertSame(model, ((SListSelectionModel)table.getSelectionModel()).getBoundModel());
232
233 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getStringProperty(), table.getValueAt(0, 0));
234 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getStringProperty(), table.getValueAt(1, 0));
235 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getIntProperty(), ((Integer)table.getValueAt(0, 1)).intValue());
236 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getIntProperty(), ((Integer)table.getValueAt(1, 1)).intValue());
237 }
238
239
240 public void testBind3() throws Exception {
241 table.setSelector(Selector.fromString("subModels"));
242 table.setSelectionSelectorString("stringProperty");
243 table.setColumnSelectorStrings(new String[]{"stringProperty","intProperty"});
244 table.setColumnNames(new String[]{"stringProperty","intProperty"});
245 assertEquals(Selector.fromString("subModels"), ((STableModel)table.getModel()).getSelector());
246
247 controller.setModel(model);
248 SuiteViewSwing.waitForAWT();
249
250 assertSame(view.getBoundModel(), model);
251 assertSame(model, table.getBoundModel());
252 assertSame(model.getSubModels(), ((STableModel)table.getModel()).getShownModel());
253 assertSame(model, ((SListSelectionModel)table.getSelectionModel()).getBoundModel());
254
255 model.getSubModels().add(new SwingDummyModel("xyz", 123));
256 SuiteViewSwing.waitForAWT();
257
258 assertEquals( ((SwingDummyModel)model.getSubModels().get(3)).getStringProperty(), table.getValueAt(3, 0));
259 assertEquals( ((SwingDummyModel)model.getSubModels().get(3)).getIntProperty(), ((Integer)table.getValueAt(3, 1)).intValue());
260 }
261
262
263 public void testValidation() throws Exception {
264 model.getStringNonIndexedProperty().add("Illegal");
265 table.setSelector(Selector.fromString("stringNonIndexedProperty"));
266 table.setSelectionSelector(Selector.fromString("stringProperty"));
267 model.setStringProperty(null);
268 controller.setModel(model);
269 SuiteViewSwing.waitForAWT();
270
271 table.setRowSelectionInterval(2, 2);
272 SuiteViewSwing.waitForAWT();
273 assertTrue(table.getSelectedRow() == 2);
274 assertTrue("getStringProperty <" + model.getStringProperty() + ">", model.getStringProperty() == null);
275 }
276
277
278 // ***** This is broken: see STableModel.setShownModel
279 // public void testKeepSelection1() throws Exception {
280 // table.setSelector(Selector.fromString("stringNonIndexedProperty"));
281 // table.setSelectionSelector(Selector.fromString("stringProperty"));
282 // controller.setModel(model);
283 // SuiteViewSwing.waitForAWT();
284 //
285 // table.setRowSelectionInterval(1, 1);
286 // java.util.ArrayList newList = (ArrayList)model.getStringNonIndexedProperty().clone();
287 // model.setStringNonIndexedProperty(newList);
288 // SuiteViewSwing.waitForAWT();
289 // assertTrue("getSelectedIndex == " + table.getSelectedRow(), table.getSelectedRow() == 1);
290 // }
291
292
293 public void testKeepSelection2() throws Exception {
294 table.setSelector(Selector.fromString("stringNonIndexedProperty"));
295 table.setSelectionSelector(Selector.fromString("stringProperty"));
296 controller.setModel(model);
297 SuiteViewSwing.waitForAWT();
298
299 table.setRowSelectionInterval(1, 1);
300 ArrayList newList = new ArrayList( java.util.Arrays.asList(new String[] { "1", "2", "3" }) );
301 model.setStringNonIndexedProperty(newList);
302 SuiteViewSwing.waitForAWT();
303 assertTrue("getSelectedIndex == " + table.getSelectedRow(), table.getSelectedRow() == -1);
304 }
305
306
307 public void testControlIssue() throws Exception {
308 // ***** How to implement?
309 }
310
311
312 public void testBindNoMCE() throws Exception {
313 table.setSelector(Selector.fromString("subModels"));
314 table.setSelectionSelectorString("stringProperty");
315 table.setColumnSelectorStrings(new String[]{"stringProperty","intProperty"});
316 table.setColumnNames(new String[]{"stringProperty","intProperty"});
317 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE();
318 model.initSubModels();
319 controller.setModel(model);
320 SuiteViewSwing.waitForAWT();
321
322 ArrayList newModels = new ArrayList();
323 newModels.add(new SwingDummyModel("aaa", 111));
324 newModels.add(new SwingDummyModel("bbb", 222));
325
326 model.setSubModels(newModels);
327 SuiteViewSwing.waitForAWT();
328
329 assertSame(view.getBoundModel(), model);
330 assertSame(model, table.getBoundModel());
331 assertTrue(model.getSubModels() != ((STableModel)table.getModel()).getShownModel());
332 assertSame(model, ((SListSelectionModel)table.getSelectionModel()).getBoundModel());
333
334 view.refresh();
335
336 assertSame(model.getSubModels(), ((STableModel)table.getModel()).getShownModel());
337 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getStringProperty(), table.getValueAt(0, 0));
338 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getStringProperty(), table.getValueAt(1, 0));
339 assertEquals( ((SwingDummyModel)model.getSubModels().get(0)).getIntProperty(), ((Integer)table.getValueAt(0, 1)).intValue());
340 assertEquals( ((SwingDummyModel)model.getSubModels().get(1)).getIntProperty(), ((Integer)table.getValueAt(1, 1)).intValue());
341 }
342
343
344 // private void testRenderer0(Class valClass, Class convClass)
345 // throws Exception {
346 // Object rend;
347 // rend = table.getDefaultRenderer(valClass);
348 // assertTrue(rend != null);
349 // assertEquals(rend.getClass(), SDefaultTableCellRenderer.class);
350 // StringConvertor conv = ((SDefaultTableCellRenderer)rend)
351 // .getStringConvertor();
352 // assertTrue(conv != null);
353 // assertEquals(conv.getClass(), convClass);
354 //
355 // }
356
357 // public void testDefaultCellRenderers() throws Exception {
358 // testRenderer0(java.util.Date.class, DateStringConvertor.class);
359 // testRenderer0(org.scopemvc.util.DateTime.class, DateTimeStringConvertor.class);
360 // testRenderer0(org.scopemvc.util.Time.class, TimeStringConvertor.class);
361 // testRenderer0(Double.class, DoubleStringConvertor.class);
362 // testRenderer0(Float.class, FloatStringConvertor.class);
363 // testRenderer0(Long.class, LongStringConvertor.class);
364 // testRenderer0(Integer.class, IntegerStringConvertor.class);
365 // testRenderer0(String.class, StringStringConvertor.class);
366 // testRenderer0(java.math.BigDecimal.class, BigDecimalStringConvertor.class);
367 // testRenderer0(java.math.BigInteger.class, BigIntegerStringConvertor.class);
368 // }
369
370 // private void testEditor0(Class valClass, Class convClass)
371 // throws Exception {
372 // Object ed;
373 // ed = table.getDefaultEditor(valClass);
374 // assertTrue(ed != null);
375 // assertEquals(ed.getClass(), STableTextCellEditor.class);
376 // StringConvertor conv = ((STableTextCellEditor)ed)
377 // .getStringConvertor();
378 // assertTrue(conv != null);
379 // assertEquals(conv.getClass(), convClass);
380 //
381 // }
382 //
383 // public void testDefaultCellEditors() throws Exception {
384 // testEditor0(java.util.Date.class, DateStringConvertor.class);
385 // testEditor0(org.scopemvc.util.DateTime.class, DateTimeStringConvertor.class);
386 // testEditor0(org.scopemvc.util.Time.class, TimeStringConvertor.class);
387 // testEditor0(Double.class, DoubleStringConvertor.class);
388 // testEditor0(Float.class, FloatStringConvertor.class);
389 // testEditor0(Long.class, LongStringConvertor.class);
390 // testEditor0(Integer.class, IntegerStringConvertor.class);
391 // testEditor0(String.class, StringStringConvertor.class);
392 // testEditor0(java.math.BigDecimal.class, BigDecimalStringConvertor.class);
393 // testEditor0(java.math.BigInteger.class, BigIntegerStringConvertor.class);
394 // }
395
396 }
This page was automatically generated by Maven