Search in sources :

Example 1 with TJDBCConnectionProperties

use of org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties in project components by Talend.

the class JDBCRollbackTestIT method testClose.

@Test
public void testClose() throws IOException, ClassNotFoundException, SQLException {
    // connection part
    TJDBCConnectionDefinition connectionDefinition = new TJDBCConnectionDefinition();
    TJDBCConnectionProperties connectionProperties = DBTestUtils.createCommonJDBCConnectionProperties(allSetting, connectionDefinition);
    JDBCSourceOrSink sourceOrSink = new JDBCSourceOrSink();
    sourceOrSink.initialize(null, connectionProperties);
    ValidationResult result = sourceOrSink.validate(container);
    assertTrue(result.getStatus() == ValidationResult.Result.OK);
    // rollback part
    TJDBCRollbackDefinition rollbackDefinition = new TJDBCRollbackDefinition();
    TJDBCRollbackProperties rollbackProperties = (TJDBCRollbackProperties) rollbackDefinition.createRuntimeProperties();
    rollbackProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
    rollbackProperties.closeConnection.setValue(true);
    JDBCRollbackSourceOrSink rollbackSourceOrSink = new JDBCRollbackSourceOrSink();
    rollbackSourceOrSink.initialize(container, rollbackProperties);
    rollbackSourceOrSink.validate(container);
    try (java.sql.Connection refConnection = (java.sql.Connection) container.getComponentData(ComponentConstants.CONNECTION_KEY, refComponentId)) {
        assertTrue(refConnection != null);
        Assert.assertTrue(refConnection.isClosed());
    }
}
Also used : JDBCSourceOrSink(org.talend.components.jdbc.runtime.JDBCSourceOrSink) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) JDBCRollbackSourceOrSink(org.talend.components.jdbc.runtime.JDBCRollbackSourceOrSink) Connection(java.sql.Connection) TJDBCRollbackProperties(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackProperties) Connection(java.sql.Connection) TJDBCRollbackDefinition(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackDefinition) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) Test(org.junit.Test)

Example 2 with TJDBCConnectionProperties

use of org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties in project components by Talend.

the class JDBCRollbackTestIT method testRollback.

@SuppressWarnings("rawtypes")
@Test
public void testRollback() throws IOException, ClassNotFoundException, SQLException {
    // connection part
    TJDBCConnectionDefinition connectionDefinition = new TJDBCConnectionDefinition();
    TJDBCConnectionProperties connectionProperties = DBTestUtils.createCommonJDBCConnectionProperties(allSetting, connectionDefinition);
    JDBCSourceOrSink sourceOrSink = new JDBCSourceOrSink();
    sourceOrSink.initialize(null, connectionProperties);
    ValidationResult result = sourceOrSink.validate(container);
    assertTrue(result.getStatus() == ValidationResult.Result.OK);
    // output part
    TJDBCOutputDefinition outputDefinition = new TJDBCOutputDefinition();
    TJDBCOutputProperties outputProperties = (TJDBCOutputProperties) outputDefinition.createRuntimeProperties();
    outputProperties.main.schema.setValue(DBTestUtils.createTestSchema(tablename));
    outputProperties.updateOutputSchemas();
    outputProperties.tableSelection.tablename.setValue(tablename);
    outputProperties.dataAction.setValue(DataAction.INSERT);
    outputProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
    outputProperties.referencedComponent.setReference(connectionProperties);
    JDBCSink sink = new JDBCSink();
    sink.initialize(container, outputProperties);
    WriteOperation writerOperation = sink.createWriteOperation();
    writerOperation.initialize(container);
    JDBCOutputInsertWriter writer = (JDBCOutputInsertWriter) writerOperation.createWriter(container);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(outputProperties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(outputProperties.main.schema.getValue());
        r2.put(0, 5);
        r2.put(1, "xiaobai");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        writer.close();
    } finally {
        writer.close();
    }
    // rollback part
    TJDBCRollbackDefinition rollbackDefinition = new TJDBCRollbackDefinition();
    TJDBCRollbackProperties rollbackProperties = (TJDBCRollbackProperties) rollbackDefinition.createRuntimeProperties();
    rollbackProperties.referencedComponent.componentInstanceId.setValue(refComponentId);
    rollbackProperties.closeConnection.setValue(false);
    JDBCRollbackSourceOrSink rollbackSourceOrSink = new JDBCRollbackSourceOrSink();
    rollbackSourceOrSink.initialize(container, rollbackProperties);
    rollbackSourceOrSink.validate(container);
    // create another session and check if the data is inserted
    int count = -1;
    try (Connection conn = JdbcRuntimeUtils.createConnection(allSetting);
        Statement statement = conn.createStatement();
        ResultSet resultset = statement.executeQuery("select count(*) from " + tablename)) {
        if (resultset.next()) {
            count = resultset.getInt(1);
        }
    }
    Assert.assertEquals(3, count);
    try (java.sql.Connection refConnection = (java.sql.Connection) container.getComponentData(ComponentConstants.CONNECTION_KEY, refComponentId)) {
        assertTrue(refConnection != null);
        Assert.assertTrue(!refConnection.isClosed());
    }
}
Also used : Connection(java.sql.Connection) IndexedRecord(org.apache.avro.generic.IndexedRecord) Statement(java.sql.Statement) Connection(java.sql.Connection) TJDBCRollbackDefinition(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackDefinition) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) JDBCOutputInsertWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter) JDBCSourceOrSink(org.talend.components.jdbc.runtime.JDBCSourceOrSink) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) JDBCRollbackSourceOrSink(org.talend.components.jdbc.runtime.JDBCRollbackSourceOrSink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) TJDBCRollbackProperties(org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackProperties) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) JDBCSink(org.talend.components.jdbc.runtime.JDBCSink) ResultSet(java.sql.ResultSet) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 3 with TJDBCConnectionProperties

use of org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties in project components by Talend.

the class DBTestUtils method createCommonJDBCConnectionProperties.

public static TJDBCConnectionProperties createCommonJDBCConnectionProperties(AllSetting allSetting, TJDBCConnectionDefinition connectionDefinition) {
    TJDBCConnectionProperties connectionProperties = (TJDBCConnectionProperties) connectionDefinition.createRuntimeProperties();
    // TODO now framework doesn't support to load the JDBC jar by the setting
    // properties.connection.driverJar.setValue(null);
    connectionProperties.connection.driverClass.setValue(allSetting.getDriverClass());
    connectionProperties.connection.jdbcUrl.setValue(allSetting.getJdbcUrl());
    connectionProperties.connection.userPassword.userId.setValue(allSetting.getUsername());
    connectionProperties.connection.userPassword.password.setValue(allSetting.getPassword());
    return connectionProperties;
}
Also used : TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties)

Example 4 with TJDBCConnectionProperties

use of org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties in project components by Talend.

the class JDBCConnectionTestIT method testConnectionWithWrongDriver.

@Test
public void testConnectionWithWrongDriver() {
    TJDBCConnectionDefinition definition = new TJDBCConnectionDefinition();
    TJDBCConnectionProperties properties = (TJDBCConnectionProperties) definition.createRuntimeProperties();
    properties.connection.driverClass.setValue("wrongDriver");
    properties.connection.jdbcUrl.setValue(allSetting.getJdbcUrl());
    properties.connection.userPassword.userId.setValue(allSetting.getUsername());
    properties.connection.userPassword.password.setValue(allSetting.getPassword());
    JDBCSourceOrSink sourceOrSink = new JDBCSourceOrSink();
    sourceOrSink.initialize(null, properties);
    ValidationResult result = sourceOrSink.validate(null);
    assertTrue(result.getStatus() == ValidationResult.Result.ERROR);
    assertTrue(result.getMessage() != null && !result.getMessage().isEmpty());
}
Also used : JDBCSourceOrSink(org.talend.components.jdbc.runtime.JDBCSourceOrSink) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) Test(org.junit.Test)

Example 5 with TJDBCConnectionProperties

use of org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties in project components by Talend.

the class JDBCConnectionTestIT method testDynamicLibLoad.

// we want to test the dynamic library loading with the field driverPath, but fail, so seems that no way to test it
// now
@Ignore
@Test
public void testDynamicLibLoad() {
    TJDBCConnectionDefinition definition = new TJDBCConnectionDefinition();
    TJDBCConnectionProperties properties = DBTestUtils.createCommonJDBCConnectionProperties(allSetting, definition);
    // the maven path below is a library at my local repository, you can change it to any jdbc driver which exists in your
    // maven repository for running the test. I can't use derby for test as i have added the derby dependency in the pom file.
    properties.connection.driverTable.drivers.setValue(Arrays.asList("mvn:org.talend.libraries/mysql-connector-java-5.1.30-bin/6.0.0"));
    RuntimeInfo runtimeInfo = definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(runtimeInfo, definition.getClass().getClassLoader())) {
        sandboxedInstance.getInstance();
        Class.forName("org.gjt.mm.mysql.Driver");
    } catch (ClassNotFoundException e) {
        Assert.fail("can't find the jdbc driver class, fail to load the library");
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCConnectionProperties(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) TJDBCConnectionDefinition(org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TJDBCConnectionProperties (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionProperties)16 Test (org.junit.Test)14 TJDBCConnectionDefinition (org.talend.components.jdbc.tjdbcconnection.TJDBCConnectionDefinition)14 JDBCSourceOrSink (org.talend.components.jdbc.runtime.JDBCSourceOrSink)12 ValidationResult (org.talend.daikon.properties.ValidationResult)12 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)2 JDBCCommitSourceOrSink (org.talend.components.jdbc.runtime.JDBCCommitSourceOrSink)2 JDBCRollbackSourceOrSink (org.talend.components.jdbc.runtime.JDBCRollbackSourceOrSink)2 JDBCSink (org.talend.components.jdbc.runtime.JDBCSink)2 JDBCOutputInsertWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputInsertWriter)2 TJDBCCommitDefinition (org.talend.components.jdbc.tjdbccommit.TJDBCCommitDefinition)2 TJDBCCommitProperties (org.talend.components.jdbc.tjdbccommit.TJDBCCommitProperties)2 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)2 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)2 TJDBCRollbackDefinition (org.talend.components.jdbc.tjdbcrollback.TJDBCRollbackDefinition)2