use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class SandboxedSimpleFileIODatasetRuntimeTest method testBasic.
@Test
public void testBasic() throws Exception {
File input = folder.newFile("stuff.csv");
try (FileWriter fw = new FileWriter(input)) {
fw.write("1;one");
}
SimpleFileIODatasetProperties props = createDatasetProperties();
props.path.setValue(input.toURI().toString());
final List<IndexedRecord> consumed = new ArrayList<>();
RuntimeInfo ri = def.getRuntimeInfo(props);
try (SandboxedInstance si = RuntimeUtil.createRuntimeClass(ri, getClass().getClassLoader())) {
DatasetRuntime runtime = (DatasetRuntime) si.getInstance();
runtime.initialize(null, props);
assertThat(runtime, not(nullValue()));
Schema s = runtime.getSchema();
assertThat(s, not(nullValue()));
runtime.getSample(100, new Consumer<IndexedRecord>() {
@Override
public void accept(IndexedRecord ir) {
consumed.add(ir);
}
});
}
assertThat(consumed, hasSize(1));
assertThat(consumed.get(0).get(0), is((Object) "1"));
assertThat(consumed.get(0).get(1), is((Object) "one"));
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class SimpleFileIODatasetRuntimeTest method testBasic.
@Test
public void testBasic() throws Exception {
File input = folder.newFile("stuff.csv");
try (FileWriter fw = new FileWriter(input)) {
fw.write("1;one");
}
SimpleFileIODatasetProperties props = createDatasetProperties();
props.path.setValue(input.toURI().toString());
final List<IndexedRecord> consumed = new ArrayList<>();
RuntimeInfo ri = def.getRuntimeInfo(props);
try (SandboxedInstance si = RuntimeUtil.createRuntimeClass(ri, getClass().getClassLoader())) {
DatasetRuntime runtime = (DatasetRuntime) si.getInstance();
runtime.initialize(null, props);
assertThat(runtime, not(nullValue()));
Schema s = runtime.getSchema();
assertThat(s, not(nullValue()));
runtime.getSample(100, new Consumer<IndexedRecord>() {
@Override
public void accept(IndexedRecord ir) {
consumed.add(ir);
}
});
}
assertThat(consumed, hasSize(1));
assertThat(consumed.get(0).get(0), is((Object) "1"));
assertThat(consumed.get(0).get(1), is((Object) "one"));
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class JDBCDatasetProperties method updateSchema.
public void updateSchema() {
JDBCDatasetDefinition definition = new JDBCDatasetDefinition();
RuntimeInfo runtimeInfo = definition.getRuntimeInfo(this);
try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(runtimeInfo, getClass().getClassLoader())) {
DatasetRuntime<JDBCDatasetProperties> runtime = (DatasetRuntime) sandboxedInstance.getInstance();
runtime.initialize(null, this);
Schema schema = runtime.getSchema();
main.schema.setValue(schema);
}
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class JdbcRowTestIT method test_reject_as_output.
@SuppressWarnings("rawtypes")
@Test
public void test_reject_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(false);
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, "a too long value"));
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);
List<IndexedRecord> rejects = writer.getRejectedWrites();
assertThat(rejects, hasSize(1));
IndexedRecord reject = rejects.get(0);
Assert.assertEquals(4, reject.get(0));
Assert.assertEquals("xiaoming", reject.get(1));
Assert.assertNotNull(reject.get(2));
Assert.assertNotNull(reject.get(3));
assertThat(writer.getSuccessfulWrites(), empty());
writer.cleanWrites();
IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
r2.put(0, 5);
r2.put(1, "xiaobai");
writer.write(r2);
rejects = writer.getRejectedWrites();
assertThat(rejects, hasSize(1));
reject = rejects.get(0);
Assert.assertEquals(5, reject.get(0));
Assert.assertEquals("xiaobai", reject.get(1));
Assert.assertNotNull(reject.get(2));
Assert.assertNotNull(reject.get(3));
assertThat(writer.getSuccessfulWrites(), empty());
writer.cleanWrites();
writer.close();
} finally {
writer.close();
}
}
}
use of org.talend.daikon.sandbox.SandboxedInstance 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