More tests

pull/1/head
Brett Wooldridge 11 years ago
parent 837c4bdb55
commit 315d09d62f

@ -0,0 +1,55 @@
package com.zaxxer.hikari;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Test;
public class ExceptionTest
{
@Test
public void testException1() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumPoolSize(1);
config.setMaximumPoolSize(2);
config.setAcquireIncrement(1);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections());
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections());
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
Assert.assertNotNull(statement);
ResultSet resultSet = statement.executeQuery();
Assert.assertNotNull(resultSet);
try
{
resultSet.getFloat(1);
Assert.fail();
}
catch (SQLException e)
{
Assert.assertSame(SQLException.class, e.getClass());
}
connection.close();
Assert.assertSame("Totals connections not as expected", 0, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections());
}
}

@ -0,0 +1,45 @@
package com.zaxxer.hikari;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Assert;
import org.junit.Test;
public class StatementTest
{
@Test
public void testStatementClose() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumPoolSize(1);
config.setMaximumPoolSize(2);
config.setAcquireIncrement(1);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections());
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections());
Statement statement = connection.createStatement();
Assert.assertNotNull(statement);
ResultSet resultSet = statement.executeQuery("SELECT * from foo");
Assert.assertNotNull(resultSet);
connection.close();
Assert.assertTrue(statement.isClosed());
Assert.assertTrue(resultSet.isClosed());
}
}

@ -43,7 +43,7 @@ import java.util.Calendar;
*
* @author Brett Wooldridge
*/
public class StubPreparedStatement implements PreparedStatement
public class StubPreparedStatement extends StubStatement implements PreparedStatement
{
/** {@inheritDoc} */
@ -58,11 +58,6 @@ public class StubPreparedStatement implements PreparedStatement
return 0;
}
/** {@inheritDoc} */
public void close() throws SQLException
{
}
/** {@inheritDoc} */
public int getMaxFieldSize() throws SQLException
{
@ -256,12 +251,6 @@ public class StubPreparedStatement implements PreparedStatement
return 0;
}
/** {@inheritDoc} */
public boolean isClosed() throws SQLException
{
return false;
}
/** {@inheritDoc} */
public void setPoolable(boolean poolable) throws SQLException
{

@ -45,6 +45,7 @@ import java.util.Map;
public class StubResultSet implements ResultSet
{
private int counter;
private boolean closed;
/** {@inheritDoc} */
public <T> T unwrap(Class<T> iface) throws SQLException
@ -68,6 +69,7 @@ public class StubResultSet implements ResultSet
/** {@inheritDoc} */
public void close() throws SQLException
{
closed = true;
}
/** {@inheritDoc} */
@ -115,7 +117,7 @@ public class StubResultSet implements ResultSet
/** {@inheritDoc} */
public float getFloat(int columnIndex) throws SQLException
{
return 0;
throw new SQLException("No reason", "08999");
}
/** {@inheritDoc} */
@ -865,7 +867,7 @@ public class StubResultSet implements ResultSet
/** {@inheritDoc} */
public boolean isClosed() throws SQLException
{
return false;
return closed;
}
/** {@inheritDoc} */

@ -21,6 +21,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
/**
*
@ -28,6 +29,9 @@ import java.sql.Statement;
*/
public class StubStatement implements Statement
{
private boolean closed;
private ArrayList<ResultSet> resultSets = new ArrayList<ResultSet>();
/** {@inheritDoc} */
public <T> T unwrap(Class<T> iface) throws SQLException
{
@ -43,7 +47,9 @@ public class StubStatement implements Statement
/** {@inheritDoc} */
public ResultSet executeQuery(String sql) throws SQLException
{
return new StubResultSet();
StubResultSet resultSet = new StubResultSet();
resultSets.add(resultSet);
return resultSet;
}
/** {@inheritDoc} */
@ -55,6 +61,12 @@ public class StubStatement implements Statement
/** {@inheritDoc} */
public void close() throws SQLException
{
for (ResultSet resultSet : resultSets)
{
resultSet.close();
}
closed = true;
}
/** {@inheritDoc} */
@ -253,7 +265,7 @@ public class StubStatement implements Statement
/** {@inheritDoc} */
public boolean isClosed() throws SQLException
{
return false;
return closed;
}
/** {@inheritDoc} */

Loading…
Cancel
Save