|
16 | 16 | */ |
17 | 17 | package org.apache.commons.io.input; |
18 | 18 |
|
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.io.CharArrayReader; |
19 | 23 | import java.io.IOException; |
20 | 24 | import java.io.StringReader; |
| 25 | +import java.nio.charset.Charset; |
21 | 26 | import java.util.Random; |
22 | 27 |
|
23 | | -import junit.framework.TestCase; |
| 28 | +import org.junit.Ignore; |
| 29 | +import org.junit.Test; |
24 | 30 |
|
25 | | -public class ReaderInputStreamTest extends TestCase { |
| 31 | +public class ReaderInputStreamTest { |
26 | 32 | private static final String TEST_STRING = "\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre"; |
27 | 33 | private static final String LARGE_TEST_STRING; |
28 | 34 |
|
@@ -73,29 +79,61 @@ private void testWithBufferedRead(String testString, String charsetName) throws |
73 | 79 | } |
74 | 80 | } |
75 | 81 |
|
| 82 | + @Test |
76 | 83 | public void testUTF8WithSingleByteRead() throws IOException { |
77 | 84 | testWithSingleByteRead(TEST_STRING, "UTF-8"); |
78 | 85 | } |
79 | 86 |
|
| 87 | + @Test |
80 | 88 | public void testLargeUTF8WithSingleByteRead() throws IOException { |
81 | 89 | testWithSingleByteRead(LARGE_TEST_STRING, "UTF-8"); |
82 | 90 | } |
83 | 91 |
|
| 92 | + @Test |
84 | 93 | public void testUTF8WithBufferedRead() throws IOException { |
85 | 94 | testWithBufferedRead(TEST_STRING, "UTF-8"); |
86 | 95 | } |
87 | 96 |
|
| 97 | + @Test |
88 | 98 | public void testLargeUTF8WithBufferedRead() throws IOException { |
89 | 99 | testWithBufferedRead(LARGE_TEST_STRING, "UTF-8"); |
90 | 100 | } |
91 | 101 |
|
| 102 | + @Test |
92 | 103 | public void testUTF16WithSingleByteRead() throws IOException { |
93 | 104 | testWithSingleByteRead(TEST_STRING, "UTF-16"); |
94 | 105 | } |
95 | 106 |
|
| 107 | + @Test |
96 | 108 | public void testReadZero() throws Exception { |
97 | 109 | ReaderInputStream r = new ReaderInputStream(new StringReader("test")); |
98 | 110 | byte[] bytes = new byte[30]; |
99 | 111 | assertEquals(0, r.read(bytes, 0, 0)); |
100 | 112 | } |
| 113 | + |
| 114 | + /** |
| 115 | + * Tests https://issues.apache.org/jira/browse/IO-277 |
| 116 | + * |
| 117 | + * @throws IOException |
| 118 | + */ |
| 119 | + @Test |
| 120 | + @Ignore |
| 121 | + public void testCharsetMismatchInfiniteLoop() throws IOException { |
| 122 | + // Input is UTF-8 bytes: 0xE0 0xB2 0xA0 |
| 123 | + byte[] input = new byte[] { (byte) 0xE0, (byte) 0xB2, (byte) 0xA0 }; |
| 124 | + char[] inputChars = new char[] { (char) 0xE0, (char) 0xB2, (char) 0xA0 }; |
| 125 | + System.out.println("Input: " + new String(input, Charset.forName("UTF-8"))); |
| 126 | + |
| 127 | + // Charset charset = Charset.forName("UTF-8"); // works |
| 128 | + Charset charset = Charset.forName("ASCII"); // infinite loop |
| 129 | + |
| 130 | + ReaderInputStream stream = new ReaderInputStream(new CharArrayReader(inputChars), charset); |
| 131 | + try { |
| 132 | + while (stream.read() != -1) { |
| 133 | + } |
| 134 | + } finally { |
| 135 | + stream.close(); |
| 136 | + } |
| 137 | + |
| 138 | + } |
101 | 139 | } |
0 commit comments