@Override public boolean exists() { return this.gzipped.exists(); }
@Override @Nullable public Object findTemplateSource(String name) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Looking for FreeMarker template with name [" + name + "]"); } Resource resource = this.resourceLoader.getResource(this.templateLoaderPath + name); return (resource.exists() ? resource : null); }
/** * Set the temporary directory where uploaded files get stored. * Default is the servlet container's temporary directory for the web application. * @see org.springframework.web.util.WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE */ public void setUploadTempDir(Resource uploadTempDir) throws IOException { if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { throw new IllegalArgumentException("Given uploadTempDir [" + uploadTempDir + "] could not be created"); } this.fileItemFactory.setRepository(uploadTempDir.getFile()); this.uploadTempDirSpecified = true; }
@Nullable protected Resource getResource(String location) { if (this.resourceLoaderPaths != null) { for (String path : this.resourceLoaderPaths) { Resource resource = obtainApplicationContext().getResource(path + location); if (resource.exists()) { return resource; } } } return null; }
@Nullable protected Resource getResource(String location) { if (this.resourceLoaderPaths != null) { for (String path : this.resourceLoaderPaths) { Resource resource = obtainApplicationContext().getResource(path + location); if (resource.exists()) { return resource; } } } return null; }
private void testCheckResource(Resource location, String requestPath) throws IOException { List<Resource> locations = singletonList(location); Resource actual = this.resolver.resolveResource(null, requestPath, locations, null).block(TIMEOUT); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } assertNull(actual); }
private void testCheckResource(Resource location, String requestPath) throws IOException { List<Resource> locations = Collections.singletonList(location); Resource actual = this.resolver.resolveResource(null, requestPath, locations, null); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } assertNull(actual); }
@Test public void sunnyDay() throws Exception { PropertyEditor editor = new ResourceEditor(); editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class"); Resource resource = (Resource) editor.getValue(); assertNotNull(resource); assertTrue(resource.exists()); }
@Test public void testPatternResource() throws Exception { // N.B. this will sometimes fail if you use classpath: instead of classpath*:. // The result depends on the classpath - if test-classes are segregated from classes // and they come first on the classpath (like in Maven) then it breaks, if classes // comes first (like in Spring Build) then it is OK. PropertyEditor editor = new ResourceArrayPropertyEditor(); editor.setAsText("classpath*:org/springframework/core/io/support/Resource*Editor.class"); Resource[] resources = (Resource[]) editor.getValue(); assertNotNull(resources); assertTrue(resources[0].exists()); }
@Test public void testVanillaResource() throws Exception { PropertyEditor editor = new ResourceArrayPropertyEditor(); editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class"); Resource[] resources = (Resource[]) editor.getValue(); assertNotNull(resources); assertTrue(resources[0].exists()); }
@Test public void testServletContextResourceLoader() { MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context"); ServletContextResourceLoader rl = new ServletContextResourceLoader(sc); assertTrue(rl.getResource("/WEB-INF/web.xml").exists()); assertTrue(rl.getResource("WEB-INF/web.xml").exists()); assertTrue(rl.getResource("../context/WEB-INF/web.xml").exists()); assertTrue(rl.getResource("/../context/WEB-INF/web.xml").exists()); }
@Test public void testByteArrayResource() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes()); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void testInputStreamResource() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new InputStreamResource(is)); }
@Test public void testInputStreamResourceWithDescription() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is, "my description"); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new InputStreamResource(is)); }
@Test public void testByteArrayResourceWithDescription() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes(), "my description"); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void webApplicationContextIsLoaded() { // from: src/test/webapp/resources/Spring.js Resource resource = wac.getResource("/resources/Spring.js"); assertNotNull(resource); assertTrue(resource.exists()); }
private void testResolvePathWithTraversal(Resource location, String requestPath) throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath); this.response = new MockHttpServletResponse(); this.handler.handleRequest(this.request, this.response); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } assertEquals(HttpStatus.NOT_FOUND.value(), this.response.getStatus()); }
@Ignore @Test // this test is quite slow. TODO: re-enable with JUnit categories public void testNonFileResourceExists() throws Exception { Resource resource = new UrlResource("http://www.springframework.org"); assertTrue(resource.exists()); }
@Test public void testPersistenceUnitRootUrlWithJar() throws Exception { ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar"); String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml"; Resource insideArchive = new UrlResource(newRoot); // make sure the location actually exists assertTrue(insideArchive.exists()); URL url = PersistenceUnitReader.determinePersistenceUnitRootUrl(insideArchive); assertTrue("the archive location should have been returned", archive.getURL().sameFile(url)); }