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

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

Introduction

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

Prototype

Resource createRelative(String relativePath) throws IOException;

Source Link

Document

Create a resource relative to this resource.

Usage

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

@Test
public void testExecuteCopyFileToExistingDir() throws Exception {
    FileUtils.forceMkdir(new File("target/CP-testfiles"));
    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-testfiles/"));
    when(aDestResource.exists()).thenReturn(true);
    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);
    assertTrue(aDestResource.getFile().exists());
    assertTrue(aDestResource.getFile().isDirectory());
    aTasklet.setSourceFactory(aSourceFactory);
    aTasklet.setDestinationFactory(aDestinationFactory);
    aTasklet.setOperation(Operation.COPY);
    aTasklet.afterPropertiesSet();/*from   w ww .j a 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());
    assertTrue(aDestResource.getFile().isDirectory());
    assertEquals(1, aDestResource.getFile().list().length);
}

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();//from w w w  . j ava 2  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  . j  a v 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 testExecuteCopyRecursiveDirectory() 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(true);//  w  w w  .  j a va  2  s .c om
    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(2, aDestResource.getFile().list().length);
    assertArrayEquals(aFileResource1.getFile().list(), aDestResource.getFile().list());
    assertArrayEquals(aFileResource1.getFile().listFiles()[0].list(),
            aDestResource.getFile().listFiles()[0].list());
}

From source file:org.ireland.jnetty.webapp.ServletContextImpl.java

/**
 * Returns an enumeration of all the resources.
 * @see  org.springframework.mock.web.MockServletContext.getResourcePaths(String path)
 *///from  w w w  . j  a va2s . c o m
@Override
public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {
        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) {
        log.warn("Couldn't get resource paths for " + resource, ex);
        return null;
    }
}

From source file:org.jruby.rack.mock.MockServletContext.java

public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {//from  ww  w  . ja va2 s .co  m
        File file = resource.getFile();
        String[] fileList = file.list();
        if (fileList == null || fileList.length == 0) {
            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.log("WARN: Couldn't get resource paths for " + resource, ex);
        return 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 {// w w w  . j  a  v  a  2  s . c o  m
        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:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public Resource createPath(Resource resource, String path) {
    Assert.isInstanceOf(FileSystemResource.class, resource, "Expected a FileSystemResource");
    try {/*www  . j av a2 s  . c om*/
        if (!resource.exists()) {
            File rootFile = resource.getFile();
            while (rootFile.getAbsolutePath().length() > 1 && !rootFile.exists()) {
                rootFile = rootFile.getParentFile();
            }
            IOUtils.makeDirectories(resource.getFile(), rootFile);
        }
        FileSystemResource relativeResource = (FileSystemResource) resource.createRelative(path);
        if (!relativeResource.exists()) {
            if (relativeResource.getPath().endsWith("/")) {
                IOUtils.makeDirectories(relativeResource.getFile(), resource.getFile());
            } else {
                IOUtils.makeDirectories(relativeResource.getFile().getParentFile(), resource.getFile());
            }
        }
        return relativeResource;
    } catch (IOException ex) {
        throw new WMRuntimeException(ex);
    }
}

From source file:org.brekka.stillingar.spring.resource.ScanningResourceSelector.java

/**
 * Search the specified base directory for files with names matching those in <code>names</code>. If the location
 * gets rejected then it should be added to the list of rejected locations.
 * /*from   w w w  .j  a v  a  2s.c o m*/
 * @param locationBase
 *            the location to search
 * @param names
 *            the names of files to find within the base location
 * @param rejected
 *            collects failed locations.
 * @return the resource or null if one cannot be found.
 */
protected Resource findInBaseDir(BaseDirectory locationBase, Set<String> names,
        List<RejectedSnapshotLocation> rejected) {
    Resource dir = locationBase.getDirResource();
    if (dir instanceof UnresolvableResource) {
        UnresolvableResource res = (UnresolvableResource) dir;
        rejected.add(new Rejected(locationBase.getDisposition(), null, res.getMessage()));
    } else {
        String dirPath = null;
        try {
            URI uri = dir.getURI();
            if (uri != null) {
                dirPath = uri.toString();
            }
        } catch (IOException e) {
            if (log.isWarnEnabled()) {
                log.warn(format("Resource dir '%s' has a bad uri", locationBase), e);
            }
        }
        String message;
        if (dir.exists()) {
            StringBuilder messageBuilder = new StringBuilder();
            for (String name : names) {
                try {
                    Resource location = dir.createRelative(name);
                    if (location.exists()) {
                        if (location.isReadable()) {
                            // We have found a file
                            return location;
                        }
                        if (messageBuilder.length() > 0) {
                            messageBuilder.append(" ");
                        }
                        messageBuilder.append("File '%s' exists but cannot be read.");
                    } else {
                        // Fair enough, it does not exist
                    }
                } catch (IOException e) {
                    // Location could not be resolved, log as warning, then move on to the next one.
                    if (log.isWarnEnabled()) {
                        log.warn(format("Resource location '%s' encountered problem", locationBase), e);
                    }
                }
            }
            if (messageBuilder.length() == 0) {
                message = "no configuration files found";
            } else {
                message = messageBuilder.toString();
            }
        } else {
            message = "Directory does not exist";
        }
        rejected.add(new Rejected(locationBase.getDisposition(), dirPath, message));
    }
    // No resource found
    return null;
}

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

@Override
public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {//from  w  w  w  .j a  va  2  s .co m
        File file = resource.getFile();
        String[] fileList = file.list();
        if (ObjectUtils.isEmpty(fileList)) {
            return null;
        }
        Set<String> resourcePaths = new LinkedHashSet<>(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;
    }
}