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

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

Introduction

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

Prototype

boolean exists();

Source Link

Document

Determine whether this resource actually exists in physical form.

Usage

From source file:com.thoughtworks.go.http.mocks.MockServletContext.java

@Override
public URL getResource(String path) throws MalformedURLException {
    Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
    if (!resource.exists()) {
        return null;
    }//w w w  . ja  v  a2 s .c o m
    try {
        return resource.getURL();
    } catch (MalformedURLException ex) {
        throw ex;
    } catch (IOException ex) {
        logger.warn("Couldn't get URL for " + resource, ex);
        return null;
    }
}

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

@Test
public void testExecuteCopyNullDestinationFactory() throws Exception {
    exception.expect(IllegalArgumentException.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 });
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(null);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();//from w w w  . jav  a 2  s .  com
    aTasklet.execute(null, null);
}

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

@Test
public void testExecuteCopyNullDestination() 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);
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();//w  ww.j  a  v  a 2 s .c  om
    aTasklet.execute(null, null);
}

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();// ww  w  . j a v  a  2 s.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());
    assertArrayEquals(aFileResource1.getFile().list(), aDestResource.getFile().list());
}

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  w w  . j  av a  2  s.  com
    aTasklet.execute(null, null);
}

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

@Test
public void testExecuteMoveDirToDir() throws Exception {
    FileUtils.forceMkdir(new File("target/CP-testfiles/source"));
    FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"),
            new File("target/CP-testfiles/source/CP-input.csv"));
    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("target/CP-testfiles/source/"));
    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/destination/"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target/CP-testfiles/destination/"));
    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.MOVE);
    aTasklet.afterPropertiesSet();//  w  w w  . j  a  va2 s .co  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());
    assertFalse(aFileResource1.getFile().exists());
    assertEquals("source", aDestResource.getFile().list()[0]);
    assertEquals("CP-input.csv", aDestResource.getFile().listFiles()[0].list()[0]);
}

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

@Test
public void testExecuteCopyNonRecursiveDirectory() throws Exception {
    FileUtils.forceMkdir(new File("target/CP-testfiles/source/subdir"));
    FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"),
            new File("target/CP-testfiles/source/CP0-input.csv"));
    FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"),
            new File("target/CP-testfiles/source/subdir/CP1-input.csv"));

    FilesOperationTasklet aTasklet = new FilesOperationTasklet();
    ResourcesFactory aSourceFactory = mock(ResourcesFactory.class);
    Resource aFileResource1 = mock(Resource.class);
    when(aFileResource1.getFile()).thenReturn(new File("target/CP-testfiles/source/"));
    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/destination/"));
    when(aDestResource.exists()).thenReturn(false);
    Resource aRelativeResource = mock(Resource.class);
    when(aRelativeResource.getFile()).thenReturn(new File("target/CP-testfiles/destination/"));
    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.setRecursive(false);//from  w  w  w  . jav a  2  s . c  o m
    aTasklet.afterPropertiesSet();
    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());
    assertEquals(1, aDestResource.getFile().list().length);
    assertEquals("CP0-input.csv", aDestResource.getFile().list()[0]);
}

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();/* w ww .j  a  va 2  s  .  c  om*/
    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 w w  w  .  j  a va2 s  .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();/*from  ww  w.  ja v a2 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());
}