1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.Font; 25 import java.awt.GraphicsConfiguration; 26 import java.awt.GraphicsEnvironment; 27 import java.awt.Image; 28 import java.awt.Rectangle; 29 import java.awt.font.FontRenderContext; 30 import java.awt.font.GlyphVector; 31 import java.awt.image.BufferedImage; 32 import java.awt.image.VolatileImage; 33 import java.util.concurrent.ArrayBlockingQueue; 34 import java.util.concurrent.BlockingQueue; 35 import java.util.concurrent.TimeUnit; 36 37 import static java.awt.image.BufferedImage.TYPE_INT_ARGB; 38 39 /** 40 * @test 41 * @bug 8158072 7172749 42 */ 43 public final class ClassCastExceptionForInvalidSurface { 44 45 static GraphicsEnvironment ge 46 = GraphicsEnvironment.getLocalGraphicsEnvironment(); 47 48 static GraphicsConfiguration gc 49 = ge.getDefaultScreenDevice().getDefaultConfiguration(); 50 51 static volatile VolatileImage vi = gc.createCompatibleVolatileImage(10, 10); 52 53 static volatile Throwable failed; 54 55 static BlockingQueue<VolatileImage> list = new ArrayBlockingQueue<>(50); 56 57 // Will run the test no more than 15 seconds 58 static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15); 59 60 public static void main(final String[] args) throws InterruptedException { 61 62 // Catch all uncaught exceptions and treat them as test failure 63 Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e); 64 65 // Data for rendering 66 BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB); 67 FontRenderContext frc = new FontRenderContext(null, false, false); 68 Font font = new Font("Serif", Font.PLAIN, 12); 69 GlyphVector gv = font.createGlyphVector(frc, new char[]{'a', '1'}); 70 71 Thread t1 = new Thread(() -> { 72 while (!isComplete()) { 73 vi = gc.createCompatibleVolatileImage(10, 10); 74 if (!list.offer(vi)) { 75 vi.flush(); 76 } 77 } 78 list.forEach(Image::flush); 79 }); 80 Thread t2 = new Thread(() -> { 81 while (!isComplete()) { 82 VolatileImage vi = list.poll(); 83 if (vi != null) { 84 vi.flush(); 85 } 86 } 87 }); 88 89 Thread t3 = new Thread(() -> { 90 while (!isComplete()) { 91 vi.createGraphics().drawImage(bi, 1, 1, null); 92 } 93 }); 94 Thread t4 = new Thread(() -> { 95 while (!isComplete()) { 96 vi.createGraphics().drawGlyphVector(gv, 0, 0); 97 vi.createGraphics().drawOval(0, 0, 10, 10); 98 vi.createGraphics().drawLine(0, 0, 10, 10); 99 vi.createGraphics().drawString("123", 1, 1); 100 vi.createGraphics().draw(new Rectangle(0, 0, 10, 10)); 101 vi.createGraphics().fillOval(0, 0, 10, 10); 102 } 103 }); 104 t1.start(); 105 t2.start(); 106 t3.start(); 107 t4.start(); 108 t1.join(); 109 t2.join(); 110 t3.join(); 111 t4.join(); 112 113 if (failed != null) { 114 System.err.println("Test failed"); 115 failed.printStackTrace(); 116 } 117 } 118 119 private static boolean isComplete() { 120 return endtime - System.nanoTime() < 0 || failed != null; 121 } 122 }