~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to swingx-core/src/test/java/org/jdesktop/swingx/event/WeakEventListenerListTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: WeakEventListenerListTest.java 3473 2009-08-27 13:17:10Z kleopatra $
 
3
 *
 
4
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 *
 
21
 */
 
22
package org.jdesktop.swingx.event;
 
23
 
 
24
import javax.swing.event.ChangeListener;
 
25
 
 
26
import junit.framework.TestCase;
 
27
 
 
28
import org.jdesktop.test.ChangeReport;
 
29
import org.junit.Test;
 
30
import org.junit.runner.RunWith;
 
31
import org.junit.runners.JUnit4;
 
32
 
 
33
 
 
34
/**
 
35
 * Unit tests for SwingX' EventListenerList. Testing the 
 
36
 * mechanics of mutations and returned listener arrays. <p>
 
37
 * 
 
38
 * PENDING: any way to unit test memory leaks?
 
39
 * 
 
40
 * @author Jeanette Winzenburg
 
41
 */
 
42
@RunWith(JUnit4.class)
 
43
public class WeakEventListenerListTest extends TestCase {
 
44
 
 
45
    /**
 
46
     * Test intial getListenerList
 
47
     *
 
48
     */
 
49
    @Test
 
50
    public void testGetListenerListInitial() {
 
51
        WeakEventListenerList list = new WeakEventListenerList();
 
52
        // must return empty array
 
53
        assertNotNull("array must be not null", list.getListenerList());
 
54
        assertEquals("array must be empty", 0, list.getListenerList().length);
 
55
    }
 
56
    
 
57
    /**
 
58
     * test sequence in array.
 
59
     *
 
60
     */
 
61
    @Test
 
62
    public void testGetListenerListAddRemove() {
 
63
        WeakEventListenerList list = new WeakEventListenerList();
 
64
        // add one changeListener
 
65
        ChangeReport changeReport = new ChangeReport();
 
66
        list.add(ChangeListener.class, changeReport);
 
67
        Object[] array = list.getListenerList();
 
68
        assertEquals("array must have 2 elements", 2, array.length);
 
69
        assertSame("listener class must be at 0", ChangeListener.class, array[0]);
 
70
        assertSame("added listener must be at 1", changeReport, array[1]);
 
71
        // add a second
 
72
        ChangeReport otherReport = new ChangeReport();
 
73
        list.add(ChangeListener.class, otherReport);
 
74
        Object[] otherArray = list.getListenerList();
 
75
        assertEquals("array must have 4 elements", 4, otherArray.length);
 
76
        assertSame(ChangeListener.class, otherArray[0]);
 
77
        assertSame(changeReport, otherArray[1]);
 
78
        assertSame(ChangeListener.class, otherArray[2]);
 
79
        assertSame(otherReport, otherArray[3]);
 
80
        list.remove(ChangeListener.class, changeReport);
 
81
        Object[] removedArray = list.getListenerList();
 
82
        assertEquals("array must have 2 elements", 2, removedArray.length);
 
83
        assertSame("listener class must be at 0", ChangeListener.class, removedArray[0]);
 
84
        assertSame("added listener must be at 1", otherReport, removedArray[1]);
 
85
    }
 
86
    
 
87
    @Test
 
88
    public void testGetListenersInitial() {
 
89
        WeakEventListenerList list = new WeakEventListenerList();
 
90
        // must return empty array
 
91
        assertNotNull("array must be not null", list.getListeners(ChangeListener.class));
 
92
        assertEquals("array must be empty", 0, list.getListeners(ChangeListener.class).length);
 
93
        
 
94
    }
 
95
 
 
96
    /**
 
97
     * test sequence in array.
 
98
     *
 
99
     */
 
100
    @Test
 
101
    public void testGetListenersAddRemove() {
 
102
        WeakEventListenerList list = new WeakEventListenerList();
 
103
        // add one changeListener
 
104
        ChangeReport changeReport = new ChangeReport();
 
105
        list.add(ChangeListener.class, changeReport);
 
106
        ChangeListener[] array = list.getListeners(ChangeListener.class);
 
107
        assertEquals("array must have 2 elements", 1, array.length);
 
108
        assertSame("added listener must be at 0", changeReport, array[0]);
 
109
        // add a second
 
110
        ChangeReport otherReport = new ChangeReport();
 
111
        list.add(ChangeListener.class, otherReport);
 
112
        ChangeListener[] otherArray = list.getListeners(ChangeListener.class);
 
113
        assertEquals("array must have 2 elements", 2, otherArray.length);
 
114
        assertSame(changeReport, otherArray[0]);
 
115
        assertSame(otherReport, otherArray[1]);
 
116
        list.remove(ChangeListener.class, changeReport);
 
117
        ChangeListener[] removedArray = list.getListeners(ChangeListener.class);
 
118
        assertEquals("array length", 1, removedArray.length);
 
119
        assertSame("last added listener must be at 0", otherReport, removedArray[0]);
 
120
    }
 
121
 
 
122
}