1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.binding.value.support;
17
18 import java.util.Arrays;
19
20 import javax.swing.event.ListDataEvent;
21 import javax.swing.event.ListDataListener;
22
23 import junit.framework.TestCase;
24
25 import org.easymock.EasyMock;
26
27
28
29
30
31
32
33 public class ListListModelTests extends TestCase {
34
35 private ListDataListener mockListener;
36
37 protected void setUp() throws Exception {
38 mockListener = (ListDataListener) EasyMock.createMock(ListDataListener.class);
39 }
40
41 public void testAddAllCollection() {
42 ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
43 model.addListDataListener(mockListener);
44
45 mockListener.intervalAdded(eq(new ListDataEvent(model, ListDataEvent.INTERVAL_ADDED, 3, 5)));
46 EasyMock.replay(mockListener);
47
48 model.addAll(Arrays.asList(new Object[] { "4", "5", "6" }));
49
50 assertEquals(Arrays.asList(new Object[] { "1", "2", "3", "4", "5", "6" }), model);
51
52 EasyMock.verify(mockListener);
53 }
54
55 public void testRetainAll() {
56 ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3", "4", "5" }));
57 model.addListDataListener(mockListener);
58
59 mockListener.contentsChanged(eq(new ListDataEvent(model, ListDataEvent.CONTENTS_CHANGED, -1, -1)));
60 EasyMock.replay(mockListener);
61
62 model.retainAll(Arrays.asList(new Object[] { "2", "5" }));
63
64 assertEquals(Arrays.asList(new Object[] { "2", "5" }), model);
65
66 EasyMock.verify(mockListener);
67 }
68
69 public void testRemoveAll() {
70 ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3", "4", "5" }));
71 model.addListDataListener(mockListener);
72
73 mockListener.contentsChanged(eq(new ListDataEvent(model, ListDataEvent.CONTENTS_CHANGED, -1, -1)));
74 EasyMock.replay(mockListener);
75
76 model.removeAll(Arrays.asList(new Object[] { "2", "5" }));
77
78 assertEquals(Arrays.asList(new Object[] { "1", "3", "4" }), model);
79
80 EasyMock.verify(mockListener);
81 }
82
83 public void testRemove() {
84 ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
85 model.addListDataListener(mockListener);
86
87 mockListener.intervalRemoved(eq(new ListDataEvent(model, ListDataEvent.INTERVAL_REMOVED, 1, 1)));
88 EasyMock.replay(mockListener);
89
90 model.remove(1);
91
92 assertEquals(Arrays.asList(new Object[] { "1", "3" }), model);
93
94 EasyMock.verify(mockListener);
95 }
96
97 public void testRemoveObject() {
98 ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
99 model.addListDataListener(mockListener);
100
101 mockListener.intervalRemoved(eq(new ListDataEvent(model, ListDataEvent.INTERVAL_REMOVED, 1, 1)));
102 EasyMock.replay(mockListener);
103
104 model.remove("2");
105
106 assertEquals(Arrays.asList(new Object[] { "1", "3" }), model);
107
108 EasyMock.verify(mockListener);
109 }
110
111 public static ListDataEvent eq(ListDataEvent expected) {
112 EasyMock.reportMatcher(new ListDataEventArgumentMatcher(expected));
113
114 return null;
115 }
116 }