/** * This implementation creates a FileSystemResource, applying the given path * relative to the path of the underlying file of this resource descriptor. * @see org.springframework.util.StringUtils#applyRelativePath(String, String) */ @Override public Resource createRelative(String relativePath) { String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); return (this.file != null ? new FileSystemResource(pathToUse) : new FileSystemResource(this.filePath.getFileSystem(), pathToUse)); }
/** * Resolve resource paths as file system paths. * <p>Note: Even if a given path starts with a slash, it will get * interpreted as relative to the current VM working directory. * This is consistent with the semantics in a Servlet container. * @param path path to the resource * @return the Resource handle * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath */ @Override protected Resource getResourceByPath(String path) { if (path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); }
@Override public Resource convert(String source) { return new FileSystemResource(source + ".xml"); }
/** * Find all resources in the file system that match the given location pattern * via the Ant-style PathMatcher. * @param rootDir the root directory in the file system * @param subPattern the sub pattern to match (below the root directory) * @return a mutable Set of matching Resource instances * @throws IOException in case of I/O errors * @see #retrieveMatchingFiles * @see org.springframework.util.PathMatcher */ protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String subPattern) throws IOException { if (logger.isTraceEnabled()) { logger.trace("Looking for matching resources in directory tree [" + rootDir.getPath() + "]"); } Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern); Set<Resource> result = new LinkedHashSet<>(matchingFiles.size()); for (File file : matchingFiles) { result.add(new FileSystemResource(file)); } return result; }
@Test public void testFileSystemResourceWithPlainPath() { Resource resource = new FileSystemResource("core/io/Resource.class"); assertEquals(resource, new FileSystemResource("core/../core/io/./Resource.class")); }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
@Test public void testFileSystemResourceWithFilePath() throws Exception { Path filePath = Paths.get(getClass().getResource("Resource.class").toURI()); Resource resource = new FileSystemResource(filePath); doTestResource(resource); assertEquals(new FileSystemResource(filePath), resource); }
@Test public void testFileSystemResource() throws IOException { String file = getClass().getResource("Resource.class").getFile(); Resource resource = new FileSystemResource(file); doTestResource(resource); assertEquals(new FileSystemResource(file), resource); }
@Test(expected = FileNotFoundException.class) public void testInputStreamNotFoundOnFileSystemResource() throws IOException { new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").getInputStream(); }
@Test public void testFileSystemResourceWithRelativePath() throws IOException { Resource resource = new FileSystemResource("dir/"); Resource relative = resource.createRelative("subdir"); assertEquals(new FileSystemResource("dir/subdir"), relative); }
@Test public void testResourceArrayPropertyEditor() throws IOException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD); Service service = (Service) ctx.getBean("service"); assertEquals(3, service.getResources().length); List<Resource> resources = Arrays.asList(service.getResources()); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_A).getFile()))); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_B).getFile()))); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_C).getFile()))); ctx.close(); }
@Test(expected = FileNotFoundException.class) public void testReadableChannelNotFoundOnFileSystemResource() throws IOException { new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").readableChannel(); }
@Test public void mixedInNulls() throws Exception { List<Resource> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(null); resources.add(new FileSystemResource("test")); resources.add(new TestResource()); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test public void testReadableChannel() throws IOException { Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile()); ReadableByteChannel channel = null; try { channel = resource.readableChannel(); ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength()); channel.read(buffer); buffer.rewind(); assertTrue(buffer.limit() > 0); } finally { if (channel != null) { channel.close(); } } }
@Test public void differentImpls() throws Exception { List<Resource> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(new FileSystemResource("test")); resources.add(new TestResource()); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test(expected = IllegalStateException.class) public void testSetBarfOnResourceNotFound() { this.factory.setResources(new FileSystemResource("non-exsitent-file.yml")); assertEquals(0, this.factory.getObject().size()); }
@Test public void testFileSystemResourceWithImport() throws URISyntaxException { String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath(); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
@Test(expected = IOException.class) public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception { FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); fcfb.setConfigLocation(new FileSystemResource("myprops.properties")); Properties props = new Properties(); props.setProperty("myprop", "/mydir"); fcfb.setFreemarkerSettings(props); fcfb.afterPropertiesSet(); }
@Test public void testSetIgnoreResourceNotFound() { this.factory.setResolutionMethod(YamlMapFactoryBean.ResolutionMethod.OVERRIDE_AND_IGNORE); this.factory.setResources(new FileSystemResource("non-exsitent-file.yml")); assertEquals(0, this.factory.getObject().size()); }