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