Path: blob/master/test/jdk/java/io/CharArrayReader/ReadCharBuffer.java
41152 views
/*1* Copyright (c) 2021, 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/*24* @test25* @bug 492631426* @summary Test for CharArrayReader#read(CharBuffer).27* @run testng ReadCharBuffer28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;323334import java.io.CharArrayReader;35import java.io.IOException;36import java.io.Reader;37import java.nio.ByteBuffer;38import java.nio.CharBuffer;39import java.util.Arrays;4041import static org.testng.Assert.assertEquals;4243public class ReadCharBuffer {4445private static final int BUFFER_SIZE = 7;4647@DataProvider(name = "buffers")48public Object[][] createBuffers() {49// test both on-heap and off-heap buffers as they may use different code paths50return new Object[][]{51new Object[]{CharBuffer.allocate(BUFFER_SIZE)},52new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}53};54}5556@Test(dataProvider = "buffers")57public void read(CharBuffer buffer) throws IOException {58fillBuffer(buffer);5960try (Reader reader = new CharArrayReader("ABCD".toCharArray())) {61buffer.limit(3);62buffer.position(1);63assertEquals(reader.read(buffer), 2);64assertEquals(buffer.position(), 3);65assertEquals(buffer.limit(), 3);6667buffer.limit(7);68buffer.position(4);69assertEquals(reader.read(buffer), 2);70assertEquals(buffer.position(), 6);71assertEquals(buffer.limit(), 7);7273assertEquals(reader.read(buffer), -1);74}7576buffer.clear();77assertEquals(buffer.toString(), "xABxCDx");78}7980private void fillBuffer(CharBuffer buffer) {81char[] filler = new char[BUFFER_SIZE];82Arrays.fill(filler, 'x');83buffer.put(filler);84buffer.clear();85}8687}888990