Example usage for org.springframework.core.io Resource getFile

List of usage examples for org.springframework.core.io Resource getFile

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getFile.

Prototype

File getFile() throws IOException;

Source Link

Document

Return a File handle for this resource.

Usage

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyEmptySource() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class))).thenReturn(new Resource[] {});
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();// ww  w  .jav  a2s  .com
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null));
    assertFalse(aDestResource.getFile().exists());
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyEmptyDestination() throws Exception {
    exception.expect(FileOperationException.class);
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    // aDestResource.getFile() returns null
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from   w ww  .  j  a v a2  s . c om*/
    aTasklet.execute(null, null);
}

From source file:com.jpoweredcart.common.mock.servlet.MockServletContext.java

public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {/*from w  ww  .  ja v  a  2  s  .com*/
        File file = resource.getFile();
        String[] fileList = file.list();
        if (ObjectUtils.isEmpty(fileList)) {
            return null;
        }
        Set<String> resourcePaths = new LinkedHashSet<String>(fileList.length);
        for (String fileEntry : fileList) {
            String resultPath = actualPath + fileEntry;
            if (resource.createRelative(fileEntry).getFile().isDirectory()) {
                resultPath += "/";
            }
            resourcePaths.add(resultPath);
        }
        return resourcePaths;
    } catch (IOException ex) {
        logger.warn("Couldn't get resource paths for " + resource, ex);
        return null;
    }
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopySourceDoesNotExist() throws Exception {
    exception.expect(FileOperationException.class);
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/unknown.file"));
    when(aFileResource1.exists()).thenReturn(false);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-unknown.file"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();//  w  w w  . ja  va2 s  . c o  m
    aTasklet.execute(null, null);
}

From source file:fr.acxio.tools.agia.io.ResourceCopyFactory.java

@Override
public Resource getResource(Map<? extends Object, ? extends Object> sParameters)
        throws ResourceCreationException {
    Resource aResult = null;//  w  w w.j av a2s  .c  om
    try {
        Resource aSourceResource = (Resource) sParameters.get(ResourceFactoryConstants.PARAM_SOURCE);
        String aFilename = aSourceResource.getFilename();
        String aFileNameWOExtension = aFilename.substring(0, aFilename.lastIndexOf('.'));
        String aExtension = extension;
        if (aExtension == null) {
            aExtension = aFilename.substring(aFilename.lastIndexOf('.'));
        }
        File aParentPath = (parentFolder != null) ? parentFolder.getFile() : null;
        if (aParentPath == null) {
            aParentPath = aSourceResource.getFile().getParentFile();
        }
        StringBuilder aNewFilename = new StringBuilder();
        if (prefix != null) {
            aNewFilename.append(prefix);
        }
        aNewFilename.append(aFileNameWOExtension);
        if (suffix != null) {
            aNewFilename.append(suffix);
        }
        aNewFilename.append(aExtension);
        File aNewFile = new File(aParentPath, aNewFilename.toString());
        aResult = new FileSystemResource(aNewFile);
    } catch (Exception e) {
        throw new ResourceCreationException(e);
    }
    return aResult;
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyWithEmptyChunkContext() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from  w w w  .j  a va 2  s.c  o  m*/
    ChunkContext aChunkContext = mock(ChunkContext.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, aChunkContext));
    verify(aChunkContext, times(1)).getStepContext();
    assertTrue(aDestResource.getFile().exists());
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopy() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from ww  w.ja  va  2s .  c om*/
    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(1)).incrementReadCount();
    verify(aStepContribution, times(1)).incrementWriteCount(1);
    assertTrue(aDestResource.getFile().exists());
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyNoStepContribution() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*  w  ww  .  jav a 2 s.  c  o m*/
    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null));
    verify(aStepContribution, times(0)).incrementReadCount();
    verify(aStepContribution, times(0)).incrementWriteCount(1);
    assertTrue(aDestResource.getFile().exists());
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyDirectory() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-testfiles/"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target/CP-testfiles/"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    assertFalse(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from w  w  w  . j  av a2s. c o m*/
    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(1)).incrementReadCount();
    verify(aStepContribution, times(1)).incrementWriteCount(1);
    assertTrue(aDestResource.getFile().exists());
    assertArrayEquals(aFileResource1.getFile().list(), aDestResource.getFile().list());
}

From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java

@Test
public void testExecuteCopyReplace() throws Exception {
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("src/test/resources/testFiles/input.csv"));
    when(aFileResource1.exists()).thenReturn(true);
    when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class)))
            .thenReturn(new Resource[] { aFileResource1 });
    ResourceFactory aDestinationFactory = mock(ResourceFactory.class);
    Resource aDestResource = mock(Resource.class);
    when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv"));
    when(aDestResource.exists()).thenReturn(true);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target/"));
    when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource);
    when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource);
    FileUtils.copyFile(aFileResource1.getFile(), aDestResource.getFile());
    assertTrue(aDestResource.getFile().exists());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from  w w w.  j a v a 2s  .  com*/
    StepContribution aStepContribution = mock(StepContribution.class);
    assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
    verify(aStepContribution, times(1)).incrementReadCount();
    verify(aStepContribution, times(1)).incrementWriteCount(1);
    assertTrue(aDestResource.getFile().exists());
}