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