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  * @run main DirClasspathTest
  31  */
  32 
  33 import jdk.test.lib.Platform;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 import java.io.File;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 
  40 public class DirClasspathTest {
  41     private static final int MAX_PATH = 260;
  42 
  43     public static void main(String[] args) throws Exception {
  44         File dir = new File(System.getProperty("user.dir"));
  45         File emptydir = new File(dir, "emptydir");
  46         emptydir.mkdir();
  47 
  48         /////////////////////////////////////////////////////////////////
  49         // The classlist only contains boot class in following test cases
  50         /////////////////////////////////////////////////////////////////
  51         String bootClassList[] = {"java/lang/Object"};
  52 
  53         // Empty dir in -cp: should be OK
  54         OutputAnalyzer output;
  55         output = TestCommon.dump(emptydir.getPath(), bootClassList, "-Xlog:class+path=info");
  56         TestCommon.checkDump(output);
  57 
  58         // Long path to empty dir in -cp: should be OK
  59         Path classDir = Paths.get(System.getProperty("test.classes"));
  60         Path destDir = classDir;
  61         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
  62         if (subDirLen > 0) {
  63             char[] chars = new char[subDirLen];
  64             Arrays.fill(chars, 'x');
  65             String subPath = new String(chars);
  66             destDir = Paths.get(System.getProperty("test.classes"), subPath);
  67         }
  68         File longDir = destDir.toFile();
  69         longDir.mkdir();
  70         File subDir = new File(longDir, "subdir");
  71         subDir.mkdir();
  72         output = TestCommon.dump(subDir.getPath(), bootClassList, "-Xlog:class+path=info");
  73         TestCommon.checkDump(output);
  74 
  75         // Non-empty dir in -cp: should be OK
  76         // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir>
  77         output = TestCommon.dump(dir.getPath(), bootClassList, "-Xlog:class+path=info");
  78         TestCommon.checkDump(output);
  79 
  80         // Long path to non-empty dir in -cp: should be OK
  81         // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir>
  82         output = TestCommon.dump(longDir.getPath(), bootClassList, "-Xlog:class+path=info");
  83         TestCommon.checkDump(output);
  84 
  85         /////////////////////////////////////////////////////////////////
  86         // The classlist contains non-boot class in following test cases
  87         /////////////////////////////////////////////////////////////////
  88         String appClassList[] = {"java/lang/Object", "com/sun/tools/javac/Main"};
  89 
  90         // Non-empty dir in -cp: should report error
  91         output = TestCommon.dump(dir.getPath(), appClassList, "-Xlog:class+path=info");
  92         output.shouldNotHaveExitValue(0);
  93         output.shouldContain("Cannot have non-empty directory in paths");
  94 
  95         // Long path to non-empty dir in -cp: should report error
  96         output = TestCommon.dump(longDir.getPath(), appClassList, "-Xlog:class+path=info");
  97         output.shouldNotHaveExitValue(0);
  98         output.shouldContain("Cannot have non-empty directory in paths");
  99     }
 100 }