jdk/test/sun/misc/InvokeCleaner.java
author lana
Fri, 09 Jun 2017 21:34:51 +0000
changeset 45482 78b5361fba3d
parent 42707 6059f74bacdf
permissions -rw-r--r--
Added tag jdk-10+11 for changeset d2cd50ed7a23
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42707
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     1
/*
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     4
 *
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     8
 *
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    13
 * accompanied this code).
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    14
 *
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    18
 *
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    21
 * questions.
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    22
 */
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    23
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    24
/* @test
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    25
 * @bug 8171377
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    26
 * @summary Basic test for Unsafe::invokeCleaner
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    27
 * @modules jdk.unsupported
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    28
 * @run testng/othervm InvokeCleaner
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    29
 */
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    30
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    31
import java.io.Closeable;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    32
import java.lang.reflect.Field;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    33
import java.nio.ByteBuffer;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    34
import java.nio.MappedByteBuffer;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    35
import java.nio.channels.FileChannel;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    36
import java.nio.file.Files;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    37
import java.nio.file.Path;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    38
import java.nio.file.Paths;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    39
import java.util.ArrayList;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    40
import java.util.List;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    41
import sun.misc.Unsafe;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    42
import org.testng.annotations.AfterClass;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    43
import org.testng.annotations.BeforeClass;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    44
import org.testng.annotations.Test;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    45
import org.testng.annotations.DataProvider;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    46
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    47
public class InvokeCleaner {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    48
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    49
    static Unsafe UNSAFE;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    50
    static Path bob = Paths.get("bob");
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    51
    static List<Closeable> closeables = new ArrayList<>();
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    52
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    53
    @BeforeClass
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    54
    static void init() throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    55
        UNSAFE = getUnsafe();
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    56
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    57
        byte[] srcData = new byte[20];
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    58
        for (int i=0; i<20; i++)
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    59
            srcData[i] = (byte)i;
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    60
        Files.write(bob, srcData);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    61
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    62
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    63
    @DataProvider(name = "badBuffers")
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    64
    static Object[][] createBadBuffers() throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    65
        FileChannel fc = FileChannel.open(bob);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    66
        closeables.add(fc);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    67
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    68
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    69
        return new Object[][] {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    70
                { ByteBuffer.allocate(0) },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    71
                { ByteBuffer.allocate(10) },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    72
                { ByteBuffer.allocate(10).duplicate() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    73
                { ByteBuffer.allocate(10).slice() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    74
                { ByteBuffer.allocateDirect(10).duplicate() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    75
                { ByteBuffer.allocateDirect(10).slice() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    76
                { ByteBuffer.allocateDirect(0).duplicate() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    77
                { ByteBuffer.allocateDirect(0).slice() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    78
                { mbb.duplicate() },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    79
                { mbb.slice() }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    80
        };
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    81
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    82
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    83
    @Test(dataProvider="badBuffers",
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    84
          expectedExceptions = IllegalArgumentException.class)
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    85
    public void badBuffers(ByteBuffer buffer) throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    86
        UNSAFE.invokeCleaner(buffer);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    87
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    88
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    89
    @DataProvider(name = "goodBuffers")
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    90
    static Object[][] createGoodBuffers() throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    91
        FileChannel fc = FileChannel.open(bob);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    92
        closeables.add(fc);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    93
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    94
        mbb.load();
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    95
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    96
        return new Object[][] {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    97
                { ByteBuffer.allocateDirect(0) },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    98
                { ByteBuffer.allocateDirect(10) },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
    99
                { mbb },
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   100
                { fc.map(FileChannel.MapMode.READ_ONLY, 1, 11) }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   101
        };
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   102
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   103
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   104
    @Test(dataProvider="goodBuffers")
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   105
    public void goodBuffers(ByteBuffer buffer) throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   106
        UNSAFE.invokeCleaner(buffer);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   107
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   108
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   109
    @Test(expectedExceptions = NullPointerException.class)
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   110
    public void npe() throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   111
        UNSAFE.invokeCleaner(null);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   112
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   113
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   114
    static Unsafe getUnsafe() throws ReflectiveOperationException {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   115
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   116
        f.setAccessible(true);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   117
        return (Unsafe)f.get(null);
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   118
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   119
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   120
    @AfterClass
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   121
    public void cleanup() throws Exception {
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   122
        for(Closeable fc : closeables)
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   123
            fc.close();
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   124
    }
6059f74bacdf 8171377: Add sun.misc.Unsafe::invokeCleaner
chegar
parents:
diff changeset
   125
}