Search in sources :

Example 1 with FileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo in project data-access by pentaho.

the class FileUtilsTest method testDeleteFile.

@Test
public void testDeleteFile() {
    FileUtils fu = new FileUtils();
    String testUniqueFileName = "testFileToDelete-" + System.currentTimeMillis();
    File testUniqueFile = new File(fu.getRelativeSolutionPath() + File.separatorChar + testUniqueFileName);
    try {
        testUniqueFile.createNewFile();
    } catch (IOException ioe) {
        fail("File creation failed");
    }
    // Make sure the file got deleted
    if (!testUniqueFile.exists()) {
        fail("File could not be created");
    }
    FileInfo[] files = fu.listFiles();
    if (files != null && files.length > 0) {
        fu.deleteFile(testUniqueFileName);
        FileInfo[] files1 = fu.listFiles();
        System.err.println("File1 length " + files1.length);
        System.err.println("Files length " + files.length);
        try {
            assertTrue(files1.length < files.length);
        } finally {
            for (FileInfo file : files1) {
                if (file.getName().contains(testUniqueFileName)) {
                    testUniqueFile.deleteOnExit();
                    fail("File should have been deleted. We will delete the file ourselves");
                }
            }
        }
    }
}
Also used : FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 2 with FileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo in project data-access by pentaho.

the class CsvDatasourceServiceImplTest method testHasPermissions.

@Test
public void testHasPermissions() throws Exception {
    hasPermissions = true;
    final ISystemSettings systemSettings = mock(ISystemSettings.class);
    when(systemSettings.getSystemSetting("data-access-override", "false")).thenReturn("false");
    PentahoSystem.setSystemSettingsService(systemSettings);
    String filename = "anotherStageFile_CsvFile.csv";
    File file = createTmpCsvFile(filename);
    file.deleteOnExit();
    try {
        ModelInfo modelInfo = service.stageFile(filename, ",", "\n", true, "utf-8");
        CsvFileInfo fileInfo = modelInfo.getFileInfo();
        assertEquals("One header row", 1, fileInfo.getHeaderRows());
        assertEquals("Header + content row", 2, fileInfo.getContents().size());
        assertEquals(filename, fileInfo.getTmpFilename());
        final FileInfo[] stagedFiles = service.getStagedFiles();
        assertNotNull(stagedFiles);
        boolean present = false;
        for (FileInfo info : stagedFiles) {
            if (filename.equals(info.getName())) {
                present = true;
                break;
            }
        }
        assertTrue(present);
        final String encoding = service.getEncoding(filename);
        assertNotNull(encoding);
        final List<String> previewRows = service.getPreviewRows(filename, true, 1, "utf-8");
        assertNotNull(previewRows);
        assertEquals(1, previewRows.size());
        assertEquals("col1,col2", previewRows.get(0));
        final DatasourceDTO datasourceDto = mock(DatasourceDTO.class);
        when(datasourceDto.getCsvModelInfo()).thenReturn(modelInfo);
        try {
            final FileTransformStats fileTransformStats = service.generateDomain(datasourceDto);
        } catch (Exception e) {
        // Testing this logic is not a purpose of this junit
        }
        // Passed permissions check
        verify(datasourceDto, times(1)).getCsvModelInfo();
    } finally {
        file.delete();
    }
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) Matchers.anyString(org.mockito.Matchers.anyString) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats) Test(org.junit.Test)

Example 3 with FileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo in project data-access by pentaho.

the class CsvDatasourceServiceImpl method getStagedFiles.

public FileInfo[] getStagedFiles() throws Exception {
    checkPermissions();
    FileInfo[] files;
    try {
        FileUtils fileService = new FileUtils();
        files = fileService.listFiles();
    } catch (Exception e) {
        logger.error(e);
        throw e;
    }
    return files;
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) FileUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.FileUtils) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Example 4 with FileInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo in project data-access by pentaho.

the class FileUtils method listFiles.

public FileInfo[] listFiles() {
    List<FileInfo> fileList = new ArrayList<FileInfo>();
    String path = getRelativeSolutionPath();
    File folder = new File(path);
    if (folder.exists()) {
        File[] files = folder.listFiles();
        for (File file : files) {
            String name = file.getName();
            if (file.isFile()) {
                long lastModified = file.lastModified();
                DateFormat fmt = LocaleHelper.getShortDateFormat(true, true);
                Date modified = new Date();
                modified.setTime(lastModified);
                String modifiedStr = fmt.format(modified);
                long size = file.length();
                FileInfo info = new FileInfo();
                info.setModified(modifiedStr);
                info.setName(name);
                info.setSize(size);
                fileList.add(info);
            }
        }
    }
    return fileList.toArray(new FileInfo[fileList.size()]);
}
Also used : FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) File(java.io.File) Date(java.util.Date)

Aggregations

FileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo)4 File (java.io.File)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 CsvFileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo)2 FileNotFoundException (java.io.FileNotFoundException)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ISystemSettings (org.pentaho.platform.api.engine.ISystemSettings)1 FileUtils (org.pentaho.platform.dataaccess.datasource.wizard.csv.FileUtils)1 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)1 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)1 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)1 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)1 FileTransformStats (org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)1