Path: blob/master/test/jdk/java/awt/FontClass/CreateFont/fileaccess/FontFile.java
41159 views
/*1* Copyright (c) 2008, 2015, 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*/2223/*24* @test25* @bug 665292926* @summary verify handling of File.getPath()27* @compile FontFile.java28* @run shell TestFontFile.sh29*/3031/*32* When using jtreg this test needs to be run by shell script,33* since otherwise jtreg reflectively invokes the main method34* and the codebase for the purposes of the security manager35* is that of the jtreg harness, not the codebase (class file location)36* of this program, thus access to read to that location is not available.37*/3839import java.awt.*;40import java.io.*;4142public class FontFile {43public static void main(String[] args) throws Exception {44String sep = System.getProperty("file.separator");45String fname = ".." + sep + "A.ttf";46//String dir = System.getProperty("test.src");47String dir = System.getenv("TESTSRC");48if (dir != null) {49fname = dir + sep + fname;50}51//String classesDir = System.getProperty("test.classes");52String classesDir = System.getenv("TESTCLASSES");53System.out.println("classesDir="+classesDir);54String testfile = "somefile";55if (classesDir != null) {56testfile = classesDir + sep + testfile;57}58final String somefile = testfile;59System.out.println("somefile="+somefile);60System.out.println("userdir="+System.getProperty("user.dir"));61final String name = fname;62System.out.println("Will try to access " + name);63if (!(new File(name)).canRead()) {64System.out.println("File not available : can't run test");65return;66}67System.out.println("File is available. Verify no access under SM");6869System.setSecurityManager(new SecurityManager());707172// Check cannot read file.73try {74new FileInputStream(name);75throw new Error("Something wrong with test environment");76} catch (SecurityException exc) {77// Good.78}7980try {81Font font = Font.createFont(Font.TRUETYPE_FONT,82new File("nosuchfile") {83private boolean read;84@Override public String getPath() {85if (read) {86return name;87} else {88read = true;89return somefile;90}91}92@Override public boolean canRead() {93return true;94}95}96);97System.err.println(font.getFontName());98throw new RuntimeException("No expected exception");99} catch (IOException e) {100System.err.println("Test passed.");101}102}103}104105106