Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/ClassCastExceptionForInvalidSurface.java
38833 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* @bug 8158072 717274943*/44public final class ClassCastExceptionForInvalidSurface {4546static GraphicsEnvironment ge47= GraphicsEnvironment.getLocalGraphicsEnvironment();4849static GraphicsConfiguration gc50= ge.getDefaultScreenDevice().getDefaultConfiguration();5152static volatile VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);5354static volatile Throwable failed;5556static BlockingQueue<VolatileImage> list = new ArrayBlockingQueue<>(50);5758// Will run the test no more than 15 seconds59static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);6061public static void main(final String[] args) throws InterruptedException {6263// Catch all uncaught exceptions and treat them as test failure64Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);6566// Data for rendering67BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB);68FontRenderContext frc = new FontRenderContext(null, false, false);69Font font = new Font("Serif", Font.PLAIN, 12);70GlyphVector gv = font.createGlyphVector(frc, new char[]{'a', '1'});7172Thread t1 = new Thread(() -> {73while (!isComplete()) {74vi = gc.createCompatibleVolatileImage(10, 10);75if (!list.offer(vi)) {76vi.flush();77}78}79list.forEach(Image::flush);80});81Thread t2 = new Thread(() -> {82while (!isComplete()) {83VolatileImage vi = list.poll();84if (vi != null) {85vi.flush();86}87}88});8990Thread t3 = new Thread(() -> {91while (!isComplete()) {92vi.createGraphics().drawImage(bi, 1, 1, null);93}94});95Thread t4 = new Thread(() -> {96while (!isComplete()) {97vi.createGraphics().drawGlyphVector(gv, 0, 0);98vi.createGraphics().drawOval(0, 0, 10, 10);99vi.createGraphics().drawLine(0, 0, 10, 10);100vi.createGraphics().drawString("123", 1, 1);101vi.createGraphics().draw(new Rectangle(0, 0, 10, 10));102vi.createGraphics().fillOval(0, 0, 10, 10);103final Graphics2D graphics = vi.createGraphics();104graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,105RenderingHints.VALUE_ANTIALIAS_ON);106graphics.fillPolygon(new int[] {0, 10, 10, 0},107new int [] {0, 0, 10, 10}, 4);108}109});110t1.start();111t2.start();112t3.start();113t4.start();114t1.join();115t2.join();116t3.join();117t4.join();118119if (failed != null) {120System.err.println("Test failed");121failed.printStackTrace();122}123}124125private static boolean isComplete() {126return endtime - System.nanoTime() < 0 || failed != null;127}128}129130131