1 /* 2 * Copyright (c) 2015, 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 8072480 27 * @summary Unit test for CreateSymbols 28 * @clean * 29 * @run main CreateSymbolsTest 30 */ 31 32 import java.io.IOException; 33 import java.net.URL; 34 import java.net.URLClassLoader; 35 import java.nio.file.FileVisitResult; 36 import java.nio.file.FileVisitor; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.nio.file.attribute.BasicFileAttributes; 41 import java.util.Arrays; 42 import javax.tools.JavaCompiler; 43 import javax.tools.StandardJavaFileManager; 44 import javax.tools.ToolProvider; 45 46 public class CreateSymbolsTest { 47 48 static final String CREATE_SYMBOLS_NAME = "symbolgenerator.CreateSymbols"; 49 50 public static void main(String... args) throws Exception { 51 new CreateSymbolsTest().doRun(); 52 } 53 54 void doRun() throws Exception { 55 Path testClasses = Paths.get(System.getProperty("test.classes")); 56 Path compileDir = testClasses.resolve("data"); 57 deleteRecursively(compileDir); 58 Files.createDirectories(compileDir); 59 Path createSymbols = findFile("../../make/src/build/tools/symbolgenerator/CreateSymbols.java"); 60 61 if (createSymbols == null) { 62 System.err.println("Warning: cannot find CreateSymbols, skipping."); 63 return ; 64 } 65 66 Path createTestImpl = findFile("../../make/test/tools/sym/CreateSymbolsTestImpl.java"); 67 68 if (createTestImpl == null) { 69 System.err.println("Warning: cannot find CreateSymbols, skipping."); 70 return ; 71 } 72 73 Path toolBox = findFile("../../langtools/test/tools/lib/ToolBox.java"); 74 75 if (toolBox == null) { 76 System.err.println("Warning: cannot find ToolBox, skipping."); 77 return ; 78 } 79 80 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 81 82 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 83 compiler.getTask(null, 84 null, 85 null, 86 Arrays.asList("-d", compileDir.toAbsolutePath().toString()), 87 null, 88 fm.getJavaFileObjects(createSymbols, createTestImpl, toolBox) 89 ).call(); 90 } 91 92 URLClassLoader cl = new URLClassLoader(new URL[] {testClasses.toUri().toURL(), compileDir.toUri().toURL()}); 93 Class<?> createSymbolTest = cl.loadClass("CreateSymbolsTestImpl"); 94 95 createSymbolTest.getMethod("main", String[].class).invoke(null, (Object) new String[0]); 96 } 97 98 Path findFile(String path) { 99 Path testSrc = Paths.get(System.getProperty("test.src", ".")); 100 101 for (Path d = testSrc; d != null; d = d.getParent()) { 102 if (Files.exists(d.resolve("TEST.ROOT"))) { 103 Path createSymbols = d.resolve(path); 104 if (Files.exists(createSymbols)) { 105 return createSymbols; 106 } 107 } 108 } 109 110 return null; 111 } 112 113 void deleteRecursively(Path dir) throws IOException { 114 Files.walkFileTree(dir, new FileVisitor<Path>() { 115 @Override 116 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 117 return FileVisitResult.CONTINUE; 118 } 119 @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 120 Files.delete(file); 121 return FileVisitResult.CONTINUE; 122 } 123 @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 124 return FileVisitResult.CONTINUE; 125 } 126 @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 127 Files.delete(dir); 128 return FileVisitResult.CONTINUE; 129 } 130 }); 131 } 132 }