Search in sources :

Example 26 with TJDBCInputProperties

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties in project components by Talend.

the class JDBCInputTestIT method testGetSchemaNames.

@Test
public void testGetSchemaNames() throws Exception {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.main.schema.setValue(DBTestUtils.createTestSchema(tablename));
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue(DBTestUtils.getSQL(tablename));
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    List<NamedThing> schemaNames = source.getSchemaNames(null);
    assertTrue(schemaNames != null);
    assertTrue(!schemaNames.isEmpty());
    boolean exists = false;
    for (NamedThing name : schemaNames) {
        if (tablename.equals(name.getName().toUpperCase())) {
            exists = true;
            break;
        }
    }
    assertTrue(exists);
}
Also used : TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) NamedThing(org.talend.daikon.NamedThing) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 27 with TJDBCInputProperties

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties in project components by Talend.

the class JDBCInputTestIT method testGetSchemaFromQueryWithException2.

@Test(expected = ComponentException.class)
public void testGetSchemaFromQueryWithException2() throws Exception {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.connection.driverClass.setValue("notexist");
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    try {
        source.getSchemaFromQuery(null, "select * from " + tablename);
    } catch (ComponentException e) {
        String message = CommonUtils.getClearExceptionInfo(e);
        assertTrue(message.contains("notexist"));
        throw e;
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 28 with TJDBCInputProperties

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties in project components by Talend.

the class JDBCInputTestIT method testGetSchemaWithException.

@Test(expected = ComponentException.class)
public void testGetSchemaWithException() throws Exception {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.connection.driverClass.setValue("notexist");
    JDBCSource source = DBTestUtils.createCommonJDBCSource(properties);
    try {
        source.getEndpointSchema(null, tablename);
    } catch (ComponentException e) {
        String message = CommonUtils.getClearExceptionInfo(e);
        assertTrue(message.contains("notexist"));
        throw e;
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCSource(org.talend.components.jdbc.runtime.JDBCSource) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 29 with TJDBCInputProperties

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties in project components by Talend.

the class JDBCInputTestIT method testReaderAllTypesString.

/**
 * Checks {@link JDBCInputReader} outputs {@link IndexedRecord} which contains nullable String type data for every SQL/JDBC
 * type
 */
@Test
public void testReaderAllTypesString() throws IOException {
    TJDBCInputDefinition definition = new TJDBCInputDefinition();
    TJDBCInputProperties properties = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition);
    properties.main.schema.setValue(DBTestUtils.createAllTypesSchema(tablename_all_type));
    properties.tableSelection.tablename.setValue(tablename_all_type);
    properties.sql.setValue(DBTestUtils.getSQL(tablename_all_type));
    Reader reader = DBTestUtils.createCommonJDBCInputReader(properties);
    reader.start();
    IndexedRecord record = (IndexedRecord) reader.getCurrent();
    Short col0 = (Short) record.get(0);
    Integer col1 = (Integer) record.get(1);
    Long col2 = (Long) record.get(2);
    Float col3 = (Float) record.get(3);
    Double col4 = (Double) record.get(4);
    BigDecimal col5 = (BigDecimal) record.get(5);
    String col6 = (String) record.get(6);
    String col7 = (String) record.get(7);
    String col8 = (String) record.get(8);
    String col9 = (String) record.get(9);
    Long col10 = (Long) record.get(10);
    Long col11 = (Long) record.get(11);
    Long col12 = (Long) record.get(12);
    Boolean col13 = (Boolean) record.get(13);
    assertEquals(32767, col0.shortValue());
    assertEquals(2147483647, col1.intValue());
    assertEquals(9223372036854775807l, col2.longValue());
    assertTrue(col3 > 1);
    assertTrue(col4 > 2);
    assertEquals(new BigDecimal("1234567890.1234567890"), col5);
    assertEquals("abcd", col6);
    assertEquals("abcdefg", col7);
    assertEquals("00010203040506070809", col8);
    assertEquals("abcdefg", col9);
    assertEquals("2016-12-28", new SimpleDateFormat("yyyy-MM-dd").format(new Date(col10)));
    assertEquals("14:30:33", new SimpleDateFormat("HH:mm:ss").format(new Date(col11)));
    assertEquals("2016-12-28 14:31:56.123", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(col12)));
    assertEquals(true, col13);
    Schema actualSchema = record.getSchema();
    List<Field> actualFields = actualSchema.getFields();
    assertEquals(14, actualFields.size());
    reader.close();
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) JDBCInputReader(org.talend.components.jdbc.runtime.reader.JDBCInputReader) Reader(org.talend.components.api.component.runtime.Reader) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Field(org.apache.avro.Schema.Field) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) SimpleDateFormat(java.text.SimpleDateFormat) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) Test(org.junit.Test)

Example 30 with TJDBCInputProperties

use of org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties in project components by Talend.

the class JDBCRowTestIT method commonAssert.

private void commonAssert(TJDBCRowProperties properties, Schema schema) throws IOException {
    JDBCRowSink sink = new JDBCRowSink();
    sink.initialize(null, properties);
    ValidationResult result = sink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    WriteOperation operation = sink.createWriteOperation();
    JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 5);
        r2.put(1, "xiaobai");
        writer.write(r2);
        DBTestUtils.assertSuccessRecord(writer, r2);
        writer.close();
    } finally {
        writer.close();
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(5));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
    Assert.assertEquals(new Integer(4), records.get(4).get(0));
    Assert.assertEquals("momo", records.get(4).get(1));
}
Also used : WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)

Aggregations

TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)35 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)32 Test (org.junit.Test)30 IndexedRecord (org.apache.avro.generic.IndexedRecord)23 Schema (org.apache.avro.Schema)19 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)12 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)12 JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)11 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)9 Reader (org.talend.components.api.component.runtime.Reader)6 ComponentException (org.talend.components.api.exception.ComponentException)6 ValidationResult (org.talend.daikon.properties.ValidationResult)6 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)5 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)5 Field (org.apache.avro.Schema.Field)4 JDBCRowSourceOrSink (org.talend.components.jdbc.runtime.JDBCRowSourceOrSink)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)3