KickJava   Java API By Example, From Geeks To Geeks.

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


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.ChangeEvent;
33 import nextapp.echo2.app.event.ChangeListener;
34 import nextapp.echo2.app.list.DefaultListSelectionModel;
35 import junit.framework.TestCase;
36
37 /**
38  * Unit test(s) for the <code>nextapp.echo2.app.list.ListSelectionModel</code>
39  * and derivatives.
40  */

41 public class ListSelectionModelTest extends TestCase {
42     
43     public class TestChangeListener
44     implements ChangeListener {
45         
46         private ChangeEvent e;
47         
48         /**
49          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
50          */

51         public void stateChanged(ChangeEvent e) {
52             this.e = e;
53         }
54     }
55     
56     public void testChangeSelectionMode() {
57         DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
58         TestChangeListener changeListener = new TestChangeListener();
59         selectionModel.addChangeListener(changeListener);
60         selectionModel.setSelectionMode(DefaultListSelectionModel.MULTIPLE_SELECTION);
61         assertEquals(DefaultListSelectionModel.MULTIPLE_SELECTION, selectionModel.getSelectionMode());
62         selectionModel.setSelectedIndex(25, true);
63         selectionModel.setSelectedIndex(27, true);
64         selectionModel.setSelectedIndex(50, true);
65         selectionModel.setSelectedIndex(44, true);
66         assertEquals(25, selectionModel.getMinSelectedIndex());
67         assertEquals(50, selectionModel.getMaxSelectedIndex());
68         changeListener.e = null;
69         
70         selectionModel.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
71         assertEquals(DefaultListSelectionModel.SINGLE_SELECTION, selectionModel.getSelectionMode());
72         assertEquals(25, selectionModel.getMinSelectedIndex());
73         assertEquals(25, selectionModel.getMaxSelectedIndex());
74         assertNotNull(changeListener.e);
75         assertEquals(selectionModel, changeListener.e.getSource());
76     }
77     
78     public void testInitialState() {
79         DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
80         
81         assertTrue(selectionModel.isSelectionEmpty());
82         assertEquals(-1, selectionModel.getMinSelectedIndex());
83         assertEquals(-1, selectionModel.getMaxSelectedIndex());
84         assertFalse(selectionModel.isSelectedIndex(0));
85         assertFalse(selectionModel.isSelectedIndex(1));
86         assertFalse(selectionModel.isSelectedIndex(2));
87     }
88     
89     public void testMultipleSelection() {
90         DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
91         selectionModel.setSelectionMode(DefaultListSelectionModel.MULTIPLE_SELECTION);
92         assertEquals(DefaultListSelectionModel.MULTIPLE_SELECTION, selectionModel.getSelectionMode());
93         
94         TestChangeListener changeListener = new TestChangeListener();
95         selectionModel.addChangeListener(changeListener);
96         
97         selectionModel.setSelectedIndex(50, true);
98         assertFalse(selectionModel.isSelectionEmpty());
99         assertTrue(selectionModel.isSelectedIndex(50));
100         assertFalse(selectionModel.isSelectedIndex(0));
101         assertFalse(selectionModel.isSelectedIndex(5));
102         assertFalse(selectionModel.isSelectedIndex(49));
103         assertFalse(selectionModel.isSelectedIndex(51));
104         assertEquals(50, selectionModel.getMinSelectedIndex());
105         assertEquals(50, selectionModel.getMaxSelectedIndex());
106         assertNotNull(changeListener.e);
107         assertEquals(selectionModel, changeListener.e.getSource());
108         changeListener.e = null;
109         
110         selectionModel.setSelectedIndex(75, true);
111         assertFalse(selectionModel.isSelectionEmpty());
112         assertTrue(selectionModel.isSelectedIndex(50));
113         assertTrue(selectionModel.isSelectedIndex(75));
114         assertEquals(50, selectionModel.getMinSelectedIndex());
115         assertEquals(75, selectionModel.getMaxSelectedIndex());
116         assertNotNull(changeListener.e);
117         assertEquals(selectionModel, changeListener.e.getSource());
118         changeListener.e = null;
119         
120         selectionModel.setSelectedIndex(67, true);
121         assertFalse(selectionModel.isSelectionEmpty());
122         assertTrue(selectionModel.isSelectedIndex(50));
123         assertTrue(selectionModel.isSelectedIndex(67));
124         assertTrue(selectionModel.isSelectedIndex(75));
125         assertEquals(50, selectionModel.getMinSelectedIndex());
126         assertEquals(75, selectionModel.getMaxSelectedIndex());
127         assertNotNull(changeListener.e);
128         assertEquals(selectionModel, changeListener.e.getSource());
129         changeListener.e = null;
130         
131         selectionModel.setSelectedIndex(21, true);
132         assertFalse(selectionModel.isSelectionEmpty());
133         assertTrue(selectionModel.isSelectedIndex(21));
134         assertTrue(selectionModel.isSelectedIndex(50));
135         assertTrue(selectionModel.isSelectedIndex(67));
136         assertTrue(selectionModel.isSelectedIndex(75));
137         assertEquals(21, selectionModel.getMinSelectedIndex());
138         assertEquals(75, selectionModel.getMaxSelectedIndex());
139         assertNotNull(changeListener.e);
140         assertEquals(selectionModel, changeListener.e.getSource());
141         changeListener.e = null;
142
143         selectionModel.setSelectedIndex(75, false);
144         assertFalse(selectionModel.isSelectionEmpty());
145         assertTrue(selectionModel.isSelectedIndex(21));
146         assertTrue(selectionModel.isSelectedIndex(50));
147         assertTrue(selectionModel.isSelectedIndex(67));
148         assertFalse(selectionModel.isSelectedIndex(75));
149         assertEquals(21, selectionModel.getMinSelectedIndex());
150         assertEquals(67, selectionModel.getMaxSelectedIndex());
151         assertNotNull(changeListener.e);
152         assertEquals(selectionModel, changeListener.e.getSource());
153         changeListener.e = null;
154
155         selectionModel.setSelectedIndex(21, false);
156         assertFalse(selectionModel.isSelectionEmpty());
157         assertFalse(selectionModel.isSelectedIndex(21));
158         assertTrue(selectionModel.isSelectedIndex(50));
159         assertTrue(selectionModel.isSelectedIndex(67));
160         assertFalse(selectionModel.isSelectedIndex(75));
161         assertEquals(50, selectionModel.getMinSelectedIndex());
162         assertEquals(67, selectionModel.getMaxSelectedIndex());
163         assertNotNull(changeListener.e);
164         assertEquals(selectionModel, changeListener.e.getSource());
165         changeListener.e = null;
166         
167         selectionModel.clearSelection();
168         assertTrue(selectionModel.isSelectionEmpty());
169         assertFalse(selectionModel.isSelectedIndex(21));
170         assertFalse(selectionModel.isSelectedIndex(50));
171         assertFalse(selectionModel.isSelectedIndex(67));
172         assertFalse(selectionModel.isSelectedIndex(75));
173         assertEquals(-1, selectionModel.getMinSelectedIndex());
174         assertEquals(-1, selectionModel.getMaxSelectedIndex());
175         assertNotNull(changeListener.e);
176         assertEquals(selectionModel, changeListener.e.getSource());
177         changeListener.e = null;
178     }
179     
180     public void testSingleSelection() {
181         DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
182         assertEquals(DefaultListSelectionModel.SINGLE_SELECTION, selectionModel.getSelectionMode());
183         TestChangeListener changeListener = new TestChangeListener();
184         selectionModel.addChangeListener(changeListener);
185         
186         selectionModel.setSelectedIndex(50, true);
187         assertFalse(selectionModel.isSelectionEmpty());
188         assertTrue(selectionModel.isSelectedIndex(50));
189         assertFalse(selectionModel.isSelectedIndex(0));
190         assertFalse(selectionModel.isSelectedIndex(5));
191         assertFalse(selectionModel.isSelectedIndex(49));
192         assertFalse(selectionModel.isSelectedIndex(51));
193         assertEquals(50, selectionModel.getMinSelectedIndex());
194         assertEquals(50, selectionModel.getMaxSelectedIndex());
195         assertNotNull(changeListener.e);
196         assertEquals(selectionModel, changeListener.e.getSource());
197         changeListener.e = null;
198         
199         selectionModel.setSelectedIndex(75, true);
200         assertFalse(selectionModel.isSelectionEmpty());
201         assertFalse(selectionModel.isSelectedIndex(50));
202         assertTrue(selectionModel.isSelectedIndex(75));
203         assertEquals(75, selectionModel.getMinSelectedIndex());
204         assertEquals(75, selectionModel.getMaxSelectedIndex());
205         assertNotNull(changeListener.e);
206         assertEquals(selectionModel, changeListener.e.getSource());
207         changeListener.e = null;
208
209         selectionModel.clearSelection();
210         assertTrue(selectionModel.isSelectionEmpty());
211         assertFalse(selectionModel.isSelectedIndex(50));
212         assertFalse(selectionModel.isSelectedIndex(75));
213         assertEquals(-1, selectionModel.getMinSelectedIndex());
214         assertEquals(-1, selectionModel.getMaxSelectedIndex());
215         assertNotNull(changeListener.e);
216         assertEquals(selectionModel, changeListener.e.getSource());
217         changeListener.e = null;
218     }
219 }
220
Popular Tags