langtools/test/tools/javac/modules/ModuleVersion.java
author sadayapalam
Mon, 06 Feb 2017 18:14:51 +0530
changeset 43584 63e67712246b
parent 42822 a84956e7ee4d
permissions -rw-r--r--
8166420: Confusing error message when reading bad module declaration Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     1
/*
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     4
 *
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     8
 *
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    13
 * accompanied this code).
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    14
 *
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    18
 *
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    21
 * questions.
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    22
 */
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    23
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    24
/**
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    25
 * @test
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    26
 * @summary simple tests of module uses
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    27
 * @library /tools/lib
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.main
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    30
 *          jdk.jdeps/com.sun.tools.classfile
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    31
 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    32
 * @run main ModuleVersion
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    33
 */
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    34
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    35
import java.io.IOException;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    36
import java.nio.file.Files;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    37
import java.nio.file.Path;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    38
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    39
import com.sun.tools.classfile.Attribute;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    40
import com.sun.tools.classfile.ClassFile;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    41
import com.sun.tools.classfile.ConstantPoolException;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    42
import com.sun.tools.classfile.Module_attribute;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    43
import toolbox.JavacTask;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    44
import toolbox.Task.Expect;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    45
import toolbox.Task.OutputKind;
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    46
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    47
public class ModuleVersion extends ModuleTestBase {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    48
    public static void main(String... args) throws Exception {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    49
        ModuleVersion t = new ModuleVersion();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    50
        t.runTests();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    51
    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    52
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    53
    @Test
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    54
    public void testSetSingleModuleVersion(Path base) throws Exception {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    55
        Path src = base.resolve("src");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    56
        tb.writeJavaFiles(src,
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    57
                "module m { }");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    58
        Path classes = base.resolve("classes");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    59
        Files.createDirectories(classes);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    60
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    61
        String version = "1.2.3.4";
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    62
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    63
        new JavacTask(tb)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    64
            .options("--module-version", version)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    65
            .outdir(classes)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    66
            .files(findJavaFiles(src))
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    67
            .run()
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    68
            .writeAll();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    69
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    70
        checkModuleVersion(classes.resolve("module-info.class"), version);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    71
    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    72
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    73
    @Test
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    74
    public void testMultipleModuleVersions(Path base) throws Exception {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    75
        Path src = base.resolve("src");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    76
        Path m1 = src.resolve("m1");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    77
        tb.writeJavaFiles(m1,
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    78
                "module m1 { }");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    79
        Path m2 = src.resolve("m2");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    80
        tb.writeJavaFiles(m2,
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    81
                "module m2 { }");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    82
        Path classes = base.resolve("classes");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    83
        Files.createDirectories(classes);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    84
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    85
        String version = "1.2.3.4";
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    86
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    87
        new JavacTask(tb)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    88
            .options("--module-source-path", src.toString(),
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    89
                     "--module-version", version)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    90
            .outdir(classes)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    91
            .files(findJavaFiles(src))
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    92
            .run()
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    93
            .writeAll();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    94
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    95
        checkModuleVersion(classes.resolve("m1").resolve("module-info.class"), version);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    96
        checkModuleVersion(classes.resolve("m2").resolve("module-info.class"), version);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    97
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    98
        String log = new JavacTask(tb, JavacTask.Mode.CMDLINE)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    99
            .options("--module-source-path", src.toString(),
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   100
                     "--module-version", "b",
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   101
                     "-XDrawDiagnostics")
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   102
            .outdir(classes)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   103
            .files(findJavaFiles(src))
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   104
            .run(Expect.FAIL)
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   105
            .writeAll()
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   106
            .getOutput(OutputKind.DIRECT);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   107
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   108
        String expectedLog = "bad value for --module-version option: 'b'";
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   109
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   110
        if (!log.contains(expectedLog)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   111
            throw new AssertionError("Incorrect log: " + log);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   112
        }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   113
    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   114
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   115
    private void checkModuleVersion(Path classfile, String version) throws IOException, ConstantPoolException {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   116
        ClassFile cf = ClassFile.read(classfile);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   117
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   118
        Module_attribute moduleAttribute = (Module_attribute) cf.attributes.get(Attribute.Module);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   119
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   120
        if (moduleAttribute == null) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   121
            throw new AssertionError("Version attribute missing!");
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   122
        }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   123
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   124
        String actualVersion = cf.constant_pool.getUTF8Value(moduleAttribute.module_version_index);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   125
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   126
        if (!version.equals(actualVersion)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   127
            throw new AssertionError("Incorrect version in the classfile: " + actualVersion);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   128
        }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   129
    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   130
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   131
}