001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import nextapp.echo2.app.event.ListDataEvent;
033: import nextapp.echo2.app.event.ListDataListener;
034: import nextapp.echo2.app.list.DefaultListModel;
035: import junit.framework.TestCase;
036:
037: /**
038: * Unit test(s) for the <code>nextapp.echo2.app.list.ListModel</code> and
039: * derivatives.
040: */
041: public class ListModelTest extends TestCase {
042:
043: private class ListenerTestDefaultListModel extends DefaultListModel {
044:
045: public int getEventListenerCount() {
046: return getEventListenerList().getListenerCount(
047: ListDataListener.class);
048: }
049: }
050:
051: private class TestListDataListener implements ListDataListener {
052:
053: private ListDataEvent changeEvent, addEvent, removeEvent;
054:
055: /**
056: * @see nextapp.echo2.app.event.ListDataListener#contentsChanged(nextapp.echo2.app.event.ListDataEvent)
057: */
058: public void contentsChanged(ListDataEvent e) {
059: changeEvent = e;
060: }
061:
062: /**
063: * @see nextapp.echo2.app.event.ListDataListener#intervalAdded(nextapp.echo2.app.event.ListDataEvent)
064: */
065: public void intervalAdded(ListDataEvent e) {
066: addEvent = e;
067: }
068:
069: /**
070: * @see nextapp.echo2.app.event.ListDataListener#intervalRemoved(nextapp.echo2.app.event.ListDataEvent)
071: */
072: public void intervalRemoved(ListDataEvent e) {
073: removeEvent = e;
074: }
075: }
076:
077: public void testAddAtEnd() {
078: DefaultListModel listModel = new DefaultListModel(new Object[] {
079: "alpha", "bravo", "charlie" });
080: TestListDataListener testListener = new TestListDataListener();
081: listModel.addListDataListener(testListener);
082: listModel.add("delta");
083: assertEquals(4, listModel.size());
084: assertEquals("alpha", listModel.get(0));
085: assertEquals("bravo", listModel.get(1));
086: assertEquals("charlie", listModel.get(2));
087: assertEquals("delta", listModel.get(3));
088: assertNull(testListener.changeEvent);
089: assertNull(testListener.removeEvent);
090: assertNotNull(testListener.addEvent);
091: assertEquals(listModel, testListener.addEvent.getSource());
092: assertEquals(3, testListener.addEvent.getIndex0());
093: assertEquals(3, testListener.addEvent.getIndex1());
094: assertEquals(ListDataEvent.INTERVAL_ADDED,
095: testListener.addEvent.getType());
096: }
097:
098: public void testAddAtIndex() {
099: DefaultListModel listModel = new DefaultListModel(new Object[] {
100: "alpha", "bravo", "charlie" });
101: TestListDataListener testListener = new TestListDataListener();
102: listModel.addListDataListener(testListener);
103: listModel.add(2, "delta");
104: assertEquals(4, listModel.size());
105: assertEquals("alpha", listModel.get(0));
106: assertEquals("bravo", listModel.get(1));
107: assertEquals("delta", listModel.get(2));
108: assertEquals("charlie", listModel.get(3));
109: assertNull(testListener.changeEvent);
110: assertNull(testListener.removeEvent);
111: assertNotNull(testListener.addEvent);
112: assertEquals(listModel, testListener.addEvent.getSource());
113: assertEquals(2, testListener.addEvent.getIndex0());
114: assertEquals(2, testListener.addEvent.getIndex1());
115: assertEquals(ListDataEvent.INTERVAL_ADDED,
116: testListener.addEvent.getType());
117: }
118:
119: public void testBasic() {
120: DefaultListModel listModel = new DefaultListModel(new Object[] {
121: "alpha", "bravo", "charlie" });
122: assertEquals(3, listModel.size());
123: assertEquals("alpha", listModel.get(0));
124: assertEquals("bravo", listModel.get(1));
125: assertEquals("charlie", listModel.get(2));
126: assertEquals(1, listModel.indexOf("bravo"));
127: }
128:
129: public void testListenerManagement() {
130: ListenerTestDefaultListModel listModel = new ListenerTestDefaultListModel();
131: TestListDataListener testListener = new TestListDataListener();
132: assertEquals(0, listModel.getEventListenerCount());
133: listModel.addListDataListener(testListener);
134: assertEquals(1, listModel.getEventListenerCount());
135: listModel.removeListDataListener(testListener);
136: assertEquals(0, listModel.getEventListenerCount());
137: }
138:
139: public void testRemoveByIndex() {
140: DefaultListModel listModel = new DefaultListModel(new Object[] {
141: "alpha", "bravo", "charlie" });
142: TestListDataListener testListener = new TestListDataListener();
143: listModel.addListDataListener(testListener);
144: listModel.remove(1);
145: assertEquals(2, listModel.size());
146: assertEquals("alpha", listModel.get(0));
147: assertEquals("charlie", listModel.get(1));
148: assertNull(testListener.changeEvent);
149: assertNull(testListener.addEvent);
150: assertNotNull(testListener.removeEvent);
151: assertEquals(listModel, testListener.removeEvent.getSource());
152: assertEquals(1, testListener.removeEvent.getIndex0());
153: assertEquals(1, testListener.removeEvent.getIndex1());
154: assertEquals(ListDataEvent.INTERVAL_REMOVED,
155: testListener.removeEvent.getType());
156: }
157:
158: public void testRemoveByItem() {
159: DefaultListModel listModel = new DefaultListModel(new Object[] {
160: "alpha", "bravo", "charlie" });
161: TestListDataListener testListener = new TestListDataListener();
162: listModel.addListDataListener(testListener);
163: listModel.remove("bravo");
164: assertEquals(2, listModel.size());
165: assertEquals("alpha", listModel.get(0));
166: assertEquals("charlie", listModel.get(1));
167: assertNull(testListener.changeEvent);
168: assertNull(testListener.addEvent);
169: assertNotNull(testListener.removeEvent);
170: assertEquals(listModel, testListener.removeEvent.getSource());
171: assertEquals(1, testListener.removeEvent.getIndex0());
172: assertEquals(1, testListener.removeEvent.getIndex1());
173: assertEquals(ListDataEvent.INTERVAL_REMOVED,
174: testListener.removeEvent.getType());
175: }
176: }
|