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