jdk/test/tools/jlink/asmplugin/NegativeTest.java
author sundar
Mon, 16 May 2016 14:47:27 +0530
changeset 38320 e24c7029e8ba
parent 36511 9d0388c6b336
permissions -rw-r--r--
8156914: jlink API minor cleanups Reviewed-by: mchung
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
 * Asm plugin testing.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @summary Test basic functionality.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @author Andrei Eremeev
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.org.objectweb.asm
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.internal.plugins.asm
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @build AsmPluginTestBase
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @run main NegativeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.internal.org.objectweb.asm.ClassReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassVisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.internal.org.objectweb.asm.Opcodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.plugin.Plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    46
import jdk.tools.jlink.internal.ModulePoolImpl;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.tools.jlink.internal.StringTable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.tools.jlink.internal.plugins.asm.AsmGlobalPool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.tools.jlink.internal.plugins.asm.AsmModulePool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import jdk.tools.jlink.internal.plugins.asm.AsmPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.tools.jlink.internal.plugins.asm.AsmPool.ResourceFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.tools.jlink.internal.plugins.asm.AsmPools;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.tools.jlink.plugin.PluginException;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    54
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
public class NegativeTest extends AsmPluginTestBase {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        if (!isImageBuild()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            System.err.println("Test not run. Not image build.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        new NegativeTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        testNull();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        testUnknownPackage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    private void testUnknownPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        AsmPlugin t = new AsmPlugin() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            public void visit(AsmPools pools) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                    AsmGlobalPool globalPool = pools.getGlobalPool();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                    AsmModulePool javabase = pools.getModulePool("java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                    ClassReader cr = new ClassReader(NegativeTest.class.getResourceAsStream("NegativeTest.class"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                    cr.accept(new RenameClassVisitor(cw), ClassReader.EXPAND_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                    action(() -> globalPool.getTransformedClasses().addClass(cw),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                    action(() -> javabase.getTransformedClasses().addClass(cw),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                    ResourceFile newResFile = new ResourceFile("java/aaa/file", new byte[0]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                    action(() -> globalPool.getTransformedResourceFiles().addResourceFile(newResFile),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                    action(() -> javabase.getTransformedResourceFiles().addResourceFile(newResFile),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                    action(() -> globalPool.getTransformedClasses().forgetClass("java/aaa/file"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                    action(() -> javabase.getTransformedClasses().forgetClass("java/aaa/file"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                    action(() -> globalPool.getTransformedResourceFiles().forgetResourceFile("java/aaa/file"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                    action(() -> javabase.getTransformedResourceFiles().forgetResourceFile("java/aaa/file"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                            "Unknown package", PluginException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                   throw new UncheckedIOException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        };
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   105
        ModulePool resources = new ModulePoolImpl(ByteOrder.BIG_ENDIAN, new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                throw new UnsupportedOperationException("Not supported yet.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        t.visit(getPool(), resources);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    private static class RenameClassVisitor extends ClassVisitor {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        public RenameClassVisitor(ClassWriter cv) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            super(Opcodes.ASM5, cv);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            super.visit(version, access, "RENAMED", signature, superName, interfaces);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    private void testNull() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        AsmPlugin t = new AsmPlugin() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            public void visit(AsmPools pools) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                action(() -> pools.getModulePool(null), "Module name is null", NullPointerException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                action(() -> pools.fillOutputResources(null), "Output resource is null", NullPointerException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        };
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   139
        ModulePool resources = new ModulePoolImpl(ByteOrder.BIG_ENDIAN, new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                throw new UnsupportedOperationException("Not supported yet.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        action(() -> t.visit(null, resources), "Input resource is null", NullPointerException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        action(() -> t.visit(resources, null), "Output resource is null", NullPointerException.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        t.visit(resources, resources);
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
    private void action(Action action, String message, Class<? extends Exception> expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            System.err.println("Testing: " + message);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            action.call();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            throw new AssertionError(message + ": should have failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            if (!expected.isInstance(e)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                throw new RuntimeException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                System.err.println("Got exception as expected: " + e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
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
    private interface Action {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        void call() throws Exception;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
}