Path: blob/master/test/jdk/sun/java2d/ClassCastExceptionForInvalidSurface.java
41145 views
/*1* Copyright (c) 2016, 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.Font;24import java.awt.Graphics2D;25import java.awt.GraphicsConfiguration;26import java.awt.GraphicsEnvironment;27import java.awt.Image;28import java.awt.Rectangle;29import java.awt.RenderingHints;30import java.awt.font.FontRenderContext;31import java.awt.font.GlyphVector;32import java.awt.image.BufferedImage;33import java.awt.image.VolatileImage;34import java.util.concurrent.ArrayBlockingQueue;35import java.util.concurrent.BlockingQueue;36import java.util.concurrent.TimeUnit;3738import static java.awt.image.BufferedImage.TYPE_INT_ARGB;3940/**41* @test42* @key headful43* @bug 8158072 717274944*/45public final class ClassCastExceptionForInvalidSurface {4647static GraphicsEnvironment ge48= GraphicsEnvironment.getLocalGraphicsEnvironment();4950static GraphicsConfiguration gc51= ge.getDefaultScreenDevice().getDefaultConfiguration();5253static volatile VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);5455static volatile Throwable failed;5657static BlockingQueue<VolatileImage> list = new ArrayBlockingQueue<>(50);5859// Will run the test no more than 15 seconds60static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);6162public static void main(final String[] args) throws InterruptedException {6364// Catch all uncaught exceptions and treat them as test failure65Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);6667// Data for rendering68BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB);69FontRenderContext frc = new FontRenderContext(null, false, false);70Font font = new Font("Serif", Font.PLAIN, 12);71GlyphVector gv = font.createGlyphVector(frc, new char[]{'a', '1'});7273Thread t1 = new Thread(() -> {74while (!isComplete()) {75vi = gc.createCompatibleVolatileImage(10, 10);76if (!list.offer(vi)) {77vi.flush();78}79}80list.forEach(Image::flush);81});82Thread t2 = new Thread(() -> {83while (!isComplete()) {84VolatileImage vi = list.poll();85if (vi != null) {86vi.flush();87}88}89});9091Thread t3 = new Thread(() -> {92while (!isComplete()) {93vi.createGraphics().drawImage(bi, 1, 1, null);94}95});96Thread t4 = new Thread(() -> {97while (!isComplete()) {98vi.createGraphics().drawGlyphVector(gv, 0, 0);99vi.createGraphics().drawOval(0, 0, 10, 10);100vi.createGraphics().drawLine(0, 0, 10, 10);101vi.createGraphics().drawString("123", 1, 1);102vi.createGraphics().draw(new Rectangle(0, 0, 10, 10));103vi.createGraphics().fillOval(0, 0, 10, 10);104final Graphics2D graphics = vi.createGraphics();105graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,106RenderingHints.VALUE_ANTIALIAS_ON);107graphics.fillPolygon(new int[] {0, 10, 10, 0},108new int [] {0, 0, 10, 10}, 4);109}110});111t1.start();112t2.start();113t3.start();114t4.start();115t1.join();116t2.join();117t3.join();118t4.join();119120if (failed != null) {121System.err.println("Test failed");122failed.printStackTrace();123}124}125126private static boolean isComplete() {127return endtime - System.nanoTime() < 0 || failed != null;128}129}130131132