1/* 2 * Copyright (c) 2008, 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 6508981 27 * @summary cleanup file separator handling in JavacFileManager 28 * (This test is specifically to test the new impl of inferBinaryName) 29 * @library /tools/lib 30 * @modules jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.main 32 * @build toolbox.ToolBox toolbox.JarTask p.A 33 * @run main TestInferBinaryName 34 */ 35 36import java.io.*; 37import java.util.*; 38import javax.tools.*; 39 40import static javax.tools.JavaFileObject.Kind.*; 41import static javax.tools.StandardLocation.*; 42 43import toolbox.JarTask; 44import toolbox.ToolBox; 45 46/** 47 * Verify the various implementations of inferBinaryName, but configuring 48 * different instances of a file manager, getting a file object, and checking 49 * the impl of inferBinaryName for that file object. 50 */ 51public class TestInferBinaryName { 52 public static void main(String... args) throws Exception { 53 new TestInferBinaryName().run(); 54 } 55 56 void run() throws Exception { 57 testDirectory(); 58 59 File testJar = createJar(); 60 testZipArchive(testJar); 61 62 if (errors > 0) 63 throw new Exception(errors + " error found"); 64 } 65 66 File createJar() throws IOException { 67 File f = new File("test.jar"); 68 try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler() 69 .getStandardFileManager(null, null, null)) { 70 ToolBox tb = new ToolBox(); 71 new JarTask(tb, f.getPath()) 72 .files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*") 73 .run(); 74 } 75 return f; 76 } 77 78 void testDirectory() throws IOException { 79 String testClassName = "p.A"; 80 List<File> testClasses = Arrays.asList(new File(System.getProperty("test.classes"))); 81 try (JavaFileManager fm = getFileManager(testClasses)) { 82 test("testDirectory", 83 fm, testClassName, "SimpleFileObject"); 84 } 85 } 86 87 void testZipArchive(File testJar) throws IOException { 88 String testClassName = "java.lang.String"; 89 List<File> path = Arrays.asList(testJar); 90 try (JavaFileManager fm = getFileManager(path)) { 91 test("testZipArchive", 92 fm, testClassName, "JarFileObject"); 93 } 94 } 95 96 /** 97 * @param testName for debugging 98 * @param fm suitably configured file manager 99 * @param testClassName the classname to test 100 * @param implClassName the expected classname of the JavaFileObject impl, 101 * used for checking that we are checking the expected impl of 102 * inferBinaryName 103 */ 104 void test(String testName, 105 JavaFileManager fm, String testClassName, String implClassName) throws IOException { 106 JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS); 107 if (fo == null) { 108 System.err.println("Can't find " + testClassName); 109 errors++; 110 return; 111 } 112 113 String cn = fo.getClass().getSimpleName(); 114 String bn = fm.inferBinaryName(CLASS_PATH, fo); 115 System.err.println(testName + " " + cn + " " + bn); 116 checkEqual(cn, implClassName); 117 checkEqual(bn, testClassName); 118 System.err.println("OK"); 119 } 120 121 JavaFileManager getFileManager(List<File> path) 122 throws IOException { 123 StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler() 124 .getStandardFileManager(null, null, null); 125 fm.setLocation(CLASS_PATH, path); 126 return fm; 127 } 128 129 List<File> getPath(String s) { 130 List<File> path = new ArrayList<>(); 131 for (String f: s.split(File.pathSeparator)) { 132 if (f.length() > 0) 133 path.add(new File(f)); 134 } 135 //System.err.println("path: " + path); 136 return path; 137 } 138 139 void checkEqual(String found, String expect) { 140 if (!found.equals(expect)) { 141 System.err.println("Expected: " + expect); 142 System.err.println(" Found: " + found); 143 errors++; 144 } 145 } 146 147 private int errors; 148} 149 150class A { } 151 152