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