jdk/test/tools/launcher/modules/classpath/JavaClassPathTest.java
author mchung
Thu, 27 Oct 2016 13:42:12 -0700
changeset 41814 a0333150713e
child 42170 0bb91d845f04
permissions -rw-r--r--
8168205: Should not default class path to CWD if -cp is not specified but -m is specified Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41814
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     1
/*
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     4
 *
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     8
 *
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    13
 * accompanied this code).
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    14
 *
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    18
 *
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    21
 * questions.
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    22
 */
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    23
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    24
import java.nio.file.Files;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    25
import java.nio.file.Path;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    26
import java.nio.file.Paths;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    27
import java.util.ArrayList;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    28
import java.util.List;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    29
import java.util.Map;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    30
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    31
import jdk.testlibrary.OutputAnalyzer;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    32
import org.testng.annotations.BeforeTest;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    33
import org.testng.annotations.DataProvider;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    34
import org.testng.annotations.Test;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    35
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    36
import static org.testng.Assert.assertTrue;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    37
import static jdk.testlibrary.ProcessTools.*;
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    38
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    39
/**
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    40
 * @test
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    41
 * @bug 8168205
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    42
 * @summary Test the default class path if -Djava.class.path is set
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    43
 * @library /lib/testlibrary
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    44
 * @modules jdk.compiler
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    45
 * @build CompilerUtils jdk.testlibrary.*
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    46
 * @run testng JavaClassPathTest
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    47
 */
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    48
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    49
public class JavaClassPathTest {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    50
    private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"),
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    51
                                                  "src");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    52
    private static final Path MODS_DIR = Paths.get("mods");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    53
    private static final String TEST_MODULE = "m";
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    54
    private static final String TEST_MAIN = "jdk.test.Main";
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    55
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    56
    @BeforeTest
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    57
    public void setup() throws Exception {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    58
        boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    59
                                                 MODS_DIR.resolve(TEST_MODULE));
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    60
        assertTrue(compiled, "module " + TEST_MODULE + " did not compile");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    61
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    62
        // add the class and a resource to the current working directory
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    63
        Path file = Paths.get("jdk/test/Main.class");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    64
        Files.createDirectories(file.getParent());
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    65
        Files.copy(MODS_DIR.resolve(TEST_MODULE).resolve(file), file);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    66
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    67
        Path res = Paths.get("jdk/test/res.properties");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    68
        Files.createFile(res);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    69
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    70
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    71
    @DataProvider(name = "classpath")
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    72
    public Object[][] classpath() {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    73
        return new Object[][]{
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    74
            // true indicates that class path default to current working directory
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    75
            { "",                              true  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    76
            { "-Djava.class.path",             true  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    77
            { "-Djava.class.path=",            true  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    78
            { "-Djava.class.path=.",           true  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    79
        };
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    80
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    81
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    82
    @Test(dataProvider = "classpath")
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    83
    public void testUnnamedModule(String option, boolean expected) throws Throwable {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    84
        List<String> args = new ArrayList<>();
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    85
        if (!option.isEmpty()) {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    86
            args.add(option);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    87
        }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    88
        args.add(TEST_MAIN);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    89
        args.add(Boolean.toString(expected));
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    90
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    91
        assertTrue(execute(args).getExitValue() == 0);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    92
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    93
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    94
    @DataProvider(name = "moduleAndClassPath")
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    95
    public Object[][] moduleAndClassPath() {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    96
        return new Object[][]{
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    97
            // true indicates that class path default to current working directory
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    98
            { "",                              false  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
    99
            { "-Djava.class.path",             false  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   100
            { "-Djava.class.path=",            false  },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   101
            { "-Djava.class.path=.",           true   },
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   102
        };
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   103
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   104
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   105
    @Test(dataProvider = "moduleAndClassPath")
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   106
    public void testNamedModule(String option, boolean expected) throws Throwable {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   107
        List<String> args = new ArrayList<>();
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   108
        if (!option.isEmpty()) {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   109
            args.add(option);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   110
        }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   111
        args.add("--module-path");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   112
        args.add(MODS_DIR.toString());
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   113
        args.add("-m");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   114
        args.add(TEST_MODULE + "/" + TEST_MAIN);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   115
        args.add(Boolean.toString(expected));
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   116
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   117
        assertTrue(execute(args).getExitValue() == 0);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   118
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   119
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   120
    private OutputAnalyzer execute(List<String> options) throws Throwable {
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   121
        ProcessBuilder pb =
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   122
            createJavaProcessBuilder(options.toArray(new String[0]));
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   123
        Map<String,String> env = pb.environment();
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   124
        // remove CLASSPATH environment variable
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   125
        String value = env.remove("CLASSPATH");
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   126
        return executeCommand(pb)
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   127
                    .outputTo(System.out)
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   128
                    .errorTo(System.out);
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   129
    }
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   130
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents:
diff changeset
   131
}