Path: blob/master/test/jdk/javax/swing/JFileChooser/4966171/bug4966171.java
41153 views
/*1* Copyright (c) 2011, 2020, 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*/2223import java.awt.EventQueue;24import java.io.ByteArrayInputStream;25import java.io.ByteArrayOutputStream;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.util.concurrent.TimeUnit;2930import javax.swing.JFileChooser;31import javax.swing.UIManager;32import javax.swing.UnsupportedLookAndFeelException;3334import static javax.swing.UIManager.getInstalledLookAndFeels;3536/**37* @test38* @bug 4966171 824069039* @key headful40* @summary Tests that JFileChooser can be serialized41*/42public final class bug4966171 {4344public static void main(String[] args) throws Exception {45for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {46EventQueue.invokeAndWait(() -> setLookAndFeel(laf));47EventQueue.invokeAndWait(bug4966171::test);48}49}5051private static void test() {52// Will run the test no more than 10 seconds per L&F53long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);54while (System.nanoTime() < endtime) {55try {56var byteOut = new ByteArrayOutputStream();57try (var out = new ObjectOutputStream(byteOut)) {58out.writeObject(new JFileChooser());59}60var byteIn = new ByteArrayInputStream(byteOut.toByteArray());61try (var in = new ObjectInputStream(byteIn)) {62JFileChooser readFc = (JFileChooser) in.readObject();63}64} catch (Throwable e) {65throw new RuntimeException(e);66}67}68}6970private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {71try {72UIManager.setLookAndFeel(laf.getClassName());73} catch (UnsupportedLookAndFeelException ignored) {74System.out.println("Unsupported L&F: " + laf.getClassName());75} catch (ClassNotFoundException | InstantiationException76| IllegalAccessException e) {77throw new RuntimeException(e);78}79}80}818283