aboutsummaryrefslogtreecommitdiffstats
blob: 0e8b5f6d5b557ef10e3ef3d6de452a71c860edf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.tests.viewers;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.jface.viewers.StructuredSelection;

public class StructuredSelectionTest extends TestCase {

    public StructuredSelectionTest(String name) {
        super(name);
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(StructuredSelectionTest.class);
    }

    public void testEquals() {
        String element = "A selection";
        StructuredSelection sel1 = new StructuredSelection(element);
        StructuredSelection sel2 = new StructuredSelection(element);
        assertEquals(sel1, sel2);
    }

    public void testEquals2() {
        String element1 = "A selection";
        String element2 = "A selection";
        String element3 = "Other";
        StructuredSelection sel1 = new StructuredSelection(element1);
        StructuredSelection sel2 = new StructuredSelection(element2);
        StructuredSelection sel3 = new StructuredSelection(element3);
        assertEquals(sel1, sel2);
        assertTrue(!sel1.equals(sel3));
    }

    public void testEquals3() { // two empty selections
        StructuredSelection empty1 = new StructuredSelection();
        StructuredSelection empty2 = new StructuredSelection();
        assertTrue(empty1.equals(empty2));
        assertTrue(empty2.equals(empty1));
    }

    public void testEquals4() { // empty selection with non-empty selection
        StructuredSelection sel = new StructuredSelection("A selection");
        StructuredSelection empty = new StructuredSelection();
        assertTrue(!sel.equals(empty));
        assertTrue(!empty.equals(sel));
    }

    public void testEquals5() { // equality is order-dependent
        List<String> l1 = new ArrayList<String>();
        l1.add("element 1");
        l1.add("element 2");

        List<String> l2 = new ArrayList<String>();
        l2.add("element 2");
        l2.add("element 1");

        StructuredSelection sel1 = new StructuredSelection(l1);
        StructuredSelection sel2 = new StructuredSelection(l2);
        assertTrue(!sel1.equals(sel2));
        assertTrue(!sel2.equals(sel1));
    }

    public void testEquals6() { // different selections
        List<String> l1 = new ArrayList<String>();
        l1.add("element 1");
        l1.add("element 2");

        List<String> l2 = new ArrayList<String>();
        l2.add("element 2");
        l2.add("element 3");
        l2.add("element 1");

        StructuredSelection sel1 = new StructuredSelection(l1);
        StructuredSelection sel2 = new StructuredSelection(l2);
        assertTrue(!sel1.equals(sel2));
        assertTrue(!sel2.equals(sel1));
    }

    /**
     * Empty selections via different constructors.
     * Regression test for bug 40245.
     */
    public void testEquals7() {
        StructuredSelection empty1 = new StructuredSelection();
        StructuredSelection empty2 = new StructuredSelection(new Object[0]);
        assertTrue(empty1.equals(empty2));
        assertTrue(empty2.equals(empty1));
    }
}