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