/**
   * Test method for {@link
   * org.talend.dq.writer.AElementPersistance#saveWithDependencies(org.talend.core.model.properties.Item,
   * orgomg.cwm.objectmodel.core.ModelElement)} .
   *
   * @throws PersistenceException
   */
  @Test
  public void testSaveWithDependenciesAnalysis() throws PersistenceException {
    PowerMockito.mockStatic(ProxyRepositoryFactory.class);
    ProxyRepositoryFactory mockProxyRepositoryFactory =
        PowerMockito.mock(ProxyRepositoryFactory.class);
    when(ProxyRepositoryFactory.getInstance()).thenReturn(mockProxyRepositoryFactory);
    stub(
        method(
            ProxyRepositoryFactory.class,
            "save",
            org.talend.core.model.properties.Item.class,
            boolean.class)); // $NON-NLS-1$

    PowerMockito.mockStatic(IRepositoryFactory.class);
    IRepositoryFactory mockIRepositoryFactory = PowerMockito.mock(IRepositoryFactory.class);
    when(mockProxyRepositoryFactory.getRepositoryFactoryFromProvider())
        .thenReturn(mockIRepositoryFactory);

    PowerMockito.mockStatic(XmiResourceManager.class);
    when(mockIRepositoryFactory.getResourceManager()).thenReturn(new XmiResourceManager());

    AnalysisWriter createAnalysisWrite = ElementWriterFactory.getInstance().createAnalysisWrite();
    Analysis createAnalysis = AnalysisFactory.eINSTANCE.createAnalysis();
    AnalysisResult createAnalysisResult = AnalysisFactory.eINSTANCE.createAnalysisResult();
    AnalysisContext createAnalysisContext = AnalysisFactory.eINSTANCE.createAnalysisContext();
    TDQAnalysisItem analysisItem =
        org.talend.dataquality.properties.PropertiesFactory.eINSTANCE.createTDQAnalysisItem();
    analysisItem.setAnalysis(createAnalysis);
    String anaName = "ana1"; // $NON-NLS-1$
    String exceptedFileName = anaName + "_0.1.ana"; // $NON-NLS-1$
    createAnalysis.setName(anaName);
    createAnalysis.setResults(createAnalysisResult);
    createAnalysis.setContext(createAnalysisContext);

    ReturnCode create = createAnalysisWrite.saveWithDependencies(analysisItem, createAnalysis);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName, analysisItem.getFilename());

    String anaName2 = "ana2"; // $NON-NLS-1$
    String exceptedFileName2 = anaName2 + "_0.1.ana"; // $NON-NLS-1$
    createAnalysis.setName(anaName2);

    create = createAnalysisWrite.saveWithDependencies(analysisItem, createAnalysis);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName2, analysisItem.getFilename());
  }
  /**
   * Test method for {@link
   * org.talend.dq.writer.AElementPersistance#saveWithoutDependencies(org.talend.core.model.properties.Item,
   * orgomg.cwm.objectmodel.core.ModelElement)} .
   *
   * @throws PersistenceException
   */
  @Test
  public void testSaveWithoutDependenciesReport() throws PersistenceException {
    PowerMockito.mockStatic(ProxyRepositoryFactory.class);
    ProxyRepositoryFactory mockProxyRepositoryFactory =
        PowerMockito.mock(ProxyRepositoryFactory.class);
    when(ProxyRepositoryFactory.getInstance()).thenReturn(mockProxyRepositoryFactory);
    stub(
        method(
            ProxyRepositoryFactory.class,
            "save",
            org.talend.core.model.properties.Item.class,
            boolean.class)); // $NON-NLS-1$

    PowerMockito.mockStatic(IRepositoryFactory.class);
    IRepositoryFactory mockIRepositoryFactory = PowerMockito.mock(IRepositoryFactory.class);
    when(mockProxyRepositoryFactory.getRepositoryFactoryFromProvider())
        .thenReturn(mockIRepositoryFactory);

    PowerMockito.mockStatic(XmiResourceManager.class);
    when(mockIRepositoryFactory.getResourceManager()).thenReturn(new XmiResourceManager());

    ReportWriter createReportWriter = ElementWriterFactory.getInstance().createReportWriter();
    TdReport createTdReport = ReportsFactory.eINSTANCE.createTdReport();
    TDQReportItem reportItem =
        org.talend.dataquality.properties.PropertiesFactory.eINSTANCE.createTDQReportItem();
    reportItem.setReport(createTdReport);

    String repName = "rep1"; // $NON-NLS-1$
    String exceptedFileName = repName + "_0.1.rep"; // $NON-NLS-1$
    createTdReport.setName(repName);

    ReturnCode create = createReportWriter.saveWithoutDependencies(reportItem, createTdReport);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName, reportItem.getFilename());

    String repName2 = "rep2"; // $NON-NLS-1$
    String exceptedFileName2 = repName2 + "_0.1.rep"; // $NON-NLS-1$
    createTdReport.setName(repName2);

    create = createReportWriter.saveWithDependencies(reportItem, createTdReport);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName2, reportItem.getFilename());
  }
  @Test
  public void testExecuteSqlQuery_delimetd() throws Exception {
    String empty = "";
    Analysis analysis = mock(Analysis.class);
    DelimitedFileIndicatorEvaluator delFileIndiEvaluator =
        new DelimitedFileIndicatorEvaluator(analysis);
    DelimitedFileIndicatorEvaluator spyEvaluator = Mockito.spy(delFileIndiEvaluator);
    stub(method(DelimitedFileIndicatorEvaluator.class, "handleByARow"));
    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);
    String path = "test.txt";
    when(deliFileConn.getFilePath()).thenReturn(path);
    IPath iPath = mock(IPath.class);
    File file = new File(path);
    BufferedWriter output = new BufferedWriter(new FileWriter(file));
    String str =
        "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"
            + "7;blank;231233\n";
    output.write(str);
    output.close();
    when(iPath.toFile()).thenReturn(file);
    when(deliFileConn.getFieldSeparatorValue()).thenReturn(";");
    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);
    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);
  }