test/jdk/java/awt/geom/GeneralPath/EmptyRectHitTest.java
changeset 50344 8039dc75f125
equal deleted inserted replaced
50343:80a5ff734fcd 50344:8039dc75f125
       
     1 /*
       
     2  * Copyright (c) 2006, 2018, 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 /*
       
    25  * @test
       
    26  * @bug 6284484
       
    27  * @summary Verifies that GeneralPath objects do not "contain" or "intersect"
       
    28  *          empty rectangles
       
    29  */
       
    30 
       
    31 import java.awt.Rectangle;
       
    32 import java.awt.Shape;
       
    33 import java.awt.geom.AffineTransform;
       
    34 import java.awt.geom.GeneralPath;
       
    35 
       
    36 public class EmptyRectHitTest {
       
    37     public static int numerrors;
       
    38 
       
    39     public static void main(String argv[]) {
       
    40         Rectangle select1 = new Rectangle(1, 1, 1, 1);
       
    41         Rectangle select2 = new Rectangle(1, 1, 0, 0);
       
    42         Rectangle select3 = new Rectangle(100, 100, 1, 1);
       
    43         Rectangle select4 = new Rectangle(100, 100, 0, 0);
       
    44 
       
    45         Rectangle rect = new Rectangle(-5, -5, 10, 10);
       
    46         test(rect, select1, true,  true);
       
    47         test(rect, select2, false, false);
       
    48         test(rect, select3, false, false);
       
    49         test(rect, select4, false, false);
       
    50 
       
    51         GeneralPath gp = new GeneralPath(rect);
       
    52         test(gp, select1, true,  true);
       
    53         test(gp, select2, false, false);
       
    54         test(gp, select3, false, false);
       
    55         test(gp, select4, false, false);
       
    56 
       
    57         AffineTransform xform = new AffineTransform();
       
    58         xform.setToRotation(Math.PI / 4);
       
    59         Shape shape = xform.createTransformedShape(rect);
       
    60         test(shape, select1, true,  true);
       
    61         test(shape, select2, false, false);
       
    62         test(shape, select3, false, false);
       
    63         test(shape, select4, false, false);
       
    64 
       
    65         if (numerrors > 0) {
       
    66             throw new RuntimeException(numerrors+" errors in tests");
       
    67         }
       
    68     }
       
    69 
       
    70     public static void test(Shape testshape, Rectangle rect,
       
    71                             boolean shouldcontain, boolean shouldintersect)
       
    72     {
       
    73         if (testshape.contains(rect) != shouldcontain) {
       
    74             error(testshape, rect, "contains", !shouldcontain);
       
    75         }
       
    76         if (testshape.intersects(rect) != shouldintersect) {
       
    77             error(testshape, rect, "intersects", !shouldintersect);
       
    78         }
       
    79     }
       
    80 
       
    81     public static void error(Shape t, Rectangle r, String type, boolean res) {
       
    82         numerrors++;
       
    83         System.err.println(t+type+"("+r+") == "+res);
       
    84     }
       
    85 }