Search in sources :

Example 1 with FileDelimitedProperties

use of org.talend.components.filedelimited.FileDelimitedProperties in project components by Talend.

the class FileDelimitedWizardTestIT method testWizardProperties.

protected void testWizardProperties() throws Throwable {
    FileDelimitedProperties props = (FileDelimitedProperties) new FileDelimitedProperties("wizard").init();
    Form wizardForm = props.getForm(FileDelimitedProperties.FORM_WIZARD);
    ComponentTestUtils.checkSerialize(props, errorCollector);
    LOGGER.debug(wizardForm.toString());
    assertEquals(FileDelimitedProperties.FORM_WIZARD, wizardForm.getName());
    assertFalse(wizardForm.isAllowFinish());
    assertFalse(wizardForm.getWidget(props.name.getName()).isHidden());
    assertFalse(wizardForm.getWidget(props.fileName.getName()).isHidden());
    Form encodingForm = wizardForm.getChildForm(props.encoding.getName());
    assertFalse(encodingForm.getWidget(props.encoding.encodingType.getName()).isHidden());
    assertTrue(encodingForm.getWidget(props.encoding.customEncoding.getName()).isHidden());
    assertEquals(EncodingTypeProperties.ENCODING_TYPE_UTF_8, props.encoding.encodingType.getValue());
    assertFalse(wizardForm.getWidget(props.rowSeparator.getName()).isHidden());
    assertEquals("\\n", props.rowSeparator.getValue());
    assertFalse(wizardForm.getWidget(props.fieldSeparator.getName()).isHidden());
    assertEquals(";", props.fieldSeparator.getValue());
    assertFalse(wizardForm.getWidget(props.csvOptions.getName()).isHidden());
    assertFalse(props.csvOptions.getValue());
    assertTrue(wizardForm.getWidget(props.textEnclosure.getName()).isHidden());
    assertTrue(wizardForm.getWidget(props.escapeChar.getName()).isHidden());
    assertFalse(wizardForm.getWidget(props.header.getName()).isHidden());
    assertEquals(0, (int) props.header.getValue());
    assertFalse(wizardForm.getWidget(props.footer.getName()).isHidden());
    assertEquals(0, (int) props.footer.getValue());
    assertFalse(wizardForm.getWidget(props.removeEmptyRow.getName()).isHidden());
    assertTrue(props.removeEmptyRow.getValue());
    assertFalse(wizardForm.getWidget(props.preview.getName()).isHidden());
    assertFalse(wizardForm.getWidget(props.previewTable.getName()).isHidden());
    // Change to CSV mode
    props.csvOptions.setValue(true);
    assertTrue(wizardForm.getWidget(props.csvOptions.getName()).isCallAfter());
    getComponentService().afterProperty(props.csvOptions.getName(), props);
    assertFalse(wizardForm.getWidget(props.escapeChar.getName()).isHidden());
    assertEquals("\\\"", props.escapeChar.getValue());
    assertFalse(wizardForm.getWidget(props.textEnclosure.getName()).isHidden());
    assertEquals("\\\"", props.textEnclosure.getValue());
    // Change name
    props.name.setValue("wizard");
    PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "name", props);
    assertTrue(wizardForm.isAllowFinish());
}
Also used : Form(org.talend.daikon.properties.presentation.Form) FileDelimitedProperties(org.talend.components.filedelimited.FileDelimitedProperties)

Example 2 with FileDelimitedProperties

use of org.talend.components.filedelimited.FileDelimitedProperties in project components by Talend.

the class FileDelimitedSource method validate.

@Override
public ValidationResult validate(RuntimeContainer container) {
    ValidationResultMutable vr = new ValidationResultMutable(super.validate(container));
    // also check that the properties is the right type
    if (vr.getStatus() != ValidationResult.Result.ERROR) {
        if (!(properties instanceof FileDelimitedProperties)) {
            return new ValidationResult(ValidationResult.Result.ERROR, "properties should be of type :" + FileDelimitedProperties.class.getCanonicalName());
        }
    }
    Object fileOrStream = ((FileDelimitedProperties) properties).fileName.getValue();
    if (fileOrStream == null) {
        vr.setMessage(messages.getMessage("error.fileNameIsNull"));
        vr.setStatus(ValidationResult.Result.ERROR);
    } else {
        if (fileOrStream instanceof InputStream) {
            LOGGER.debug("Source is a stream");
        } else {
            File file = new File(String.valueOf(fileOrStream));
            if (!file.exists()) {
                vr.setMessage(messages.getMessage("error.fileNotFound", file.getPath()));
                vr.setStatus(ValidationResult.Result.ERROR);
            }
        }
    }
    return vr;
}
Also used : FileDelimitedProperties(org.talend.components.filedelimited.FileDelimitedProperties) InputStream(java.io.InputStream) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) ValidationResult(org.talend.daikon.properties.ValidationResult) File(java.io.File)

Example 3 with FileDelimitedProperties

use of org.talend.components.filedelimited.FileDelimitedProperties in project components by Talend.

the class FileDelimitedReaderTestIT method basicInputTest.

protected void basicInputTest(Object file, boolean previewData, boolean isCsvMode, boolean sourceIsStream) throws Throwable {
    FileDelimitedProperties properties = null;
    if (previewData) {
        properties = createWizaredProperties(createInputProperties(file, isCsvMode));
    } else {
        properties = createInputProperties(file, isCsvMode);
    }
    if (sourceIsStream) {
        // header=1 && footer=0 && no limit
        String fileName = properties.fileName.getStringValue();
        properties.fileName.setValue(new FileInputStream(new File(fileName)));
        testBasicInput(properties, 20, previewData);
        // header=1 && footer=5 && limit=10
        properties.fileName.setValue(new FileInputStream(new File(fileName)));
        properties.limit.setValue(10);
        testBasicInput(properties, 10, previewData);
    } else {
        // header=1 && footer=0 && no limit
        testBasicInput(properties, 20, previewData);
        // header=1 && footer=5 && no limit
        properties.footer.setValue(5);
        testBasicInput(properties, 15, previewData);
        // header=1 && footer=5 && limit=10
        properties.limit.setValue(10);
        testBasicInput(properties, 10, previewData);
    }
}
Also used : FileDelimitedProperties(org.talend.components.filedelimited.FileDelimitedProperties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with FileDelimitedProperties

use of org.talend.components.filedelimited.FileDelimitedProperties in project components by Talend.

the class FileDelimitedWizardTestIT method testWizard.

@Test
public void testWizard() throws Throwable {
    final List<RepoProps> repoProps = new ArrayList<>();
    Repository repo = new TestRepository(repoProps);
    getComponentService().setRepository(repo);
    Set<ComponentWizardDefinition> wizards = getComponentService().getTopLevelComponentWizards();
    int count = 0;
    ComponentWizardDefinition wizardDef = null;
    for (ComponentWizardDefinition wizardDefinition : wizards) {
        if (wizardDefinition instanceof FileDelimitedWizardDefinition) {
            wizardDef = wizardDefinition;
            count++;
        }
    }
    assertEquals(1, count);
    assertEquals("file delimited", wizardDef.getMenuItemName());
    ComponentWizard wiz = getComponentService().getComponentWizard(FileDelimitedWizardDefinition.COMPONENT_WIZARD_NAME, "nodeFileDelimited");
    assertNotNull(wiz);
    assertEquals("nodeFileDelimited", wiz.getRepositoryLocation());
    FileDelimitedWizard swiz = (FileDelimitedWizard) wiz;
    List<Form> forms = wiz.getForms();
    Form formWizard = forms.get(0);
    assertEquals("Wizard", formWizard.getName());
    assertFalse(formWizard.isAllowBack());
    assertFalse(formWizard.isAllowForward());
    assertFalse(formWizard.isAllowFinish());
    assertEquals("Delimited File Settings", formWizard.getTitle());
    assertEquals("", formWizard.getSubtitle());
    FileDelimitedProperties wizardProps = (FileDelimitedProperties) formWizard.getProperties();
    Object image = getComponentService().getWizardPngImage(FileDelimitedWizardDefinition.COMPONENT_WIZARD_NAME, WizardImageType.TREE_ICON_16X16);
    assertNotNull(image);
    image = getComponentService().getWizardPngImage(FileDelimitedWizardDefinition.COMPONENT_WIZARD_NAME, WizardImageType.WIZARD_BANNER_75X66);
    assertNotNull(image);
    // Check the non-top-level wizard
    assertEquals("Name", wizardProps.getProperty("name").getDisplayName());
    wizardProps.name.setValue("connName");
    setupProps(wizardProps);
    Form encodingForm = (Form) formWizard.getWidget("encoding").getContent();
    assertEquals("Main", encodingForm.getDisplayName());
    Property encodingType = (Property) encodingForm.getWidget("encodingType").getContent();
    assertEquals("Encoding", encodingType.getDisplayName());
    assertFalse(formWizard.getWidget(wizardProps.encoding.getName()).isHidden());
    assertFalse(encodingForm.getWidget(wizardProps.encoding.encodingType.getName()).isHidden());
    assertTrue(encodingForm.getWidget(wizardProps.encoding.customEncoding.getName()).isHidden());
    wizardProps.encoding.encodingType.setValue(EncodingTypeProperties.ENCODING_TYPE_CUSTOM);
    assertTrue(encodingForm.getWidget(wizardProps.encoding.encodingType.getName()).isCallAfter());
    getComponentService().afterProperty(wizardProps.encoding.encodingType.getName(), wizardProps.encoding);
    assertFalse(encodingForm.getWidget(wizardProps.encoding.customEncoding.getName()).isHidden());
    wizardProps.main.schema.setValue(BASIC_SCHEMA);
    ValidationResult result = wizardProps.afterFormFinishWizard(repo);
    assertEquals(ValidationResult.OK, result);
// TODO Continue when finish the wizard
}
Also used : Form(org.talend.daikon.properties.presentation.Form) ArrayList(java.util.ArrayList) ValidationResult(org.talend.daikon.properties.ValidationResult) Repository(org.talend.daikon.properties.service.Repository) ComponentWizard(org.talend.components.api.wizard.ComponentWizard) FileDelimitedProperties(org.talend.components.filedelimited.FileDelimitedProperties) ComponentWizardDefinition(org.talend.components.api.wizard.ComponentWizardDefinition) Property(org.talend.daikon.properties.property.Property) Test(org.junit.Test)

Example 5 with FileDelimitedProperties

use of org.talend.components.filedelimited.FileDelimitedProperties in project components by Talend.

the class FileDelimitedWizardTestIT method testWizardPreviewData.

@Test
public void testWizardPreviewData() throws Throwable {
    String resources = getClass().getResource("/runtime/input").toURI().getPath();
    FileDelimitedProperties props = (FileDelimitedProperties) new FileDelimitedProperties("wizard").init();
    props.rowSeparator.setValue("\n");
    Form wizardForm = props.getForm(FileDelimitedProperties.FORM_WIZARD);
    // File name is empty means you can edit the schema manually
    props.fileName.setValue("");
    props = (FileDelimitedProperties) PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "preview", props);
    LOGGER.debug("File is not specified, you can edit the schema manual!");
    assertEquals(ValidationResult.Result.OK, props.getValidationResult().getStatus());
    // File is not exist
    props.fileName.setValue(resources + "/not_exist_file.csv");
    props = (FileDelimitedProperties) PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "preview", props);
    LOGGER.debug(props.getValidationResult().getMessage());
    assertEquals(ValidationResult.Result.ERROR, props.getValidationResult().getStatus());
    // File is exist
    props.name.setValue("test_wizard");
    props.fileName.setValue(resources + "/test_input_delimited.csv");
    props = (FileDelimitedProperties) PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "preview", props);
    assertNotNull(props.main.schema.getValue());
    assertEquals(19, props.main.schema.getValue().getFields().size());
    assertEquals(0, props.main.schema.getValue().getField("Column0").pos());
    assertEquals(4, props.main.schema.getValue().getField("Column4").pos());
    assertEquals(7, props.main.schema.getValue().getField("Column7").pos());
    assertEquals(9, props.main.schema.getValue().getField("Column9").pos());
    assertEquals(11, props.main.schema.getValue().getField("Column11").pos());
    assertEquals(18, props.main.schema.getValue().getField("Column18").pos());
    assertEquals(ValidationResult.Result.OK, props.getValidationResult().getStatus());
    resources = getClass().getResource("/runtime/wizard").toURI().getPath();
    // File is empty
    props.fileName.setValue(resources + "/wizard_file_empty.csv");
    props = (FileDelimitedProperties) PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "preview", props);
    assertEquals("{\"data\":[]}", props.previewTable.getValue());
    assertNotNull(props.main.schema.getValue());
    assertEquals(0, props.main.schema.getValue().getFields().size());
    assertEquals(ValidationResult.Result.OK, props.getValidationResult().getStatus());
    // Data is empty
    props.fileName.setValue(resources + "/wizard_data_empty.csv");
    props.header.setValue(1);
    props = (FileDelimitedProperties) PropertiesTestUtils.checkAndValidate(getComponentService(), wizardForm, "preview", props);
    assertEquals("{\"columnNames\":[\"TestBoolean\",\"TestByte\",\"TestBytes\",\"TestChar\",\"TestDate\",\"TestDouble\",\"TestFloat\",\"TestBigDecimal\",\"TestInteger\",\"TestLong\",\"TestObject\"],\"data\":[]}", props.previewTable.getValue());
    assertNotNull(props.main.schema.getValue());
    assertEquals(11, props.main.schema.getValue().getFields().size());
    assertEquals(0, props.main.schema.getValue().getField("TestBoolean").pos());
    assertEquals(4, props.main.schema.getValue().getField("TestDate").pos());
    assertEquals(7, props.main.schema.getValue().getField("TestBigDecimal").pos());
    assertEquals(9, props.main.schema.getValue().getField("TestLong").pos());
    assertEquals(ValidationResult.Result.OK, props.getValidationResult().getStatus());
}
Also used : Form(org.talend.daikon.properties.presentation.Form) FileDelimitedProperties(org.talend.components.filedelimited.FileDelimitedProperties) Test(org.junit.Test)

Aggregations

FileDelimitedProperties (org.talend.components.filedelimited.FileDelimitedProperties)5 Form (org.talend.daikon.properties.presentation.Form)3 File (java.io.File)2 Test (org.junit.Test)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ComponentWizard (org.talend.components.api.wizard.ComponentWizard)1 ComponentWizardDefinition (org.talend.components.api.wizard.ComponentWizardDefinition)1 ValidationResultMutable (org.talend.daikon.properties.ValidationResultMutable)1 Property (org.talend.daikon.properties.property.Property)1 Repository (org.talend.daikon.properties.service.Repository)1