public void testConstructorLBlob() throws Exception {
    boolean isAbnormal = false;
    MockSerialBlob mockBlob = new MockSerialBlob(isAbnormal);
    SerialBlob serialBlob = new SerialBlob(mockBlob);
    // SerialBlob constructor initiliases with the data of given blob,
    // therefore, blob.getBytes is invoked.
    assertTrue(mockBlob.isGetBytesInvoked);
    assertEquals(1, serialBlob.length());

    isAbnormal = true;
    mockBlob = new MockSerialBlob(isAbnormal);
    try {
      new SerialBlob(mockBlob);
      fail("should throw SQLException");
    } catch (SQLException e) {
      // expected
    }

    try {
      new SerialBlob((Blob) null);
      fail("should throw SQLException");
    } catch (SQLException e) {
      // expected
    }
  }
  public void testSetBinaryStreamJ() throws Exception {
    MockSerialBlob mockBlob = new MockSerialBlob();
    mockBlob.binaryStream = new ByteArrayOutputStream();
    SerialBlob serialBlob = new SerialBlob(mockBlob);
    OutputStream os = serialBlob.setBinaryStream(1);
    assertTrue(mockBlob.isSetBinaryStreamInvoked);
    assertEquals(1L, mockBlob.pos);
    assertSame(mockBlob.binaryStream, os);

    mockBlob.binaryStream = null;
    try {
      serialBlob.setBinaryStream(1);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    byte[] buf = new byte[1];
    serialBlob = new SerialBlob(buf);
    try {
      // Non bug difference from RI, Harmony-2971
      serialBlob.setBinaryStream(1);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }
  }
  public void testConstructor$B() throws Exception {
    byte[] buf = new byte[8];
    SerialBlob serialBlob = new SerialBlob(buf);
    assertEquals(8, serialBlob.length());

    try {
      new SerialBlob((byte[]) null);
      fail("should throw NullPointerException");
    } catch (NullPointerException e) {
      // expected
    }
  }
  public void testGetBytesJI() throws Exception {
    byte[] buf = {1, 2, 3, 4, 5, 6, 7, 8};
    SerialBlob serialBlob = new SerialBlob(buf);
    byte[] data = serialBlob.getBytes(1, 1);
    assertEquals(1, data.length);
    assertEquals(1, data[0]);

    data = serialBlob.getBytes(2, 3);
    assertEquals(3, data.length);
    assertEquals(2, data[0]);
    assertEquals(3, data[1]);
    assertEquals(4, data[2]);

    // Harmony-2725 RI's bug : RI throws SerialException here.
    data = serialBlob.getBytes(2, 1);
    assertEquals(1, data.length);
    assertEquals(2, data[0]);

    data = serialBlob.getBytes(1, 10);
    assertEquals(8, data.length);
    assertTrue(Arrays.equals(buf, data));

    try {
      data = serialBlob.getBytes(2, -1);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    try {
      data = serialBlob.getBytes(0, 2);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    try {
      data = serialBlob.getBytes(-1, 2);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    try {
      data = serialBlob.getBytes(10, 11);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }
  }
  public void testTruncateJ() throws Exception {
    byte[] buf = {1, 2, 3, 4, 5, 6, 7, 8};
    SerialBlob serialBlob1 = new SerialBlob(buf);
    MockSerialBlob mockBlob = new MockSerialBlob();
    mockBlob.buf = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
    SerialBlob serialBlob2 = new SerialBlob(mockBlob);
    SerialBlob[] serialBlobs = {serialBlob1, serialBlob2};

    for (SerialBlob serialBlob : serialBlobs) {

      serialBlob.truncate(3);
      assertEquals(3L, serialBlob.length());
      byte[] truncatedBytes = serialBlob.getBytes(1, 3);
      assertTrue(Arrays.equals(new byte[] {1, 2, 3}, truncatedBytes));

      try {
        serialBlob.truncate(-1);
        fail("should throw SerialException");
      } catch (SerialException e) {
        // expected
      }

      // Non bug difference from RI, Harmony-2937
      assertEquals(3L, serialBlob.length());

      try {
        serialBlob.truncate(10);
        fail("should throw SerialException");
      } catch (SerialException e) {
        // expected
      }
    }
  }
  public void testSetBytesJ$B() throws Exception {
    byte[] buf = {1, 2, 3, 4, 5, 6, 7, 8};
    byte[] theBytes = {9, 10, 11};
    SerialBlob serialBlob = new SerialBlob(buf);

    int count = serialBlob.setBytes(1, theBytes);
    byte[] res = serialBlob.getBytes(1, buf.length);
    byte[] expected = {9, 10, 11, 4, 5, 6, 7, 8};
    assertTrue(Arrays.equals(expected, res));
    assertEquals(3, count);

    count = serialBlob.setBytes(2, theBytes);
    res = serialBlob.getBytes(1, buf.length);
    expected = new byte[] {9, 9, 10, 11, 5, 6, 7, 8};
    assertTrue(Arrays.equals(expected, res));
    assertEquals(3, count);

    count = serialBlob.setBytes(6, theBytes);
    res = serialBlob.getBytes(1, buf.length);
    expected = new byte[] {9, 9, 10, 11, 5, 9, 10, 11};
    assertTrue(Arrays.equals(expected, res));
    assertEquals(3, count);

    try {
      serialBlob.setBytes(-1, theBytes);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    try {
      serialBlob.setBytes(10, theBytes);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }

    // Non bug difference from RI, Harmony-2836
    try {
      serialBlob.setBytes(7, theBytes);
      fail("should throw SerialException");
    } catch (SerialException e) {
      // expected
    }
  }
  public void testGetBinaryStream() throws Exception {
    byte[] buf = {1, 2, 3, 4, 5, 6, 7, 8};
    SerialBlob serialBlob = new SerialBlob(buf);
    InputStream is = serialBlob.getBinaryStream();
    int i = 0;
    while (true) {
      int b = is.read();
      if (b == -1) {
        if (i < buf.length) {
          fail("returned input stream contains too few data");
        }
        break;
      }

      if (i > buf.length) {
        fail("returned input stream contains too much data");
      }
      assertEquals(buf[i++], b);
    }
  }