1   /*
2    * Copyright 2002-2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * Testcase for <code>ListListModel</code>
29   * 
30   * @author Keith Donald
31   * @author Peter De Bruycker
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 }