Path: blob/master/test/jdk/java/awt/FontClass/CreateFont/CreateFontArrayTest.java
41153 views
/*1* Copyright (c) 2016, 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 8055463 815327226* @summary Test createFont APIs27* @run main CreateFontArrayTest28*/2930import java.awt.Font;31import java.io.BufferedInputStream;32import java.io.File;33import java.io.FileInputStream;34import java.io.InputStream;3536/*37* This test pokes around in platform folders/directories to try38* to find some fonts with which to test. It will do different things39* on different platforms and may not do anything at all if the platform40* directories aren't where it expects. For example if /usr/share/fonts41* is not used on a particular Linux distro or on Windows the fonts are42* not in c:\windows\fonts (which would be the right place on 99.99% of43* systems you will find today.44* It ought to be very reliable but it is not 100% guaranteed.45* Failure to find fonts to test is 'not a product bug'.46* Fonts on a system having different content than we expect based on47* file extension is also 'not a product bug'.48* The former will cause silent success, the latter may cause 'noisy' failure49* and the test would then need to be dialled back to be more cautious.50*/5152public class CreateFontArrayTest {5354public static void main(String[] args) throws Exception {55test(".ttc", 2, -1, true);56test(".ttf", 1, 1, true);57test(".otf", 1, 1, true);58test(".pfa", 0, 0, false);59test(".pfb", 0, 0, false);60}6162static File getPlatformFontFolder(String ext) throws Exception {63boolean recurse = false;64String folder = null;65String os = System.getProperty("os.name");66if (os.startsWith("Win")) {67folder = "c:\\windows\\fonts";68} else if (os.startsWith("Linux")) {69folder = "/usr/share/fonts";70recurse = true; // need to dig to find fonts.71} else if (os.startsWith("Mac")) {72// Disabled until createFont can handle mac font names.73//folder = "/Library/Fonts";74}75if (folder == null) {76return null;77}78File dir = new File(folder);79if (!dir.exists() || !dir.isDirectory()) {80return null;81}82// Have a root.83if (!recurse) {84return dir;85}8687// If "recurse" is set, try to find a sub-folder which contains88// fonts with the specified extension89return findSubFolder(dir, ext);90}9192static File findSubFolder(File folder, String ext) {93File[] files =94folder.listFiles(f -> f.getName().toLowerCase().endsWith(ext));95if (files != null && files.length > 0) {96return folder;97}98File[] subdirs = folder.listFiles(f -> f.isDirectory());99for (File f : subdirs) {100File subfolder = findSubFolder(f, ext);101if (subfolder != null) {102return subfolder;103}104}105return null;106}107108static void test(String ext, int min, int max,109boolean expectSuccess ) throws Exception {110111File dir = getPlatformFontFolder(ext);112if (dir == null) {113System.out.println("No folder to test for " + ext);114return;115}116File[] files =117dir.listFiles(f -> f.getName().toLowerCase().endsWith(ext));118if (files == null || files.length == 0) {119System.out.println("No files to test for " + ext);120return;121}122System.out.println("Create from file " + files[0]);123Font[] fonts = null;124try {125fonts = Font.createFonts(files[0]);126System.out.println("createFont from file returned " + fonts);127} catch (Exception e) {128if (expectSuccess) {129throw new RuntimeException("Unexpected exception", e);130} else {131System.out.println("Got expected exception " + e);132return;133}134}135for (Font f : fonts) {136System.out.println(ext + " component : " + f);137}138if (fonts.length < min) {139throw new RuntimeException("Expected at least " + min +140" but got " + fonts.length);141}142if (max > 0 && fonts.length > max) {143throw new RuntimeException("Expected no more than " + max +144" but got " + fonts.length);145}146FileInputStream fis = null;147try {148System.out.println("Create from stream " + files[0]);149fis = new FileInputStream(files[0]);150InputStream s = new BufferedInputStream(fis);151fonts = null;152try {153fonts = Font.createFonts(s);154System.out.println("createFont from stream returned " + fonts);155} catch (Exception e) {156if (expectSuccess) {157throw new RuntimeException("Unexpected exception", e);158} else {159System.out.println("Got expected exception " + e);160return;161}162}163for (Font f : fonts) {164System.out.println(ext + " component : " + f);165}166if (fonts.length < min) {167throw new RuntimeException("Expected at least " + min +168" but got " + fonts.length);169}170if (max > 0 && fonts.length > max) {171throw new RuntimeException("Expected no more than " + max +172" but got " + fonts.length);173}174} finally {175if (fis != null) {176fis.close();177}178}179}180}181182183