KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > support > PathMatchingResourcePatternResolverTests


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.support;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.springframework.core.io.Resource;
27
28 /**
29  * @author Oliver Hutchison
30  * @since 17.11.2004
31  */

32 public class PathMatchingResourcePatternResolverTests extends TestCase {
33
34     private static final String JavaDoc[] CLASSES_IN_CORE_IO_SUPPORT =
35             new String JavaDoc[] {"PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
36                     "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class"};
37
38     private static final String JavaDoc[] TEST_CLASSES_IN_CORE_IO_SUPPORT =
39             new String JavaDoc[] {"PathMatchingResourcePatternResolverTests.class"};
40
41     private static final String JavaDoc[] CLASSES_IN_AOPALLIANCE =
42             new String JavaDoc[] {"Advice.class", "AspectException.class", "ConstructorInterceptor.class",
43                     "ConstructorInvocation.class", "Interceptor.class", "Invocation.class",
44                     "Joinpoint.class", "MethodInterceptor.class", "MethodInvocation.class"};
45
46     private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
47
48     public void testSingleResourceOnFileSystem() throws IOException JavaDoc {
49         Resource[] resources =
50                 resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class");
51         assertEquals(1, resources.length);
52         assertProtocolAndFilename(resources[0], "file", "PathMatchingResourcePatternResolverTests.class");
53     }
54
55     public void testSingleResourceInJar() throws IOException JavaDoc {
56         Resource[] resources = resolver.getResources("java/net/URL.class");
57         assertEquals(1, resources.length);
58         assertProtocolAndFilename(resources[0], "jar", "URL.class");
59     }
60
61     public void testClasspathStarWithPatternOnFileSystem() throws IOException JavaDoc {
62         Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class");
63         assertProtocolAndFilenames(resources, "file", CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT);
64     }
65
66     public void testClasspathWithPatternInJar() throws IOException JavaDoc {
67         Resource[] resources = resolver.getResources("classpath:org/aopalliance/**/*.class");
68         assertProtocolAndFilenames(resources, "jar", CLASSES_IN_AOPALLIANCE);
69     }
70
71     public void testClasspathStartWithPatternInJar() throws IOException JavaDoc {
72         Resource[] resources = resolver.getResources("classpath*:org/aopalliance/**/*.class");
73         assertProtocolAndFilenames(resources, "jar", CLASSES_IN_AOPALLIANCE);
74     }
75
76     private void assertProtocolAndFilename(Resource resource, String JavaDoc urlProtocol, String JavaDoc fileName) throws IOException JavaDoc {
77         assertProtocolAndFilenames(new Resource[]{resource}, urlProtocol, new String JavaDoc[] {fileName});
78     }
79
80     private void assertProtocolAndFilenames(
81             Resource[] resources, String JavaDoc urlProtocol, String JavaDoc[] fileNames1, String JavaDoc[] fileNames2) throws IOException JavaDoc {
82         List JavaDoc fileNames = new ArrayList JavaDoc(Arrays.asList(fileNames1));
83         fileNames.addAll(Arrays.asList(fileNames2));
84         assertProtocolAndFilenames(resources, urlProtocol, (String JavaDoc[]) fileNames.toArray(new String JavaDoc[fileNames.size()]));
85     }
86
87     private void assertProtocolAndFilenames(Resource[] resources, String JavaDoc urlProtocol, String JavaDoc[] fileNames)
88             throws IOException JavaDoc {
89         assertEquals("Correct number of files found", fileNames.length, resources.length);
90         for (int i = 0; i < resources.length; i++) {
91             Resource resource = resources[i];
92             assertEquals(urlProtocol, resource.getURL().getProtocol());
93             assertFileNameIn(resource, fileNames);
94         }
95     }
96
97     private void assertFileNameIn(Resource resource, String JavaDoc[] fileNames) {
98         for (int i = 0; i < fileNames.length; i++) {
99             if (resource.getFilename().endsWith(fileNames[i])) {
100                 return;
101             }
102         }
103         fail("resource [" + resource + "] does not have a filename that matches and of the names in 'fileNames'");
104     }
105
106 }
107
Popular Tags