1 16 17 package org.springframework.core.io; 18 19 import java.io.IOException ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.mock.web.MockServletContext; 24 import org.springframework.web.context.support.ServletContextResource; 25 26 30 public class ResourceTests extends TestCase { 31 32 public void testClassPathResource() throws IOException { 33 Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class"); 34 doTestResource(resource); 35 } 36 37 public void testClassPathResourceWithClassLoader() throws IOException { 38 Resource resource = 39 new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader()); 40 doTestResource(resource); 41 } 42 43 public void testClassPathResourceWithClass() throws IOException { 44 Resource resource = new ClassPathResource("Resource.class", getClass()); 45 doTestResource(resource); 46 } 47 48 public void testFileSystemResource() throws IOException { 49 Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile()); 50 doTestResource(resource); 51 } 52 53 public void testUrlResource() throws IOException { 54 Resource resource = new UrlResource(getClass().getResource("Resource.class")); 55 doTestResource(resource); 56 } 57 58 public void testServletContextResource() throws IOException { 59 MockServletContext sc = new MockServletContext(); 60 Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class"); 61 doTestResource(resource); 62 } 63 64 private void doTestResource(Resource resource) throws IOException { 65 assertEquals("Resource.class", resource.getFilename()); 66 assertTrue(resource.getURL().getFile().endsWith("Resource.class")); 67 68 Resource relative1 = resource.createRelative("ClassPathResource.class"); 69 assertEquals("ClassPathResource.class", relative1.getFilename()); 70 assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class")); 71 assertTrue(relative1.exists()); 72 73 Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class"); 74 assertEquals("ResourcePatternResolver.class", relative2.getFilename()); 75 assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class")); 76 assertTrue(relative2.exists()); 77 78 Resource relative3 = resource.createRelative("../SpringVersion.class"); 79 assertEquals("SpringVersion.class", relative3.getFilename()); 80 assertTrue(relative3.getURL().getFile().endsWith("SpringVersion.class")); 81 assertTrue(relative3.exists()); 82 } 83 84 public void testClassPathResourceWithRelativePath() throws IOException { 85 Resource resource = new ClassPathResource("dir/"); 86 Resource relative = resource.createRelative("subdir"); 87 assertEquals(new ClassPathResource("dir/subdir"), relative); 88 } 89 90 public void testFileSystemResourceWithRelativePath() throws IOException { 91 Resource resource = new FileSystemResource("dir/"); 92 Resource relative = resource.createRelative("subdir"); 93 assertEquals(new FileSystemResource("dir/subdir"), relative); 94 } 95 96 public void testUrlResourceWithRelativePath() throws IOException { 97 Resource resource = new UrlResource("file:dir/"); 98 Resource relative = resource.createRelative("subdir"); 99 assertEquals(new UrlResource("file:dir/subdir"), relative); 100 } 101 102 public void testServletContextResourceWithRelativePath() throws IOException { 103 MockServletContext sc = new MockServletContext(); 104 Resource resource = new ServletContextResource(sc, "dir/"); 105 Resource relative = resource.createRelative("subdir"); 106 assertEquals(new ServletContextResource(sc, "dir/subdir"), relative); 107 } 108 109 } 110 | Popular Tags |