jdk/test/tools/jlink/JLinkNegativeTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
child 43729 21db38703675
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Negative tests for jlink
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @bug 8130861
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @author Andrei Eremeev
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          java.base/jdk.internal.module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @run testng JLinkNegativeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.io.OutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.nio.file.FileVisitResult;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.nio.file.SimpleFileVisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.nio.file.attribute.BasicFileAttributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.module.ModuleInfoWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import org.testng.SkipException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import org.testng.annotations.BeforeClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import tests.JImageGenerator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import tests.JImageGenerator.InMemoryFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import tests.Result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
@Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
public class JLinkNegativeTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    private Helper helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    @BeforeClass
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    public void setUp() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        if (helper == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            throw new SkipException("Not run");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        helper.generateDefaultModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    private void deleteDirectory(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                Files.delete(file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                return FileVisitResult.CONTINUE;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                Files.delete(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                return FileVisitResult.CONTINUE;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public void testModuleNotExist() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        helper.generateDefaultImage("failure1").assertFailure("Error: Module failure1 not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    public void testNotExistInAddMods() {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
    98
        // cannot find jmod from --add-modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                .modulePath(".")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                .addMods("not_exist")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                .output(helper.getImageDir().resolve("failure2"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                .call().assertFailure("Error: Module not_exist not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    public void test() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        helper.generateDefaultJModule("failure3");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        Path image = helper.generateDefaultImage("failure3").assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                .output(image)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                .addMods("leaf1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                .limitMods("leaf1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                .call().assertFailure("Error: directory already exists: .*failure3.image(\n|\r|.)*");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    public void testOutputIsFile() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        // output == file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        Path image = helper.createNewImageDir("failure4");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        Files.createFile(image);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                .output(image)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                .addMods("leaf1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                .call().assertFailure("Error: directory already exists: .*failure4.image(\n|\r|.)*");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    public void testModuleNotFound() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        // limit module is not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        Path imageFile = helper.createNewImageDir("test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                .output(imageFile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                .addMods("leaf1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                .limitMods("leaf1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                .limitMods("failure5")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                .call().assertFailure("Error: Module failure5 not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    public void testJmodIsDir() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        Path imageFile = helper.createNewImageDir("test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        Path dirJmod = helper.createNewJmodFile("dir");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        Files.createDirectory(dirJmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                    .output(imageFile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                    .addMods("dir")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                    .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    .call().assertFailure("Error: Module dir not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            deleteDirectory(dirJmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    public void testJarIsDir() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        Path imageFile = helper.createNewImageDir("test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        Path dirJar = helper.createNewJarFile("dir");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        Files.createDirectory(dirJar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                    .output(imageFile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    .addMods("dir")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                    .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                    .call().assertFailure("Error: Module dir not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            deleteDirectory(dirJar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    public void testMalformedJar() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        Path imageFile = helper.createNewImageDir("test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        Path jar = helper.createNewJarFile("not_zip");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        Files.createFile(jar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                    .output(imageFile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                    .addMods("not_zip")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                    .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                    .call().assertFailure("Error: java.util.zip.ZipException: zip file is empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            deleteDirectory(jar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    public void testMalformedJmod() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        Path imageFile = helper.createNewImageDir("test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        Path jmod = helper.createNewJmodFile("not_zip");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        Files.createFile(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                    .output(imageFile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    .addMods("not_zip")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    .modulePath(helper.defaultModulePath())
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   194
                    .call().assertFailure("Error: java.io.IOException: Invalid jmod file");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            deleteDirectory(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
    // Temporarily exclude; the jmod tool can no longer be used to create a jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
    // with a class in the unnamed package. Find another way, or remove.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
//    public void testAddDefaultPackage() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
//        String moduleName = "hacked1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
//        Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
//                moduleName, Arrays.asList("hacked1.Main", "A", "B"), "leaf1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
//        JImageGenerator
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
//                .getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
//                .addClassPath(module)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
//                .jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
//                .create().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
//        Path image = helper.generateDefaultImage(moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
//        helper.checkImage(image, moduleName, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
//    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
    public void testAddSomeTopLevelFiles() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        String moduleName = "hacked2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        Files.createFile(module.resolve("top-level-file"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        Path jmod = JImageGenerator
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
                .getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                .addClassPath(module)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
                .jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                .create().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            Path image = helper.generateDefaultImage(moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            helper.checkImage(image, moduleName, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            deleteDirectory(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
    public void testAddNonStandardSection() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        String moduleName = "hacked3";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        Path module = helper.generateDefaultJModule(moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        JImageGenerator.addFiles(module, new InMemoryFile("unknown/A.class", new byte[0]));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            Result result = helper.generateDefaultImage(moduleName);
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   239
            System.err.println(result.getMessage());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   240
            if (result.getExitCode() == 0) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                throw new AssertionError("Crash expected");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            deleteDirectory(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
    @Test(enabled = true)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    public void testSectionsAreFiles() throws IOException {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   250
        String moduleName = "hacked4";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        Path jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        JImageGenerator.addFiles(jmod,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                new InMemoryFile("/native", new byte[0]),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                new InMemoryFile("/conf", new byte[0]),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                new InMemoryFile("/bin", new byte[0]));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            Result result = helper.generateDefaultImage(moduleName);
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   258
            System.err.println(result.getMessage());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 40261
diff changeset
   259
            if (result.getExitCode() == 0) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                throw new AssertionError("Crash expected");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
            deleteDirectory(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
    public void testDuplicateModule1() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        String moduleName1 = "dupRes1Jmod1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        String moduleName2 = "dupRes1Jmod2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        List<String> classNames = Arrays.asList("java.A", "javax.B");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        Path module1 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName1, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        Path module2 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName2, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   277
            ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName1)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                    .requires("java.base").build(), out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        Path jmod1 = JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                .addClassPath(module1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                .jmod(helper.createNewJmodFile(moduleName1))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                .create()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
                .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        Path jmod2 = JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                .addClassPath(module2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                .jmod(helper.createNewJmodFile(moduleName2))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                .create()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
            helper.generateDefaultImage(moduleName1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                    .assertFailure("Error: Two versions of module dupRes1Jmod1 found in");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            deleteDirectory(jmod1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            deleteDirectory(jmod2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
    public void testDuplicateModule2() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        String moduleName = "dupRes2Jmod";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        List<String> classNames = Arrays.asList("java.A", "javax.B");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        Path module1 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
        Path module2 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        Path jmod = JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                .addClassPath(module1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                .jmod(helper.createNewJmodFile(moduleName))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                .create()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        Path jar = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName + ".jar"), module2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        Path newJar = helper.getJmodDir().resolve(jar.getFileName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        Files.move(jar, newJar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            helper.generateDefaultImage(moduleName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                    .assertFailure("Error: Two versions of module dupRes2Jmod found in");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            deleteDirectory(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            deleteDirectory(newJar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
    public void testDuplicateModule3() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        String moduleName1 = "dupRes3Jar1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        String moduleName2 = "dupRes3Jar2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        List<String> classNames = Arrays.asList("java.A", "javax.B");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
        Path module1 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName1, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        Path module2 = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName2, classNames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   335
            ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName1)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                    .requires("java.base").build(), out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
        Path jar1 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName1 + ".jar"), module1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        Path jar2 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName2 + ".jar"), module2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            helper.generateDefaultImage(moduleName1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                    .assertFailure("Error: Two versions of module dupRes3Jar1 found in");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            deleteDirectory(jar1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            deleteDirectory(jar2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
}