Path: blob/master/test/jdk/javax/swing/JComboBox/7031551/bug7031551.java
41153 views
/*1* Copyright (c) 2011, 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/* @test24* @bug 703155125* @summary Generics: JComboBox26* @author Pavel Porvatov27*/2829import javax.swing.*;30import java.util.Vector;3132public class bug7031551 {3334private static final String TEST_ELEMENT1 = "Test1";35private static final String TEST_ELEMENT2 = "Test2";36private static final String TEST_ELEMENT3 = "Test3";3738/**39* @param args the command line arguments40*/41@SuppressWarnings("unchecked")42public static void main(String[] args) {43testRawSignatures();44testGenericSignatures();45}4647@SuppressWarnings("unchecked")48private static void testRawSignatures() {49// Test JComboBox50ComboBoxModel rawTestModel = new DefaultComboBoxModel();5152JComboBox rawTestComboBox = new JComboBox();53rawTestComboBox = new JComboBox(rawTestModel);54rawTestComboBox = new JComboBox(new Object[]{TEST_ELEMENT1});55rawTestComboBox = new JComboBox(new Vector());5657Object unused1 = rawTestComboBox.getPrototypeDisplayValue();58rawTestComboBox.setPrototypeDisplayValue(TEST_ELEMENT1);5960ListCellRenderer unused2 = rawTestComboBox.getRenderer();61rawTestComboBox.setRenderer(new DefaultListCellRenderer());6263ComboBoxModel unused3 = rawTestComboBox.getModel();64rawTestComboBox.setModel(rawTestModel);6566rawTestComboBox.addItem(TEST_ELEMENT2);67rawTestComboBox.insertItemAt(TEST_ELEMENT3, 1);68rawTestComboBox.removeItem(TEST_ELEMENT2);69assertEquals(rawTestComboBox.getItemAt(0), TEST_ELEMENT3);70rawTestComboBox.removeAllItems();7172// Test DefaultComboBoxModel73DefaultComboBoxModel testModel = new DefaultComboBoxModel();74testModel = new DefaultComboBoxModel(new Vector());75testModel = new DefaultComboBoxModel(new Object[]{TEST_ELEMENT1});7677testModel.addElement(TEST_ELEMENT2);78testModel.insertElementAt(TEST_ELEMENT3, 1);79assertEquals(testModel.getElementAt(2), TEST_ELEMENT2);80}8182private static void testGenericSignatures() {83// Test JComboBox84ComboBoxModel<String> stringTestModel = new DefaultComboBoxModel<String>();8586JComboBox<String> stringTestComboBox = new JComboBox<String>();87stringTestComboBox = new JComboBox<String>(stringTestModel);88stringTestComboBox = new JComboBox<String>(new String[]{TEST_ELEMENT1});89stringTestComboBox = new JComboBox<String>(new Vector<String>());9091String unused1 = stringTestComboBox.getPrototypeDisplayValue();92stringTestComboBox.setPrototypeDisplayValue(TEST_ELEMENT1);9394ListCellRenderer<? super String> unused2 = stringTestComboBox.getRenderer();95stringTestComboBox.setRenderer(new DefaultListCellRenderer());9697ComboBoxModel unused3 = stringTestComboBox.getModel();98stringTestComboBox.setModel(stringTestModel);99100stringTestComboBox.addItem(TEST_ELEMENT2);101stringTestComboBox.insertItemAt(TEST_ELEMENT3, 1);102stringTestComboBox.removeItem(TEST_ELEMENT2);103assertEquals(stringTestComboBox.getItemAt(0), TEST_ELEMENT3);104stringTestComboBox.removeAllItems();105106// Test DefaultComboBoxModel107DefaultComboBoxModel<String> testModel = new DefaultComboBoxModel<String>();108testModel = new DefaultComboBoxModel<String>(new Vector<String>());109testModel = new DefaultComboBoxModel<String>(new String[]{TEST_ELEMENT1});110111testModel.addElement(TEST_ELEMENT2);112testModel.insertElementAt(TEST_ELEMENT3, 1);113assertEquals(testModel.getElementAt(2), TEST_ELEMENT2);114}115116private static void assertEquals(Object expectedObject, Object actualObject) {117if (!expectedObject.equals(actualObject)) {118throw new RuntimeException("Expected: " + expectedObject + " but was: " + actualObject);119}120}121}122123124125