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