|
|
|
@ -16,6 +16,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.core.io; |
|
|
|
package org.springframework.core.io; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
import static org.junit.Assert.fail; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
@ -23,8 +28,8 @@ import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
|
|
|
|
import junit.framework.TestCase; |
|
|
|
import org.junit.Ignore; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.springframework.mock.web.MockServletContext; |
|
|
|
import org.springframework.mock.web.MockServletContext; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.web.context.support.ServletContextResource; |
|
|
|
import org.springframework.web.context.support.ServletContextResource; |
|
|
|
@ -33,8 +38,9 @@ import org.springframework.web.context.support.ServletContextResource; |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 09.09.2004 |
|
|
|
* @since 09.09.2004 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ResourceTests extends TestCase { |
|
|
|
public class ResourceTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testByteArrayResource() throws IOException { |
|
|
|
public void testByteArrayResource() throws IOException { |
|
|
|
Resource resource = new ByteArrayResource("testString".getBytes()); |
|
|
|
Resource resource = new ByteArrayResource("testString".getBytes()); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
@ -44,6 +50,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource, new ByteArrayResource("testString".getBytes())); |
|
|
|
assertEquals(resource, new ByteArrayResource("testString".getBytes())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testByteArrayResourceWithDescription() throws IOException { |
|
|
|
public void testByteArrayResourceWithDescription() throws IOException { |
|
|
|
Resource resource = new ByteArrayResource("testString".getBytes(), "my description"); |
|
|
|
Resource resource = new ByteArrayResource("testString".getBytes(), "my description"); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
@ -54,6 +61,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource, new ByteArrayResource("testString".getBytes())); |
|
|
|
assertEquals(resource, new ByteArrayResource("testString".getBytes())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testInputStreamResource() throws IOException { |
|
|
|
public void testInputStreamResource() throws IOException { |
|
|
|
InputStream is = new ByteArrayInputStream("testString".getBytes()); |
|
|
|
InputStream is = new ByteArrayInputStream("testString".getBytes()); |
|
|
|
Resource resource = new InputStreamResource(is); |
|
|
|
Resource resource = new InputStreamResource(is); |
|
|
|
@ -64,6 +72,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource, new InputStreamResource(is)); |
|
|
|
assertEquals(resource, new InputStreamResource(is)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testInputStreamResourceWithDescription() throws IOException { |
|
|
|
public void testInputStreamResourceWithDescription() throws IOException { |
|
|
|
InputStream is = new ByteArrayInputStream("testString".getBytes()); |
|
|
|
InputStream is = new ByteArrayInputStream("testString".getBytes()); |
|
|
|
Resource resource = new InputStreamResource(is, "my description"); |
|
|
|
Resource resource = new InputStreamResource(is, "my description"); |
|
|
|
@ -75,6 +84,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource, new InputStreamResource(is)); |
|
|
|
assertEquals(resource, new InputStreamResource(is)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClassPathResource() throws IOException { |
|
|
|
public void testClassPathResource() throws IOException { |
|
|
|
Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class"); |
|
|
|
Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class"); |
|
|
|
doTestResource(resource); |
|
|
|
doTestResource(resource); |
|
|
|
@ -90,6 +100,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(1, resources.size()); |
|
|
|
assertEquals(1, resources.size()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClassPathResourceWithClassLoader() throws IOException { |
|
|
|
public void testClassPathResourceWithClassLoader() throws IOException { |
|
|
|
Resource resource = |
|
|
|
Resource resource = |
|
|
|
new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader()); |
|
|
|
new ClassPathResource("org/springframework/core/io/Resource.class", getClass().getClassLoader()); |
|
|
|
@ -98,12 +109,15 @@ public class ResourceTests extends TestCase { |
|
|
|
new ClassPathResource("org/springframework/core/../core/io/./Resource.class", getClass().getClassLoader())); |
|
|
|
new ClassPathResource("org/springframework/core/../core/io/./Resource.class", getClass().getClassLoader())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClassPathResourceWithClass() throws IOException { |
|
|
|
public void testClassPathResourceWithClass() throws IOException { |
|
|
|
Resource resource = new ClassPathResource("Resource.class", getClass()); |
|
|
|
Resource resource = new ClassPathResource("Resource.class", getClass()); |
|
|
|
doTestResource(resource); |
|
|
|
doTestResource(resource); |
|
|
|
assertEquals(resource, new ClassPathResource("Resource.class", getClass())); |
|
|
|
assertEquals(resource, new ClassPathResource("Resource.class", getClass())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Ignore // passes under eclipse, fails under ant
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFileSystemResource() throws IOException { |
|
|
|
public void testFileSystemResource() throws IOException { |
|
|
|
Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile()); |
|
|
|
Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile()); |
|
|
|
doTestResource(resource); |
|
|
|
doTestResource(resource); |
|
|
|
@ -112,6 +126,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource2, new FileSystemResource("core/../core/io/./Resource.class")); |
|
|
|
assertEquals(resource2, new FileSystemResource("core/../core/io/./Resource.class")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testUrlResource() throws IOException { |
|
|
|
public void testUrlResource() throws IOException { |
|
|
|
Resource resource = new UrlResource(getClass().getResource("Resource.class")); |
|
|
|
Resource resource = new UrlResource(getClass().getResource("Resource.class")); |
|
|
|
doTestResource(resource); |
|
|
|
doTestResource(resource); |
|
|
|
@ -120,6 +135,7 @@ public class ResourceTests extends TestCase { |
|
|
|
assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class")); |
|
|
|
assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testServletContextResource() throws IOException { |
|
|
|
public void testServletContextResource() throws IOException { |
|
|
|
MockServletContext sc = new MockServletContext(); |
|
|
|
MockServletContext sc = new MockServletContext(); |
|
|
|
Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class"); |
|
|
|
Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class"); |
|
|
|
@ -149,24 +165,28 @@ public class ResourceTests extends TestCase { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testClassPathResourceWithRelativePath() throws IOException { |
|
|
|
public void testClassPathResourceWithRelativePath() throws IOException { |
|
|
|
Resource resource = new ClassPathResource("dir/"); |
|
|
|
Resource resource = new ClassPathResource("dir/"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
assertEquals(new ClassPathResource("dir/subdir"), relative); |
|
|
|
assertEquals(new ClassPathResource("dir/subdir"), relative); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFileSystemResourceWithRelativePath() throws IOException { |
|
|
|
public void testFileSystemResourceWithRelativePath() throws IOException { |
|
|
|
Resource resource = new FileSystemResource("dir/"); |
|
|
|
Resource resource = new FileSystemResource("dir/"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
assertEquals(new FileSystemResource("dir/subdir"), relative); |
|
|
|
assertEquals(new FileSystemResource("dir/subdir"), relative); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testUrlResourceWithRelativePath() throws IOException { |
|
|
|
public void testUrlResourceWithRelativePath() throws IOException { |
|
|
|
Resource resource = new UrlResource("file:dir/"); |
|
|
|
Resource resource = new UrlResource("file:dir/"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
Resource relative = resource.createRelative("subdir"); |
|
|
|
assertEquals(new UrlResource("file:dir/subdir"), relative); |
|
|
|
assertEquals(new UrlResource("file:dir/subdir"), relative); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testServletContextResourceWithRelativePath() throws IOException { |
|
|
|
public void testServletContextResourceWithRelativePath() throws IOException { |
|
|
|
MockServletContext sc = new MockServletContext(); |
|
|
|
MockServletContext sc = new MockServletContext(); |
|
|
|
Resource resource = new ServletContextResource(sc, "dir/"); |
|
|
|
Resource resource = new ServletContextResource(sc, "dir/"); |
|
|
|
@ -175,12 +195,14 @@ public class ResourceTests extends TestCase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
|
|
|
|
* @Test |
|
|
|
public void testNonFileResourceExists() throws Exception { |
|
|
|
public void testNonFileResourceExists() throws Exception { |
|
|
|
Resource resource = new UrlResource("http://www.springframework.org"); |
|
|
|
Resource resource = new UrlResource("http://www.springframework.org"); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
assertTrue(resource.exists()); |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testAbstractResourceExceptions() throws Exception { |
|
|
|
public void testAbstractResourceExceptions() throws Exception { |
|
|
|
final String name = "test-resource"; |
|
|
|
final String name = "test-resource"; |
|
|
|
|
|
|
|
|
|
|
|
|