Skip to content

Commit 2ed769a

Browse files
committed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1147907 13f79535-47bb-0310-9956-ffa450edef68
1 parent 02dc8c7 commit 2ed769a

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.io.CharArrayReader;
1923
import java.io.IOException;
2024
import java.io.StringReader;
25+
import java.nio.charset.Charset;
2126
import java.util.Random;
2227

23-
import junit.framework.TestCase;
28+
import org.junit.Ignore;
29+
import org.junit.Test;
2430

25-
public class ReaderInputStreamTest extends TestCase {
31+
public class ReaderInputStreamTest {
2632
private static final String TEST_STRING = "\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre";
2733
private static final String LARGE_TEST_STRING;
2834

@@ -73,29 +79,61 @@ private void testWithBufferedRead(String testString, String charsetName) throws
7379
}
7480
}
7581

82+
@Test
7683
public void testUTF8WithSingleByteRead() throws IOException {
7784
testWithSingleByteRead(TEST_STRING, "UTF-8");
7885
}
7986

87+
@Test
8088
public void testLargeUTF8WithSingleByteRead() throws IOException {
8189
testWithSingleByteRead(LARGE_TEST_STRING, "UTF-8");
8290
}
8391

92+
@Test
8493
public void testUTF8WithBufferedRead() throws IOException {
8594
testWithBufferedRead(TEST_STRING, "UTF-8");
8695
}
8796

97+
@Test
8898
public void testLargeUTF8WithBufferedRead() throws IOException {
8999
testWithBufferedRead(LARGE_TEST_STRING, "UTF-8");
90100
}
91101

102+
@Test
92103
public void testUTF16WithSingleByteRead() throws IOException {
93104
testWithSingleByteRead(TEST_STRING, "UTF-16");
94105
}
95106

107+
@Test
96108
public void testReadZero() throws Exception {
97109
ReaderInputStream r = new ReaderInputStream(new StringReader("test"));
98110
byte[] bytes = new byte[30];
99111
assertEquals(0, r.read(bytes, 0, 0));
100112
}
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+
}
101139
}

0 commit comments

Comments
 (0)