Search in sources :

Example 1 with ExpectedException

use of org.junit.rules.ExpectedException in project hive by apache.

the class TestJdbcDriver2 method testPrepareStatement.

@Test
public void testPrepareStatement() {
    String sql = "FROM (SELECT 1 FROM " + tableName + " where   'not?param?not?param' <> 'not_param??not_param' and ?=? " + " and 1=? and 2=? and 3.0=? and 4.0=? and 'test\\'string\"'=? and 5=? and ?=? " + " and date '2012-01-01' = date ?" + " and timestamp '2012-04-22 09:00:00.123456789' = timestamp ?" + " ) t SELECT '2011-03-25' ddate,'China',true bv, 10 num LIMIT 1";
    // executed twice: once with the typed ps setters, once with the generic setObject
    try {
        try (PreparedStatement ps = createPreapredStatementUsingSetXXX(sql);
            ResultSet res = ps.executeQuery()) {
            assertPreparedStatementResultAsExpected(res);
        }
        try (PreparedStatement ps = createPreapredStatementUsingSetObject(sql);
            ResultSet res = ps.executeQuery()) {
            assertPreparedStatementResultAsExpected(res);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    }
    // set nothing for prepared sql
    Exception expectedException = null;
    try (PreparedStatement ps = con.prepareStatement(sql);
        ResultSet ignored = ps.executeQuery()) {
    } catch (Exception e) {
        expectedException = e;
    }
    assertNotNull("Execute the un-setted sql statement should throw exception", expectedException);
    // set some of parameters for prepared sql, not all of them.
    expectedException = null;
    try (PreparedStatement ps = con.prepareStatement(sql)) {
        ps.setBoolean(1, true);
        ps.setBoolean(2, true);
        try (ResultSet ignored = ps.executeQuery()) {
        }
    } catch (Exception e) {
        expectedException = e;
    }
    assertNotNull("Execute the invalid setted sql statement should throw exception", expectedException);
    // set the wrong type parameters for prepared sql.
    expectedException = null;
    try (PreparedStatement ps = con.prepareStatement(sql)) {
        // wrong type here
        ps.setString(1, "wrong");
        try (ResultSet res = ps.executeQuery()) {
            assertFalse("ResultSet was not empty", res.next());
        }
    } catch (Exception e) {
        expectedException = e;
    }
    assertNotNull("Execute the invalid setted sql statement should throw exception", expectedException);
    // setObject to the yet unknown type java.util.Date
    expectedException = null;
    try (PreparedStatement ps = con.prepareStatement(sql)) {
        ps.setObject(1, new Date());
        try (ResultSet ignored = ps.executeQuery()) {
        }
    } catch (Exception e) {
        expectedException = e;
    }
    assertNotNull("Setting to an unknown type should throw an exception", expectedException);
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) String(java.lang.String) SQLTimeoutException(java.sql.SQLTimeoutException) ParseException(java.text.ParseException) Exception(java.lang.Exception) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ExpectedException

use of org.junit.rules.ExpectedException in project alluxio by Alluxio.

the class IndexedSetTest method addTheSameObjectMultipleTimes.

/**
   * Tests that the {@link IndexedSet} works correctly when adding the same object multiple times.
   */
@Test
public void addTheSameObjectMultipleTimes() {
    final ExpectedException exception = ExpectedException.none();
    for (int i = 0; i < 3; i++) {
        Assert.assertEquals(9, mSet.size());
        Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, i).size());
        for (Pair p : mSet.getByField(mNonUniqueIntIndex, i)) {
            exception.expect(IllegalStateException.class);
            exception.expectMessage("Adding more than one value to a unique index.");
            mSet.add(p);
        }
        Assert.assertEquals(9, mSet.size());
        Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, i).size());
    }
    try {
        mSet.add(new Pair(1, 9L));
    } catch (IllegalStateException e) {
        Assert.fail();
    }
    Assert.assertEquals(10, mSet.size());
    Assert.assertEquals(4, mSet.getByField(mNonUniqueIntIndex, 1).size());
}
Also used : ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ExpectedException (org.junit.rules.ExpectedException)2 Exception (java.lang.Exception)1 String (java.lang.String)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1