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