KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > TableModelExtAdapterTest


1 /*
2  * $Id: TableModelExtAdapterTest.java,v 1.3 2005/02/25 13:29:06 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.data;
8
9 import java.net.URL JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.logging.Logger JavaDoc;
12
13 import org.jdesktop.swing.util.TestSupport;
14
15 /**
16  * TableModelExtAdapter specifics.
17  *
18  * @author Jeanette Winzenburg
19  */

20 public class TableModelExtAdapterTest extends AbstractDataModelTst {
21     // private static final String PREFIX = "resources/";
22
private static final String JavaDoc PREFIX = "/org/jdesktop/swing/resources/";
23
24     private static final String JavaDoc STRINGDATA = PREFIX + "stringdata.csv";
25
26     private URL JavaDoc stringDataURL;
27
28     /**
29      * test change propagation between adapter and tabularData.
30      *
31      */

32     public void testBidiNotification() {
33         DefaultTableModelExt tabularData = TestSupport.loadTabularCSVData(stringDataURL, true);
34         TableModelExtAdapter adapter = new TableModelExtAdapter(tabularData);
35         adapter.setRecordIndex(0);
36         adapter.addValueChangeListener(valueReport);
37         tabularData.setValueAt("dummy", 0, 0);
38         String JavaDoc[] fieldNames = adapter.getFieldNames();
39         assertTrue("must have fired", valueReport.gotEvent(fieldNames[0]));
40         assertEquals(adapter.getValue(fieldNames[0]), tabularData
41                 .getValueAt(0, 0));
42         valueReport.clear();
43         adapter.setValue( fieldNames[0], "othervalue");
44         assertTrue("must have fired valueChange",
45                 valueReport.gotEvent(fieldNames[0]));
46         assertEquals(adapter.getValue(fieldNames[0]), tabularData.getValueAt(0, 0));
47         
48         
49     }
50
51     /**
52      * Test that model fires valueChangeEvents if values
53      * are changed after moving selection.
54      *
55      */

56     public void testFireValueChangeOnIndexChange() {
57         TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel();
58         if (model.getRecordCount() < 2) {
59             Logger.global.info("valueChangeOnIndexChange not run for " + model);
60             return;
61         }
62         assertFalse("sanity check: we are moving the recordIndex", model.getRecordIndex() == (model.getRecordCount() - 1));
63         List JavaDoc oldValues = getValues(model);
64         model.addValueChangeListener(valueReport);
65         model.setRecordIndex(model.getRecordCount() - 1);
66         int changeCount = assertValueChangeFired(oldValues, model);
67         assertTrue("# changed values", changeCount > 0);
68     }
69
70     /**
71      * test that value change events are fired on deselect.
72      * test that all values in deselect state of model are null.
73      *
74      */

75     public void testDeselect() {
76         TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel();
77         if (model.getRecordCount() < 2) {
78             Logger.global.info("deselect not run for " + model);
79             return;
80         }
81         model.setRecordIndex(model.getRecordCount() - 1);
82         List JavaDoc oldValues = getValues(model);
83         model.addValueChangeListener(valueReport);
84         model.setRecordIndex(-1);
85         int changeCount = assertValueChangeFired(oldValues, model);
86         assertTrue("# changed values", changeCount > 0);
87         assertNullValues(getValues(model));
88     }
89
90     /**
91      * test that value change events are fired on deselect.
92      * test that all values in deselect state of model are null.
93      *
94      */

95     public void testSelect() {
96         TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel();
97         if (model.getRecordCount() < 2) {
98             Logger.global.info("Select not run for " + model);
99             return;
100         }
101         List JavaDoc oldValues = getValues(model);
102         
103         model.addValueChangeListener(valueReport);
104         model.setRecordIndex(model.getRecordCount() - 1);
105         assertEquals("recordIndex set", model.getRecordCount() - 1, model.getRecordIndex());
106         assertTrue("value changes fired", valueReport.hasEvents());
107         int changeCount = assertValueChangeFired(oldValues, model);
108         assertTrue("# changed values", changeCount > 0);
109     }
110
111     /**
112      * DataModel which allows empty selection (recordIndex == -1)
113      * should start deselected. This is in analogy to RecordSet/RowSet.
114      *
115      * As getValue() has no precondition, it must succeed always, returning
116      * null if deselected. Same with setValue().
117      *
118      * related to Issue #86: TableModelExtAdapter could not cope with rowIndex == -1.
119      */

120
121     public void testEmptySelectionOnInit() {
122         TableModelExtAdapter adapter = (TableModelExtAdapter) createEmptyDataModel(new MetaData[] { metaData }, 1 );
123         assertEquals("recordIndex on start", -1, adapter.getRecordIndex());
124         String JavaDoc[] fields = adapter.getFieldNames();
125         assertNull("value must be null if empty current", adapter.getValue(fields[0]));
126         adapter.setValue(fields[0], VALUE);
127     }
128     
129 //----------------------- super
130

131
132     protected boolean guaranteeNonEmptySelection(DataModel model) {
133       ((TableModelExtAdapter) model).setRecordIndex(0);
134       return true;
135     }
136     
137     protected DataModel createDataModelWithValidator(boolean valid) {
138         TableModelExtAdapter model = (TableModelExtAdapter) createEmptyDataModel(new MetaData[] { metaData }, 1);
139         model.setRecordIndex(0);
140         model.addValidator(TestDataModelFactory.createValidator(valid));
141         return model;
142     }
143
144     protected DataModel createEmptyDataModel(MetaData[] metaData, int rowCount) {
145         DefaultTableModelExt tabularData = TestSupport.createTabularDataModel(metaData, rowCount);
146         return new TableModelExtAdapter(tabularData);
147     }
148
149     protected DataModel createFilledDataModel() {
150         DefaultTableModelExt data = TestSupport.loadTabularCSVData(stringDataURL, true);
151         assertEquals("row count", 10, data.getRowCount());
152         TableModelExtAdapter adapter = new TableModelExtAdapter(data);
153         return adapter;
154     }
155
156    
157     protected void setUp() throws Exception JavaDoc {
158         super.setUp();
159         System.setProperty(DataLoader.READER_PRIORITY_KEY, String
160                 .valueOf(Thread.NORM_PRIORITY));
161         stringDataURL = getClass().getResource(STRINGDATA);
162     }
163     protected void tearDown() throws Exception JavaDoc {
164         System.getProperties().remove(DataLoader.READER_PRIORITY_KEY);
165         super.tearDown();
166     }
167 }
168
Popular Tags