Path: blob/master/test/jdk/javax/swing/JComboBox/DefaultComboBoxModelAddAllElementsTest.java
41152 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 484265826* @summary Tests the addAllElements, addAllElementsAt methods of DefaultComboBoxModel.27* @run main DefaultComboBoxModelAddAllElementsTest28*/29import javax.swing.DefaultComboBoxModel;30import javax.swing.event.ListDataEvent;31import javax.swing.event.ListDataListener;32import java.util.ArrayList;33import java.util.TreeSet;34import java.util.Vector;35import java.util.stream.IntStream;363738public class DefaultComboBoxModelAddAllElementsTest {39private static final int START = 0;40private static final int END = 50;41private static final Vector<Integer> vector =42IntStream.range(START, END).collect(Vector::new,43Vector::add,44Vector::addAll);4546private static final TreeSet<Integer> set =47IntStream.range(START, END).collect(TreeSet::new,48TreeSet::add,49TreeSet::addAll);5051private static final ArrayList<Integer> arrayList =52IntStream.range(START, END).collect(ArrayList::new,53ArrayList::add,54ArrayList::addAll);5556public static void main(String[] args) {57checkAddAll();58checkAddAllWithIndex();59System.out.println("Test case passed.");60}6162private static class MyListDataListener implements ListDataListener {63@Override public void intervalAdded(ListDataEvent e) {64if (e.getIndex1() - e.getIndex0() != END - START - 1) {65throw new RuntimeException("Test case failed. Expected " + (END - START) +66" elements to be added, but only got " + (e.getIndex1() - e.getIndex0()));67}68}6970@Override public void intervalRemoved(ListDataEvent e) {}71@Override public void contentsChanged(ListDataEvent e) {}72}7374private static void checkAddAll() {75DefaultComboBoxModel<Integer> cm = new DefaultComboBoxModel<>();76cm.addListDataListener(new MyListDataListener());7778try {79cm.addAll(arrayList);80System.out.println("Successfully added " + (END - START) + "elements.");81} catch (Exception e) {82throw new RuntimeException("Test case failed. " + e.getMessage());83}84}8586private static void checkAddAllWithIndex() {87DefaultComboBoxModel<Integer> cm = new DefaultComboBoxModel<>();8889cm.addListDataListener(new MyListDataListener());90cm.addAll(set);9192try {93cm.addAll(START - 1, vector);94throw new RuntimeException("Test case failed. Expected failure not reported.");95} catch (ArrayIndexOutOfBoundsException e){96System.out.println("Encountered exception as expected, when trying to add elements" +97"before the start of the list.");98}99100try {101cm.addAll(15, vector);102System.out.println("Successfully added elements at a particular index");103} catch (Exception e) {104throw new RuntimeException("Unexpected failure: " + e.getMessage());105}106107try {108cm.addAll(cm.getSize() + 1, vector);109throw new RuntimeException("Test case failed. Expected failure not reported.");110} catch (ArrayIndexOutOfBoundsException e){111System.out.println("Encountered exception as expected, when trying to add elements" +112"after the end of the list.");113}114}115}116117118