Path: blob/master/test/jdk/sun/misc/InvokeCleaner.java
41145 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 817137725* @summary Basic test for Unsafe::invokeCleaner26* @modules jdk.unsupported27* @run testng/othervm InvokeCleaner28*/2930import java.io.Closeable;31import java.lang.reflect.Field;32import java.nio.ByteBuffer;33import java.nio.MappedByteBuffer;34import java.nio.channels.FileChannel;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.ArrayList;39import java.util.List;40import sun.misc.Unsafe;41import org.testng.annotations.AfterClass;42import org.testng.annotations.BeforeClass;43import org.testng.annotations.Test;44import org.testng.annotations.DataProvider;4546public class InvokeCleaner {4748static Unsafe UNSAFE;49static Path bob = Paths.get("bob");50static List<Closeable> closeables = new ArrayList<>();5152@BeforeClass53static void init() throws Exception {54UNSAFE = getUnsafe();5556byte[] srcData = new byte[20];57for (int i=0; i<20; i++)58srcData[i] = (byte)i;59Files.write(bob, srcData);60}6162@DataProvider(name = "badBuffers")63static Object[][] createBadBuffers() throws Exception {64FileChannel fc = FileChannel.open(bob);65closeables.add(fc);66MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);6768return new Object[][] {69{ ByteBuffer.allocate(0) },70{ ByteBuffer.allocate(10) },71{ ByteBuffer.allocate(10).duplicate() },72{ ByteBuffer.allocate(10).slice() },73{ ByteBuffer.allocateDirect(10).duplicate() },74{ ByteBuffer.allocateDirect(10).slice() },75{ ByteBuffer.allocateDirect(0).duplicate() },76{ ByteBuffer.allocateDirect(0).slice() },77{ mbb.duplicate() },78{ mbb.slice() }79};80}8182@Test(dataProvider="badBuffers",83expectedExceptions = IllegalArgumentException.class)84public void badBuffers(ByteBuffer buffer) throws Exception {85UNSAFE.invokeCleaner(buffer);86}8788@DataProvider(name = "goodBuffers")89static Object[][] createGoodBuffers() throws Exception {90FileChannel fc = FileChannel.open(bob);91closeables.add(fc);92MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);93mbb.load();9495return new Object[][] {96{ ByteBuffer.allocateDirect(0) },97{ ByteBuffer.allocateDirect(10) },98{ mbb },99{ fc.map(FileChannel.MapMode.READ_ONLY, 1, 11) }100};101}102103@Test(dataProvider="goodBuffers")104public void goodBuffers(ByteBuffer buffer) throws Exception {105UNSAFE.invokeCleaner(buffer);106}107108@Test(expectedExceptions = NullPointerException.class)109public void npe() throws Exception {110UNSAFE.invokeCleaner(null);111}112113static Unsafe getUnsafe() throws ReflectiveOperationException {114Field f = Unsafe.class.getDeclaredField("theUnsafe");115f.setAccessible(true);116return (Unsafe)f.get(null);117}118119@AfterClass120public void cleanup() throws Exception {121for(Closeable fc : closeables)122fc.close();123}124}125126127