Search in sources :

Example 36 with Support_OutputStream

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

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 37 with Support_OutputStream

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

the class OldOutputStreamWriterTest method test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder.

public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder() throws IOException {
    OutputStreamWriter writer;
    Support_OutputStream out = new Support_OutputStream();
    Charset cs = Charset.forName("ascii");
    CharsetEncoder enc = cs.newEncoder();
    try {
        writer = new OutputStreamWriter(null, enc);
        fail("Test 1: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    try {
        writer = new OutputStreamWriter(out, (CharsetEncoder) null);
        fail("Test 2: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    writer = new OutputStreamWriter(out, cs);
    assertEquals("Test 3: CharacterEncoder not set correctly. ", cs, Charset.forName(writer.getEncoding()));
    writer.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 38 with Support_OutputStream

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

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)

Example 39 with Support_OutputStream

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

the class DigestOutputStreamTest method test_write$BII_7.

/**
     * java.io.DigestOutputStream#write(byte[], int, int)
     */
public void test_write$BII_7() throws IOException, NoSuchAlgorithmException {
    Support_OutputStream sos = new Support_OutputStream(MY_MESSAGE_LEN);
    MessageDigest md = MessageDigest.getInstance(algorithmName[0]);
    DigestOutputStream dos = new DigestOutputStream(sos, md);
    dos.write(myMessage, 0, MY_MESSAGE_LEN);
    try {
        // Support_OutputStream throws an IOException if the internal
        // buffer is full, which it should be now.
        dos.write(myMessage, 0, MY_MESSAGE_LEN);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) DigestOutputStream(java.security.DigestOutputStream) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 40 with Support_OutputStream

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

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: ", "\u0001", str);
    writer.write(2);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 3: ", "\u0001\u0002", str);
    writer.write(-1);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 4: ", "\u0001\u0002\uffff", str);
    writer.write(0xfedcb);
    writer.flush();
    str = new String(out.toByteArray(), "utf-8");
    assertEquals("Test 5: ", "\u0001\u0002\uffff\uedcb", 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)

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