Path: blob/master/test/jdk/java/io/Reader/Skip.java
41149 views
/*1* Copyright (c) 1998, 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/* @test24* @bug 4134311 824791825* @summary Test if skip works correctly26* @run testng Skip27*/2829import java.io.CharArrayReader;30import java.io.File;31import java.io.FileReader;32import java.io.IOException;33import java.io.LineNumberReader;34import java.io.PushbackReader;35import java.io.RandomAccessFile;36import java.io.Reader;37import java.io.StringReader;3839import org.testng.Assert;40import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;4243public class Skip {44private static String FILENAME =45System.getProperty("test.src", ".") + File.separator + "SkipInput.txt";46private static File file = new File(FILENAME);4748@Test49public void skip() throws IOException {50try (FileReader fr = new FileReader(file)) {51long nchars = 8200;52long actual = fr.skip(nchars);5354Assert.assertFalse(actual > nchars,55"Should skip " + nchars + ", but skipped " +actual+" chars");56}57}5859@DataProvider(name = "readers")60public Object[][] getReaders() throws IOException {61return new Object[][] {62{new LineNumberReader(new FileReader(file))},63{new CharArrayReader(new char[] {27})},64{new PushbackReader(new FileReader(file))},65{new FileReader(file)},66{new StringReader(new String(new byte[] {(byte)42}))}67};68}6970@Test(dataProvider = "readers")71public void eof(Reader r) throws IOException {72r.skip(Long.MAX_VALUE);73Assert.assertEquals(r.skip(1), 0);74Assert.assertEquals(r.read(), -1);75}7677@DataProvider(name = "skipIAE")78public Object[][] getSkipIAEs() throws IOException {79return new Object[][] {80{new LineNumberReader(new FileReader(file))},81{new PushbackReader(new FileReader(file))},82{new FileReader(file)}83};84}8586@Test(dataProvider = "skipIAE", expectedExceptions = IllegalArgumentException.class)87public void testThrowsIAE(Reader r) throws IOException {88r.skip(-1);89}9091@DataProvider(name = "skipNoIAE")92public Object[][] getSkipNoIAEs() throws IOException {93return new Object[][] {94{new CharArrayReader(new char[] {27})},95{new StringReader(new String(new byte[] {(byte)42}))}96};97}9899@Test(dataProvider = "skipNoIAE")100public void testNoIAE(Reader r) throws IOException {101r.skip(-1);102}103}104105106