KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > DataModelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.EventObject JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.event.ListDataEvent JavaDoc;
29 import junit.framework.TestCase;
30 import org.netbeans.swing.tabcontrol.event.ComplexListDataEvent;
31 import org.netbeans.swing.tabcontrol.event.ComplexListDataListener;
32
33 /** Some basic tests for DefaultTabDataModel, etc. Can be fleshed out into proper
34  * unit tests later.
35  *
36  * @author Tim Boudreau
37  */

38 public class DataModelTest extends TestCase implements ComplexListDataListener {
39     ComplexListDataEvent lastEvent = null;
40     String JavaDoc lastListenerCall=null;
41     
42
43     public DataModelTest(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     TabDataModel mdl=null;
48     Icon JavaDoc ic = new Icon JavaDoc () {
49         public int getIconWidth() {
50             return 16;
51         }
52         public int getIconHeight() {
53             return 16;
54         }
55         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
56             //do nothing
57
}
58     };
59     
60     Icon JavaDoc sameSizeIcon = new Icon JavaDoc () {
61         public int getIconWidth() {
62             return 16;
63         }
64         public int getIconHeight() {
65             return 16;
66         }
67         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
68             //do nothing
69
}
70     };
71     
72     Icon JavaDoc biggerIcon = new Icon JavaDoc () {
73         public int getIconWidth() {
74             return 22;
75         }
76         public int getIconHeight() {
77             return 22;
78         }
79         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
80             //do nothing
81
}
82     };
83     
84     public void setUp () {
85         prepareModel();
86         mdl.addComplexListDataListener(this);
87     }
88     
89     /** Weird, but this class was adapted from a standalone test written
90      * long ago and rescued from cvs history. It didn't use JUnit, and
91      * the assertTrue argument order was reversed. So in the interest of
92      * laziness... */

93     private void assertPravda (boolean val, String JavaDoc msg) {
94         assertTrue (msg, val);
95     }
96     
97     private void assertText (String JavaDoc name, int index) {
98         TabData td = mdl.getTab (index);
99         assertPravda (td.getText().equals (name), "Text at " + index + " was not " + name + " it was " + td.getText());
100     }
101     
102     private void assertData (int index, TabData data) {
103         TabData other = mdl.getTab (index);
104         assertPravda (data.equals(other), "TabData at " + index + " is not " + data + " it is " + other);
105     }
106     
107     private void assertListenerCall (String JavaDoc call) {
108         assertPravda (lastEvent != null, "Last listener call should be non-null");
109         assertPravda (call.equals (lastListenerCall), "Last listener call should have been " + call + " but was " + lastListenerCall);
110     }
111     
112     private void assertEventIndices (int indices[]) {
113         assertPravda (lastEvent != null, "Last event should have been fired but is null");
114         int[] ids = lastEvent.getIndices();
115         Arrays.sort (indices);
116         Arrays.sort (ids);
117         assertPravda (ids.length == indices.length, "Indices length in last change should be " + indices.length + " but is " + ids.length + " expected indices: " + arrToStr (indices) + " actual indices " + arrToStr(ids));
118         for (int i=0; i < ids.length; i++) {
119             assertPravda (ids[i] == indices[i], "Event indices should be " + arrToStr(indices) + " but are " + arrToStr(ids));
120         }
121     }
122     
123     private void assertEventIndices (int start, int end) {
124         assertPravda (lastEvent != null, "Last event should have been fired but is null");
125         assertPravda (start == lastEvent.getIndex0(), "Event start index should be " + start + " but is " + lastEvent.getIndex0());
126         assertPravda (end == lastEvent.getIndex1(), "Event end index should be " + end + " but is " + lastEvent.getIndex1());
127     }
128     
129     private void assertWidthChanged () {
130         assertPravda (lastEvent != null, "Last event should have been fired but is null");
131         assertPravda (lastEvent.isTextChanged(), "Last event should have been a text change event but wasn't");
132     }
133     
134     private void assertWidthNotChanged () {
135         assertPravda (lastEvent != null, "Last event should have been fired but is null");
136         assertPravda (!lastEvent.isTextChanged(), "Last event should not have been a text change event but was");
137     }
138     
139     /**
140      * Since this test was written in non-junit style, the order of the tests
141      * is important, so we have one junit method for all of the actual tests.
142      */

143     public void testEverything() {
144         doTestContentsValid();
145         doTestAdd();
146         doTestRemove();
147         doTestAddContiguous();
148         doTestRemoveContiguous();
149         doTestAddNonContiguous();
150         doTestRemoveNonContiguous();
151         doTestSetTab();
152         doTestSetTextNonContiguous();
153         doTestSetIconNonContiguous();
154         doTestSetIconAndTextNonContiguous();
155         System.err.println("All tests passed");
156     }
157     
158     public void doTestContentsValid () {
159         System.err.println("testContentsValid");
160         _testContentsValid();
161     }
162     
163     private void _testContentsValid () {
164         int ct=0;
165         for (char c='a'; c < 'z'; c++) {
166             TabData td = mdl.getTab(ct);
167             assertPravda (td.getText().charAt(0) == c, "Name at " + ct + " is not " + c + " it is " + td.getText());
168             ct++;
169         }
170     }
171     
172     public void doTestAdd () {
173         System.err.println("testAdd");
174         TabData td = new TabData (new JLabel JavaDoc ("testAdd"), ic, "testAdd", "Tip:testAdd");
175         mdl.addTabs(0, new TabData[] {td});
176         assertData (0, td);
177     }
178     
179     public void doTestRemove () {
180         System.err.println("testRemove");
181         mdl.removeTab(0);
182         assertText ("a", 0);
183     }
184     
185     public void doTestAddContiguous() {
186         System.err.println("testAddContiguous");
187         int formerSize = mdl.size();
188         TabData[] td = new TabData[10];
189         for (int i=0; i < 10; i++) {
190             String JavaDoc name = Integer.toString (i);
191             Component JavaDoc comp = new JLabel JavaDoc();
192             td[i] = new TabData (comp, ic, name, "Tip:"+name);
193         }
194         mdl.addTabs(10, td);
195         int expectedSize=formerSize+10;
196         assertPravda (mdl.size() == expectedSize, "Model size should be " + expectedSize + " after adding 10 items, but is " + mdl.size());
197         for (int i=10; i < 20; i++) {
198             assertData (i, td[i-10]);
199         }
200     }
201     
202     public void doTestRemoveContiguous() {
203         System.err.println("testRemoveContiguous");
204         int formerSize = mdl.size();
205         mdl.removeTabs (10, 19);
206         int expectedSize=formerSize - 10;
207         assertPravda (mdl.size() == expectedSize, "Model size should be " + expectedSize + " after removing 10 items, but is " + mdl.size());
208         try {
209             _testContentsValid();
210         } catch (RuntimeException JavaDoc e) {
211             e.printStackTrace();
212             fail("After removing 10 items, contents should be original contents, but are " + mdl.toString());
213         }
214     }
215     
216     TabData[] data=null;
217     public void doTestAddNonContiguous () {
218         System.err.println("testAddNonContiguous");
219         int[] indices = new int[] {3, 1, 5};
220         data = new TabData[3];
221         for (int i=0; i < indices.length; i++) {
222             String JavaDoc name = Integer.toString (indices[i]);
223             data[i] = new TabData (new JLabel JavaDoc(), ic, name, "Tip:"+name);
224         }
225         mdl.addTabs(indices, data);
226         assertData (3, data[0]);
227         assertData (1, data[1]);
228         assertData (5, data[2]);
229         Arrays.sort (indices);
230         assertEventIndices (indices);
231         assertListenerCall ("indicesAdded");
232     }
233     
234     public void doTestRemoveNonContiguous () {
235         int[] indices = new int[] {5, 1, 3};
236         mdl.removeTabs(indices);
237         try {
238             _testContentsValid();
239         } catch (RuntimeException JavaDoc e) {
240             System.err.println("After non-contiguous removal of " + arrToStr(indices) + ", contents should be original contents, but are " + mdl.toString());
241             throw e;
242         }
243         Arrays.sort(indices);
244         assertEventIndices (indices);
245         assertListenerCall ("indicesRemoved");
246     }
247     
248     public void doTestSetTab () {
249         System.err.println("testSetTab");
250         TabData former = mdl.getTab (22);
251         TabData nue = new TabData (new JLabel JavaDoc(), ic, "foo", "Tip:foo");
252         mdl.setTab (22, nue);
253         assertData (22, nue);
254         mdl.setTab (22, former);
255         assertListenerCall ("contentsChanged");
256         assertEventIndices (22, 22);
257         //Make sure an event is not generated for changes that should not generate one
258
noEvent = true;
259         mdl.setTab (22, former);
260         mdl.setText(22, former.getText());
261         mdl.setIcon (22, ic);
262         noEvent = false;
263     }
264     
265     public void doTestSetTextNonContiguous() {
266         System.err.println("testSetTextNonContiguous");
267         String JavaDoc[] names = new String JavaDoc [5];
268         int[] indices = new int[] {22,11,15,8,3};
269         for (int i=0; i < names.length; i++) {
270             names[i] = mdl.getTab(indices[i]).getText();
271         }
272         noEvent = true;
273         //should produce no event since the names haven't changed
274
mdl.setText(indices, names);
275         noEvent = false;
276         String JavaDoc[] s = new String JavaDoc[names.length];
277         for (int i=0; i < s.length; i++) {
278             s[i] = names[i] + "modified";
279         }
280         mdl.setText(indices, s);
281         for (int i=0; i < s.length; i++) {
282             assertText(s[i], indices[i]);
283         }
284         Arrays.sort (indices);
285         assertEventIndices(indices);
286         assertListenerCall("contentsChanged");
287         assertWidthChanged();
288         //restore the original text
289
mdl.setText(indices, names);
290     }
291     
292     public void doTestSetIcon () {
293         System.err.println("testSetIcon");
294         TabData td = mdl.getTab (20);
295         noEvent = true;
296         mdl.setIcon(20, td.getIcon());
297         noEvent = false;
298         
299         mdl.setIcon(20, sameSizeIcon);
300         assertEventIndices(20,20);
301         assertListenerCall("contentsChanged");
302         assertPravda (td.getIcon() == sameSizeIcon, "Icon was changed but same old still returned from TabData");
303         assertWidthNotChanged();
304         EventObject JavaDoc last = lastEvent;
305         
306         mdl.setIcon(20, biggerIcon);
307         assertWidthChanged();
308         assertPravda (last != lastEvent, "Icon changed but no event fired");
309         
310         //restore the state
311
mdl.setIcon(20,ic);
312     }
313
314     public void doTestSetIconNonContiguous() {
315         System.err.println("testSetIconNonContiguous");
316         Icon JavaDoc[] icons = new Icon JavaDoc[5];
317         int[] indices = new int[] {22,11,15,8,3};
318         for (int i=0; i < icons.length; i++) {
319             icons[i] = mdl.getTab(indices[i]).getIcon();
320         }
321         noEvent = true;
322         //should produce no event since the names haven't changed
323
mdl.setIcon(indices, icons);
324         noEvent = false;
325         
326         EventObject JavaDoc last = lastEvent;
327         
328         Arrays.fill (icons, sameSizeIcon);
329         mdl.setIcon (indices, icons);
330         assertPravda (last != lastEvent, "Icons changed but no event fired");
331         last = lastEvent;
332         assertListenerCall("contentsChanged");
333         assertWidthNotChanged();
334         
335         Arrays.fill (icons, biggerIcon);
336         icons[2] = sameSizeIcon;
337         int[] expectedIndices = new int[]{3,8,22,11};
338         
339         mdl.setIcon(indices, icons);
340         assertPravda (last != lastEvent, "Icons changed but no event fired");
341         assertListenerCall("contentsChanged");
342         assertWidthChanged();
343         assertEventIndices(expectedIndices);
344         
345         Arrays.fill (icons, ic);
346         //restore the original text
347
mdl.setIcon(indices, icons);
348     }
349     
350     public void doTestSetIconAndTextNonContiguous() {
351         System.err.println("testSetIconAndTextNonContiguous");
352         int indices[] = new int[] {3, 10, 5};
353         Icon JavaDoc[] icons = new Icon JavaDoc[3];
354         Arrays.fill (icons, ic);
355         String JavaDoc[] sts = new String JavaDoc[3];
356         for (int i=0; i < 3; i++) {
357             sts[i] = mdl.getTab(i).getText();
358         }
359         //ensure expected results
360
mdl.setIconsAndText (indices, sts, icons);
361         
362         noEvent = true;
363         mdl.setIconsAndText(indices, sts, icons);
364         noEvent = false;
365         String JavaDoc[] realText = new String JavaDoc[sts.length];
366         System.arraycopy(sts, 0, realText, 0, sts.length);
367         EventObject JavaDoc last = lastEvent;
368         
369         icons[0] = sameSizeIcon;
370         mdl.setIconsAndText(indices, sts, icons);
371         assertPravda (last != lastEvent, "Icons and text changed but no event fired");
372         assertWidthNotChanged();
373         assertEventIndices(new int[] {3});
374         last = lastEvent;
375         
376         icons[0] = biggerIcon;
377         sts[1] = "foobar";
378         mdl.setIconsAndText(indices, sts, icons);
379         assertPravda (last != lastEvent, "Icons and text changed but no event fired");
380         assertWidthChanged();
381         assertEventIndices(new int[] {3, 10});
382         last = lastEvent;
383         
384         //Also test the simpler firing code for when all changed icons have changed text
385
icons[0] = ic;
386         sts[1] = "boo";
387         mdl.setIconsAndText(indices, sts, icons);
388         assertPravda (last != lastEvent, "Icons and text changed but no event fired");
389         assertWidthChanged();
390         assertEventIndices(new int[] {3, 10});
391         
392         //restore the state
393
Arrays.fill (icons, ic);
394         mdl.setIconsAndText (indices, realText, icons);
395     }
396     
397     
398     static String JavaDoc arrToStr (int[] ints) {
399         if (ints == null) return "null";
400         StringBuffer JavaDoc out = new StringBuffer JavaDoc (ints.length * 3);
401         for (int i=0; i < ints.length; i++) {
402             out.append (ints[i]);
403             if (i != ints.length-1) {
404                 out.append (",");
405             }
406         }
407         return out.toString();
408     }
409     
410     static String JavaDoc arrToStr (Object JavaDoc[] o) {
411         if (o == null) return "null";
412         StringBuffer JavaDoc out = new StringBuffer JavaDoc (o.length * 3);
413         for (int i=0; i < o.length; i++) {
414             out.append (o[i]);
415             if (i != o.length-1) {
416                 out.append (",");
417             }
418         }
419         return out.toString();
420     }
421     
422     private void prepareModel() {
423         if (mdl != null) {
424             mdl.removeComplexListDataListener(this);
425         }
426         TabData[] td = new TabData[25];
427         int ct = 0;
428         for (char c='a'; c < 'z'; c++) {
429             String JavaDoc name = new String JavaDoc (new char[]{c});
430             Component JavaDoc comp = new JLabel JavaDoc(name);
431             comp.setName (name);
432             td[ct] = new TabData (comp, ic, name, "tip:"+name);
433             ct++;
434         }
435         mdl = new DefaultTabDataModel (td);
436     }
437     
438     /**
439      * @param args the command line arguments
440      */

441     public static void main(String JavaDoc[] args) {
442         new DataModelTest("foo").run();
443     }
444     
445     public void contentsChanged(ListDataEvent JavaDoc e) {
446         lastListenerCall="contentsChanged";
447         lastEvent = (ComplexListDataEvent)e;
448         if (noEvent) {
449             assertPravda (false, "No event expected but " + e + " receieved");
450         }
451     }
452     
453     public void indicesAdded(ComplexListDataEvent e) {
454         lastListenerCall="indicesAdded";
455         lastEvent = e;
456         if (noEvent) {
457             assertPravda (false, "No event expected but " + e + " receieved");
458         }
459     }
460     
461     public void indicesChanged(ComplexListDataEvent e) {
462         lastListenerCall="indicesChanged";
463         lastEvent = e;
464         if (noEvent) {
465             assertPravda (false, "No event expected but " + e + " receieved");
466         }
467     }
468     
469     public void indicesRemoved(ComplexListDataEvent e) {
470         lastListenerCall="indicesRemoved";
471         lastEvent = e;
472         if (noEvent) {
473             assertPravda (false, "No event expected but " + e + " receieved");
474         }
475     }
476     
477     public void intervalAdded(ListDataEvent JavaDoc e) {
478         lastListenerCall="intervalAdded";
479         assertPravda (e.getIndex0() <= e.getIndex1(), "Event start index > end index");
480
481         lastEvent = (ComplexListDataEvent)e;
482         if (noEvent) {
483             assertPravda (false, "No event expected but " + e + " receieved");
484         }
485     }
486     
487     public void intervalRemoved(ListDataEvent JavaDoc e) {
488         lastListenerCall="intervalRemoved";
489         lastEvent = (ComplexListDataEvent)e;
490         if (noEvent) {
491             assertPravda (false, "No event expected but " + e + " receieved");
492         }
493     }
494     
495     boolean noEvent = false;
496 }
497
Popular Tags