Path: blob/master/test/jdk/javax/swing/JList/GetSelectedValueTest.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*/22/*23* @test24* @key headful25* @bug 710828026* @summary Verifies that getSelectedValue works fine without crash when27* the setSelectionInterval was called with indices outside the28* range of data present in DataModel29* @run main GetSelectedValueTest30*/3132import javax.swing.SwingUtilities;33import javax.swing.DefaultListModel;34import javax.swing.JList;35import javax.swing.ListSelectionModel;36import java.util.Objects;3738public class GetSelectedValueTest {39public static void main(String[] args) throws Exception {4041SwingUtilities.invokeAndWait(new Runnable() {42public void run() {43// Create a JList with 2 elements44DefaultListModel dlm = new DefaultListModel();45JList list = new JList<String>(dlm);46list.setSelectionMode(47ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);48dlm.addElement("1");49dlm.addElement("2");5051// Set the selection interval from 0-2 (3 elements instead52// of 2). The getSelectedValue should return the first53// selected element54list.setSelectionInterval(0, 2);55checkSelectedIndex(list, "1");5657//here the smallest selection index is bigger than number of58// elements in list. This should return null.59list.setSelectionInterval(4, 5);60checkSelectedIndex(list,null);61}62});63}6465static void checkSelectedIndex(JList list, Object value)66throws RuntimeException {67Object selectedObject = list.getSelectedValue();68if (!Objects.equals(value, selectedObject)) {69System.out.println("Expected: " + value);70System.out.println("Actual: " + selectedObject);71throw new RuntimeException("Wrong selection");72}73}74}757677