Search in sources :

Example 1 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier 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 form the temporary XLS file
    XLSInstanceReader reader = new XLSInstanceReader();
    reader.setSourceSchema(schema);
    reader.setParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE, 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
    InstanceCollection 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 FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class XLSInstanceWriterTest method testWriteNotNestedProperties.

/**
 * Test - write data of complex schema and analyze result The
 * implementation, this test based on, does not work correctly at the
 * moment.
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testWriteNotNestedProperties() throws Exception {
    TransformationExample example = TransformationExamples.getExample(TransformationExamples.SIMPLE_COMPLEX);
    // alternative the data could be generated by iterating through the
    // exempleproject's sourcedata
    List<String> header = Arrays.asList("id", "name");
    List<String> firstDataRow = Arrays.asList("id0", "name0");
    // set instances to xls instance writer
    XLSInstanceWriter writer = new XLSInstanceWriter();
    IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
    writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
    File tmpFile = tmpFolder.newFile("excelNotNestedProperties.xls");
    writer.setInstances(example.getSourceInstances());
    // write instances to a temporary XLS file
    writer.setTarget(new FileIOSupplier(tmpFile));
    writer.setContentType(contentType);
    IOReport report = writer.execute(null);
    assertTrue(report.isSuccess());
    Workbook wb = WorkbookFactory.create(tmpFile);
    Sheet sheet = wb.getSheetAt(0);
    checkHeader(sheet, header);
    checkSheetName(sheet, "person");
    checkFirstDataRow(sheet, firstDataRow);
}
Also used : XLSInstanceWriter(eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter) TransformationExample(eu.esdihumboldt.cst.test.TransformationExample) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) IContentType(org.eclipse.core.runtime.content.IContentType) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 3 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class MsAccessDataReaderTestSuit method schemaReaderTest.

/**
 * Test - reads a sample MsAccess Database schema. UCanAccess lib should not
 * throw any error.
 *
 * @throws Exception if an error occurs
 */
public void schemaReaderTest() throws Exception {
    MsAccessSchemaReader schemaReader = new MsAccessSchemaReader();
    schemaReader.setSource(new FileIOSupplier(getSourceTempFilePath()));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_USER, Value.of(USER_NAME));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_PASSWORD, Value.of(PASSWORD));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    TEMP_SOURCE_FILE_NAME = null;
    Schema schema = schemaReader.getSchema();
    assertTrue(schema != null);
    Collection<? extends TypeDefinition> k = schema.getMappingRelevantTypes();
    for (TypeDefinition def : k) System.out.println(def.getDisplayName());
    checkTables(k);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) MsAccessSchemaReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessSchemaReader) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 4 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class MsAccessDataReaderTestSuit method readInstances.

/**
 * Reads instances from from a MsAccess database file with the provided
 * schema.
 *
 * @param sourceSchema the schema of the source database
 * @param sourceFile the file of the source database.
 * @return the read instances
 * @throws Exception any exception thrown by {@link MsAccessInstanceReader}
 */
public InstanceCollection readInstances(Schema sourceSchema, File sourceFile) throws Exception {
    MsAccessInstanceReader instanceReader = new MsAccessInstanceReader();
    instanceReader.setSource(new FileIOSupplier(sourceFile));
    instanceReader.setSourceSchema(sourceSchema);
    instanceReader.setParameter(JDBCInstanceReader.PARAM_USER, Value.of(USER_NAME));
    instanceReader.setParameter(JDBCInstanceReader.PARAM_PASSWORD, Value.of(PASSWORD));
    // Test instances
    IOReport report = instanceReader.execute(new LogProgressIndicator());
    assertTrue("Data import was not successfull.", report.isSuccess());
    return instanceReader.getInstances();
}
Also used : MsAccessInstanceReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessInstanceReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)

Example 5 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class SpatiaLiteTestSuite method schemaReaderTest.

/**
 * Test - reads a sample SpatiaLite schema
 *
 * @throws Exception if an error occurs
 */
public void schemaReaderTest() throws Exception {
    if (!isSpatiaLiteExtensionAvailable()) {
        log.info("Skipping test because SpatiaLite extension is not available");
        return;
    }
    Set<String> propertyNames = new HashSet<String>(Arrays.asList(SOUURCE_TYPE_PROPERTY_NAMES));
    SpatiaLiteSchemaReader schemaReader = new SpatiaLiteSchemaReader();
    schemaReader.setSource(new FileIOSupplier(new File(getSourceTempFilePath())));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    Schema schema = schemaReader.getSchema();
    assertEquals(1, schema.getMappingRelevantTypes().size());
    TypeDefinition type = schema.getMappingRelevantTypes().iterator().next();
    checkType(type, SOUURCE_TYPE_LOCAL_NAME, propertyNames);
}
Also used : Schema(eu.esdihumboldt.hale.common.schema.model.Schema) SpatiaLiteSchemaReader(eu.esdihumboldt.hale.io.jdbc.spatialite.reader.internal.SpatiaLiteSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) File(java.io.File) HashSet(java.util.HashSet) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)34 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)22 File (java.io.File)22 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)9 IOException (java.io.IOException)9 IContentType (org.eclipse.core.runtime.content.IContentType)8 URI (java.net.URI)6 AlignmentWriter (eu.esdihumboldt.hale.common.align.io.AlignmentWriter)4 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultSchemaSpace (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace)4 NullProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator)3 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)3 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)3 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)3 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)3 OutputStream (java.io.OutputStream)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 ProjectReader (eu.esdihumboldt.hale.common.core.io.project.ProjectReader)2 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)2