|
1 | 1 | /* |
2 | | - * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | /* @test |
25 | | - @bug 4134311 |
26 | | - @summary Test if skip works correctly |
| 25 | + * @bug 4134311 8247918 |
| 26 | + * @summary Test if skip works correctly |
| 27 | + * @run testng Skip |
27 | 28 | */ |
28 | 29 |
|
| 30 | +import java.io.CharArrayReader; |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileReader; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.LineNumberReader; |
| 35 | +import java.io.PushbackReader; |
| 36 | +import java.io.RandomAccessFile; |
| 37 | +import java.io.Reader; |
| 38 | +import java.io.StringReader; |
29 | 39 |
|
30 | | - |
31 | | -import java.io.*; |
| 40 | +import org.testng.Assert; |
| 41 | +import org.testng.annotations.DataProvider; |
| 42 | +import org.testng.annotations.Test; |
32 | 43 |
|
33 | 44 | public class Skip { |
34 | | - public static void main(String argv[]) throws Exception { |
35 | | - File f = new File(System.getProperty("test.src", "."), |
36 | | - "SkipInput.txt"); |
37 | | - FileReader fr = new FileReader(f); |
38 | | - try { |
| 45 | + private static String FILENAME = |
| 46 | + System.getProperty("test.src", ".") + File.separator + "SkipInput.txt"; |
| 47 | + private static File file = new File(FILENAME); |
| 48 | + |
| 49 | + @Test |
| 50 | + public void skip() throws IOException { |
| 51 | + try (FileReader fr = new FileReader(file)) { |
39 | 52 | long nchars = 8200; |
40 | 53 | long actual = fr.skip(nchars); |
41 | 54 |
|
42 | | - if (actual > nchars) { |
43 | | - throw new Exception |
44 | | - ("Should skip " + nchars + ", but skipped " +actual+" chars"); |
45 | | - } |
46 | | - } finally { |
47 | | - fr.close(); |
| 55 | + Assert.assertFalse(actual > nchars, |
| 56 | + "Should skip " + nchars + ", but skipped " +actual+" chars"); |
48 | 57 | } |
49 | 58 | } |
| 59 | + |
| 60 | + @DataProvider(name = "readers") |
| 61 | + public Object[][] getReaders() throws IOException { |
| 62 | + return new Object[][] { |
| 63 | + {new LineNumberReader(new FileReader(file))}, |
| 64 | + {new CharArrayReader(new char[] {27})}, |
| 65 | + {new PushbackReader(new FileReader(file))}, |
| 66 | + {new FileReader(file)}, |
| 67 | + {new StringReader(new String(new byte[] {(byte)42}))} |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + @Test(dataProvider = "readers") |
| 72 | + public void eof(Reader r) throws IOException { |
| 73 | + r.skip(Long.MAX_VALUE); |
| 74 | + Assert.assertEquals(r.skip(1), 0); |
| 75 | + Assert.assertEquals(r.read(), -1); |
| 76 | + } |
| 77 | + |
| 78 | + @DataProvider(name = "skipIAE") |
| 79 | + public Object[][] getSkipIAEs() throws IOException { |
| 80 | + return new Object[][] { |
| 81 | + {new LineNumberReader(new FileReader(file))}, |
| 82 | + {new PushbackReader(new FileReader(file))}, |
| 83 | + {new FileReader(file)} |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + @Test(dataProvider = "skipIAE", expectedExceptions = IllegalArgumentException.class) |
| 88 | + public void testThrowsIAE(Reader r) throws IOException { |
| 89 | + r.skip(-1); |
| 90 | + } |
| 91 | + |
| 92 | + @DataProvider(name = "skipNoIAE") |
| 93 | + public Object[][] getSkipNoIAEs() throws IOException { |
| 94 | + return new Object[][] { |
| 95 | + {new CharArrayReader(new char[] {27})}, |
| 96 | + {new StringReader(new String(new byte[] {(byte)42}))} |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + @Test(dataProvider = "skipNoIAE") |
| 101 | + public void testNoIAE(Reader r) throws IOException { |
| 102 | + r.skip(-1); |
| 103 | + } |
50 | 104 | } |
0 commit comments