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 addAll methods of DefaultListModel.
  28  * @run main DefaultListModelAddAllTest
  29  */
  30 
  31 import javax.swing.DefaultListModel;
  32 import javax.swing.event.ListDataEvent;
  33 import javax.swing.event.ListDataListener;
  34 import java.util.ArrayList;
  35 import java.util.TreeSet;
  36 import java.util.Vector;
  37 import java.util.stream.IntStream;
  38 
  39 public class DefaultListModelAddAllTest {
  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         DefaultListModel<Integer> lm = new DefaultListModel<>();
  77         lm.addListDataListener(new MyListDataListener());
  78 
  79         try {
  80             lm.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         DefaultListModel<Integer> lm = new DefaultListModel<>();
  89 
  90         lm.addListDataListener(new MyListDataListener());
  91         lm.addAll(set);
  92 
  93         try {
  94             lm.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             lm.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             lm.addAll(lm.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 }