1 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 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 58 public void contentsChanged(ListDataEvent e) { 59 changeEvent = e; 60 } 61 62 65 public void intervalAdded(ListDataEvent e) { 66 addEvent = e; 67 } 68 69 72 public void intervalRemoved(ListDataEvent e) { 73 removeEvent = e; 74 } 75 } 76 77 public void testAddAtEnd() { 78 DefaultListModel listModel = new DefaultListModel(new Object []{"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 []{"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 []{"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 []{"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 []{"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 |