Skip to content

Commit

Permalink
Add test for ResultSetIteratingPipe (frankframework#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored Jul 28, 2021
1 parent 4c26329 commit 955806f
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.configuration.ConfigurationWarning;
import nl.nn.adapterframework.core.HasPhysicalDestination;
import nl.nn.adapterframework.core.IDataIterator;
import nl.nn.adapterframework.core.PipeLineSession;
Expand Down Expand Up @@ -97,7 +97,6 @@ public void stop() {

protected abstract IDataIterator<String> getIterator(IDbmsSupport dbmsSupport, Connection conn, ResultSet rs) throws SenderException;

@SuppressWarnings("finally")
@Override
protected IDataIterator<String> getIterator(Message message, PipeLineSession session, Map<String,Object> threadContext) throws SenderException {
Connection connection = null;
Expand All @@ -122,21 +121,12 @@ protected IDataIterator<String> getIterator(Message message, PipeLineSession ses
if (rs!=null) {
JdbcUtil.fullClose(connection, rs);
} else {
if (statement!=null) {
JdbcUtil.fullClose(connection, statement);
} else {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e1) {
log.debug(getLogPrefix(session) + "caught exception closing sender after exception",e1);
}
}
}
JdbcUtil.fullClose(connection, statement);
}
} finally {
throw new SenderException(getLogPrefix(session),t);
} catch (Throwable t2) {
t.addSuppressed(t2);
}
throw new SenderException(getLogPrefix(session), t);
}
}

Expand All @@ -150,6 +140,8 @@ public String getPhysicalDestinationName() {
return querySender.getPhysicalDestinationName();
}

@Deprecated
@ConfigurationWarning("Please use attribute dataSourceName instead")
public void setJmsRealm(String jmsRealmName) {
querySender.setJmsRealm(jmsRealmName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ResultSetIterator implements IDataIterator<String> {
private IDbmsSupport dbmsSupport;
private Connection conn;
private ResultSet rs;

private ResultSetMetaData rsmeta;
private boolean lineChecked=true; // assumes at least one line is present, and cursor is on it!
private boolean lineAvailable=true;
Expand All @@ -63,8 +63,8 @@ public ResultSetIterator(IDbmsSupport dbmsSupport, Connection conn, ResultSet rs
public boolean hasNext() throws SenderException {
try {
if (!lineChecked) {
lineAvailable=!rs.isClosed() && rs.next();
lineChecked=true;
lineAvailable = rs.next();
lineChecked = true;
}
return lineAvailable;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public void run() {
if (semaphore!=null) {
semaphore.release();
}
guard.releaseResource();
if(guard != null) {
guard.releaseResource();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package nl.nn.adapterframework.jdbc;

import org.junit.After;
import org.junit.Before;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.configuration.ConfigurationWarnings;
import nl.nn.adapterframework.core.Adapter;
import nl.nn.adapterframework.core.IPipe;
import nl.nn.adapterframework.core.PipeForward;
import nl.nn.adapterframework.core.PipeLine;
import nl.nn.adapterframework.core.PipeLineExit;
import nl.nn.adapterframework.core.PipeLineSession;
import nl.nn.adapterframework.core.PipeRunException;
import nl.nn.adapterframework.core.PipeRunResult;
import nl.nn.adapterframework.stream.Message;
import nl.nn.adapterframework.testutil.TestConfiguration;

public abstract class JdbcEnabledPipeTestBase<P extends IPipe> extends JdbcTestBase {

protected PipeLineSession session = new PipeLineSession();

protected P pipe;
protected PipeLine pipeline;
protected Adapter adapter;
private static TestConfiguration configuration;

private TestConfiguration getConfiguration() {
if(configuration == null) {
configuration = new TestConfiguration();
}
return configuration;
}

public abstract P createPipe();

@Override
@Before
public void setup() throws Exception {
super.setup();
pipe = createPipe();
autowireByType(pipe);
pipe.registerForward(new PipeForward("success", "exit"));
pipe.setName(pipe.getClass().getSimpleName()+" under test");
pipeline = getConfiguration().createBean(PipeLine.class);
pipeline.addPipe(pipe);
PipeLineExit exit = new PipeLineExit();
exit.setPath("exit");
exit.setState("success");
pipeline.registerPipeLineExit(exit);
adapter = getConfiguration().createBean(Adapter.class);
adapter.setName("TestAdapter-for-".concat(pipe.getClass().getSimpleName()));
adapter.setPipeLine(pipeline);
}

@After
public void tearDown() throws Exception {
getConfigurationWarnings().destroy();
getConfigurationWarnings().afterPropertiesSet();
pipe = null;
pipeline = null;
adapter = null;
}

protected void autowireByType(Object bean) {
getConfiguration().autowireByType(bean);
}

protected void autowireByName(Object bean) {
getConfiguration().autowireByName(bean);
}

protected ConfigurationWarnings getConfigurationWarnings() {
return getConfiguration().getConfigurationWarnings();
}

/**
* Configure the pipe, don't forget to call {@link IPipe#start()} after configuring!
*/
protected void configurePipe() throws ConfigurationException {
pipeline.configure();
}

protected PipeRunResult doPipe(String input) throws PipeRunException {
return doPipe(new Message(input));
}
protected PipeRunResult doPipe(Message input) throws PipeRunException {
return pipe.doPipe(input, session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public class JdbcTableListenerTest extends JdbcTestBase {
*/
private boolean testNegativePeekWhileGet = false;

public JdbcTableListenerTest(DataSource dataSourceName) throws SQLException {
super(dataSourceName);
public JdbcTableListenerTest() {
listener = new JdbcTableListener() {

@Override
Expand Down
11 changes: 7 additions & 4 deletions core/src/test/java/nl/nn/adapterframework/jdbc/JdbcTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
Expand All @@ -32,24 +33,26 @@ public abstract class JdbcTestBase {
protected String productKey = "unknown";

protected static Connection connection; // only to be used for setup and teardown like actions
protected DataSource dataSource;

@Parameterized.Parameter(0)
public DataSource dataSource;
protected IDbmsSupport dbmsSupport;

@Parameters(name= "{index}: {0}")
public static Iterable<DataSource> data() {
return dataSourceFactory.getAvailableDataSources();
}

public JdbcTestBase(DataSource dataSource) throws SQLException {
this.dataSource = dataSource;

@Before
public void setup() throws Exception {
if(dataSource instanceof DriverManagerDataSource) {
Properties dataSourceProperties = ((DriverManagerDataSource)dataSource).getConnectionProperties();
productKey = dataSourceProperties.getProperty(URLDataSourceFactory.PRODUCT_KEY);
testPeekShouldSkipRecordsAlreadyLocked = Boolean.parseBoolean(dataSourceProperties.getProperty(URLDataSourceFactory.TEST_PEEK_KEY));
}

connection = dataSource.getConnection();

DbmsSupportFactory factory = new DbmsSupportFactory();
dbmsSupport = factory.getDbmsSupport(connection);
try {
Expand Down
Loading

0 comments on commit 955806f

Please sign in to comment.