Skip to content

Commit

Permalink
Merge branch 'master' into dev_results_db
Browse files Browse the repository at this point in the history
  • Loading branch information
jakob-zwiener committed May 20, 2014
2 parents f785036 + c931c69 commit dc2b7e5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package de.uni_potsdam.hpi.metanome.algorithm_integration.input;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Generates new copies of {@link RelationalInput}s or {@link ResultSet}s.
Expand All @@ -42,4 +43,9 @@ public interface SqlInputGenerator extends AutoCloseable {
* @throws InputGenerationException if the result cannot be generated
*/
ResultSet generateResultSetFromSql(String queryString) throws InputGenerationException;

/**
* Closes all executed statements.
*/
void closeAllStatements() throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ public ImmutableList<String> columnNames() {
@Override
public void close() throws Exception {
resultSet.close();
resultSet.getStatement().close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import de.uni_potsdam.hpi.metanome.algorithm_integration.input.SqlInputGenerator;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class SqlIteratorGenerator implements SqlInputGenerator {

protected Connection dbConnection;
private List<Statement> statements = new LinkedList<>();

/**
* Exists for tests.
Expand Down Expand Up @@ -67,13 +70,13 @@ protected ResultSet executeQuery(String queryString) throws InputGenerationExcep
Statement sqlStatement;
try {
sqlStatement = dbConnection.createStatement();
statements.add(sqlStatement);
} catch (SQLException e) {
throw new InputGenerationException("Could not create sql statement on connection.");
}
ResultSet resultSet;
try {
resultSet = sqlStatement.executeQuery(queryString);
sqlStatement.closeOnCompletion();
} catch (SQLException e) {
throw new InputGenerationException("Could not execute sql statement.");
}
Expand All @@ -86,6 +89,13 @@ public ResultSet generateResultSetFromSql(String queryString) throws InputGenera
return executeQuery(queryString);
}

@Override
public void closeAllStatements() throws SQLException {
for (Statement statement : statements) {
statement.close();
}
}

@Override
public void close() throws SQLException {
dbConnection.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package de.uni_potsdam.hpi.metanome.input.sql;

import de.uni_potsdam.hpi.metanome.algorithm_integration.input.InputGenerationException;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

/**
* Tests for {@link de.uni_potsdam.hpi.metanome.input.sql.SqlIterator}
Expand All @@ -31,7 +33,6 @@
*/
public class SqlIteratorGeneratorTest {


/**
* Test method for {@link SqlIteratorGenerator#close()}
* <p/>
Expand All @@ -52,4 +53,33 @@ public void testClose() throws SQLException {
// Check result
verify(connection).close();
}

/**
* Test method for {@link de.uni_potsdam.hpi.metanome.input.sql.SqlIteratorGenerator#executeQuery(String)} and {@link SqlIteratorGenerator#closeAllStatements()}
* <p/>
* All executed statements should be stored and closed on closeAllStatements.
*/
@Test
public void testCloseAllStatements() throws SQLException, InputGenerationException {
// Setup
SqlIteratorGenerator sqlIteratorGenerator = new SqlIteratorGenerator();
Connection connection = mock(Connection.class);
sqlIteratorGenerator.dbConnection = connection;

Statement statementMock1 = mock(Statement.class);
when(statementMock1.executeQuery(anyString())).thenReturn(mock(ResultSet.class));
Statement statementMock2 = mock(Statement.class);
when(statementMock2.executeQuery(anyString())).thenReturn(mock(ResultSet.class));

when(connection.createStatement()).thenReturn(statementMock1, statementMock2);

// Execute functionality
sqlIteratorGenerator.executeQuery("some query 1");
sqlIteratorGenerator.executeQuery("some query 2");
sqlIteratorGenerator.closeAllStatements();

// Check result
verify(statementMock1).close();
verify(statementMock2).close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -178,21 +179,24 @@ public void testColumnNames() throws SQLException {
/**
* Test method for {@link SqlIterator#close()}
* <p/>
* The sql iterator should be closeable. After closing the iterator, the underlying result set should be closed.
* The sql iterator should be closeable. After closing the iterator, the underlying result set and statement should be closed.
*
* @throws java.lang.Exception
*/
@Test
public void testClose() throws Exception {
// Setup
ResultSet resultSetMock = twoLinesResultSetFixture.getTestData();
Statement statementMock = mock(Statement.class);
when(resultSetMock.getStatement()).thenReturn(statementMock);
SqlIterator sqlIterator = new SqlIterator(resultSetMock);

// Execute functionality
sqlIterator.close();

// Check result
verify(resultSetMock).close();
verify(statementMock).close();
}

}

0 comments on commit dc2b7e5

Please sign in to comment.