You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/01/28 13:08:04 UTC
svn commit: r615857 [5/5] -
/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamWriterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamWriterTest.java?rev=615857&r1=615856&r2=615857&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamWriterTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamWriterTest.java Mon Jan 28 04:07:47 2008
@@ -1,726 +1,702 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.harmony.luni.tests.java.io;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetEncoder;
-
-import junit.framework.TestCase;
-
-/**
- *
- */
-public class OutputStreamWriterTest extends TestCase {
-
- private static final int UPPER = 0xd800;
-
- private static final int BUFFER_SIZE = 10000;
-
- private ByteArrayOutputStream out;
-
- private OutputStreamWriter writer;
-
- static private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
-
- static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII",
- "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" };
-
- OutputStreamWriter osw;
-
- InputStreamReader isr;
-
- private ByteArrayOutputStream fos;
-
- String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
-
- /*
- * @see TestCase#setUp()
- */
- protected void setUp() throws Exception {
- super.setUp();
- out = new ByteArrayOutputStream();
- writer = new OutputStreamWriter(out, "utf-8");
-
- fos = new ByteArrayOutputStream();
- osw = new OutputStreamWriter(fos);
- }
-
- /*
- * @see TestCase#tearDown()
- */
- protected void tearDown() throws Exception {
- try {
- writer.close();
-
- if (isr != null)
- isr.close();
- osw.close();
- } catch (Exception e) {
- }
-
- super.tearDown();
- }
-
- public void testClose() throws Exception {
- writer.flush();
- writer.close();
- try {
- writer.flush();
- fail();
- } catch (IOException e) {
- }
- }
-
- public void testFlush() throws Exception {
- writer.write(source);
- writer.flush();
- String result = out.toString("utf-8");
- assertEquals(source, result);
- }
-
- /*
- * Class under test for void write(char[], int, int)
- */
- public void testWritecharArrayintint() throws IOException {
- char[] chars = source.toCharArray();
-
- //throws IndexOutOfBoundsException if offset is negative
- try {
- writer.write((char[]) null, -1, -1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- //throws NullPointerException though count is negative
- try {
- writer.write((char[]) null, 1, -1);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- //expected
- }
-
- try {
- writer.write((char[]) null, 1, 1);
- fail();
- } catch (NullPointerException e) {
- }
- try {
- writer.write(new char[0], 0, 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- }
- try {
- writer.write(chars, -1, 1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- }
- try {
- writer.write(chars, 0, -1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- }
- try {
- writer.write(chars, 1, chars.length);
- fail();
- } catch (IndexOutOfBoundsException e) {
- }
- writer.write(chars, 1, 2);
- writer.flush();
- assertEquals("hi", out.toString("utf-8"));
- writer.write(chars, 0, chars.length);
- writer.flush();
- assertEquals("hi" + source, out.toString("utf-8"));
-
- writer.close();
- //after the stream is closed ,should throw IOException first
- try {
- writer.write((char[]) null, -1, -1);
- fail("should throw IOException");
- } catch (IOException e) {
- //expected
- }
-
- }
-
- /*
- * Class under test for void write(int)
- */
- public void testWriteint() throws IOException {
- writer.write(1);
- writer.flush();
- String str = new String(out.toByteArray(), "utf-8");
- assertEquals("\u0001", str);
-
- writer.write(2);
- writer.flush();
- str = new String(out.toByteArray(), "utf-8");
- assertEquals("\u0001\u0002", str);
-
- writer.write(-1);
- writer.flush();
- str = new String(out.toByteArray(), "utf-8");
- assertEquals("\u0001\u0002\uffff", str);
-
- writer.write(0xfedcb);
- writer.flush();
- str = new String(out.toByteArray(), "utf-8");
- assertEquals("\u0001\u0002\uffff\uedcb", str);
-
- writer.close();
- //after the stream is closed ,should throw IOException
- try {
- writer.write(1);
- fail("should throw IOException");
- } catch (IOException e) {
- //expected
- }
-
-
- }
-
- /*
- * Class under test for void write(String, int, int)
- */
- public void testWriteStringintint() throws IOException {
- try {
- writer.write((String) null, 1, 1);
- fail();
- } catch (NullPointerException e) {
- }
- try {
- writer.write("", 0, 1);
- fail();
- } catch (StringIndexOutOfBoundsException e) {
- }
- try {
- writer.write("abc", -1, 1);
- fail();
- } catch (StringIndexOutOfBoundsException e) {
- }
- try {
- writer.write("abc", 0, -1);
- fail();
- } catch (IndexOutOfBoundsException e) {
- }
- try {
- writer.write("abc", 1, 3);
- fail();
- } catch (StringIndexOutOfBoundsException e) {
- }
-
- //throws IndexOutOfBoundsException before NullPointerException if count is negative
- try {
- writer.write((String) null, -1, -1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- //throws NullPointerException before StringIndexOutOfBoundsException
- try {
- writer.write((String) null, -1, 0);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- //expected
- }
-
- writer.write("abc", 1, 2);
- writer.flush();
- assertEquals("bc", out.toString("utf-8"));
- writer.write(source, 0, source.length());
- writer.flush();
- assertEquals("bc" + source, out.toString("utf-8"));
-
- writer.close();
- //throws IndexOutOfBoundsException first if count is negative
- try {
- writer.write((String) null, 0, -1);
- fail("should throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- //expected
- }
-
- try {
- writer.write((String) null, -1, 0);
- fail("should throw NullPointerException");
- } catch (NullPointerException e) {
- //expected
- }
-
- try {
- writer.write("abc", -1, 0);
- fail("should throw StringIndexOutOfBoundsException");
- } catch (StringIndexOutOfBoundsException e) {
- //expected
- }
-
- //throws IOException
- try {
- writer.write("abc", 0, 1);
- fail("should throw IOException");
- } catch (IOException e) {
- //expected
- }
-
- }
-
- /*
- * Class under test for void OutputStreamWriter(OutputStream)
- */
- public void testOutputStreamWriterOutputStream() throws IOException {
- try {
- writer = new OutputStreamWriter(null);
- fail();
- } catch (NullPointerException e) {
- }
- OutputStreamWriter writer2 = new OutputStreamWriter(out);
- writer2.close();
- }
-
- /*
- * Class under test for void OutputStreamWriter(OutputStream, String)
- */
- public void testOutputStreamWriterOutputStreamString() throws IOException {
- try {
- writer = new OutputStreamWriter(null, "utf-8");
- fail();
- } catch (NullPointerException e) {
- }
- try {
- writer = new OutputStreamWriter(out, "");
- fail();
- } catch (UnsupportedEncodingException e) {
- }
- try {
- writer = new OutputStreamWriter(out, "badname");
- fail();
- } catch (UnsupportedEncodingException e) {
- }
- try {
- writer = new OutputStreamWriter(out, (String) null);
- fail();
- } catch (NullPointerException e) {
- }
- OutputStreamWriter writer2 = new OutputStreamWriter(out, "ascii");
- assertEquals(Charset.forName("ascii"), Charset.forName(writer2
- .getEncoding()));
- writer2.close();
- }
-
- /*
- * Class under test for void OutputStreamWriter(OutputStream)
- */
- public void testOutputStreamWriterOutputStreamCharset() throws IOException {
- Charset cs = Charset.forName("ascii");
- try {
- writer = new OutputStreamWriter(null, cs);
- fail();
- } catch (NullPointerException e) {
- }
- try {
- writer = new OutputStreamWriter(out, (Charset) null);
- fail();
- } catch (NullPointerException e) {
- }
- OutputStreamWriter writer2 = new OutputStreamWriter(out, cs);
- assertEquals(cs, Charset.forName(writer2.getEncoding()));
- writer2.close();
- }
-
- /*
- * Class under test for void OutputStreamWriter(OutputStream, String)
- */
- public void testOutputStreamWriterOutputStreamCharsetEncoder()
- throws IOException {
- Charset cs = Charset.forName("ascii");
- CharsetEncoder enc = cs.newEncoder();
- try {
- writer = new OutputStreamWriter(null, enc);
- fail();
- } catch (NullPointerException e) {
- }
- try {
- writer = new OutputStreamWriter(out, (CharsetEncoder) null);
- fail();
- } catch (NullPointerException e) {
- }
- OutputStreamWriter writer2 = new OutputStreamWriter(out, enc);
- assertEquals(cs, Charset.forName(writer2.getEncoding()));
- writer2.close();
- }
-
- public void testGetEncoding() {
- Charset cs = Charset.forName("utf-8");
- assertEquals(cs, Charset.forName(writer.getEncoding()));
- }
-
- public void testHandleEarlyEOFChar_1() {
- String str = "All work and no play makes Jack a dull boy\n"; //$NON-NLS-1$
- int NUMBER = 2048;
- int j = 0;
- int len = str.length() * NUMBER;
- /* == 88064 *//* NUMBER compulsively written copies of the same string */
- char[] strChars = new char[len];
- for (int i = 0; i < NUMBER; ++i) {
- for (int k = 0; k < str.length(); ++k) {
- strChars[j++] = str.charAt(k);
- }
- }
- File f = null;
- FileWriter fw = null;
- try {
- f = File.createTempFile("ony", "by_one");
- fw = new FileWriter(f);
- fw.write(strChars);
- fw.close();
- InputStreamReader in = null;
- FileInputStream fis = new FileInputStream(f);
- in = new InputStreamReader(fis);
- int b;
- int errors = 0;
- for (int offset = 0; offset < strChars.length; ++offset) {
- b = in.read();
- if (b == -1) {
- fail("Early EOF at offset " + offset + "\n");
- return;
- }
- }
- assertEquals(0, errors);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void testHandleEarlyEOFChar_2() throws IOException {
- int capacity = 65536;
- byte[] bytes = new byte[capacity];
- byte[] bs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
- for (int i = 0; i < bytes.length; i++) {
- bytes[i] = bs[i / 8192];
- }
- String inputStr = new String(bytes);
- int len = inputStr.length();
- File f = File.createTempFile("FileWriterBugTest ", null); //$NON-NLS-1$
- FileWriter writer = new FileWriter(f);
- writer.write(inputStr);
- writer.close();
- long flen = f.length();
-
- FileReader reader = new FileReader(f);
- char[] outChars = new char[capacity];
- int outCount = reader.read(outChars);
- String outStr = new String(outChars, 0, outCount);
-
- f.deleteOnExit();
- assertEquals(len, flen);
- assertEquals(inputStr, outStr);
-
- }
-
- public void testSingleCharIO() throws Exception {
- InputStreamReader isr = null;
- for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
- try {
- out = new ByteArrayOutputStream();
- writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
-
- int upper = UPPER;
- switch (i) {
- case 0:
- upper = 128;
- break;
- case 1:
- upper = 256;
- break;
- }
-
- for (int c = 0; c < upper; ++c) {
- writer.write(c);
- }
- writer.flush();
- byte[] result = out.toByteArray();
-
- isr = new InputStreamReader(new ByteArrayInputStream(result),
- MINIMAL_CHARSETS[i]);
- for (int expected = 0; expected < upper; ++expected) {
- assertEquals("Error when reading bytes in "
- + MINIMAL_CHARSETS[i], expected, isr.read());
- }
- } finally {
- try {
- isr.close();
- } catch (Exception e) {
- }
- try {
- writer.close();
- } catch (Exception e) {
- }
- }
- }
- }
-
- public void testBlockIO() throws Exception {
- InputStreamReader isr = null;
- char[] largeBuffer = new char[BUFFER_SIZE];
- for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
- try {
- out = new ByteArrayOutputStream();
- writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
-
- int upper = UPPER;
- switch (i) {
- case 0:
- upper = 128;
- break;
- case 1:
- upper = 256;
- break;
- }
-
- int m = 0;
- for (int c = 0; c < upper; ++c) {
- largeBuffer[m++] = (char) c;
- if (m == BUFFER_SIZE) {
- writer.write(largeBuffer);
- m = 0;
- }
- }
- writer.write(largeBuffer, 0, m);
- writer.flush();
- byte[] result = out.toByteArray();
-
- isr = new InputStreamReader(new ByteArrayInputStream(result),
- MINIMAL_CHARSETS[i]);
- int expected = 0, read = 0, j = 0;
- while (expected < upper) {
- if (j == read) {
- read = isr.read(largeBuffer);
- j = 0;
- }
- assertEquals("Error when reading bytes in "
- + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]);
- }
- } finally {
- try {
- isr.close();
- } catch (Exception e) {
- }
- try {
- writer.close();
- } catch (Exception e) {
- }
- }
- }
- }
-
- /**
- * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
- */
- public void test_ConstructorLjava_io_OutputStream() {
- // Test for method java.io.OutputStreamWriter(java.io.OutputStream)
- assertTrue("Used in tests", true);
- }
-
- /**
- * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
- * java.lang.String)
- */
- public void test_ConstructorLjava_io_OutputStreamLjava_lang_String() {
- // Test for method java.io.OutputStreamWriter(java.io.OutputStream,
- // java.lang.String)
- try {
- osw = new OutputStreamWriter(fos, "8859_1");
- } catch (UnsupportedEncodingException e) {
- fail("Unable to create output stream : " + e.getMessage());
- }
- try {
- osw = new OutputStreamWriter(fos, "Bogus");
- } catch (UnsupportedEncodingException e) {
- return;
- }
- fail("Failed to throw Unsupported Encoding exception");
- }
-
- /**
- * @tests java.io.OutputStreamWriter#close()
- */
- public void test_close() {
- // Test for method void java.io.OutputStreamWriter.close()
- boolean exception = false;
- try {
- osw.close();
- osw.write(testString, 0, testString.length());
- } catch (IOException e) {
- exception = true;
- }
- assertTrue("Chars written after close", exception);
-
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- try {
- OutputStreamWriter writer = new OutputStreamWriter(bout,
- "ISO2022JP");
- writer.write(new char[] { 'a' });
- writer.close();
- // the default is ASCII, there should not be any mode changes
- String converted = new String(bout.toByteArray(), "ISO8859_1");
- assertTrue("invalid conversion 1: " + converted, converted
- .equals("a"));
-
- bout.reset();
- writer = new OutputStreamWriter(bout, "ISO2022JP");
- writer.write(new char[] { '\u3048' });
- writer.flush();
- // the byte sequence should not switch to ASCII mode until the
- // stream is closed
- converted = new String(bout.toByteArray(), "ISO8859_1");
- assertTrue("invalid conversion 2: " + converted, converted
- .equals("\u001b$B$("));
- writer.close();
- converted = new String(bout.toByteArray(), "ISO8859_1");
- assertTrue("invalid conversion 3: " + converted, converted
- .equals("\u001b$B$(\u001b(B"));
-
- bout.reset();
- writer = new OutputStreamWriter(bout, "ISO2022JP");
- writer.write(new char[] { '\u3048' });
- writer.write(new char[] { '\u3048' });
- writer.close();
- // there should not be a mode switch between writes
- assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B", new String(bout.toByteArray(),
- "ISO8859_1"));
- } catch (UnsupportedEncodingException e) {
- // Can't test missing converter
- System.out.println(e);
- } catch (IOException e) {
- fail("Unexpected: " + e);
- }
- }
-
- /**
- * @tests java.io.OutputStreamWriter#flush()
- */
- public void test_flush() throws Exception {
- // Test for method void java.io.OutputStreamWriter.flush()
- char[] buf = new char[testString.length()];
- osw.write(testString, 0, testString.length());
- osw.flush();
- openInputStream();
- isr.read(buf, 0, buf.length);
- assertTrue("Chars not flushed", new String(buf, 0, buf.length)
- .equals(testString));
- }
-
- /**
- * @tests java.io.OutputStreamWriter#getEncoding()
- */
- public void test_getEncoding() {
- // Test for method java.lang.String
- // java.io.OutputStreamWriter.getEncoding()
- try {
- osw = new OutputStreamWriter(fos, "8859_1");
- } catch (UnsupportedEncodingException e) {
- assertEquals("Returned incorrect encoding",
- "8859_1", osw.getEncoding());
- }
-
- OutputStreamWriter out = null;
- try {
- out = new OutputStreamWriter(new ByteArrayOutputStream(),
- "UTF-16BE");
- } catch (UnsupportedEncodingException e) {
- fail("Should not throw UnsupportedEncodingException");
- }
- try {
- out.close();
- } catch (IOException e) {
- fail("Should not throw IOException");
- }
- String result = out.getEncoding();
- assertNull(result);
-
-
- out = null;
- try {
- out = new OutputStreamWriter(new ByteArrayOutputStream(),
- "UTF-16BE");
- } catch (UnsupportedEncodingException e) {
- // ok
- }
- result = out.getEncoding();
- assertEquals("UnicodeBigUnmarked", result);
- }
-
- /**
- * @tests java.io.OutputStreamWriter#write(char[], int, int)
- */
- public void test_write$CII() throws Exception {
- // Test for method void java.io.OutputStreamWriter.write(char [], int,
- // int)
- char[] buf = new char[testString.length()];
- osw.write(testString, 0, testString.length());
- osw.close();
- openInputStream();
- isr.read(buf, 0, buf.length);
- assertTrue("Incorrect chars returned", new String(buf, 0,
- buf.length).equals(testString));
- }
-
- /**
- * @tests java.io.OutputStreamWriter#write(int)
- */
- public void test_writeI() throws Exception {
- // Test for method void java.io.OutputStreamWriter.write(int)
- osw.write('T');
- osw.close();
- openInputStream();
- int c = isr.read();
- assertEquals("Incorrect char returned", 'T', (char) c);
- }
-
- /**
- * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int)
- */
- public void test_writeLjava_lang_StringII() throws Exception {
- // Test for method void
- // java.io.OutputStreamWriter.write(java.lang.String, int, int)
-
- char[] buf = new char[testString.length()];
- osw.write(testString, 0, testString.length());
- osw.close();
- openInputStream();
- isr.read(buf);
- assertTrue("Incorrect chars returned", new String(buf, 0,
- buf.length).equals(testString));
- }
-
- private void openInputStream() {
- isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()));
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.luni.tests.java.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+
+import junit.framework.TestCase;
+
+public class OutputStreamWriterTest extends TestCase {
+
+ private static final int UPPER = 0xd800;
+
+ private static final int BUFFER_SIZE = 10000;
+
+ private ByteArrayOutputStream out;
+
+ private OutputStreamWriter writer;
+
+ static private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
+
+ static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII",
+ "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" };
+
+ OutputStreamWriter osw;
+
+ InputStreamReader isr;
+
+ private ByteArrayOutputStream fos;
+
+ String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
+
+ /*
+ * @see TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ out = new ByteArrayOutputStream();
+ writer = new OutputStreamWriter(out, "utf-8");
+
+ fos = new ByteArrayOutputStream();
+ osw = new OutputStreamWriter(fos);
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ try {
+ writer.close();
+
+ if (isr != null) {
+ isr.close();
+ }
+ osw.close();
+ } catch (Exception e) {
+ // Ignored
+ }
+
+ super.tearDown();
+ }
+
+ public void testClose() throws Exception {
+ writer.flush();
+ writer.close();
+ try {
+ writer.flush();
+ fail();
+ } catch (IOException e) {
+ // Expected
+ }
+ }
+
+ public void testFlush() throws Exception {
+ writer.write(source);
+ writer.flush();
+ String result = out.toString("utf-8");
+ assertEquals(source, result);
+ }
+
+ /*
+ * Class under test for void write(char[], int, int)
+ */
+ public void testWritecharArrayintint() throws IOException {
+ char[] chars = source.toCharArray();
+
+ // Throws IndexOutOfBoundsException if offset is negative
+ try {
+ writer.write((char[]) null, -1, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ // throws NullPointerException though count is negative
+ try {
+ writer.write((char[]) null, 1, -1);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ writer.write((char[]) null, 1, 1);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ try {
+ writer.write(new char[0], 0, 1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write(chars, -1, 1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write(chars, 0, -1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write(chars, 1, chars.length);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ writer.write(chars, 1, 2);
+ writer.flush();
+ assertEquals("hi", out.toString("utf-8"));
+ writer.write(chars, 0, chars.length);
+ writer.flush();
+ assertEquals("hi" + source, out.toString("utf-8"));
+
+ writer.close();
+ // After the stream is closed, should throw IOException first
+ try {
+ writer.write((char[]) null, -1, -1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // Expected
+ }
+ }
+
+ /*
+ * Class under test for void write(int)
+ */
+ public void testWriteint() throws IOException {
+ writer.write(1);
+ writer.flush();
+ String str = new String(out.toByteArray(), "utf-8");
+ assertEquals("\u0001", str);
+
+ writer.write(2);
+ writer.flush();
+ str = new String(out.toByteArray(), "utf-8");
+ assertEquals("\u0001\u0002", str);
+
+ writer.write(-1);
+ writer.flush();
+ str = new String(out.toByteArray(), "utf-8");
+ assertEquals("\u0001\u0002\uffff", str);
+
+ writer.write(0xfedcb);
+ writer.flush();
+ str = new String(out.toByteArray(), "utf-8");
+ assertEquals("\u0001\u0002\uffff\uedcb", str);
+
+ writer.close();
+ // After the stream is closed, should throw IOException
+ try {
+ writer.write(1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for void write(String, int, int)
+ */
+ public void testWriteStringintint() throws IOException {
+ try {
+ writer.write((String) null, 1, 1);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ try {
+ writer.write("", 0, 1);
+ fail();
+ } catch (StringIndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write("abc", -1, 1);
+ fail();
+ } catch (StringIndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write("abc", 0, -1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ try {
+ writer.write("abc", 1, 3);
+ fail();
+ } catch (StringIndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ // Throws IndexOutOfBoundsException before NullPointerException if count
+ // is negative
+ try {
+ writer.write((String) null, -1, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ // Throws NullPointerException before StringIndexOutOfBoundsException
+ try {
+ writer.write((String) null, -1, 0);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ writer.write("abc", 1, 2);
+ writer.flush();
+ assertEquals("bc", out.toString("utf-8"));
+ writer.write(source, 0, source.length());
+ writer.flush();
+ assertEquals("bc" + source, out.toString("utf-8"));
+
+ writer.close();
+ // Throws IndexOutOfBoundsException first if count is negative
+ try {
+ writer.write((String) null, 0, -1);
+ fail("should throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ try {
+ writer.write((String) null, -1, 0);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ writer.write("abc", -1, 0);
+ fail("should throw StringIndexOutOfBoundsException");
+ } catch (StringIndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ // Throws IOException
+ try {
+ writer.write("abc", 0, 1);
+ fail("should throw IOException");
+ } catch (IOException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for void OutputStreamWriter(OutputStream)
+ */
+ public void testOutputStreamWriterOutputStream() throws IOException {
+ try {
+ writer = new OutputStreamWriter(null);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ OutputStreamWriter writer2 = new OutputStreamWriter(out);
+ writer2.close();
+ }
+
+ /*
+ * Class under test for void OutputStreamWriter(OutputStream, String)
+ */
+ public void testOutputStreamWriterOutputStreamString() throws IOException {
+ try {
+ writer = new OutputStreamWriter(null, "utf-8");
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ try {
+ writer = new OutputStreamWriter(out, "");
+ fail();
+ } catch (UnsupportedEncodingException e) {
+ // Expected
+ }
+ try {
+ writer = new OutputStreamWriter(out, "badname");
+ fail();
+ } catch (UnsupportedEncodingException e) {
+ // Expected
+ }
+ try {
+ writer = new OutputStreamWriter(out, (String) null);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ OutputStreamWriter writer2 = new OutputStreamWriter(out, "ascii");
+ assertEquals(Charset.forName("ascii"), Charset.forName(writer2
+ .getEncoding()));
+ writer2.close();
+ }
+
+ /*
+ * Class under test for void OutputStreamWriter(OutputStream)
+ */
+ public void testOutputStreamWriterOutputStreamCharset() throws IOException {
+ Charset cs = Charset.forName("ascii");
+ try {
+ writer = new OutputStreamWriter(null, cs);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ try {
+ writer = new OutputStreamWriter(out, (Charset) null);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ OutputStreamWriter writer2 = new OutputStreamWriter(out, cs);
+ assertEquals(cs, Charset.forName(writer2.getEncoding()));
+ writer2.close();
+ }
+
+ /*
+ * Class under test for void OutputStreamWriter(OutputStream, String)
+ */
+ public void testOutputStreamWriterOutputStreamCharsetEncoder()
+ throws IOException {
+ Charset cs = Charset.forName("ascii");
+ CharsetEncoder enc = cs.newEncoder();
+ try {
+ writer = new OutputStreamWriter(null, enc);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ try {
+ writer = new OutputStreamWriter(out, (CharsetEncoder) null);
+ fail();
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ OutputStreamWriter writer2 = new OutputStreamWriter(out, enc);
+ assertEquals(cs, Charset.forName(writer2.getEncoding()));
+ writer2.close();
+ }
+
+ public void testGetEncoding() {
+ Charset cs = Charset.forName("utf-8");
+ assertEquals(cs, Charset.forName(writer.getEncoding()));
+ }
+
+ public void testHandleEarlyEOFChar_1() throws IOException {
+ String str = "All work and no play makes Jack a dull boy\n"; //$NON-NLS-1$
+ int NUMBER = 2048;
+ int j = 0;
+ int len = str.length() * NUMBER;
+ char[] strChars = new char[len];
+ for (int i = 0; i < NUMBER; ++i) {
+ for (int k = 0; k < str.length(); ++k) {
+ strChars[j++] = str.charAt(k);
+ }
+ }
+
+ File f = File.createTempFile("one", "by_one");
+ FileWriter fw = new FileWriter(f);
+ fw.write(strChars);
+ fw.close();
+ FileInputStream fis = new FileInputStream(f);
+ InputStreamReader in = new InputStreamReader(fis);
+ for (int offset = 0; offset < strChars.length; ++offset) {
+ int b = in.read();
+ assertFalse("Early EOF at offset", -1 == b);
+ }
+ }
+
+ public void testHandleEarlyEOFChar_2() throws IOException {
+ int capacity = 65536;
+ byte[] bytes = new byte[capacity];
+ byte[] bs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = bs[i / 8192];
+ }
+ String inputStr = new String(bytes);
+ int len = inputStr.length();
+ File f = File.createTempFile("FileWriterBugTest ", null); //$NON-NLS-1$
+ FileWriter writer = new FileWriter(f);
+ writer.write(inputStr);
+ writer.close();
+ long flen = f.length();
+
+ FileReader reader = new FileReader(f);
+ char[] outChars = new char[capacity];
+ int outCount = reader.read(outChars);
+ String outStr = new String(outChars, 0, outCount);
+
+ f.deleteOnExit();
+ assertEquals(len, flen);
+ assertEquals(inputStr, outStr);
+ }
+
+ public void testSingleCharIO() throws Exception {
+ InputStreamReader isr = null;
+ for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
+ try {
+ out = new ByteArrayOutputStream();
+ writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
+
+ int upper = UPPER;
+ switch (i) {
+ case 0:
+ upper = 128;
+ break;
+ case 1:
+ upper = 256;
+ break;
+ }
+
+ for (int c = 0; c < upper; ++c) {
+ writer.write(c);
+ }
+ writer.flush();
+ byte[] result = out.toByteArray();
+
+ isr = new InputStreamReader(new ByteArrayInputStream(result),
+ MINIMAL_CHARSETS[i]);
+ for (int expected = 0; expected < upper; ++expected) {
+ assertEquals("Error when reading bytes in "
+ + MINIMAL_CHARSETS[i], expected, isr.read());
+ }
+ } finally {
+ try {
+ isr.close();
+ } catch (Exception e) {
+ }
+ try {
+ writer.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ public void testBlockIO() throws Exception {
+ InputStreamReader isr = null;
+ char[] largeBuffer = new char[BUFFER_SIZE];
+ for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
+ try {
+ out = new ByteArrayOutputStream();
+ writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
+
+ int upper = UPPER;
+ switch (i) {
+ case 0:
+ upper = 128;
+ break;
+ case 1:
+ upper = 256;
+ break;
+ }
+
+ int m = 0;
+ for (int c = 0; c < upper; ++c) {
+ largeBuffer[m++] = (char) c;
+ if (m == BUFFER_SIZE) {
+ writer.write(largeBuffer);
+ m = 0;
+ }
+ }
+ writer.write(largeBuffer, 0, m);
+ writer.flush();
+ byte[] result = out.toByteArray();
+
+ isr = new InputStreamReader(new ByteArrayInputStream(result),
+ MINIMAL_CHARSETS[i]);
+ int expected = 0, read = 0, j = 0;
+ while (expected < upper) {
+ if (j == read) {
+ read = isr.read(largeBuffer);
+ j = 0;
+ }
+ assertEquals("Error when reading bytes in "
+ + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]);
+ }
+ } finally {
+ try {
+ isr.close();
+ } catch (Exception e) {
+ }
+ try {
+ writer.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
+ */
+ public void test_ConstructorLjava_io_OutputStream() {
+ assertTrue("Used in tests", true);
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
+ throws UnsupportedEncodingException {
+ osw = new OutputStreamWriter(fos, "8859_1");
+ try {
+ osw = new OutputStreamWriter(fos, "Bogus");
+ fail("Failed to throw Unsupported Encoding exception");
+ } catch (UnsupportedEncodingException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#close()
+ */
+ public void test_close() throws IOException {
+ osw.close();
+
+ try {
+ osw.write(testString, 0, testString.length());
+ fail("Chars written after close");
+ } catch (IOException e) {
+ // Expected
+ }
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ try {
+ OutputStreamWriter writer = new OutputStreamWriter(bout,
+ "ISO2022JP");
+ writer.write(new char[] { 'a' });
+ writer.close();
+ // the default is ASCII, there should not be any mode changes
+ String converted = new String(bout.toByteArray(), "ISO8859_1");
+ assertTrue("invalid conversion 1: " + converted, converted
+ .equals("a"));
+
+ bout.reset();
+ writer = new OutputStreamWriter(bout, "ISO2022JP");
+ writer.write(new char[] { '\u3048' });
+ writer.flush();
+ // the byte sequence should not switch to ASCII mode until the
+ // stream is closed
+ converted = new String(bout.toByteArray(), "ISO8859_1");
+ assertTrue("invalid conversion 2: " + converted, converted
+ .equals("\u001b$B$("));
+ writer.close();
+ converted = new String(bout.toByteArray(), "ISO8859_1");
+ assertTrue("invalid conversion 3: " + converted, converted
+ .equals("\u001b$B$(\u001b(B"));
+
+ bout.reset();
+ writer = new OutputStreamWriter(bout, "ISO2022JP");
+ writer.write(new char[] { '\u3048' });
+ writer.write(new char[] { '\u3048' });
+ writer.close();
+ // there should not be a mode switch between writes
+ assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B",
+ new String(bout.toByteArray(), "ISO8859_1"));
+ } catch (UnsupportedEncodingException e) {
+ // Can't test missing converter
+ System.out.println(e);
+ }
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#flush()
+ */
+ public void test_flush() throws IOException {
+ char[] buf = new char[testString.length()];
+ osw.write(testString, 0, testString.length());
+ osw.flush();
+ openInputStream();
+ isr.read(buf, 0, buf.length);
+ assertTrue("Chars not flushed", new String(buf, 0, buf.length)
+ .equals(testString));
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#getEncoding()
+ */
+ public void test_getEncoding() throws IOException {
+ try {
+ osw = new OutputStreamWriter(fos, "8859_1");
+ } catch (UnsupportedEncodingException e) {
+ assertEquals("Returned incorrect encoding", "8859_1", osw
+ .getEncoding());
+ }
+
+ OutputStreamWriter out = new OutputStreamWriter(
+ new ByteArrayOutputStream(), "UTF-16BE");
+ out.close();
+
+ String result = out.getEncoding();
+ assertNull(result);
+
+ out = null;
+ try {
+ out = new OutputStreamWriter(new ByteArrayOutputStream(),
+ "UTF-16BE");
+ } catch (UnsupportedEncodingException e) {
+ // ok
+ }
+ result = out.getEncoding();
+ assertEquals("UnicodeBigUnmarked", result);
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#write(char[], int, int)
+ */
+ public void test_write$CII() throws IOException {
+ char[] buf = new char[testString.length()];
+ osw.write(testString, 0, testString.length());
+ osw.close();
+ openInputStream();
+ isr.read(buf, 0, buf.length);
+ assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
+ .equals(testString));
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#write(int)
+ */
+ public void test_writeI() throws IOException {
+ osw.write('T');
+ osw.close();
+ openInputStream();
+ int c = isr.read();
+ assertEquals("Incorrect char returned", 'T', (char) c);
+ }
+
+ /**
+ * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int)
+ */
+ public void test_writeLjava_lang_StringII() throws IOException {
+ char[] buf = new char[testString.length()];
+ osw.write(testString, 0, testString.length());
+ osw.close();
+ openInputStream();
+ isr.read(buf);
+ assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
+ .equals(testString));
+ }
+
+ private void openInputStream() {
+ isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()));
+ }
+}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java?rev=615857&r1=615856&r2=615857&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java Mon Jan 28 04:07:47 2008
@@ -20,63 +20,63 @@
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
-public class PipedInputStreamTest extends junit.framework.TestCase {
+import junit.framework.TestCase;
- static class PWriter implements Runnable {
- PipedOutputStream pos;
+public class PipedInputStreamTest extends TestCase {
- public byte bytes[];
+ static class PWriter implements Runnable {
+ PipedOutputStream pos;
- public void run() {
- try {
- pos.write(bytes);
- synchronized (this) {
- notify();
- }
- } catch (IOException e) {
- e.printStackTrace(System.out);
- System.out.println("Could not write bytes");
- }
- }
-
- public PWriter(PipedOutputStream pout, int nbytes) {
- pos = pout;
- bytes = new byte[nbytes];
- for (int i = 0; i < bytes.length; i++)
- bytes[i] = (byte) (System.currentTimeMillis() % 9);
- }
- }
-
- Thread t;
-
- PWriter pw;
-
- PipedInputStream pis;
-
- PipedOutputStream pos;
-
- /**
- * @tests java.io.PipedInputStream#PipedInputStream()
- */
- public void test_Constructor() {
- // Test for method java.io.PipedInputStream()
- // Used in tests
- }
-
- /**
- * @tests java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
- */
- public void test_ConstructorLjava_io_PipedOutputStream() throws Exception {
- // Test for method java.io.PipedInputStream(java.io.PipedOutputStream)
+ public byte bytes[];
+
+ public void run() {
+ try {
+ pos.write(bytes);
+ synchronized (this) {
+ notify();
+ }
+ } catch (IOException e) {
+ e.printStackTrace(System.out);
+ System.out.println("Could not write bytes");
+ }
+ }
+
+ public PWriter(PipedOutputStream pout, int nbytes) {
+ pos = pout;
+ bytes = new byte[nbytes];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) (System.currentTimeMillis() % 9);
+ }
+ }
+ }
+
+ Thread t;
+
+ PWriter pw;
+
+ PipedInputStream pis;
+
+ PipedOutputStream pos;
+
+ /**
+ * @tests java.io.PipedInputStream#PipedInputStream()
+ */
+ public void test_Constructor() {
+ // Used in tests
+ }
+
+ /**
+ * @tests java.io.PipedInputStream#PipedInputStream(java.io.PipedOutputStream)
+ */
+ public void test_ConstructorLjava_io_PipedOutputStream() throws IOException {
pis = new PipedInputStream(new PipedOutputStream());
pis.available();
}
- /**
- * @tests java.io.PipedInputStream#available()
- */
- public void test_available() throws Exception {
- // Test for method int java.io.PipedInputStream.available()
+ /**
+ * @tests java.io.PipedInputStream#available()
+ */
+ public void test_available() throws Exception {
pis = new PipedInputStream();
pos = new PipedOutputStream();
@@ -95,38 +95,35 @@
// We know the PipedInputStream buffer size is 1024.
// Writing another byte would cause the write to wait
// for a read before returning
- for (int i = 0; i < 1024; i++)
+ for (int i = 0; i < 1024; i++) {
pout.write(i);
- assertEquals("Incorrect available count", 1024 , pin.available());
+ }
+ assertEquals("Incorrect available count", 1024, pin.available());
}
- /**
- * @tests java.io.PipedInputStream#close()
- */
- public void test_close() throws IOException {
- // Test for method void java.io.PipedInputStream.close()
- pis = new PipedInputStream();
- pos = new PipedOutputStream();
+ /**
+ * @tests java.io.PipedInputStream#close()
+ */
+ public void test_close() throws IOException {
+ pis = new PipedInputStream();
+ pos = new PipedOutputStream();
pis.connect(pos);
pis.close();
- try {
- pos.write((byte) 127);
+ try {
+ pos.write((byte) 127);
fail("Failed to throw expected exception");
- } catch (IOException e) {
- // The spec for PipedInput saya an exception should be thrown if
- // a write is attempted to a closed input. The PipedOuput spec
- // indicates that an exception should be thrown only when the
- // piped input thread is terminated without closing
- return;
- }
- }
-
- /**
- * @tests java.io.PipedInputStream#connect(java.io.PipedOutputStream)
- */
- public void test_connectLjava_io_PipedOutputStream() throws Exception {
- // Test for method void
- // java.io.PipedInputStream.connect(java.io.PipedOutputStream)
+ } catch (IOException e) {
+ // The spec for PipedInput saya an exception should be thrown if
+ // a write is attempted to a closed input. The PipedOuput spec
+ // indicates that an exception should be thrown only when the
+ // piped input thread is terminated without closing
+ }
+ }
+
+ /**
+ * @tests java.io.PipedInputStream#connect(java.io.PipedOutputStream)
+ */
+ public void test_connectLjava_io_PipedOutputStream() throws Exception {
pis = new PipedInputStream();
pos = new PipedOutputStream();
assertEquals("Non-conected pipe returned non-zero available bytes", 0,
@@ -143,11 +140,10 @@
.available());
}
- /**
- * @tests java.io.PipedInputStream#read()
- */
- public void test_read() throws Exception {
- // Test for method int java.io.PipedInputStream.read()
+ /**
+ * @tests java.io.PipedInputStream#read()
+ */
+ public void test_read() throws Exception {
pis = new PipedInputStream();
pos = new PipedOutputStream();
@@ -164,11 +160,10 @@
.read());
}
- /**
- * @tests java.io.PipedInputStream#read(byte[], int, int)
- */
- public void test_read$BII() throws Exception {
- // Test for method int java.io.PipedInputStream.read(byte [], int, int)
+ /**
+ * @tests java.io.PipedInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() throws Exception {
pis = new PipedInputStream();
pos = new PipedOutputStream();
@@ -189,8 +184,8 @@
}
/**
- * @tests java.io.PipedInputStream#read(byte[], int, int)
- * Regression for HARMONY-387
+ * @tests java.io.PipedInputStream#read(byte[], int, int) Regression for
+ * HARMONY-387
*/
public void test_read$BII_2() throws IOException {
PipedInputStream obj = new PipedInputStream();
@@ -250,8 +245,9 @@
public void run() {
try {
pos.write(1);
- while (readerAlive)
+ while (readerAlive) {
;
+ }
try {
// should throw exception since reader thread
// is now dead
@@ -259,7 +255,8 @@
} catch (IOException e) {
pass = true;
}
- } catch (IOException e) {}
+ } catch (IOException e) {
+ }
}
}
WriteRunnable writeRunnable = new WriteRunnable();
@@ -272,7 +269,8 @@
try {
pis.read();
pass = true;
- } catch (IOException e) {}
+ } catch (IOException e) {
+ }
}
}
;
@@ -280,12 +278,14 @@
Thread readThread = new Thread(readRunnable);
writeThread.start();
readThread.start();
- while (readThread.isAlive())
+ while (readThread.isAlive()) {
;
+ }
writeRunnable.readerAlive = false;
assertTrue("reader thread failed to read", readRunnable.pass);
- while (writeThread.isAlive())
+ while (writeThread.isAlive()) {
;
+ }
assertTrue("writer thread failed to recognize dead reader",
writeRunnable.pass);
@@ -316,7 +316,8 @@
try {
// wait for thread t to get to the call to pis.receive
Thread.sleep(100);
- } catch (InterruptedException e) {}
+ } catch (InterruptedException e) {
+ }
// now we close
pos.close();
}
@@ -331,17 +332,18 @@
myRun.pass);
}
- /**
- * Tears down the fixture, for example, close a network connection. This
- * method is called after a test is executed.
- */
- protected void tearDown() throws Exception {
- try {
- if (t != null) {
- t.interrupt();
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ try {
+ if (t != null) {
+ t.interrupt();
}
- } catch (Exception ignore) {
- }
+ } catch (Exception ignore) {
+ }
super.tearDown();
- }
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedOutputStreamTest.java?rev=615857&r1=615856&r2=615857&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedOutputStreamTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedOutputStreamTest.java Mon Jan 28 04:07:47 2008
@@ -21,92 +21,91 @@
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
-public class PipedOutputStreamTest extends junit.framework.TestCase {
+import junit.framework.TestCase;
- static class PReader implements Runnable {
- PipedInputStream reader;
+public class PipedOutputStreamTest extends TestCase {
+
+ static class PReader implements Runnable {
+ PipedInputStream reader;
+
+ public PipedInputStream getReader() {
+ return reader;
+ }
+
+ public PReader(PipedOutputStream out) {
+ try {
+ reader = new PipedInputStream(out);
+ } catch (Exception e) {
+ System.out.println("Couldn't start reader");
+ }
+ }
+
+ public int available() {
+ try {
+ return reader.available();
+ } catch (Exception e) {
+ return -1;
+ }
+ }
+
+ public void run() {
+ try {
+ while (true) {
+ Thread.sleep(1000);
+ Thread.yield();
+ }
+ } catch (InterruptedException e) {
+ }
+ }
+
+ public String read(int nbytes) {
+ byte[] buf = new byte[nbytes];
+ try {
+ reader.read(buf, 0, nbytes);
+ return new String(buf);
+ } catch (IOException e) {
+ System.out.println("Exception reading info");
+ return "ERROR";
+ }
+ }
+ }
+
+ Thread rt;
+
+ PReader reader;
+
+ PipedOutputStream out;
+
+ /**
+ * @tests java.io.PipedOutputStream#PipedOutputStream()
+ */
+ public void test_Constructor() {
+ // Used in tests
+ }
+
+ /**
+ * @tests java.io.PipedOutputStream#PipedOutputStream(java.io.PipedInputStream)
+ */
+ public void test_ConstructorLjava_io_PipedInputStream() throws Exception {
+ out = new PipedOutputStream(new PipedInputStream());
+ out.write('b');
+ }
+
+ /**
+ * @tests java.io.PipedOutputStream#close()
+ */
+ public void test_close() throws Exception {
+ out = new PipedOutputStream();
+ rt = new Thread(reader = new PReader(out));
+ rt.start();
+ out.close();
+ }
- public PipedInputStream getReader() {
- return reader;
- }
-
- public PReader(PipedOutputStream out) {
- try {
- reader = new PipedInputStream(out);
- } catch (Exception e) {
- System.out.println("Couldn't start reader");
- }
- }
-
- public int available() {
- try {
- return reader.available();
- } catch (Exception e) {
- return -1;
- }
- }
-
- public void run() {
- try {
- while (true) {
- Thread.sleep(1000);
- Thread.yield();
- }
- } catch (InterruptedException e) {
- }
- }
-
- public String read(int nbytes) {
- byte[] buf = new byte[nbytes];
- try {
- reader.read(buf, 0, nbytes);
- return new String(buf);
- } catch (IOException e) {
- System.out.println("Exception reading info");
- return "ERROR";
- }
- }
- }
-
- Thread rt;
-
- PReader reader;
-
- PipedOutputStream out;
-
- /**
- * @tests java.io.PipedOutputStream#PipedOutputStream()
- */
- public void test_Constructor() {
- // Test for method java.io.PipedOutputStream()
- // Used in tests
- }
-
- /**
- * @tests java.io.PipedOutputStream#PipedOutputStream(java.io.PipedInputStream)
- */
- public void test_ConstructorLjava_io_PipedInputStream() throws Exception {
- // Test for method java.io.PipedOutputStream(java.io.PipedInputStream)
-
- out = new PipedOutputStream(new PipedInputStream());
- out.write('b');
- }
-
- /**
- * @tests java.io.PipedOutputStream#close()
- */
- public void test_close() throws Exception {
- // Test for method void java.io.PipedOutputStream.close()
- out = new PipedOutputStream();
- rt = new Thread(reader = new PReader(out));
- rt.start();
- out.close();
- }
-
/**
* @tests java.io.PipedOutputStream#connect(java.io.PipedInputStream)
*/
- public void test_connectLjava_io_PipedInputStream_Exception() throws IOException {
+ public void test_connectLjava_io_PipedInputStream_Exception()
+ throws IOException {
out = new PipedOutputStream();
out.connect(new PipedInputStream());
try {
@@ -117,72 +116,54 @@
}
}
- /**
- * @tests java.io.PipedOutputStream#connect(java.io.PipedInputStream)
- */
- public void test_connectLjava_io_PipedInputStream() {
- // Test for method void
- // java.io.PipedOutputStream.connect(java.io.PipedInputStream)
- try {
- out = new PipedOutputStream();
- rt = new Thread(reader = new PReader(out));
- rt.start();
- out.connect(new PipedInputStream());
- } catch (IOException e) {
- // Correct
- return;
- }
- fail(
- "Failed to throw exception attempting connect on already connected stream");
-
- }
-
- /**
- * @tests java.io.PipedOutputStream#flush()
- */
- public void test_flush() {
- // Test for method void java.io.PipedOutputStream.flush()
- try {
- out = new PipedOutputStream();
- rt = new Thread(reader = new PReader(out));
- rt.start();
- out.write("HelloWorld".getBytes(), 0, 10);
- assertTrue("Bytes written before flush", reader.available() != 0);
- out.flush();
- assertEquals("Wrote incorrect bytes",
- "HelloWorld", reader.read(10));
- } catch (IOException e) {
- fail("IOException during write test : " + e.getMessage());
- }
- }
-
- /**
- * @tests java.io.PipedOutputStream#write(byte[], int, int)
- */
- public void test_write$BII() {
- // Test for method void java.io.PipedOutputStream.write(byte [], int,
- // int)
- try {
- out = new PipedOutputStream();
- rt = new Thread(reader = new PReader(out));
- rt.start();
- out.write("HelloWorld".getBytes(), 0, 10);
- out.flush();
- assertEquals("Wrote incorrect bytes",
- "HelloWorld", reader.read(10));
- } catch (IOException e) {
- fail("IOException during write test : " + e.getMessage());
- }
- }
+ /**
+ * @tests java.io.PipedOutputStream#connect(java.io.PipedInputStream)
+ */
+ public void test_connectLjava_io_PipedInputStream() {
+ try {
+ out = new PipedOutputStream();
+ rt = new Thread(reader = new PReader(out));
+ rt.start();
+ out.connect(new PipedInputStream());
+ fail("Failed to throw exception attempting connect on already connected stream");
+ } catch (IOException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * @tests java.io.PipedOutputStream#flush()
+ */
+ public void test_flush() throws IOException {
+ out = new PipedOutputStream();
+ rt = new Thread(reader = new PReader(out));
+ rt.start();
+ out.write("HelloWorld".getBytes(), 0, 10);
+ assertTrue("Bytes written before flush", reader.available() != 0);
+ out.flush();
+ assertEquals("Wrote incorrect bytes", "HelloWorld", reader.read(10));
+ }
/**
* @tests java.io.PipedOutputStream#write(byte[], int, int)
- * Regression for HARMONY-387
+ */
+ public void test_write$BII() throws IOException {
+ out = new PipedOutputStream();
+ rt = new Thread(reader = new PReader(out));
+ rt.start();
+ out.write("HelloWorld".getBytes(), 0, 10);
+ out.flush();
+ assertEquals("Wrote incorrect bytes", "HelloWorld", reader.read(10));
+ }
+
+ /**
+ * @tests java.io.PipedOutputStream#write(byte[], int, int) Regression for
+ * HARMONY-387
*/
public void test_write$BII_2() throws IOException {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = null;
- try{
+ try {
pos = new PipedOutputStream(pis);
pos.write(new byte[0], -1, -1);
fail("IndexOutOfBoundsException expected");
@@ -191,7 +172,7 @@
"IndexOutOfBoundsException rather than a subclass expected",
IndexOutOfBoundsException.class, t.getClass());
}
-
+
// Regression for HARMONY-4311
try {
pis = new PipedInputStream();
@@ -200,39 +181,29 @@
fail("should throw NullPointerException.");
} catch (NullPointerException e) {
// expected
- }
+ }
+ }
+
+ /**
+ * @tests java.io.PipedOutputStream#write(int)
+ */
+ public void test_writeI() throws IOException {
+ out = new PipedOutputStream();
+ rt = new Thread(reader = new PReader(out));
+ rt.start();
+ out.write('c');
+ out.flush();
+ assertEquals("Wrote incorrect byte", "c", reader.read(1));
}
- /**
- * @tests java.io.PipedOutputStream#write(int)
- */
- public void test_writeI() {
- // Test for method void java.io.PipedOutputStream.write(int)
- try {
- out = new PipedOutputStream();
- rt = new Thread(reader = new PReader(out));
- rt.start();
- out.write('c');
- out.flush();
- assertEquals("Wrote incorrect byte", "c", reader.read(1));
- } catch (IOException e) {
- fail("IOException during write test : " + e.getMessage());
- }
- }
-
- /**
- * Sets up the fixture, for example, open a network connection. This method
- * is called before a test is executed.
- */
- protected void setUp() {
- }
-
- /**
- * Tears down the fixture, for example, close a network connection. This
- * method is called after a test is executed.
- */
- protected void tearDown() {
- if (rt != null)
- rt.interrupt();
- }
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ @Override
+ protected void tearDown() {
+ if (rt != null) {
+ rt.interrupt();
+ }
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedReaderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedReaderTest.java?rev=615857&r1=615856&r2=615857&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedReaderTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedReaderTest.java Mon Jan 28 04:07:47 2008
@@ -21,55 +21,55 @@
import java.io.PipedReader;
import java.io.PipedWriter;
-public class PipedReaderTest extends junit.framework.TestCase {
+import junit.framework.TestCase;
- static class PWriter implements Runnable {
- public PipedWriter pw;
+public class PipedReaderTest extends TestCase {
- public PWriter(PipedReader reader) {
- try {
- pw = new PipedWriter(reader);
- } catch (Exception e) {
- System.out.println("Couldn't create writer");
- }
- }
-
- public PWriter() {
- pw = new PipedWriter();
- }
-
- public void run() {
- try {
- char[] c = new char[11];
- "Hello World".getChars(0, 11, c, 0);
- pw.write(c);
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- } catch (Exception e) {
- System.out.println("Exception occurred: " + e.toString());
- }
- }
- }
+ static class PWriter implements Runnable {
+ public PipedWriter pw;
- PipedReader preader;
+ public PWriter(PipedReader reader) {
+ try {
+ pw = new PipedWriter(reader);
+ } catch (Exception e) {
+ System.out.println("Couldn't create writer");
+ }
+ }
+
+ public PWriter() {
+ pw = new PipedWriter();
+ }
+
+ public void run() {
+ try {
+ char[] c = new char[11];
+ "Hello World".getChars(0, 11, c, 0);
+ pw.write(c);
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ } catch (Exception e) {
+ System.out.println("Exception occurred: " + e.toString());
+ }
+ }
+ }
- PWriter pwriter;
+ PipedReader preader;
- Thread t;
+ PWriter pwriter;
- /**
+ Thread t;
+
+ /**
* @tests java.io.PipedReader#PipedReader()
*/
public void test_Constructor() {
- // Test for method java.io.PipedReader()
- // Used in test
+ // Used in test
}
/**
* @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
*/
public void test_ConstructorLjava_io_PipedWriter() throws IOException {
- // Test for method java.io.PipedReader(java.io.PipedWriter)
preader = new PipedReader(new PipedWriter());
}
@@ -77,7 +77,6 @@
* @tests java.io.PipedReader#close()
*/
public void test_close() throws Exception {
- // Test for method void java.io.PipedReader.close()
char[] c = null;
preader = new PipedReader();
t = new Thread(new PWriter(preader), "");
@@ -93,7 +92,6 @@
* @tests java.io.PipedReader#connect(java.io.PipedWriter)
*/
public void test_connectLjava_io_PipedWriter() throws Exception {
- // Test for method void java.io.PipedReader.connect(java.io.PipedWriter)
char[] c = null;
preader = new PipedReader();
@@ -108,16 +106,15 @@
try {
preader.connect(pwriter.pw);
fail("Failed to throw exception connecting to pre-connected reader");
- } catch (Exception e) {
- // Correct
+ } catch (IOException e) {
+ // Expected
}
}
- /**
- * @tests java.io.PipedReader#read()
- */
- public void test_read() throws Exception {
- // Test for method int java.io.PipedReader.read()
+ /**
+ * @tests java.io.PipedReader#read()
+ */
+ public void test_read() throws Exception {
char[] c = null;
preader = new PipedReader();
t = new Thread(new PWriter(preader), "");
@@ -130,11 +127,10 @@
assertEquals("Read incorrect chars", "Hello World", new String(c));
}
- /**
- * @tests java.io.PipedReader#read(char[], int, int)
- */
- public void test_read$CII() throws Exception {
- // Test for method int java.io.PipedReader.read(char [], int, int)
+ /**
+ * @tests java.io.PipedReader#read(char[], int, int)
+ */
+ public void test_read$CII() throws Exception {
char[] c = null;
preader = new PipedReader();
t = new Thread(new PWriter(preader), "");
@@ -152,16 +148,16 @@
preader.close();
preader.read(c, 8, 7);
fail("Failed to throw exception reading from closed reader");
- } catch (Exception e) {
- // Correct
+ } catch (IOException e) {
+ // Expected
}
}
/**
* @tests java.io.PipedReader#read(char[], int, int)
- * Regression for HARMONY-387
*/
- public void test_read$CII_2() throws IOException{
+ public void test_read$CII_2() throws IOException {
+ // Regression for HARMONY-387
PipedWriter pw = new PipedWriter();
PipedReader obj = null;
try {
@@ -188,6 +184,7 @@
} catch (ArrayIndexOutOfBoundsException t) {
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException t) {
+ // Expected
}
}
@@ -204,9 +201,10 @@
} catch (ArrayIndexOutOfBoundsException t) {
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException t) {
+ // Expected
}
}
-
+
/**
* @tests java.io.PipedReader#read(char[], int, int)
*/
@@ -224,7 +222,7 @@
pw = null;
pr = null;
}
-
+
pr = new PipedReader();
buf = null;
pr.close();
@@ -236,7 +234,7 @@
} finally {
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
buf = new char[10];
@@ -250,7 +248,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
buf = new char[10];
@@ -264,7 +262,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
buf = new char[10];
@@ -278,7 +276,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
pr.close();
@@ -291,7 +289,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
pr.close();
@@ -304,7 +302,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
try {
@@ -316,7 +314,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
try {
@@ -328,7 +326,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
try {
@@ -340,7 +338,7 @@
pw = null;
pr = null;
}
-
+
pw = new PipedWriter();
pr = new PipedReader(pw);
try {
@@ -355,10 +353,9 @@
}
/**
- * @tests java.io.PipedReader#ready()
- */
- public void test_ready() throws Exception {
- // Test for method boolean java.io.PipedReader.ready()
+ * @tests java.io.PipedReader#ready()
+ */
+ public void test_ready() throws Exception {
char[] c = null;
preader = new PipedReader();
t = new Thread(new PWriter(preader), "");
@@ -372,14 +369,14 @@
preader.ready());
}
- /**
- * Tears down the fixture, for example, close a network connection. This
- * method is called after a test is executed.
- */
- protected void tearDown() throws Exception {
- if (t != null) {
- t.interrupt();
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() throws Exception {
+ if (t != null) {
+ t.interrupt();
}
super.tearDown();
- }
+ }
}