Path: blob/master/test/jdk/javax/swing/JList/GetSelectedValuesListTest.java
41149 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*/22/*23* @test24* @key headful25* @bug 710828026* @summary Verifies that getSelectedValuesList works fine without crash when27* the setSelectionInterval was called with indices outside the28* range of data present in DataModel29* @run main GetSelectedValuesListTest30*/3132import javax.swing.SwingUtilities;33import javax.swing.DefaultListModel;34import javax.swing.JList;35import javax.swing.ListSelectionModel;36import java.util.Arrays;37import java.util.Collections;38import java.util.List;394041public class GetSelectedValuesListTest {42public static void main(String[] args) throws Exception {4344SwingUtilities.invokeAndWait(new Runnable() {45public void run() {46// Create a JList with 2 elements47DefaultListModel dlm = new DefaultListModel();48JList list = new JList<String>(dlm);49list.setSelectionMode(50ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);51dlm.addElement("1");52dlm.addElement("2");5354// Set the selection interval from 0-2 (3 elements instead55// of 2). The getSelectedValuesList should return the list of56// objects present in the list in the given interval57list.setSelectionInterval(0, 2);58checkSelection(list, List.of("1", "2"));5960//here the selection interval is set greater than number of61// elements in list. This should return empty list62list.setSelectionInterval(4, 10);63checkSelection(list, Collections.emptyList());6465// This will set the selection interval from 0 to 2 index.66// The getSelectedValuesList should return the list of67// objects present in the list in the given interval68list.getSelectionModel().setSelectionInterval(0, 2);69checkSelection(list, List.of("1", "2"));70}71});72}7374static void checkSelection(JList list, List<String> selectionList)75throws RuntimeException76{77List<String> listSelection = list.getSelectedValuesList();78if (!listSelection.equals(selectionList)) {79System.out.println("Expected: " + selectionList);80System.out.println("Actual: " + listSelection);81throw new RuntimeException("Wrong selection from " +82"getSelectedValuesList");83}8485Object[] arraySelection = list.getSelectedValues();86if (!Arrays.equals(arraySelection, selectionList.toArray())) {87System.out.println("Expected: " + selectionList);88System.out.println("Actual: " + Arrays.asList(arraySelection));89throw new RuntimeException("Wrong selection from " +90"getSelectedValues");91}92}93}949596