001    /*
002     * Copyright 2002-2007 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.binding.value.support;
017    
018    import java.util.Arrays;
019    
020    import javax.swing.event.ListDataEvent;
021    import javax.swing.event.ListDataListener;
022    
023    import junit.framework.TestCase;
024    
025    import org.easymock.EasyMock;
026    
027    /**
028     * Testcase for <code>ListListModel</code>
029     * 
030     * @author Keith Donald
031     * @author Peter De Bruycker
032     */
033    public class ListListModelTests extends TestCase {
034    
035            private ListDataListener mockListener;
036    
037            protected void setUp() throws Exception {
038                    mockListener = (ListDataListener) EasyMock.createMock(ListDataListener.class);
039            }
040    
041            public void testAddAllCollection() {
042                    ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
043                    model.addListDataListener(mockListener);
044    
045                    mockListener.intervalAdded(eq(new ListDataEvent(model, ListDataEvent.INTERVAL_ADDED, 3, 5)));
046                    EasyMock.replay(mockListener);
047    
048                    model.addAll(Arrays.asList(new Object[] { "4", "5", "6" }));
049    
050                    assertEquals(Arrays.asList(new Object[] { "1", "2", "3", "4", "5", "6" }), model);
051    
052                    EasyMock.verify(mockListener);
053            }
054    
055            public void testRetainAll() {
056                    ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3", "4", "5" }));
057                    model.addListDataListener(mockListener);
058    
059                    mockListener.contentsChanged(eq(new ListDataEvent(model, ListDataEvent.CONTENTS_CHANGED, -1, -1)));
060                    EasyMock.replay(mockListener);
061    
062                    model.retainAll(Arrays.asList(new Object[] { "2", "5" }));
063    
064                    assertEquals(Arrays.asList(new Object[] { "2", "5" }), model);
065    
066                    EasyMock.verify(mockListener);
067            }
068    
069            public void testRemoveAll() {
070                    ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3", "4", "5" }));
071                    model.addListDataListener(mockListener);
072    
073                    mockListener.contentsChanged(eq(new ListDataEvent(model, ListDataEvent.CONTENTS_CHANGED, -1, -1)));
074                    EasyMock.replay(mockListener);
075    
076                    model.removeAll(Arrays.asList(new Object[] { "2", "5" }));
077    
078                    assertEquals(Arrays.asList(new Object[] { "1", "3", "4" }), model);
079    
080                    EasyMock.verify(mockListener);
081            }
082    
083            public void testRemove() {
084                    ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
085                    model.addListDataListener(mockListener);
086    
087                    mockListener.intervalRemoved(eq(new ListDataEvent(model, ListDataEvent.INTERVAL_REMOVED, 1, 1)));
088                    EasyMock.replay(mockListener);
089    
090                    model.remove(1);
091    
092                    assertEquals(Arrays.asList(new Object[] { "1", "3" }), model);
093    
094                    EasyMock.verify(mockListener);
095            }
096    
097            public void testRemoveObject() {
098                    ListListModel model = new ListListModel(Arrays.asList(new Object[] { "1", "2", "3" }));
099                    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    }