Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/marlin/CrashNaNTest.java
38838 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.Color;24import java.awt.Graphics2D;25import java.awt.RenderingHints;26import java.awt.geom.Path2D;27import java.awt.image.BufferedImage;28import java.io.File;29import java.io.IOException;30import static java.lang.Double.NaN;31import java.util.Locale;32import java.util.logging.Handler;33import java.util.logging.LogRecord;34import java.util.logging.Logger;35import javax.imageio.ImageIO;3637/**38* @test39* @bug 814933840* @summary Verifies that Marlin supports NaN coordinates and no JVM crash happens !41* @run main CrashNaNTest42*/43public class CrashNaNTest {4445static final boolean SAVE_IMAGE = false;4647public static void main(String argv[]) {48Locale.setDefault(Locale.US);4950// initialize j.u.l Looger:51final Logger log = Logger.getLogger("sun.java2d.marlin");52log.addHandler(new Handler() {53@Override54public void publish(LogRecord record) {55Throwable th = record.getThrown();56// detect any Throwable:57if (th != null) {58System.out.println("Test failed:\n" + record.getMessage());59th.printStackTrace(System.out);6061throw new RuntimeException("Test failed: ", th);62}63}6465@Override66public void flush() {67}6869@Override70public void close() throws SecurityException {71}72});7374// enable Marlin logging & internal checks:75System.setProperty("sun.java2d.renderer.log", "true");76System.setProperty("sun.java2d.renderer.useLogger", "true");77System.setProperty("sun.java2d.renderer.doChecks", "true");7879final int width = 400;80final int height = 400;8182final BufferedImage image = new BufferedImage(width, height,83BufferedImage.TYPE_INT_ARGB);8485final Graphics2D g2d = (Graphics2D) image.getGraphics();86try {87g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,88RenderingHints.VALUE_ANTIALIAS_ON);8990g2d.setBackground(Color.WHITE);91g2d.clearRect(0, 0, width, height);9293final Path2D.Double path = new Path2D.Double();94path.moveTo(30, 30);95path.lineTo(100, 100);9697for (int i = 0; i < 20000; i++) {98path.lineTo(110 + 0.01 * i, 110);99path.lineTo(111 + 0.01 * i, 100);100}101102path.lineTo(NaN, 200);103path.lineTo(200, 200);104path.lineTo(200, NaN);105path.lineTo(300, 300);106path.lineTo(NaN, NaN);107path.lineTo(100, 100);108path.closePath();109110final Path2D.Double path2 = new Path2D.Double();111path2.moveTo(0,0);112path2.lineTo(width,height);113path2.lineTo(10, 10);114path2.closePath();115116for (int i = 0; i < 1; i++) {117final long start = System.nanoTime();118g2d.setColor(Color.BLUE);119g2d.fill(path);120121g2d.fill(path2);122123final long time = System.nanoTime() - start;124System.out.println("paint: duration= " + (1e-6 * time) + " ms.");125}126127if (SAVE_IMAGE) {128try {129final File file = new File("CrashNaNTest.png");130System.out.println("Writing file: "131+ file.getAbsolutePath());132ImageIO.write(image, "PNG", file);133} catch (IOException ex) {134System.out.println("Writing file failure:");135ex.printStackTrace();136}137}138} finally {139g2d.dispose();140}141}142}143144145