KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractDataModelTst.java,v 1.4 2005/02/25 13:29:07 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
8 package org.jdesktop.swing.data;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14
15 import org.jdesktop.swing.util.PropertyChangeReport;
16 import org.jdesktop.swing.util.ValueChangeReport;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22
23 /**
24  * JUnit test class for data model interface and implementing classes.
25  *
26  * @author Jeanette Winzenburg
27  */

28 public abstract class AbstractDataModelTst extends TestCase {
29     protected static final String JavaDoc EXISTING_FIELDNAME = "existing";
30     protected static final String JavaDoc NON_EXISTING_FIELDNAME = "non_existing";
31     protected static final String JavaDoc VALUE = "somevalue";
32
33     protected MetaData metaData;
34     protected ValueChangeReport valueReport;
35
36
37     public AbstractDataModelTst() {
38     }
39     
40     public AbstractDataModelTst(String JavaDoc name) {
41         super(name);
42     }
43     /**
44      * To be decided: what should happen if code tries to
45      * access unknown fields?
46      *
47      */

48     public void testModelInstantiation() {
49         DataModel model = createEmptyDataModel(new MetaData[] { metaData }, 1 );
50         guaranteeNonEmptySelection(model);
51         
52 // try {
53
// model.getValue("non_existing");
54
// fail("model must throw on non_existing fieldName " );
55
// } catch (IllegalArgumentException ex){
56
// // nothing registered
57
// }
58
// try {
59
// model.setValue("non_existing", "dummy");
60
// fail("model must throw on non_existing fieldName " );
61
// } catch (IllegalArgumentException ex){
62
// // nothing registered
63
// }
64

65     }
66
67     /**
68      * test setting value and notification.
69      *
70      */

71     public void testNotificationDataModelSetValue() {
72        DataModel model = createEmptyDataModel(new MetaData[] { metaData }, 1 );
73        if (!guaranteeNonEmptySelection(model)) return;
74         model.addValueChangeListener(valueReport);
75         model.setValue(EXISTING_FIELDNAME, VALUE);
76         assertEquals("value must be set", VALUE, model.getValue(EXISTING_FIELDNAME));
77         assertEquals("valueChangeEvent must be fired for property ", EXISTING_FIELDNAME, valueReport.getLastFieldName());
78       }
79
80
81
82
83     /**
84      * Issue #126: AbstractDataModel.getValidators()
85      * was throwing ClassCastException.
86      *
87      */

88     public void testValidatorAccess() {
89         DataModel dataModel = createDataModelWithValidator(false);
90         dataModel.getValidators();
91     }
92     
93     /**
94      * asserts that we got a valueChangeEvent for every changed value.
95      *
96      * @param oldValues values before changing recordIndex
97      * @param model DataModel that should have fired value change events
98      * @return count of change events.
99      */

100     protected int assertValueChangeFired(List JavaDoc oldValues, DataModel model) {
101         String JavaDoc[] fieldNames = model.getFieldNames();
102         int changeCount = 0;
103         for (int i = 0; i < fieldNames.length; i++) {
104             if (!equals(oldValues.get(i), model.getValue(fieldNames[i]))) {
105                 changeCount++;
106                 assertTrue("must have fired value change for "
107                         + fieldNames[i] + "old/new: \n" +
108                         oldValues.get(i) + "/" + model.getValue(fieldNames[i]),
109                         valueReport.gotEvent(fieldNames[i]));
110             } else {
111                 assertFalse("must not fire value change for unmodified field"
112                                 + fieldNames[i] + "old/new: \n" +
113                                 oldValues.get(i) + "/" + model.getValue(fieldNames[i]),
114                         valueReport.gotEvent(fieldNames[i]));
115             }
116         }
117         return changeCount;
118         
119     }
120
121     /**
122      * convenience: asserts that all values in list are null.
123      *
124      * @param values
125      */

126     protected void assertNullValues(List JavaDoc values) {
127         for (Iterator JavaDoc iter = values.iterator(); iter.hasNext();) {
128             assertNull("value must be null ", iter.next());
129             
130         }
131         
132     }
133     
134     protected boolean equals(Object JavaDoc oldValue, Object JavaDoc value) {
135         // ARGGGHH - do it once and for all somewhere
136
if ((oldValue != null && !oldValue.equals(value)) ||
137                 (oldValue == null && value != null)) {
138             return false;
139         }
140         return true;
141     }
142
143     /**
144      * returns a list of the current values in model.
145      * They are ordered as in getFieldNames().
146      *
147      * @param model
148      * @return
149      */

150     protected List JavaDoc getValues(DataModel model) {
151         List JavaDoc values = new ArrayList JavaDoc();
152         String JavaDoc[] fieldNames = model.getFieldNames();
153         for (int i = 0; i < fieldNames.length; i++) {
154             values.add(model.getValue(fieldNames[i]));
155         }
156         return values;
157     }
158
159     /**
160      * modify the model to have a selection (if applicable). It
161      * should be in a such that:
162      * <pre>
163      * model.setValue(fieldName, value);
164      * areEqual(value, model.getValue(fieldName);
165      * </pre>
166      *
167      * @param model
168      * @return true if the above is guaranteed, false if not.
169      */

170     protected abstract boolean guaranteeNonEmptySelection(DataModel model);
171     
172     /**
173      * create a model that has at least one validator which has a constant
174      * validation state as given by the parameter.
175      * @param valid
176      * @return
177      */

178     protected abstract DataModel createDataModelWithValidator(boolean valid);
179     
180     /**
181      * create a model with the given fields and given number of empty rows.
182      * @param metaData
183      * @param rowCount
184      * @return
185      */

186     protected abstract DataModel createEmptyDataModel(MetaData[] metaData, int rowCount);
187
188     /**
189      * create a DataModel with at least 2 columns, and (if applicable) at least 2 rows
190      * values in all cells.
191      * @return
192      */

193     protected abstract DataModel createFilledDataModel();
194
195  
196     protected void setUp() throws Exception JavaDoc {
197         super.setUp();
198         metaData = new MetaData(EXISTING_FIELDNAME, EXISTING_FIELDNAME.getClass(), EXISTING_FIELDNAME + "Label");
199         valueReport = new ValueChangeReport();
200     }
201
202     
203 }
204
Popular Tags