1 16 17 package org.springframework.core.io.support; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.List ; 23 24 import junit.framework.TestCase; 25 26 import org.springframework.core.io.Resource; 27 28 32 public class PathMatchingResourcePatternResolverTests extends TestCase { 33 34 private static final String [] CLASSES_IN_CORE_IO_SUPPORT = 35 new String [] {"PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class", 36 "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class"}; 37 38 private static final String [] TEST_CLASSES_IN_CORE_IO_SUPPORT = 39 new String [] {"PathMatchingResourcePatternResolverTests.class"}; 40 41 private static final String [] CLASSES_IN_AOPALLIANCE = 42 new String [] {"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 { 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 { 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 { 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 { 67 Resource[] resources = resolver.getResources("classpath:org/aopalliance/**/*.class"); 68 assertProtocolAndFilenames(resources, "jar", CLASSES_IN_AOPALLIANCE); 69 } 70 71 public void testClasspathStartWithPatternInJar() throws IOException { 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 urlProtocol, String fileName) throws IOException { 77 assertProtocolAndFilenames(new Resource[]{resource}, urlProtocol, new String [] {fileName}); 78 } 79 80 private void assertProtocolAndFilenames( 81 Resource[] resources, String urlProtocol, String [] fileNames1, String [] fileNames2) throws IOException { 82 List fileNames = new ArrayList (Arrays.asList(fileNames1)); 83 fileNames.addAll(Arrays.asList(fileNames2)); 84 assertProtocolAndFilenames(resources, urlProtocol, (String []) fileNames.toArray(new String [fileNames.size()])); 85 } 86 87 private void assertProtocolAndFilenames(Resource[] resources, String urlProtocol, String [] fileNames) 88 throws IOException { 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 [] 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 |