1 /*
   2  * Copyright (c) 2008, 2009, 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 6448405 6519513 6745225
  27  * @summary static HashMap cache in LineBreakMeasurer can grow wihout bounds
  28  * @run main/othervm/timeout=600 -client -Xms16m -Xmx16m FRCTest
  29  * @key randomness
  30  */
  31 import java.awt.*;
  32 import java.awt.image.*;
  33 import java.awt.font.*;
  34 import java.awt.geom.*;
  35 import java.text.*;
  36 import java.util.Hashtable;
  37 
  38 public class FRCTest {
  39 
  40     static AttributedString vanGogh = new AttributedString(
  41         "Many people believe that Vincent van Gogh painted his best works " +
  42         "during the two-year period he spent in Provence. Here is where he " +
  43         "painted The Starry Night--which some consider to be his greatest " +
  44         "work of all. However, as his artistic brilliance reached new " +
  45         "heights in Provence, his physical and mental health plummeted. ",
  46         new Hashtable());
  47 
  48     public static void main(String[] args) {
  49 
  50         // First test the behaviour of Graphics2D.getFontRenderContext();
  51         BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  52         Graphics2D g2d = bi.createGraphics();
  53         AffineTransform g2dTx = new AffineTransform(2,0,2,0,1,1);
  54         g2d.setTransform(g2dTx);
  55         AffineTransform frcTx = g2d.getFontRenderContext().getTransform();
  56         AffineTransform frcExpected = new AffineTransform(2,0,2,0,0,0);
  57         if (!frcTx.equals(frcExpected)) {
  58             throw new RuntimeException("FRC Tx may have translate?");
  59         }
  60 
  61         // Now test that using different translates with LBM is OK
  62         // This test doesn't prove a lot since showing a leak really
  63         // requires a basher test that can run for a long time.
  64         for (int x=0;x<100;x++) {
  65             for (int y=0;y<100;y++) {
  66                 AttributedCharacterIterator aci = vanGogh.getIterator();
  67                 AffineTransform tx = AffineTransform.getTranslateInstance(x, y);
  68                 FontRenderContext frc = new FontRenderContext(tx, false, false);
  69                 LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
  70                 lbm.setPosition(aci.getBeginIndex());
  71                 while (lbm.getPosition() < aci.getEndIndex()) {
  72                     lbm.nextLayout(100f);
  73                 }
  74             }
  75         }
  76 
  77         for (int x=0;x<25;x++) {
  78             for (int y=0;y<25;y++) {
  79                 AttributedCharacterIterator aci = vanGogh.getIterator();
  80                 double rot = Math.random()*.4*Math.PI - .2*Math.PI;
  81                 AffineTransform tx = AffineTransform.getRotateInstance(rot);
  82                 FontRenderContext frc = new FontRenderContext(tx, false, false);
  83                 LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
  84                 lbm.setPosition(aci.getBeginIndex());
  85                 while (lbm.getPosition() < aci.getEndIndex()) {
  86                     lbm.nextLayout(100f);
  87                 }
  88             }
  89         }
  90     }
  91 }