Search in sources :

Example 21 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class DelimitedFileIndicatorEvaluatorTest method testExecuteSqlQuery_delimetd.

@Ignore
@Test
public void testExecuteSqlQuery_delimetd() throws Exception {
    // $NON-NLS-1$
    String empty = "";
    Analysis analysis = mock(Analysis.class);
    DelimitedFileIndicatorEvaluator delFileIndiEvaluator = new DelimitedFileIndicatorEvaluator(analysis);
    DelimitedFileIndicatorEvaluator spyEvaluator = Mockito.spy(delFileIndiEvaluator);
    // $NON-NLS-1$
    stub(method(DelimitedFileIndicatorEvaluator.class, "handleByARow"));
    // $NON-NLS-1$
    stub(method(DelimitedFileIndicatorEvaluator.class, "addResultToIndicatorToRowMap", Indicator.class, EMap.class));
    AnalysisContext context = mock(AnalysisContext.class);
    when(analysis.getContext()).thenReturn(context);
    DelimitedFileConnection deliFileConn = mock(DelimitedFileConnection.class);
    when(context.getConnection()).thenReturn(deliFileConn);
    when(deliFileConn.isContextMode()).thenReturn(false);
    // $NON-NLS-1$
    String path = "test.txt";
    PowerMockito.mockStatic(JavaSqlFactory.class);
    when(JavaSqlFactory.getURL(deliFileConn)).thenReturn(path);
    IPath iPath = mock(IPath.class);
    File file = new File(path);
    BufferedWriter output = new BufferedWriter(new FileWriter(file));
    String str = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    "id;Cocust(Tests);owner_id\n" + "1;yellow;3301\n" + "2;blue;3302\n" + " 4;red;3307\n" + "5;white;4563\n" + "6;pink2;457883\n" + // $NON-NLS-1$ //$NON-NLS-2$
    "7;blank;231233\n";
    output.write(str);
    output.close();
    when(iPath.toFile()).thenReturn(file);
    // $NON-NLS-1$
    when(deliFileConn.getFieldSeparatorValue()).thenReturn(";");
    // $NON-NLS-1$
    when(deliFileConn.getEncoding()).thenReturn("US-ASCII");
    AnalysisResult results = mock(AnalysisResult.class);
    when(analysis.getResults()).thenReturn(results);
    EMap<Indicator, AnalyzedDataSet> indicToRowMap = mock(EMap.class);
    when(results.getIndicToRowMap()).thenReturn(indicToRowMap);
    List<ModelElement> columnElementList = new BasicEList<ModelElement>();
    List<MetadataColumn> columnElementList2 = new BasicEList<MetadataColumn>();
    MetadataColumn mc0 = mock(MetadataColumn.class);
    MetadataColumn mc1 = mock(MetadataColumn.class);
    MetadataColumn mc2 = mock(MetadataColumn.class);
    columnElementList.add(mc0);
    columnElementList.add(mc1);
    columnElementList.add(mc2);
    columnElementList2.add(mc0);
    columnElementList2.add(mc1);
    columnElementList2.add(mc2);
    EList<ModelElement> eLs = (EList<ModelElement>) columnElementList;
    when(context.getAnalysedElements()).thenReturn(eLs);
    PowerMockito.mockStatic(ColumnHelper.class);
    MetadataTable mTable = mock(MetadataTable.class);
    when(mTable.getColumns()).thenReturn((EList<MetadataColumn>) columnElementList2);
    when(ColumnHelper.getColumnOwnerAsMetadataTable(mc0)).thenReturn(mTable);
    when(ColumnHelper.getColumnOwnerAsMetadataTable(mc1)).thenReturn(mTable);
    when(deliFileConn.getHeaderValue()).thenReturn(empty);
    when(deliFileConn.getFooterValue()).thenReturn(empty);
    when(deliFileConn.getLimitValue()).thenReturn(empty);
    when(deliFileConn.getEscapeType()).thenReturn(Escape.DELIMITED);
    // $NON-NLS-1$
    when(deliFileConn.getRowSeparatorValue()).thenReturn("\\n");
    when(deliFileConn.isRemoveEmptyRow()).thenReturn(false);
    when(deliFileConn.isSplitRecord()).thenReturn(false);
    PowerMockito.mockStatic(LanguageManager.class);
    when(LanguageManager.getCurrentLanguage()).thenReturn(ECodeLanguage.JAVA);
    Mockito.doReturn(true).when(spyEvaluator).continueRun();
    spyEvaluator.executeSqlQuery(empty);
}
Also used : IPath(org.eclipse.core.runtime.IPath) AnalyzedDataSet(org.talend.dataquality.analysis.AnalyzedDataSet) FileWriter(java.io.FileWriter) BasicEList(org.eclipse.emf.common.util.BasicEList) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) AnalysisContext(org.talend.dataquality.analysis.AnalysisContext) Indicator(org.talend.dataquality.indicators.Indicator) AnalysisResult(org.talend.dataquality.analysis.AnalysisResult) BufferedWriter(java.io.BufferedWriter) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) BasicEList(org.eclipse.emf.common.util.BasicEList) EList(org.eclipse.emf.common.util.EList) Analysis(org.talend.dataquality.analysis.Analysis) EMap(org.eclipse.emf.common.util.EMap) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) File(java.io.File) Ignore(org.junit.Ignore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class FileCSVReaderTest method testNext.

/**
 * Test method for {@link org.talend.cwm.db.connection.file.FileCSVReader#next()}.
 */
@Test
public void testNext() {
    try {
        File file = new File("./file.txt");
        if (file.exists()) {
            file.delete();
        }
        if (file.createNewFile()) {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.println("id,name,email");
            out.println("1,tom,a@b.cn");
            out.println("2,jerry,x@b.cn");
            out.println("3,bob,top@b.cn");
            out.close();
            DelimitedFileConnection delimitedFileconnection = ConnectionFactory.eINSTANCE.createDelimitedFileConnection();
            delimitedFileconnection.setFilePath(file.getAbsolutePath());
            delimitedFileconnection.setLimitValue("0");
            delimitedFileconnection.setHeaderValue("1");
            delimitedFileconnection.setFieldSeparatorValue(",");
            List<ModelElement> analysisElementList = new ArrayList<ModelElement>();
            MetadataTable table = ConnectionFactory.eINSTANCE.createMetadataTable();
            table.setName("table");
            MetadataColumn id = ConnectionFactory.eINSTANCE.createMetadataColumn();
            id.setName("id");
            id.setLabel("id");
            id.setTable(table);
            MetadataColumn name = ConnectionFactory.eINSTANCE.createMetadataColumn();
            name.setName("name");
            name.setLabel("name");
            name.setTable(table);
            MetadataColumn email = ConnectionFactory.eINSTANCE.createMetadataColumn();
            email.setName("email");
            email.setLabel("email");
            email.setTable(table);
            analysisElementList.add(id);
            analysisElementList.add(name);
            analysisElementList.add(email);
            FileCSVReader fileCSVReader = new FileCSVReader(file, delimitedFileconnection, analysisElementList);
            String fileContent = "";
            int i = 0;
            while (fileCSVReader.hasNext()) {
                i++;
                Record record = fileCSVReader.next();
                List<Attribute> attributes = record.getAttributes();
                fileContent += i + ":";
                for (Attribute attribute : attributes) {
                    fileContent += "[" + attribute.getLabel() + "," + attribute.getValue() + "]";
                }
            }
            fileCSVReader.close();
            Assert.assertEquals("1:[id,1][name,tom][email,a@b.cn]2:[id,2][name,jerry][email,x@b.cn]3:[id,3][name,bob][email,top@b.cn]", fileContent);
        }
        // delete the temp file
        if (file.exists()) {
            file.delete();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Attribute(org.talend.dataquality.matchmerge.Attribute) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) Record(org.talend.dataquality.matchmerge.Record) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)22 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)10 File (java.io.File)9 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)9 IPath (org.eclipse.core.runtime.IPath)8 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)8 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Property (org.talend.core.model.properties.Property)7 ConnectionItem (org.talend.core.model.properties.ConnectionItem)6 Analysis (org.talend.dataquality.analysis.Analysis)6 IFile (org.eclipse.core.resources.IFile)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 URL (java.net.URL)4 Connection (org.talend.core.model.metadata.builder.connection.Connection)4 AnalysisContext (org.talend.dataquality.analysis.AnalysisContext)4 Indicator (org.talend.dataquality.indicators.Indicator)4 DataManager (orgomg.cwm.foundation.softwaredeployment.DataManager)4 BufferedWriter (java.io.BufferedWriter)3