jdk/test/java/awt/FontClass/CreateFont/fileaccess/FontFile.java
author ohair
Thu, 17 Sep 2009 13:17:57 -0700
changeset 3790 ab4f91337479
parent 2606 09ad5edb5330
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     1
/*
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     4
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     8
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    13
 * accompanied this code).
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    14
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    18
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    21
 * have any questions.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    22
 */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    23
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    24
/*
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    25
 * @test
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    26
 * @bug 6652929
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    27
 * @summary verify handling of File.getPath()
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    28
 */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    29
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    30
import java.awt.*;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    31
import java.io.*;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    32
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    33
public class FontFile {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    34
    public static void main(String[] args) throws Exception {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    35
        String sep = System.getProperty("file.separator");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    36
        String fname = ".." + sep + "A.ttf";
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    37
        String dir = System.getProperty("test.src");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    38
        if (dir != null) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    39
            fname = dir + sep + fname;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    40
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    41
        final String name = fname;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    42
        System.out.println("Will try to access " + name);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    43
        if (!(new File(name)).canRead()) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    44
           System.out.println("File not available : can't run test");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    45
           return;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    46
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    47
        System.out.println("File is available. Verify no access under SM");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    48
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    49
        System.setSecurityManager(new SecurityManager());
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    50
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    51
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    52
        // Check cannot read file.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    53
        try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    54
            new FileInputStream(name);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    55
            throw new Error("Something wrong with test environment");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    56
        } catch (SecurityException exc) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    57
            // Good.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    58
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    59
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    60
        try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    61
            Font font = Font.createFont(Font.TRUETYPE_FONT,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    62
            new File("nosuchfile") {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    63
                    private boolean read;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    64
                    @Override public String getPath() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    65
                        if (read) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    66
                            return name;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    67
                        } else {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    68
                            read = true;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    69
                            return "somefile";
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    70
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    71
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    72
                    @Override public boolean canRead() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    73
                        return true;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    74
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    75
               }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    76
            );
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    77
          System.err.println(font.getFontName());
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    78
          throw new RuntimeException("No expected exception");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    79
        }  catch (IOException e) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    80
          System.err.println("Test passed.");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    81
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    82
    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    83
}