use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.
the class JDBCRowTestIT method test_basic_no_connector.
@Test
public void test_basic_no_connector() throws Exception {
TJDBCRowDefinition definition = new TJDBCRowDefinition();
TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
properties.tableSelection.tablename.setValue(tablename);
properties.sql.setValue("insert into " + tablename + " values(4, 'momo')");
properties.dieOnError.setValue(true);
randomCommit(properties);
JDBCRowSourceOrSink sourceOrSink = new JDBCRowSourceOrSink();
sourceOrSink.initialize(null, properties);
ValidationResult result = sourceOrSink.validate(null);
Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, DBTestUtils.createTestSchema(tablename), definition1, properties1);
assertThat(records, hasSize(4));
Assert.assertEquals(new Integer(4), records.get(3).get(0));
Assert.assertEquals("momo", records.get(3).get(1));
}
use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.
the class JDBCInputTestIT method testReader.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testReader() {
Reader reader = null;
try {
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));
reader = DBTestUtils.createCommonJDBCInputReader(properties);
reader.start();
IndexedRecord row = (IndexedRecord) reader.getCurrent();
Integer id = (Integer) row.get(0);
String name = (String) row.get(1);
assertEquals(1, id.intValue());
assertEquals("wangwei", name);
reader.advance();
row = (IndexedRecord) reader.getCurrent();
id = (Integer) row.get(0);
name = (String) row.get(1);
assertEquals(2, id.intValue());
assertEquals("gaoyan", name);
reader.advance();
row = (IndexedRecord) reader.getCurrent();
id = (Integer) row.get(0);
name = (String) row.get(1);
assertEquals(3, id.intValue());
assertEquals("dabao", name);
reader.close();
Map<String, Object> returnMap = reader.getReturnValues();
Assert.assertEquals(3, returnMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
}
}
use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.
the class JDBCInputTestIT method testGetSchema.
@Test
public void testGetSchema() 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);
RuntimeContainer container = new DefaultComponentRuntimeContainerImpl() {
@Override
public String getCurrentComponentId() {
return "tJDBCInput1";
}
};
// the getResource method will convert "@" to "%40" when work with maven together, not sure the bug appear where, bug make
// sure it come from the env, not the function code, so only convert here
// in the product env, the mappings_url is passed from the platform
java.net.URL mappings_url = this.getClass().getResource("/mappings");
mappings_url = DBTestUtils.correctURL(mappings_url);
container.setComponentData(container.getCurrentComponentId(), ComponentConstants.MAPPING_URL_SUBFIX, mappings_url);
Schema schema = source.getEndpointSchema(container, tablename);
assertEquals(tablename, schema.getName().toUpperCase());
List<Field> columns = schema.getFields();
DBTestUtils.testMetadata(columns);
}
use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.
the class JdbcRowTestIT method test_basic_no_connector.
@Test
public void test_basic_no_connector() throws Exception {
TJDBCRowDefinition definition = new TJDBCRowDefinition();
TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
properties.tableSelection.tablename.setValue(tablename);
properties.sql.setValue("insert into " + tablename + " values(4, 'momo')");
properties.dieOnError.setValue(true);
randomCommit(properties);
try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE), properties.getClass().getClassLoader())) {
JDBCRowSourceOrSink sourceOrSink = (JDBCRowSourceOrSink) sandboxedInstance.getInstance();
sourceOrSink.initialize(null, properties);
ValidationResult result = sourceOrSink.validate(null);
Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
}
TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, DBTestUtils.createTestSchema(tablename), definition1, properties1);
assertThat(records, hasSize(4));
Assert.assertEquals(new Integer(4), records.get(3).get(0));
Assert.assertEquals("momo", records.get(3).get(1));
}
use of org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition in project components by Talend.
the class JdbcRowTestIT method test_basic_as_output.
@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_output() throws Exception {
TJDBCRowDefinition definition = new TJDBCRowDefinition();
TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
Schema schema = DBTestUtils.createTestSchema(tablename);
properties.main.schema.setValue(schema);
properties.updateOutputSchemas();
properties.tableSelection.tablename.setValue(tablename);
properties.sql.setValue("insert into " + tablename + " values(?,?)");
properties.dieOnError.setValue(true);
randomCommit(properties);
properties.usePreparedStatement.setValue(true);
properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "momo"));
try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.INCOMING_AND_OUTGOING), properties.getClass().getClassLoader())) {
JDBCRowSink sink = (JDBCRowSink) sandboxedInstance.getInstance();
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));
}
Aggregations