1 /* 2 * Copyright (c) 2017, 2018, 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 /* 26 * @test 27 * @summary Handling of directories in -cp is based on the classlist 28 * @requires vm.cds 29 * @library /test/lib 30 * @compile test-classes/Hello.java 31 * @run driver DirClasspathTest 32 */ 33 34 import jdk.test.lib.Platform; 35 import jdk.test.lib.process.OutputAnalyzer; 36 import java.io.File; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.util.Arrays; 41 42 public class DirClasspathTest { 43 private static final int MAX_PATH = 260; 44 45 public static void main(String[] args) throws Exception { 46 File dir = new File(System.getProperty("user.dir")); 47 File emptydir = new File(dir, "emptydir"); 48 emptydir.mkdir(); 49 50 ///////////////////////////////////////////////////////////////// 51 // The classlist only contains boot class in following test cases 52 ///////////////////////////////////////////////////////////////// 53 String bootClassList[] = {"java/lang/Object"}; 54 55 // Empty dir in -cp: should be OK 56 OutputAnalyzer output; 57 output = TestCommon.dump(emptydir.getPath(), bootClassList, "-Xlog:class+path=info"); 58 TestCommon.checkDump(output); 59 60 // Long path to empty dir in -cp: should be OK 61 Path classDir = Paths.get(System.getProperty("test.classes")); 62 Path destDir = classDir; 63 int subDirLen = MAX_PATH - classDir.toString().length() - 2; 64 if (subDirLen > 0) { 65 char[] chars = new char[subDirLen]; 66 Arrays.fill(chars, 'x'); 67 String subPath = new String(chars); 68 destDir = Paths.get(System.getProperty("test.classes"), subPath); 69 } 70 File longDir = destDir.toFile(); 71 longDir.mkdir(); 72 File subDir = new File(longDir, "subdir"); 73 subDir.mkdir(); 74 output = TestCommon.dump(subDir.getPath(), bootClassList, "-Xlog:class+path=info"); 75 TestCommon.checkDump(output); 76 77 // Non-empty dir in -cp: should be OK 78 // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir> 79 output = TestCommon.dump(dir.getPath(), bootClassList, "-Xlog:class+path=info"); 80 TestCommon.checkDump(output); 81 82 // Long path to non-empty dir in -cp: should be OK 83 // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir> 84 output = TestCommon.dump(longDir.getPath(), bootClassList, "-Xlog:class+path=info"); 85 TestCommon.checkDump(output); 86 87 ///////////////////////////////////////////////////////////////// 88 // The classlist contains non-boot class in following test cases 89 ///////////////////////////////////////////////////////////////// 90 String appClassList[] = {"java/lang/Object", "com/sun/tools/javac/Main"}; 91 92 // Non-empty dir in -cp: should be OK (as long as no classes were loaded from there) 93 output = TestCommon.dump(dir.getPath(), appClassList, "-Xlog:class+path=info"); 94 TestCommon.checkDump(output); 95 96 // Long path to non-empty dir in -cp: should be OK (as long as no classes were loaded from there) 97 output = TestCommon.dump(longDir.getPath(), appClassList, "-Xlog:class+path=info"); 98 TestCommon.checkDump(output); 99 100 ///////////////////////////////////////////////////////////////// 101 // Loading an app class from a directory 102 ///////////////////////////////////////////////////////////////// 103 String appClassList2[] = {"Hello", "java/lang/Object", "com/sun/tools/javac/Main"}; 104 // Non-empty dir in -cp: should report error if a class is loaded from it 105 output = TestCommon.dump(classDir.toString(), appClassList2, "-Xlog:class+path=info"); 106 output.shouldNotHaveExitValue(0); 107 output.shouldContain("Cannot have non-empty directory in paths"); 108 109 // Long path to non-empty dir in -cp: should report error if a class is loaded from it 110 File srcClass = new File(classDir.toFile(), "Hello.class"); 111 File destClass = new File(longDir, "Hello.class"); 112 Files.copy(srcClass.toPath(), destClass.toPath()); 113 output = TestCommon.dump(longDir.getPath(), appClassList2, "-Xlog:class+path=info"); 114 output.shouldNotHaveExitValue(0); 115 output.shouldContain("Cannot have non-empty directory in paths"); 116 } 117 }