langtools/test/tools/javac/modules/ModuleVersion.java
changeset 42822 a84956e7ee4d
equal deleted inserted replaced
42812:084017ef9300 42822:a84956e7ee4d
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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  * @test
       
    26  * @summary simple tests of module uses
       
    27  * @library /tools/lib
       
    28  * @modules jdk.compiler/com.sun.tools.javac.api
       
    29  *          jdk.compiler/com.sun.tools.javac.main
       
    30  *          jdk.jdeps/com.sun.tools.classfile
       
    31  * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
       
    32  * @run main ModuleVersion
       
    33  */
       
    34 
       
    35 import java.io.IOException;
       
    36 import java.nio.file.Files;
       
    37 import java.nio.file.Path;
       
    38 
       
    39 import com.sun.tools.classfile.Attribute;
       
    40 import com.sun.tools.classfile.ClassFile;
       
    41 import com.sun.tools.classfile.ConstantPoolException;
       
    42 import com.sun.tools.classfile.Module_attribute;
       
    43 import toolbox.JavacTask;
       
    44 import toolbox.Task.Expect;
       
    45 import toolbox.Task.OutputKind;
       
    46 
       
    47 public class ModuleVersion extends ModuleTestBase {
       
    48     public static void main(String... args) throws Exception {
       
    49         ModuleVersion t = new ModuleVersion();
       
    50         t.runTests();
       
    51     }
       
    52 
       
    53     @Test
       
    54     public void testSetSingleModuleVersion(Path base) throws Exception {
       
    55         Path src = base.resolve("src");
       
    56         tb.writeJavaFiles(src,
       
    57                 "module m { }");
       
    58         Path classes = base.resolve("classes");
       
    59         Files.createDirectories(classes);
       
    60 
       
    61         String version = "1.2.3.4";
       
    62 
       
    63         new JavacTask(tb)
       
    64             .options("--module-version", version)
       
    65             .outdir(classes)
       
    66             .files(findJavaFiles(src))
       
    67             .run()
       
    68             .writeAll();
       
    69 
       
    70         checkModuleVersion(classes.resolve("module-info.class"), version);
       
    71     }
       
    72 
       
    73     @Test
       
    74     public void testMultipleModuleVersions(Path base) throws Exception {
       
    75         Path src = base.resolve("src");
       
    76         Path m1 = src.resolve("m1");
       
    77         tb.writeJavaFiles(m1,
       
    78                 "module m1 { }");
       
    79         Path m2 = src.resolve("m2");
       
    80         tb.writeJavaFiles(m2,
       
    81                 "module m2 { }");
       
    82         Path classes = base.resolve("classes");
       
    83         Files.createDirectories(classes);
       
    84 
       
    85         String version = "1.2.3.4";
       
    86 
       
    87         new JavacTask(tb)
       
    88             .options("--module-source-path", src.toString(),
       
    89                      "--module-version", version)
       
    90             .outdir(classes)
       
    91             .files(findJavaFiles(src))
       
    92             .run()
       
    93             .writeAll();
       
    94 
       
    95         checkModuleVersion(classes.resolve("m1").resolve("module-info.class"), version);
       
    96         checkModuleVersion(classes.resolve("m2").resolve("module-info.class"), version);
       
    97 
       
    98         String log = new JavacTask(tb, JavacTask.Mode.CMDLINE)
       
    99             .options("--module-source-path", src.toString(),
       
   100                      "--module-version", "b",
       
   101                      "-XDrawDiagnostics")
       
   102             .outdir(classes)
       
   103             .files(findJavaFiles(src))
       
   104             .run(Expect.FAIL)
       
   105             .writeAll()
       
   106             .getOutput(OutputKind.DIRECT);
       
   107 
       
   108         String expectedLog = "bad value for --module-version option: 'b'";
       
   109 
       
   110         if (!log.contains(expectedLog)) {
       
   111             throw new AssertionError("Incorrect log: " + log);
       
   112         }
       
   113     }
       
   114 
       
   115     private void checkModuleVersion(Path classfile, String version) throws IOException, ConstantPoolException {
       
   116         ClassFile cf = ClassFile.read(classfile);
       
   117 
       
   118         Module_attribute moduleAttribute = (Module_attribute) cf.attributes.get(Attribute.Module);
       
   119 
       
   120         if (moduleAttribute == null) {
       
   121             throw new AssertionError("Version attribute missing!");
       
   122         }
       
   123 
       
   124         String actualVersion = cf.constant_pool.getUTF8Value(moduleAttribute.module_version_index);
       
   125 
       
   126         if (!version.equals(actualVersion)) {
       
   127             throw new AssertionError("Incorrect version in the classfile: " + actualVersion);
       
   128         }
       
   129     }
       
   130 
       
   131 }