TestInferBinaryName.java revision 922:6970d9fb8e02
1/* 2 * Copyright (c) 2008, 2011 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 6508981 27 * @summary cleanup file separator handling in JavacFileManager 28 * (This test is specifically to test the new impl of inferBinaryName) 29 * @build p.A 30 * @run main TestInferBinaryName 31 */ 32 33import java.io.*; 34import java.util.*; 35import javax.tools.*; 36 37import com.sun.tools.javac.file.JavacFileManager; 38import com.sun.tools.javac.util.Context; 39import com.sun.tools.javac.util.Options; 40 41import static javax.tools.JavaFileObject.Kind.*; 42import static javax.tools.StandardLocation.*; 43 44 45/** 46 * Verify the various implementations of inferBinaryName, but configuring 47 * different instances of a file manager, getting a file object, and checking 48 * the impl of inferBinaryName for that file object. 49 */ 50public class TestInferBinaryName { 51 static final boolean IGNORE_SYMBOL_FILE = false; 52 static final boolean USE_SYMBOL_FILE = true; 53 static final boolean DONT_USE_ZIP_FILE_INDEX = false; 54 static final boolean USE_ZIP_FILE_INDEX = true; 55 56 public static void main(String... args) throws Exception { 57 new TestInferBinaryName().run(); 58 } 59 60 void run() throws Exception { 61 //System.err.println(System.getProperties()); 62 testDirectory(); 63 testSymbolArchive(); 64 testZipArchive(); 65 testZipFileIndexArchive(); 66 testZipFileIndexArchive2(); 67 if (errors > 0) 68 throw new Exception(errors + " error found"); 69 } 70 71 void testDirectory() throws IOException { 72 String testClassName = "p.A"; 73 JavaFileManager fm = 74 getFileManager("test.classes", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); 75 test("testDirectory", 76 fm, testClassName, "com.sun.tools.javac.file.RegularFileObject"); 77 } 78 79 void testSymbolArchive() throws IOException { 80 String testClassName = "java.lang.String"; 81 JavaFileManager fm = 82 getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX); 83 test("testSymbolArchive", 84 fm, testClassName, "com.sun.tools.javac.file.SymbolArchive$SymbolFileObject"); 85 } 86 87 void testZipArchive() throws IOException { 88 String testClassName = "java.lang.String"; 89 JavaFileManager fm = 90 getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX); 91 test("testZipArchive", 92 fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject"); 93 } 94 95 void testZipFileIndexArchive() throws IOException { 96 String testClassName = "java.lang.String"; 97 JavaFileManager fm = 98 getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); 99 test("testZipFileIndexArchive", 100 fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject"); 101 } 102 103 void testZipFileIndexArchive2() throws IOException { 104 String testClassName = "java.lang.String"; 105 JavaFileManager fm = 106 getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); 107 test("testZipFileIndexArchive2", 108 fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject"); 109 } 110 111 /** 112 * @param testName for debugging 113 * @param fm suitably configured file manager 114 * @param testClassName the classname to test 115 * @param implClassName the expected classname of the JavaFileObject impl, 116 * used for checking that we are checking the expected impl of 117 * inferBinaryName 118 */ 119 void test(String testName, 120 JavaFileManager fm, String testClassName, String implClassName) throws IOException { 121 JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS); 122 if (fo == null) { 123 System.err.println("Can't find " + testClassName); 124 errors++; 125 return; 126 } 127 128 String cn = fo.getClass().getName(); 129 String bn = fm.inferBinaryName(CLASS_PATH, fo); 130 System.err.println(testName + " " + cn + " " + bn); 131 check(cn, implClassName); 132 check(bn, testClassName); 133 System.err.println("OK"); 134 } 135 136 JavaFileManager getFileManager(String classpathProperty, 137 boolean symFileKind, 138 boolean zipFileIndexKind) 139 throws IOException { 140 Context ctx = new Context(); 141 Options options = Options.instance(ctx); 142 options.put("useOptimizedZip", 143 Boolean.toString(zipFileIndexKind == USE_ZIP_FILE_INDEX)); 144 145 if (symFileKind == IGNORE_SYMBOL_FILE) 146 options.put("ignore.symbol.file", "true"); 147 JavacFileManager fm = new JavacFileManager(ctx, false, null); 148 List<File> path = getPath(System.getProperty(classpathProperty)); 149 fm.setLocation(CLASS_PATH, path); 150 return fm; 151 } 152 153 List<File> getPath(String s) { 154 List<File> path = new ArrayList<File>(); 155 for (String f: s.split(File.pathSeparator)) { 156 if (f.length() > 0) 157 path.add(new File(f)); 158 } 159 //System.err.println("path: " + path); 160 return path; 161 } 162 163 void check(String found, String expect) { 164 if (!found.equals(expect)) { 165 System.err.println("Expected: " + expect); 166 System.err.println(" Found: " + found); 167 errors++; 168 } 169 } 170 171 private int errors; 172} 173 174class A { } 175 176