test/jdk/javax/swing/JList/DefaultListModelAddAllTest.java
author coleenp
Mon, 30 Sep 2019 13:10:11 -0400
changeset 58409 a595e67d6683
parent 50008 c87a5690e394
permissions -rw-r--r--
8184732: Deadlock detection improvements for 'special' locks Summary: Assert that special ranked locks cannot safepoint and allow_vm_block and remove locks from the exceptional lock list in no_safepoint_verifier. Reviewed-by: dholmes, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50008
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     1
/*
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     4
 *
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     8
 *
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    13
 * accompanied this code).
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    14
 *
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    18
 *
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    21
 * questions.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    22
 */
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    23
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    24
/*
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    25
 * @test
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    26
 * @bug 4842658
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    27
 * @summary Tests the addAll methods of DefaultListModel.
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    28
 * @run main DefaultListModelAddAllTest
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    29
 */
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    30
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    31
import javax.swing.DefaultListModel;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    32
import javax.swing.event.ListDataEvent;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    33
import javax.swing.event.ListDataListener;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    34
import java.util.ArrayList;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    35
import java.util.TreeSet;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    36
import java.util.Vector;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    37
import java.util.stream.IntStream;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    38
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    39
public class DefaultListModelAddAllTest {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    40
    private static final int START = 0;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    41
    private static final int END = 50;
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    42
    private static final Vector<Integer> vector =
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    43
            IntStream.range(START, END).collect(Vector::new,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    44
                                                Vector::add,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    45
                                                Vector::addAll);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    46
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    47
    private static final TreeSet<Integer> set =
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    48
            IntStream.range(START, END).collect(TreeSet::new,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    49
                                                TreeSet::add,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    50
                                                TreeSet::addAll);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    51
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    52
    private static final ArrayList<Integer> arrayList =
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    53
            IntStream.range(START, END).collect(ArrayList::new,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    54
                                                ArrayList::add,
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    55
                                                ArrayList::addAll);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    56
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    57
    public static void main(String[] args) {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    58
        checkAddAll();
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    59
        checkAddAllWithIndex();
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    60
        System.out.println("Test case passed.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    61
    }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    62
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    63
    private static class MyListDataListener implements ListDataListener {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    64
        @Override public void intervalAdded(ListDataEvent e) {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    65
            if (e.getIndex1() - e.getIndex0() != END - START - 1) {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    66
                throw new RuntimeException("Test case failed. Expected " + (END - START) +
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    67
                        " elements to be added, but only got " + (e.getIndex1() - e.getIndex0()));
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    68
            }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    69
        }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    70
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    71
        @Override public void intervalRemoved(ListDataEvent e) {}
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    72
        @Override public void contentsChanged(ListDataEvent e) {}
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    73
    }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    74
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    75
    private static void checkAddAll() {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    76
        DefaultListModel<Integer> lm = new DefaultListModel<>();
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    77
        lm.addListDataListener(new MyListDataListener());
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    78
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    79
        try {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    80
            lm.addAll(arrayList);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    81
            System.out.println("Successfully added " + (END - START) + "elements.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    82
        } catch (Exception e) {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    83
            throw new RuntimeException("Test case failed. " + e.getMessage());
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    84
        }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    85
    }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    86
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    87
    private static void checkAddAllWithIndex() {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    88
        DefaultListModel<Integer> lm = new DefaultListModel<>();
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    89
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    90
        lm.addListDataListener(new MyListDataListener());
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    91
        lm.addAll(set);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    92
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    93
        try {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    94
            lm.addAll(START - 1, vector);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    95
            throw new RuntimeException("Test case failed. Expected failure not reported.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    96
        } catch (ArrayIndexOutOfBoundsException e){
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    97
            System.out.println("Encountered exception as expected, when trying to add elements" +
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    98
                    "before the start of the list.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
    99
        }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   100
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   101
        try {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   102
            lm.addAll(15, vector);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   103
            System.out.println("Successfully added elements at a particular index");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   104
        } catch (Exception e) {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   105
            throw new RuntimeException("Unexpected failure: " + e.getMessage());
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   106
        }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   107
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   108
        try {
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   109
            lm.addAll(lm.getSize() + 1, vector);
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   110
            throw new RuntimeException("Test case failed. Expected failure not reported.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   111
        } catch (ArrayIndexOutOfBoundsException e){
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   112
            System.out.println("Encountered exception as expected, when trying to add elements" +
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   113
                "after the end of the list.");
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   114
        }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   115
    }
c87a5690e394 4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
kaddepalli
parents:
diff changeset
   116
}