Search in sources :

Example 1 with TestOutputStream

use of com.unboundid.util.TestOutputStream in project ldapsdk by pingidentity.

the class LDAPListenerClientConnectionTestCase method testThrowOnGetInputStream.

/**
 * Tests the behavior when a client connection has been created without a
 * listener and with a socket that throws an exception when trying to get the
 * input stream.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testThrowOnGetInputStream() throws Exception {
    final TestSocket s = new TestSocket(new TestInputStream(new ByteArrayInputStream(new byte[1024]), new IOException("foo"), 100, true), new TestOutputStream(new ByteArrayOutputStream(), new IOException("bar"), 1024, true));
    s.setThrowOnGetInputStream(true);
    s.setThrowOnClose(true);
    new LDAPListenerClientConnection(null, s, new CannedResponseRequestHandler(), null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TestOutputStream(com.unboundid.util.TestOutputStream) TestSocket(com.unboundid.util.TestSocket) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestInputStream(com.unboundid.util.TestInputStream) Test(org.testng.annotations.Test)

Example 2 with TestOutputStream

use of com.unboundid.util.TestOutputStream in project ldapsdk by pingidentity.

the class LDAPListenerClientConnectionTestCase method testThrowOnClose.

/**
 * Tests the behavior when a client connection has been created without a
 * listener and with elements that throw exceptions on closure.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testThrowOnClose() throws Exception {
    final TestSocket s = new TestSocket(new TestInputStream(new ByteArrayInputStream(new byte[1024]), new IOException("foo"), 100, true), new TestOutputStream(new ByteArrayOutputStream(), new IOException("bar"), 1024, true));
    s.setThrowOnClose(true);
    final LDAPListenerClientConnection conn = new LDAPListenerClientConnection(null, s, new CannedResponseRequestHandler(), new TestLDAPListenerExceptionHandler());
    conn.removeSearchEntryTransformer(null);
    conn.removeSearchReferenceTransformer(null);
    conn.removeIntermediateResponseTransformer(null);
    conn.close(new LDAPException(ResultCode.LOCAL_ERROR, "foo"));
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) ByteArrayInputStream(java.io.ByteArrayInputStream) TestOutputStream(com.unboundid.util.TestOutputStream) TestSocket(com.unboundid.util.TestSocket) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestInputStream(com.unboundid.util.TestInputStream) Test(org.testng.annotations.Test)

Example 3 with TestOutputStream

use of com.unboundid.util.TestOutputStream in project ldapsdk by pingidentity.

the class CollectSupportDataIRListenerTestCase method testThrowOnWrite.

/**
 * Tests the behavior for the case in which an attempt to write an archive
 * fragment throws an exception.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testThrowOnWrite() throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    final CollectSupportData tool = new CollectSupportData(out, err);
    final File outputFile = createTempFile();
    assertTrue(outputFile.delete());
    final CollectSupportDataIRListener listener = new CollectSupportDataIRListener(tool, outputFile);
    listener.handleArchiveFragmentIntermediateResponse(new CollectSupportDataArchiveFragmentIntermediateResponse("csd.zip", 10L, true, StaticUtils.getBytes("Hello")));
    assertTrue(out.size() > 0);
    assertEquals(err.size(), 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 5L);
    assertNotNull(listener.getOutputStreamReference().get());
    assertNull(listener.getFirstIOExceptionReference().get());
    out.reset();
    final OutputStream streamToWrap = listener.getOutputStreamReference().get();
    listener.getOutputStreamReference().set(new TestOutputStream(streamToWrap, new IOException("write error"), 0, false));
    listener.handleArchiveFragmentIntermediateResponse(new CollectSupportDataArchiveFragmentIntermediateResponse("csd.zip", 10L, false, StaticUtils.getBytes("There")));
    assertEquals(out.size(), 0);
    assertTrue(err.size() > 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 5L);
    assertNotNull(listener.getOutputStreamReference().get());
    assertNotNull(listener.getFirstIOExceptionReference().get());
    out.reset();
    err.reset();
    listener.handleArchiveFragmentIntermediateResponse(new CollectSupportDataArchiveFragmentIntermediateResponse("csd.zip", 10L, false, StaticUtils.getBytes("There")));
    assertEquals(out.size(), 0);
    assertEquals(err.size(), 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 5L);
    assertNotNull(listener.getOutputStreamReference().get());
    assertNotNull(listener.getFirstIOExceptionReference().get());
    out.reset();
    err.reset();
    try {
        listener.close();
        fail("Expected an exception when trying to close the output stream");
    } catch (final IOException e) {
    // This was expected.
    }
    assertEquals(out.size(), 0);
    assertEquals(err.size(), 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 5L);
    assertEquals(readFileBytes(outputFile), StaticUtils.getBytes("Hello"));
    assertNull(listener.getOutputStreamReference().get());
    assertNotNull(listener.getFirstIOExceptionReference().get());
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestOutputStream(com.unboundid.util.TestOutputStream) CollectSupportDataOutputStream(com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataOutputStream) TestOutputStream(com.unboundid.util.TestOutputStream) CollectSupportDataArchiveFragmentIntermediateResponse(com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataArchiveFragmentIntermediateResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with TestOutputStream

use of com.unboundid.util.TestOutputStream in project ldapsdk by pingidentity.

the class CollectSupportDataIRListenerTestCase method testThrowOnClose.

/**
 * Tests the behavior for the case in which an attempt to close the output
 * stream should throw an exception.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testThrowOnClose() throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    final CollectSupportData tool = new CollectSupportData(out, err);
    final File outputFile = createTempFile();
    assertTrue(outputFile.delete());
    final CollectSupportDataIRListener listener = new CollectSupportDataIRListener(tool, outputFile);
    listener.handleArchiveFragmentIntermediateResponse(new CollectSupportDataArchiveFragmentIntermediateResponse("csd.zip", 10L, true, StaticUtils.getBytes("Hello")));
    assertTrue(out.size() > 0);
    assertEquals(err.size(), 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 5L);
    assertNotNull(listener.getOutputStreamReference().get());
    assertNull(listener.getFirstIOExceptionReference().get());
    out.reset();
    final OutputStream streamToWrap = listener.getOutputStreamReference().get();
    listener.getOutputStreamReference().set(new TestOutputStream(streamToWrap, new IOException("close error"), Integer.MAX_VALUE, true));
    listener.handleArchiveFragmentIntermediateResponse(new CollectSupportDataArchiveFragmentIntermediateResponse("csd.zip", 10L, false, StaticUtils.getBytes("There")));
    assertTrue(out.size() > 0L);
    assertEquals(err.size(), 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 10L);
    assertNotNull(listener.getOutputStreamReference().get());
    assertNull(listener.getFirstIOExceptionReference().get());
    out.reset();
    try {
        listener.close();
        fail("Expected an exception when trying to close the output stream");
    } catch (final IOException e) {
    // This was expected.
    }
    assertEquals(out.size(), 0);
    assertTrue(err.size() > 0);
    assertTrue(outputFile.exists());
    assertEquals(outputFile.length(), 10L);
    assertEquals(readFileBytes(outputFile), StaticUtils.getBytes("HelloThere"));
    assertNull(listener.getOutputStreamReference().get());
    assertNotNull(listener.getFirstIOExceptionReference().get());
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestOutputStream(com.unboundid.util.TestOutputStream) CollectSupportDataOutputStream(com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataOutputStream) TestOutputStream(com.unboundid.util.TestOutputStream) CollectSupportDataArchiveFragmentIntermediateResponse(com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataArchiveFragmentIntermediateResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) Test(org.testng.annotations.Test)

Example 5 with TestOutputStream

use of com.unboundid.util.TestOutputStream in project ldapsdk by pingidentity.

the class LDAPListenerClientConnectionTestCase method testThrowOnNewRequestHandlerInstance.

/**
 * Tests the behavior when a client connection has been created without a
 * listener and with a request handler that throws an exception when trying to
 * create a new instance.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testThrowOnNewRequestHandlerInstance() throws Exception {
    final TestSocket s = new TestSocket(new TestInputStream(new ByteArrayInputStream(new byte[1024]), new IOException("foo"), 100, true), new TestOutputStream(new ByteArrayOutputStream(), new IOException("bar"), 1024, true));
    s.setThrowOnClose(true);
    TestRequestHandler.setThrowOnNewInstance(true);
    try {
        new LDAPListenerClientConnection(null, s, new TestRequestHandler(), null);
    } finally {
        TestRequestHandler.setThrowOnNewInstance(false);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TestOutputStream(com.unboundid.util.TestOutputStream) TestSocket(com.unboundid.util.TestSocket) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestInputStream(com.unboundid.util.TestInputStream) Test(org.testng.annotations.Test)

Aggregations

TestOutputStream (com.unboundid.util.TestOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 TestInputStream (com.unboundid.util.TestInputStream)4 TestSocket (com.unboundid.util.TestSocket)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 CollectSupportDataArchiveFragmentIntermediateResponse (com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataArchiveFragmentIntermediateResponse)2 CollectSupportDataOutputStream (com.unboundid.ldap.sdk.unboundidds.extensions.CollectSupportDataOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)1