package de.superx.spring.batch.reader; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import org.h2.jdbcx.JdbcDataSource; import org.junit.Assert; import org.junit.Test; import de.superx.job.ExtractAction; public class JdbcExtractReaderTest { /** * Dieser Test soll sicher stellen, dass der "JdbcExtractReader" einen Schemafilter als Parameter übergibt; * dies ist notwendig, weil er Metadaten über Datenbanktabellen ausliest, um für einzelne Tabellen SQL-Statements * zu erzeugen - ohne die Filterung nach Schemata besteht die Gefahr, dass Metadaten unterschiedlicher Tabellen * gleichen Namens vermischt werden * @throws SQLException */ @SuppressWarnings("static-method") @Test public void testSchemaConsideration() throws SQLException { ExtractAction ea = createExtractAction(); JdbcDataSource dataSource = initializeH2Datasource(); try (Connection con = dataSource.getConnection(); Statement st = con.createStatement()) { JdbcExtractReader extrR = new JdbcExtractReader(ea, dataSource); extrR.beforeStep(null); String generated_sql = extrR.selectFromTableStmt; Assert.assertTrue("generated SQL should not contain 'should_not_appear' when schema filtering works", !generated_sql.contains("should_not_appear")); } catch (SQLException sqle) { throw sqle; } } private static JdbcDataSource initializeH2Datasource() throws SQLException { String h2Url = "jdbc:h2:mem:schema_test;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA extract_schema;MODE=POSTGRESQL;DATABASE_TO_LOWER=TRUE"; JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL(h2Url); dataSource.setUser("sa"); dataSource.setPassword(""); try (Connection con = dataSource.getConnection(); Statement st = con.createStatement()) { st.execute("CREATE TABLE extract_schema.extract_table(id INT, name TEXT);"); st.execute("CREATE SCHEMA a_schema2;"); st.execute("CREATE TABLE a_schema2.extract_table(should_not_appear TEXT);"); } h2Url = "jdbc:h2:mem:schema_test;DB_CLOSE_DELAY=-1;SCHEMA=extract_schema;MODE=POSTGRESQL;DATABASE_TO_LOWER=TRUE"; dataSource.setURL(h2Url); return dataSource; } private static ExtractAction createExtractAction() { ExtractAction ea = new ExtractAction(); ea.sourceTable = "extract_table"; ea.stagingSchema = "staging_schema"; ea.stagingTable = "staging_table"; ea.extractionScript = "1=1 -- extraction_script"; ea.dataSource = "dummy"; return ea; } }