Search in sources :

Example 1 with XLSSchemaReader

use of eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader in project hale by halestudio.

the class XLSInstanceIOTest method test.

/**
 * Exports the instances created by
 * {@link XLSInstanceWriterTestExamples#createInstanceCollection} into a
 * temporary XLS file by executing {@link XLSInstanceWriter#execute}.
 * Afterwards, the schema is read by {@link XLSSchemaReader} and the
 * instances are loaded by {@link XLSInstanceReader}. Each of the imported
 * instances are compared with the original instances. In addtion, a
 * different set of instances is compared with the imported instances.
 */
@Test
public void test() {
    // set instances to xls instance writer
    XLSInstanceWriter writer = new XLSInstanceWriter();
    InstanceCollection instances = XLSInstanceWriterTestExamples.createInstanceCollection();
    IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
    writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    File tempDir = Files.createTempDir();
    File tempFile = new File(tempDir, "data.xls");
    writer.setInstances(instances);
    try {
        // write instances to a temporary XLS file
        writer.setTarget(new FileIOSupplier(tempFile));
        writer.setContentType(contentType);
        IOReport report = writer.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e) {
        fail("Execution of xls instance writer failed.");
    }
    // read the schema from the temporary XLS file
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setContentType(contentType);
    schemaReader.setSource(new FileIOSupplier(tempFile));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
    schemaReader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
    try {
        IOReport report = schemaReader.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e1) {
        fail("Execution of schema reader failed.");
    }
    Schema schema = schemaReader.getSchema();
    // read the instances from the temporary XLS file - test SKIP_N_LINES as
    // integer
    XLSInstanceReader reader = new XLSInstanceReader();
    reader.setSourceSchema(schema);
    reader.setParameter(CommonSchemaConstants.PARAM_SKIP_N_LINES, Value.of(1));
    reader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
    reader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    // read sheet with index 0 since there is only one sheet
    reader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
    reader.setContentType(contentType);
    reader.setSource(new FileIOSupplier(tempFile));
    try {
        IOReport report = reader.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e) {
        fail("Execution of xls instance reader failed.");
    }
    // compare size of instance collection
    InstanceCollection inst = reader.getInstances();
    assertEquals(4, inst.size());
    // read the instances from the temporary XLS file - test SKIP_N_LINES as
    // boolean (backward compatibility)
    reader = new XLSInstanceReader();
    reader.setSourceSchema(schema);
    reader.setParameter(CommonSchemaConstants.PARAM_SKIP_N_LINES, Value.of(true));
    reader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
    reader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    // read sheet with index 0 since there is only one sheet
    reader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
    reader.setContentType(contentType);
    reader.setSource(new FileIOSupplier(tempFile));
    try {
        IOReport report = reader.execute(null);
        assertTrue(report.isSuccess());
    } catch (IOProviderConfigurationException | IOException e) {
        fail("Execution of xls instance reader failed.");
    }
    // compare size of instance collection
    inst = reader.getInstances();
    assertEquals(4, inst.size());
    // check if instance collection contains current instance
    Iterator<Instance> instanceIt = inst.iterator();
    while (instanceIt.hasNext()) {
        Instance instance = instanceIt.next();
        assertTrue(contains(instances.iterator(), instance));
    }
    // other instance should be contained in the imported instances
    InstanceCollection falseInstances = XLSInstanceWriterTestExamples.createFalseTestInstanceCollection();
    instanceIt = inst.iterator();
    while (instanceIt.hasNext()) {
        Instance instance = instanceIt.next();
        assertFalse(contains(falseInstances.iterator(), instance));
    }
    // delete file and temporary directory
    tempFile.delete();
    tempDir.delete();
}
Also used : XLSInstanceWriter(eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) XLSInstanceReader(eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Test(org.junit.Test)

Example 2 with XLSSchemaReader

use of eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader in project hale by halestudio.

the class XLSReaderTest method testReadEmptySheet.

/**
 * Test - Check empty table
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadEmptySheet() throws Exception {
    int sheetIndex = 0;
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/emptyAndNormalSheet.xls").toURI()));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    // Try read the empty sheet
    IOReport report = schemaReader.execute(null);
    assertFalse(report.isSuccess());
    // Read the correct sheet
    sheetIndex = 1;
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    report = schemaReader.execute(null);
    assertTrue(report.isSuccess());
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) Test(org.junit.Test)

Example 3 with XLSSchemaReader

use of eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader in project hale by halestudio.

the class XLSReaderTest method readXLSSchema.

private Schema readXLSSchema(String sourceLocation, int sheetIndex, String typeName, String paramPropertyType) throws Exception {
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource(sourceLocation).toURI()));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    schemaReader.setParameter(AbstractTableSchemaReader.PARAM_PROPERTYTYPE, Value.of(paramPropertyType));
    IOReport report = schemaReader.execute(null);
    assertTrue("Schema import was not successfull.", report.isSuccess());
    return schemaReader.getSchema();
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Aggregations

IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)3 XLSSchemaReader (eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader)3 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)2 Test (org.junit.Test)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)1 XLSInstanceReader (eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader)1 XLSInstanceWriter (eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 IContentType (org.eclipse.core.runtime.content.IContentType)1