Search in sources :

Example 11 with Support_OutputStream

use of tests.support.Support_OutputStream in project robovm by robovm.

the class OldObjectOutputStreamTest method test_reset.

public void test_reset() throws Exception {
    String o = "HelloWorld";
    sos = new Support_OutputStream(200);
    oos.close();
    oos = new ObjectOutputStream(sos);
    oos.writeObject(o);
    oos.writeObject(o);
    oos.reset();
    oos.writeObject(o);
    sos.setThrowsException(true);
    try {
        oos.reset();
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    sos.setThrowsException(false);
    ois = new ObjectInputStream(new ByteArrayInputStream(sos.toByteArray()));
    assertEquals("Test 2: Incorrect object read.", o, ois.readObject());
    assertEquals("Test 3: Incorrect object read.", o, ois.readObject());
    assertEquals("Test 4: Incorrect object read.", o, ois.readObject());
    ois.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with Support_OutputStream

use of tests.support.Support_OutputStream in project robovm by robovm.

the class OldOutputStreamWriterTest method test_writeI.

public void test_writeI() throws IOException {
    Support_OutputStream out = new Support_OutputStream(500);
    OutputStreamWriter writer;
    out.setThrowsException(true);
    writer = new OutputStreamWriter(out, "utf-8");
    try {
        // one character needs to be written.
        for (int i = 0; i < 200; i++) {
            for (int j = 0; j < testString.length(); j++) {
                writer.write(testString.charAt(j));
            }
        }
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
    out.setThrowsException(false);
    writer.close();
    writer = new OutputStreamWriter(out, "utf-8");
    writer.write(1);
    writer.flush();
    String str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 2: ", "", str);
    writer.write(2);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 3: ", "", str);
    writer.write(-1);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 4: ", "￿", str);
    writer.write(0xfedcb);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 5: ", "￿", str);
    writer.close();
    try {
        writer.write(1);
        fail("Test 6: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 13 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldOutputStreamWriterTest method test_write$CII.

public void test_write$CII() throws IOException {
    char[] chars = testString.toCharArray();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Support_OutputStream out = new Support_OutputStream(500);
    OutputStreamWriter writer;
    writer = new OutputStreamWriter(out, "utf-8");
    try {
        writer.write(chars, -1, 1);
        fail("Test 1: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(chars, 0, -1);
        fail("Test 2: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(new char[0], 0, 1);
        fail("Test 3: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write((char[]) null, 0, 1);
        fail("Test 4: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    try {
        writer.write(chars, 1, chars.length);
        fail("Test 5a: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(chars, 0, chars.length + 1);
        fail("Test 5b: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(chars, chars.length, 1);
        fail("Test 5c: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(chars, chars.length + 1, 0);
        fail("Test 5d: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    out.setThrowsException(true);
    try {
        for (int i = 0; i < 200; i++) {
            writer.write(chars, 0, chars.length);
        }
        fail("Test 6: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
    out.setThrowsException(false);
    writer.close();
    writer = new OutputStreamWriter(baos, "utf-8");
    writer.write(chars, 1, 2);
    writer.flush();
    assertEquals("Test 7: write(char[], int, int) has not produced the " + "expected content in the output stream.", "hi", baos.toString("utf-8"));
    writer.write(chars, 0, chars.length);
    writer.flush();
    assertEquals("Test 8: write(char[], int, int) has not produced the " + "expected content in the output stream.", "hi" + testString, baos.toString("utf-8"));
    writer.close();
    try {
        writer.write((char[]) null, -1, -1);
        fail("Test 9: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 14 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldOutputStreamWriterTest method test_ConstructorLjava_io_OutputStream.

public void test_ConstructorLjava_io_OutputStream() throws IOException {
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(null);
        fail("Test 1: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    try {
        writer = new OutputStreamWriter(new Support_OutputStream());
    } catch (Exception e) {
        fail("Test 2: Unexpected exception: " + e.getMessage());
    }
    // Test that the default encoding has been used.
    assertEquals("Test 3: Incorrect default encoding used.", Charset.defaultCharset(), Charset.forName(writer.getEncoding()));
    if (writer != null)
        writer.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 15 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldOutputStreamWriterTest method test_writeLjava_lang_StringII.

public void test_writeLjava_lang_StringII() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Support_OutputStream out = new Support_OutputStream(500);
    OutputStreamWriter writer;
    writer = new OutputStreamWriter(out, "utf-8");
    try {
        writer.write("Lorem", -1, 0);
        fail("Test 1: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write("Lorem", 0, -1);
        fail("Test 2: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write("", 0, 1);
        fail("Test 3: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(testString, 1, testString.length());
        fail("Test 4a: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(testString, 0, testString.length() + 1);
        fail("Test 4b: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(testString, testString.length(), 1);
        fail("Test 4c: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write(testString, testString.length() + 1, 0);
        fail("Test 4d: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        writer.write((String) null, 0, 1);
        fail("Test 5: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    out.setThrowsException(true);
    try {
        for (int i = 0; i < 200; i++) {
            writer.write(testString, 0, testString.length());
        }
        fail("Test 6: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
    out.setThrowsException(false);
    writer.close();
    writer = new OutputStreamWriter(baos, "utf-8");
    writer.write("abc", 1, 2);
    writer.flush();
    assertEquals("Test 7: write(String, int, int) has not produced the " + "expected content in the output stream.", "bc", baos.toString("utf-8"));
    writer.write(testString, 0, testString.length());
    writer.flush();
    assertEquals("Test 7: write(String, int, int) has not produced the " + "expected content in the output stream.", "bc" + testString, baos.toString("utf-8"));
    writer.close();
    try {
        writer.write("abc", 0, 1);
        fail("Test 8: IOException expected.");
    } catch (IOException e) {
    // Expected
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Support_OutputStream (tests.support.Support_OutputStream)48 IOException (java.io.IOException)30 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 OutputStreamWriter (java.io.OutputStreamWriter)16 FilterOutputStream (java.io.FilterOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)6 ObjectOutputStream (java.io.ObjectOutputStream)6 DataOutputStream (java.io.DataOutputStream)4 Charset (java.nio.charset.Charset)4 ObjectInputStream (java.io.ObjectInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 DigestOutputStream (java.security.DigestOutputStream)2 MessageDigest (java.security.MessageDigest)2