Esempio n. 1
1
  /**
   * Tests the implementation of getCharacterStream(long pos, long length).
   *
   * @throws Exception
   */
  public void testGetCharacterStreamLong() throws Exception {
    String str1 = "This is a test String. This is a test String";

    Reader r1 = new java.io.StringReader(str1);

    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, r1);
    ps.execute();
    ps.close();

    Statement st = createStatement();

    ResultSet rs = st.executeQuery("select CLOBDATA from " + "BLOBCLOB where ID=" + id);
    rs.next();
    Clob clob = rs.getClob(1);

    Reader r_1 = clob.getCharacterStream(2L, 5L);
    String str2 = str1.substring(1, 6);
    Reader r_2 = new java.io.StringReader(str2);

    assertEquals(r_2, r_1);

    rs.close();
    st.close();
  }
Esempio n. 2
0
  /**
   * Tests that the Reader got from a empty Clob reflects new data in the underlying Clob.
   *
   * @throws Exception
   */
  public void testGetCharacterStreamCreateClob() throws Exception {
    // The String that will be used
    // to do the inserts into the
    // Clob.
    String str = "Hi I am the insert String";

    // The string reader corresponding to this
    // string that will be used in the comparison.
    StringReader r_string = new StringReader(str);

    // create the empty Clob.
    Clob clob = getConnection().createClob();

    // Get the Reader from this
    // Clob
    Reader r_clob = clob.getCharacterStream();

    // set the String into the clob.
    clob.setString(1, str);

    // Now compare the reader corresponding
    // to the string and the reader obtained
    // form the clob to see if they match.
    assertEquals(r_string, r_clob);
  }
Esempio n. 3
0
  /**
   * Tests that the InputStream got from a empty Clob reflects new data in the underlying Clob.
   *
   * @throws Exception
   */
  public void testGetAsciiStreamCreateClob() throws Exception {
    // The String that will be used
    // to do the inserts into the
    // Clob.
    String str = "Hi I am the insert String";

    // Create the InputStream that will
    // be used for comparing the Stream
    // that is obtained from the Blob after
    // the update.
    ByteArrayInputStream str_is = new ByteArrayInputStream(str.getBytes("US-ASCII"));

    // create the empty Clob.
    Clob clob = getConnection().createClob();

    // Get the InputStream from this
    // Clob
    InputStream is = clob.getAsciiStream();

    // set the String into the clob.
    clob.setString(1, str);

    // Ensure that the Stream obtained from
    // the clob contains the expected bytes
    assertEquals(str_is, is);
  }
Esempio n. 4
0
  /**
   * Test that <code>Clob.getCharacterStream(long,long)</code> works on CLOBs that are streamed from
   * store. (DERBY-2891)
   */
  public void testGetCharacterStreamLongOnLargeClob() throws Exception {
    getConnection().setAutoCommit(false);

    // create large (>32k) clob that can be read from store
    final int size = 33000;
    StringBuilder sb = new StringBuilder(size);
    for (int i = 0; i < size; i += 10) {
      sb.append("1234567890");
    }

    final int id = BlobClobTestSetup.getID();
    PreparedStatement ps =
        prepareStatement("insert into blobclob(id, clobdata) values (?,cast(? as clob))");
    ps.setInt(1, id);
    ps.setString(2, sb.toString());
    ps.executeUpdate();
    ps.close();

    Statement s = createStatement();
    ResultSet rs = s.executeQuery("select clobdata from blobclob where id = " + id);
    assertTrue(rs.next());
    Clob c = rs.getClob(1);

    // request a small region of the clob
    BufferedReader r = new BufferedReader(c.getCharacterStream(4L, 3L));
    assertEquals("456", r.readLine());

    r.close();
    c.free();
    rs.close();
    s.close();
    rollback();
  }
Esempio n. 5
0
  public Item(int id, String name, int price, int quantity, Clob description) throws SQLException {

    this.id = id;
    this.name = name;
    this.price = price;
    this.quantity = quantity;
    this.description = description;
    this.stringDescr = description.getSubString(1L, (int) description.length());
  }
Esempio n. 6
0
  /**
   * Creates an item object from String parameters.
   *
   * @param conn connection to database
   * @param param map of parameters to set
   * @return an item object
   */
  public static Item createItem(Connection conn, Map<String, String[]> param) throws SQLException {

    Item item = new Item();
    item.setName(param.get("name")[0]);
    item.setPrice(Integer.parseInt(param.get("price")[0]));
    item.setQuantity(Integer.parseInt(param.get("quantity")[0]));
    Clob cl = conn.createClob();
    cl.setString(1, param.get("description")[0]);
    item.setDescription(cl);
    item.setStringDescr(cl);
    return item;
  }
Esempio n. 7
0
 protected void tearDown() throws Exception {
   if (clob != null) {
     clob.free();
     clob = null;
   }
   excludedMethodSet = null;
   super.tearDown();
 }
Esempio n. 8
0
  /**
   * Inserts a Clob with the specified length, using a stream source, then fetches it from the
   * database and checks the length.
   *
   * @param length number of characters in the Clob
   * @throws IOException if reading from the source fails
   * @throws SQLException if something goes wrong
   */
  private void insertAndFetchTest(long length) throws IOException, SQLException {
    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
    long tsStart = System.currentTimeMillis();
    ps.execute();
    println(
        "Inserted "
            + length
            + " chars (length specified) in "
            + (System.currentTimeMillis() - tsStart)
            + " ms");
    Statement stmt = createStatement();
    tsStart = System.currentTimeMillis();
    ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
    assertTrue("Clob not inserted", rs.next());
    Clob aClob = rs.getClob(1);
    assertEquals("Invalid length", length, aClob.length());
    println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
    rs.close();

    // Insert same Clob again, using the lengthless override.
    id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, new LoopingAlphabetReader(length));
    tsStart = System.currentTimeMillis();
    ps.executeUpdate();
    println(
        "Inserted "
            + length
            + " chars (length unspecified) in "
            + (System.currentTimeMillis() - tsStart)
            + " ms");
    rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
    assertTrue("Clob not inserted", rs.next());
    aClob = rs.getClob(1);
    assertEquals("Invalid length", length, aClob.length());
    println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
    rs.close();

    rollback();
  }
Esempio n. 9
0
  /**
   * Test that a lock held on the corresponding row is released when free() is called on the Clob
   * object.
   *
   * @throws java.sql.SQLException
   */
  public void testLockingAfterFree() throws SQLException {
    int id = initializeLongClob(); // Opens clob object
    executeParallelUpdate(id, true); // Test that timeout occurs

    // Test that update goes through after the clob is closed
    clob.free();
    executeParallelUpdate(id, false);

    commit();
  }
Esempio n. 10
0
  /**
   * Test that a lock held on the corresponding row is released when free() is called on the Clob
   * object if the isolation level is Read Uncommitted
   *
   * @throws java.sql.SQLException
   */
  public void testLockingAfterFreeWithDirtyReads() throws SQLException {
    getConnection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    int id = initializeLongClob(); // Opens clob object
    executeParallelUpdate(id, true); // Test that timeout occurs

    // Test that update goes through after the clob is closed
    clob.free();
    executeParallelUpdate(id, false);

    commit();
  }
Esempio n. 11
0
  /**
   * Test that a lock held on the corresponding row is NOT released when free() is called on the
   * Clob object if the isolation level is Repeatable Read
   *
   * @throws java.sql.SQLException
   */
  public void testLockingAfterFreeWithRR() throws SQLException {
    getConnection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    int id = initializeLongClob(); // Opens clob object
    executeParallelUpdate(id, true); // Test that timeout occurs

    // Test that update still times out after the clob is closed
    clob.free();
    executeParallelUpdate(id, true);

    // Test that the update goes through after the transaction has committed
    commit();
    executeParallelUpdate(id, false);
  }
Esempio n. 12
0
  /**
   * Tests that the data updated in a Clob is always reflected in the InputStream got. Here the
   * updates into the Clob are done using both an OutputStream obtained from this Clob as well as
   * using Clob.setString.
   *
   * @throws Exception
   */
  public void testGetAsciiStreamClobUpdates() throws Exception {
    // The String that will be used
    // to do the inserts into the
    // Clob.
    String str1 = "Hi I am the insert string";

    // Stores the byte array representation of
    // the insert string.
    byte[] str1_bytes = str1.getBytes();

    // The String that will be used in the
    // second series of updates
    String str2 = "Hi I am the update string";

    // create the empty Clob.
    Clob clob = getConnection().createClob();

    // Get the InputStream from this
    // Clob before any writes happen.
    InputStream is_BeforeWrite = clob.getAsciiStream();

    // Get an OutputStream from this Clob
    // into which the data can be written
    OutputStream os = clob.setAsciiStream(1);
    os.write(str1_bytes);

    // Doing a setString now on the Clob
    // should reflect the same extension
    // in the InputStream also.
    clob.setString((str1_bytes.length) + 1, str2);

    // Get the input stream from the
    // Clob after the update
    InputStream is_AfterWrite = clob.getAsciiStream();

    // Now check if the two InputStreams
    // match
    assertEquals(is_BeforeWrite, is_AfterWrite);
  }
Esempio n. 13
0
  /**
   * Tests that the data updated in a Clob is always reflected in the Reader got. Here the updates
   * are done using both a Writer obtained from this Clob and using Clob.setString.
   *
   * @throws Exception
   */
  public void testGetCharacterStreamClobUpdates() throws Exception {
    // The String that will be used
    // to do the inserts into the
    // Clob.
    String str1 = "Hi I am the insert string";

    // The String that will be used in the
    // second series of updates
    String str2 = "Hi I am the update string";

    // create the empty Clob.
    Clob clob = getConnection().createClob();

    // Get the Reader from this
    // Clob
    Reader r_BeforeWrite = clob.getCharacterStream();

    // Get a writer from this Clob
    // into which the data can be written
    Writer w = clob.setCharacterStream(1);
    char[] chars_str1 = new char[str1.length()];
    str2.getChars(0, str1.length(), chars_str1, 0);
    w.write(chars_str1);

    // Doing a setString now on the Clob
    // should reflect the same extension
    // in the InputStream also.
    clob.setString((str1.length()) + 1, str2);

    // Now get the reader from the Clob after
    // the update has been done.
    Reader r_AfterWrite = clob.getCharacterStream();

    // Now compare the two readers to see that they
    // contain the same data.
    assertEquals(r_BeforeWrite, r_AfterWrite);
  }
Esempio n. 14
0
  /**
   * Tests the implementation for the free() method in the Clob interface.
   *
   * @throws SQLException if an error occurs during releasing the Clob resources
   */
  public void testFreeandMethodsAfterCallingFree()
      throws IllegalAccessException, InvocationTargetException, SQLException {
    clob = BlobClobTestSetup.getSampleClob(getConnection());

    // call the buildHashSetMethod to initialize the
    // HashSet with the method signatures that are exempted
    // from throwing a SQLException after free has been called
    // on the Clob object.
    buildHashSet();

    InputStream asciiStream = clob.getAsciiStream();
    Reader charStream = clob.getCharacterStream();
    clob.free();
    // testing the idempotence of the free() method
    // the method can be called multiple times on

    // the first are treated as no-ops
    clob.free();

    // to the free method so testing calling
    // a method on this invalid object should throw
    // an SQLException
    buildMethodList(clob);
  }
Esempio n. 15
0
  /**
   * Tests the exceptions thrown by the getCharacterStream (long pos, long length) for the following
   * conditions a) pos <= 0 b) pos > (length of LOB) c) length < 0 d) pos + length > (length of
   * LOB).
   *
   * @throws SQLException
   */
  public void testGetCharacterStreamLongExceptionConditions() throws SQLException {
    String str1 = "This is a test String. This is a test String";

    Reader r1 = new java.io.StringReader(str1);

    PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
    int id = BlobClobTestSetup.getID();
    ps.setInt(1, id);
    ps.setCharacterStream(2, r1);
    ps.execute();
    ps.close();

    Statement st = createStatement();

    ResultSet rs = st.executeQuery("select CLOBDATA from " + "BLOBCLOB where ID=" + id);
    rs.next();
    Clob clob = rs.getClob(1);
    // check the case where pos <= 0
    try {
      // set pos as negative
      clob.getCharacterStream(-2L, 5L);
      // Should not come here. The exception has to be thrown.
      fail("FAIL: Expected SQLException for pos being negative " + "not thrown");
    } catch (SQLException sqle) {
      // The SQLState for the exception thrown when pos <= 0 is XJ070
      assertSQLState("XJ070", sqle);
    }

    // check for the case pos > length of clob
    try {
      // set the pos to any value greater than the Clob length
      clob.getCharacterStream(clob.length() + 1, 5L);
      // Should not come here. The exception has to be thrown.
      fail(
          "FAIL: Expected SQLException for position being greater than "
              + "length of LOB not thrown");
    } catch (SQLException sqle) {
      // The SQLState for the exception thrown when pos > length of Clob
      // is XJ076
      assertSQLState("XJ087", sqle);
    }

    // check for the case when length < 0
    try {
      // set length as negative
      clob.getCharacterStream(2L, -5L);
      // Should not come here. The exception has to be thrown.
      fail("Fail: expected exception for the length being negative " + "not thrown");
    } catch (SQLException sqle) {
      // The SQLState for the exception thrown when length < 0 of Clob
      // is XJ071
      assertSQLState("XJ071", sqle);
    }

    // check for the case when pos + length > length of Clob
    try {
      // set pos + length > length of Clob
      clob.getCharacterStream((clob.length() - 4), 10L);
      // Should not come here. The exception has to be thrown.
      fail(
          "Fail: expected exception for the sum of position and length"
              + " being greater than the LOB size not thrown");
    } catch (SQLException sqle) {
      // The SQLState for the exception thrown when length < 0 of Clob
      // is XJ087
      assertSQLState("XJ087", sqle);
    }
  }
Esempio n. 16
0
  public void setStringDescr(Clob description) throws SQLException {

    this.stringDescr = description.getSubString(1L, (int) description.length());
  }