KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > ResourceTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.io;
18
19 import java.io.IOException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.mock.web.MockServletContext;
24 import org.springframework.web.context.support.ServletContextResource;
25
26 /**
27  * @author Juergen Hoeller
28  * @since 09.09.2004
29  */

30 public class ResourceTests extends TestCase {
31
32     public void testClassPathResource() throws IOException JavaDoc {
33         Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class");
34         doTestResource(resource);
35     }
36
37     public void testClassPathResourceWithClassLoader() throws IOException JavaDoc {
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 JavaDoc {
44         Resource resource = new ClassPathResource("Resource.class", getClass());
45         doTestResource(resource);
46     }
47
48     public void testFileSystemResource() throws IOException JavaDoc {
49         Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile());
50         doTestResource(resource);
51     }
52
53     public void testUrlResource() throws IOException JavaDoc {
54         Resource resource = new UrlResource(getClass().getResource("Resource.class"));
55         doTestResource(resource);
56     }
57
58     public void testServletContextResource() throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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