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.printer.GraalDebugHandlersFactory;
  35 import org.graalvm.compiler.test.AddExports;
  36 import org.junit.Test;
  37 
  38 @AddExports("jdk.internal.vm.compiler/org.graalvm.compiler.printer")
  39 public class GraalDebugHandlersFactoryTest extends GraalCompilerTest {
  40 
  41     @Test
  42     public void createUniqueTest() throws Exception {
  43         Field maxFileNameLengthField = GraalDebugHandlersFactory.class.getDeclaredField("MAX_FILE_NAME_LENGTH");
  44         maxFileNameLengthField.setAccessible(true);
  45         int maxFileNameLength = maxFileNameLengthField.getInt(null);
  46         Method createUniqueMethod = GraalDebugHandlersFactory.class.getDeclaredMethod("createUnique", Path.class, String.class, String.class, String.class, boolean.class);
  47         createUniqueMethod.setAccessible(true);
  48         Path tmpDir = Files.createTempDirectory(Paths.get("."), "createUniqueTest");
  49         try {
  50             for (boolean createDirectory : new boolean[]{true, false}) {
  51                 for (String ext : new String[]{"", ".bgv", ".graph-strings"}) {
  52                     for (int i = 0; i < maxFileNameLength + 5; i++) {
  53                         String id = new String(new char[i]).replace('\0', 'i');
  54                         String label = "";
  55                         createUniqueMethod.invoke(null, tmpDir, id, label, ext, createDirectory);
  56 
  57                         id = "";
  58                         label = new String(new char[i]).replace('\0', 'l');
  59                         createUniqueMethod.invoke(null, tmpDir, id, label, ext, createDirectory);
  60                     }
  61                 }
  62             }
  63         } finally {
  64             deleteTree(tmpDir);
  65         }
  66     }
  67 
  68     private static void deleteTree(Path root) throws IOException {
  69         Files.walk(root).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
  70     }
  71 }