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.File;
  26 import java.io.IOException;
  27 import java.lang.reflect.Field;
  28 import java.lang.reflect.Method;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Comparator;
  33 
  34 import org.graalvm.compiler.debug.DebugOptions;
  35 import org.graalvm.compiler.debug.PathUtilities;
  36 import org.graalvm.compiler.options.OptionKey;
  37 import org.graalvm.compiler.options.OptionValues;
  38 import org.graalvm.compiler.test.AddExports;
  39 import org.junit.Assume;
  40 import org.junit.Test;
  41 
  42 @AddExports("jdk.internal.vm.compiler/org.graalvm.compiler.printer")
  43 public class GraalDebugHandlersFactoryTest extends GraalCompilerTest {
  44 
  45     @Test
  46     public void createUniqueTest() throws Exception {
  47         Field maxFileNameLengthField = PathUtilities.class.getDeclaredField("MAX_FILE_NAME_LENGTH");
  48         try {
  49             maxFileNameLengthField.setAccessible(true);
  50         } catch (RuntimeException ex) {
  51             Assume.assumeFalse("If InaccessibleObjectException is thrown, skip the test, we are on JDK9", ex.getClass().getSimpleName().equals("InaccessibleObjectException"));
  52         }
  53         int maxFileNameLength = maxFileNameLengthField.getInt(null);
  54         Method createUniqueMethod = PathUtilities.class.getDeclaredMethod("createUnique", OptionValues.class, OptionKey.class, String.class, String.class, String.class, boolean.class);
  55         createUniqueMethod.setAccessible(true);
  56         Path tmpDir = Files.createTempDirectory(Paths.get("."), "createUniqueTest");
  57         OptionValues options = new OptionValues(OptionValues.asMap(DebugOptions.DumpPath, tmpDir.toString()));
  58         try {
  59             for (boolean createDirectory : new boolean[]{true, false}) {
  60                 for (String ext : new String[]{"", ".bgv", ".graph-strings"}) {
  61                     for (int i = 0; i < maxFileNameLength + 5; i++) {
  62                         String id = new String(new char[i]).replace('\0', 'i');
  63                         String label = "";
  64                         createUniqueMethod.invoke(null, options, null, id, label, ext, createDirectory);
  65 
  66                         id = "";
  67                         label = new String(new char[i]).replace('\0', 'l');
  68                         createUniqueMethod.invoke(null, options, null, id, label, ext, createDirectory);
  69                     }
  70                 }
  71             }
  72         } finally {
  73             deleteTree(tmpDir);
  74         }
  75     }
  76 
  77     private static void deleteTree(Path root) throws IOException {
  78         Files.walk(root).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
  79     }
  80 }