use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.
the class DataCellToJavaConversionTest method testXML.
/**
* Test XMLValue -> Document conversion.
*
* @throws Exception When something went wrong
*/
@Test
public void testXML() throws Exception {
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Document>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(XMLCell.TYPE, Document.class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<DataCell, Document> converter = (DataCellToJavaConverter<DataCell, Document>) factory.get().create();
assertNotNull(converter);
final NodeList children = converter.convert(XMLCellFactory.create("<tag/>")).getChildNodes();
assertEquals(children.getLength(), 1);
assertEquals(children.item(0).getNodeName(), "tag");
}
use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.
the class DataCellToJavaConversionTest method testCollectionTypes.
/**
* Test ListCell(IntCell) -> Integer[] conversion.
*
* @throws Exception When something went wrong
*/
@Test
public void testCollectionTypes() throws Exception {
ArrayList<DataCell> coll = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
coll.add(new IntCell(i * i));
}
// collection cells can always contain missing cells.
coll.add(new MissingCell("42"));
final ListCell listCell = CollectionCellFactory.createListCell(coll);
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[].class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<DataCell, Integer[]> converter = (DataCellToJavaConverter<DataCell, Integer[]>) factory.get().create();
assertNotNull(converter);
final Integer[] array = converter.convert(listCell);
for (int i = 0; i < 5; ++i) {
assertEquals(new Integer(i * i), array[i]);
}
assertNull(array[5]);
}
use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.
the class DataCellToJavaConversionTest method testBinaryObject.
/**
* Test BinaryObjectDataCell -> InputStream conversion.
*
* @throws Exception When something went wrong
*/
@Test
public void testBinaryObject() throws Exception {
/* retrieve converter from DataCellToJavaConverterRegistry */
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, InputStream>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(BinaryObjectDataCell.TYPE, InputStream.class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<DataCell, InputStream> converter = (DataCellToJavaConverter<DataCell, InputStream>) factory.get().create();
assertNotNull(converter);
final BinaryObjectCellFactory cellFactory = new BinaryObjectCellFactory();
assertTrue(converter.convert(cellFactory.create(new byte[] { 4, 2 })) instanceof InputStream);
/* convert a BinaryObjectDataCell */
InputStream stream = converter.convert(cellFactory.create(new byte[] { 4, 2 }));
assertEquals(stream.read(), 4);
assertEquals(stream.read(), 2);
stream.close();
}
use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.
the class DataCellToJavaConversionTest method testNestedCollectionTypes.
/**
* Test ListCell(ListCell(IntCell)) -> Integer[][] conversion.
*
* @throws Exception When something went wrong
*/
@Test
public void testNestedCollectionTypes() throws Exception {
ArrayList<DataCell> coll = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
coll.add(new IntCell(i * i));
}
// collection cells can always contain missing cells.
coll.add(new MissingCell("42"));
final ListCell listCell = CollectionCellFactory.createListCell(Arrays.asList(CollectionCellFactory.createListCell(coll)));
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[][]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[][].class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<DataCell, Integer[][]> converter = (DataCellToJavaConverter<DataCell, Integer[][]>) factory.get().create();
assertNotNull(converter);
final Integer[][] array = converter.convert(listCell);
for (int i = 0; i < 5; ++i) {
assertEquals(new Integer(i * i), array[0][i]);
}
assertNull(array[0][5]);
}
use of org.knime.core.data.convert.java.DataCellToJavaConverter in project knime-core by knime.
the class ExtensionPointTest method testDataCellToJava.
/**
* Test whether {@link StringCellToIntTestConverterFactory} was correctly registered at the
* "org.knime.core.DataCellToJavaConverter" extension point and can be used for conversion.
*
* @throws Exception
*/
@Test
public void testDataCellToJava() throws Exception {
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, Integer.class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<StringValue, Integer> converter = (DataCellToJavaConverter<StringValue, Integer>) factory.get().create();
assertNotNull(converter);
final Integer convert = converter.convert(new StringCell("Answer to Life, the Universe, and Everything"));
assertEquals(convert, new Integer(42));
}
Aggregations