1 /* 2 * Copyright (c) 2021, 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 * @bug 4833719 27 * @summary Verify MappedByteBuffer force on compact, duplicate, and slice views 28 * @run testng ForceViews 29 */ 30 package test.java.nio.MappedByteBuffer; 31 32 import java.io.IOException; 33 import java.nio.ByteBuffer; 34 import java.nio.MappedByteBuffer; 35 import java.nio.ReadOnlyBufferException; 36 import java.nio.channels.FileChannel; 37 import static java.nio.channels.FileChannel.MapMode.*; 38 import java.nio.file.Path; 39 import static java.nio.file.StandardOpenOption.*; 40 import java.util.function.BiFunction; 41 42 import org.testng.Assert; 43 import org.testng.annotations.AfterTest; 44 import org.testng.annotations.BeforeTest; 45 import org.testng.annotations.DataProvider; 46 import org.testng.annotations.Test; 47 48 public class ForceViews { 49 Segment(int position, int length)50 static record Segment(int position, int length) {} 51 52 private FileChannel fc; 53 54 // Android-changed: TestNgRunner recognizes this method as a test method. 55 // @BeforeTest(alwaysRun=true) openChannel()56 private void openChannel() throws IOException { 57 // Android-changed: test.src is a read-only directory on Android. 58 // Path file = Path.of(System.getProperty("test.src", "."), "junk"); 59 Path file = Path.of(System.getProperty("java.io.tmpdir", "."), ForceViews.class.getName()); 60 fc = FileChannel.open(file, CREATE_NEW, READ, WRITE, DELETE_ON_CLOSE); 61 ByteBuffer buf = ByteBuffer.wrap(new byte[1024]); 62 fc.write(buf); 63 fc.position(0); 64 } 65 66 // Android-changed: TestNgRunner recognizes this method as a test method. 67 // @AfterTest(alwaysRun=true) closeChannel()68 private void closeChannel() throws IOException { 69 fc.close(); 70 } 71 72 @DataProvider provider()73 public Object[][] provider() throws IOException { 74 BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> absSlice = 75 (m, s) -> { return m.slice(s.position, s.length); }; 76 BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> relSlice = 77 (m, s) -> { m.position(s.position); m.limit(s.position + s.length); 78 // Android-changed: Explicit casting due to @CovariantReturnType methods. 79 return (MappedByteBuffer) m.slice(); }; 80 BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> duplicate= 81 // Android-changed: Explicit casting due to @CovariantReturnType methods. 82 (m, s) -> { return (MappedByteBuffer) m.duplicate(); }; 83 BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> compact = 84 // Android-changed: Explicit casting due to @CovariantReturnType methods. 85 (m, s) -> { return (MappedByteBuffer) m.compact(); }; 86 87 Object[][] result = new Object[][] { 88 {"Absolute slice", fc, 256, 512, 128, 128, 32, 32, absSlice}, 89 {"Relative slice", fc, 256, 512, 0, 128, 32, 32, relSlice}, 90 {"Duplicate", fc, 256, 512, 0, 256, 32, 32, duplicate}, 91 {"Compact", fc, 256, 512, 0, 256, 32, 32, compact} 92 }; 93 94 return result; 95 } 96 97 // Android-changed: TestNgRunner isn't able to run this parameterized test. 98 // @Test(dataProvider = "provider") test(String tst, FileChannel fc, int mapPosition, int mapLength, int sliceIndex, int sliceLength, int regionOffset, int regionLength, BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> f)99 private void test(String tst, FileChannel fc, int mapPosition, int mapLength, 100 int sliceIndex, int sliceLength, int regionOffset, int regionLength, 101 BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> f) 102 throws Exception { 103 MappedByteBuffer mbb = fc.map(READ_WRITE, mapPosition, mapLength); 104 mbb = f.apply(mbb, new Segment(sliceIndex, sliceLength)); 105 for (int i = regionOffset; i < regionOffset + regionLength; i++) { 106 mbb.put(i, (byte)i); 107 } 108 mbb.force(regionOffset, regionOffset + regionLength); 109 110 int fcPos = mapPosition + sliceIndex + regionOffset; 111 int mbbPos = regionOffset; 112 int length = regionLength; 113 114 ByteBuffer buf = ByteBuffer.allocate(length); 115 fc.position(fcPos); 116 fc.read(buf); 117 for (int i = 0; i < length; i++) { 118 int fcVal = buf.get(i); 119 int mbbVal = mbb.get(mbbPos + i); 120 int val = regionOffset + i; 121 Assert.assertTrue(fcVal == val && mbbVal == val, 122 String.format("%s: i %d, fcVal %d, mbbVal %d, val %d", 123 tst, i, fcVal, mbbVal, val)); 124 } 125 } 126 127 // BEGIN Android-changed: TestNgRunner recognizes @BeforeTest and @AfterTest methods correctly. assertTest(int index)128 private void assertTest(int index) throws Exception { 129 Object[] o = provider()[index]; 130 openChannel(); 131 try { 132 test((String) o[0], fc, (int) o[2], (int) o[3], (int) o[4], (int) o[5], (int) o[6], 133 (int) o[7], (BiFunction<MappedByteBuffer, Segment, MappedByteBuffer>) o[8]); 134 } finally { 135 closeChannel(); 136 } 137 } 138 139 @Test testAbsoluteSlice()140 public void testAbsoluteSlice() throws Exception { 141 assertTest(0); 142 } 143 @Test testRelativeSlice()144 public void testRelativeSlice() throws Exception { 145 assertTest(0); 146 } 147 @Test testDuplicate()148 public void testDuplicate() throws Exception { 149 assertTest(0); 150 } 151 @Test testCompact()152 public void testCompact() throws Exception { 153 assertTest(0); 154 } 155 // END Android-changed: TestNgRunner recognizes @BeforeTest and @AfterTest methods correctly. 156 } 157