KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > test > ListModelTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.test;
31
32 import nextapp.echo2.app.event.ListDataEvent;
33 import nextapp.echo2.app.event.ListDataListener;
34 import nextapp.echo2.app.list.DefaultListModel;
35 import junit.framework.TestCase;
36
37 /**
38  * Unit test(s) for the <code>nextapp.echo2.app.list.ListModel</code> and
39  * derivatives.
40  */

41 public class ListModelTest extends TestCase {
42     
43     private class ListenerTestDefaultListModel extends DefaultListModel {
44         
45         public int getEventListenerCount() {
46             return getEventListenerList().getListenerCount(ListDataListener.class);
47         }
48     }
49     
50     private class TestListDataListener
51     implements ListDataListener {
52         
53         private ListDataEvent changeEvent, addEvent, removeEvent;
54         
55         /**
56          * @see nextapp.echo2.app.event.ListDataListener#contentsChanged(nextapp.echo2.app.event.ListDataEvent)
57          */

58         public void contentsChanged(ListDataEvent e) {
59             changeEvent = e;
60         }
61
62         /**
63          * @see nextapp.echo2.app.event.ListDataListener#intervalAdded(nextapp.echo2.app.event.ListDataEvent)
64          */

65         public void intervalAdded(ListDataEvent e) {
66             addEvent = e;
67         }
68
69         /**
70          * @see nextapp.echo2.app.event.ListDataListener#intervalRemoved(nextapp.echo2.app.event.ListDataEvent)
71          */

72         public void intervalRemoved(ListDataEvent e) {
73             removeEvent = e;
74         }
75     }
76     
77     public void testAddAtEnd() {
78         DefaultListModel listModel = new DefaultListModel(new Object JavaDoc[]{"alpha", "bravo", "charlie"});
79         TestListDataListener testListener = new TestListDataListener();
80         listModel.addListDataListener(testListener);
81         listModel.add("delta");
82         assertEquals(4, listModel.size());
83         assertEquals("alpha", listModel.get(0));
84         assertEquals("bravo", listModel.get(1));
85         assertEquals("charlie", listModel.get(2));
86         assertEquals("delta", listModel.get(3));
87         assertNull(testListener.changeEvent);
88         assertNull(testListener.removeEvent);
89         assertNotNull(testListener.addEvent);
90         assertEquals(listModel, testListener.addEvent.getSource());
91         assertEquals(3, testListener.addEvent.getIndex0());
92         assertEquals(3, testListener.addEvent.getIndex1());
93         assertEquals(ListDataEvent.INTERVAL_ADDED, testListener.addEvent.getType());
94     }
95
96     public void testAddAtIndex() {
97         DefaultListModel listModel = new DefaultListModel(new Object JavaDoc[]{"alpha", "bravo", "charlie"});
98         TestListDataListener testListener = new TestListDataListener();
99         listModel.addListDataListener(testListener);
100         listModel.add(2, "delta");
101         assertEquals(4, listModel.size());
102         assertEquals("alpha", listModel.get(0));
103         assertEquals("bravo", listModel.get(1));
104         assertEquals("delta", listModel.get(2));
105         assertEquals("charlie", listModel.get(3));
106         assertNull(testListener.changeEvent);
107         assertNull(testListener.removeEvent);
108         assertNotNull(testListener.addEvent);
109         assertEquals(listModel, testListener.addEvent.getSource());
110         assertEquals(2, testListener.addEvent.getIndex0());
111         assertEquals(2, testListener.addEvent.getIndex1());
112         assertEquals(ListDataEvent.INTERVAL_ADDED, testListener.addEvent.getType());
113     }
114     
115     public void testBasic() {
116         DefaultListModel listModel = new DefaultListModel(new Object JavaDoc[]{"alpha", "bravo", "charlie"});
117         assertEquals(3, listModel.size());
118         assertEquals("alpha", listModel.get(0));
119         assertEquals("bravo", listModel.get(1));
120         assertEquals("charlie", listModel.get(2));
121         assertEquals(1, listModel.indexOf("bravo"));
122     }
123     
124     public void testListenerManagement() {
125         ListenerTestDefaultListModel listModel = new ListenerTestDefaultListModel();
126         TestListDataListener testListener = new TestListDataListener();
127         assertEquals(0, listModel.getEventListenerCount());
128         listModel.addListDataListener(testListener);
129         assertEquals(1, listModel.getEventListenerCount());
130         listModel.removeListDataListener(testListener);
131         assertEquals(0, listModel.getEventListenerCount());
132     }
133     
134     public void testRemoveByIndex() {
135         DefaultListModel listModel = new DefaultListModel(new Object JavaDoc[]{"alpha", "bravo", "charlie"});
136         TestListDataListener testListener = new TestListDataListener();
137         listModel.addListDataListener(testListener);
138         listModel.remove(1);
139         assertEquals(2, listModel.size());
140         assertEquals("alpha", listModel.get(0));
141         assertEquals("charlie", listModel.get(1));
142         assertNull(testListener.changeEvent);
143         assertNull(testListener.addEvent);
144         assertNotNull(testListener.removeEvent);
145         assertEquals(listModel, testListener.removeEvent.getSource());
146         assertEquals(1, testListener.removeEvent.getIndex0());
147         assertEquals(1, testListener.removeEvent.getIndex1());
148         assertEquals(ListDataEvent.INTERVAL_REMOVED, testListener.removeEvent.getType());
149     }
150     
151     public void testRemoveByItem() {
152         DefaultListModel listModel = new DefaultListModel(new Object JavaDoc[]{"alpha", "bravo", "charlie"});
153         TestListDataListener testListener = new TestListDataListener();
154         listModel.addListDataListener(testListener);
155         listModel.remove("bravo");
156         assertEquals(2, listModel.size());
157         assertEquals("alpha", listModel.get(0));
158         assertEquals("charlie", listModel.get(1));
159         assertNull(testListener.changeEvent);
160         assertNull(testListener.addEvent);
161         assertNotNull(testListener.removeEvent);
162         assertEquals(listModel, testListener.removeEvent.getSource());
163         assertEquals(1, testListener.removeEvent.getIndex0());
164         assertEquals(1, testListener.removeEvent.getIndex1());
165         assertEquals(ListDataEvent.INTERVAL_REMOVED, testListener.removeEvent.getType());
166     }
167 }
168
Popular Tags