1 /* 2 * Copyright (c) 2017, 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 package org.graalvm.compiler.core.test; 26 27 import java.io.IOException; 28 import java.nio.file.DirectoryStream; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 32 import jdk.internal.vm.compiler.collections.EconomicMap; 33 import org.graalvm.compiler.debug.DebugOptions; 34 import org.graalvm.compiler.options.OptionKey; 35 import org.graalvm.compiler.options.OptionValues; 36 import org.junit.Test; 37 38 /** 39 * Check that setting the dump path results in files ending up in the right directory with matching 40 * names. 41 */ 42 public class DumpPathTest extends GraalCompilerTest { 43 44 public static Object snippet() { 45 return new String("snippet"); 46 } 47 48 @Test 49 public void testDump() throws IOException { 50 Path dumpDirectoryPath = Files.createTempDirectory("DumpPathTest"); 51 String[] extensions = new String[]{".cfg", ".bgv", ".graph-strings"}; 52 EconomicMap<OptionKey<?>, Object> overrides = OptionValues.newOptionMap(); 53 overrides.put(DebugOptions.DumpPath, dumpDirectoryPath.toString()); 54 overrides.put(DebugOptions.PrintGraphFile, true); 55 overrides.put(DebugOptions.PrintCanonicalGraphStrings, true); 56 overrides.put(DebugOptions.Dump, "*"); 57 58 // Generate dump files. 59 test(new OptionValues(getInitialOptions(), overrides), "snippet"); 60 // Check that Ideal files got created, in the right place. 61 checkForFiles(dumpDirectoryPath, extensions); 62 63 // Clean up the generated files. 64 scrubDirectory(dumpDirectoryPath); 65 } 66 67 /** 68 * Check that the given directory contains file or directory names with all the given 69 * extensions. 70 */ 71 private static void checkForFiles(Path directoryPath, String[] extensions) throws IOException { 72 String[] paths = new String[extensions.length]; 73 try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) { 74 for (Path filePath : stream) { 75 String fileName = filePath.getFileName().toString(); 76 for (int i = 0; i < extensions.length; i++) { 77 String extension = extensions[i]; 78 if (fileName.endsWith(extensions[i])) { 79 assertTrue(paths[i] == null, "multiple files found for %s in %s", extension, directoryPath); 80 paths[i] = fileName.replace(extensions[i], ""); 81 } 82 } 83 } 84 } 85 for (int i = 0; i < paths.length; i++) { 86 assertTrue(paths[i] != null, "missing file for extension %s in %s", extensions[i], directoryPath); 87 } 88 // Ensure that all file names are the same. 89 for (int i = 1; i < paths.length; i++) { 90 assertTrue(paths[0].equals(paths[i]), paths[0] + " != " + paths[i]); 91 } 92 } 93 94 /** 95 * Remove the temporary directory. 96 */ 97 private static void scrubDirectory(Path directoryPath) { 98 try { 99 try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) { 100 for (Path filePath : stream) { 101 if (Files.isRegularFile(filePath)) { 102 Files.delete(filePath); 103 } else if (Files.isDirectory(filePath)) { 104 scrubDirectory(filePath); 105 } 106 } 107 } 108 Files.delete(directoryPath); 109 } catch (IOException ioe) { 110 ioe.printStackTrace(); 111 } 112 } 113 }