001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.springframework.core.io.support; 017 018 import java.io.IOException; 019 import java.util.ArrayList; 020 import java.util.Arrays; 021 import java.util.List; 022 023 import junit.framework.TestCase; 024 025 import org.springframework.core.io.Resource; 026 027 /** 028 * @author Oliver Hutchison 029 * @since 17.11.2004 030 */ 031 public class PathMatchingResourcePatternResolverTests extends TestCase { 032 033 private static final String[] CLASSES_IN_CORE_IO_SUPPORT = 034 new String[] {"PathMatchingResourcePatternResolver.class", 035 "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class"}; 036 037 private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT = 038 new String[] {"PathMatchingResourcePatternResolverTests.class"}; 039 040 private static final String[] CLASSES_IN_AOPALLIANCE = 041 new String[] {"Advice.class", "AspectException.class", "ConstructorInterceptor.class", 042 "ConstructorInvocation.class", "Interceptor.class", "Invocation.class", 043 "Joinpoint.class", "MethodInterceptor.class", "MethodInvocation.class"}; 044 045 private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 046 047 public void testSingleResourceOnFileSystem() throws IOException { 048 Resource[] resources = 049 resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class"); 050 assertEquals(1, resources.length); 051 assertProtocolAndFilename(resources[0], "file", "PathMatchingResourcePatternResolverTests.class"); 052 } 053 054 public void testSingleResourceInJar() throws IOException { 055 Resource[] resources = resolver.getResources("java/net/URL.class"); 056 assertEquals(1, resources.length); 057 assertProtocolAndFilename(resources[0], "jar", "URL.class"); 058 } 059 060 public void testClasspathStarWithPatternOnFileSystem() throws IOException { 061 Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class"); 062 assertProtocolAndFilenames(resources, "file", CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT); 063 } 064 065 public void testClasspathWithPatternInJar() throws IOException { 066 Resource[] resources = resolver.getResources("classpath:org/aopalliance/**/*.class"); 067 assertProtocolAndFilenames(resources, "jar", CLASSES_IN_AOPALLIANCE); 068 } 069 070 public void testClasspathStartWithPatternInJar() throws IOException { 071 Resource[] resources = resolver.getResources("classpath*:org/aopalliance/**/*.class"); 072 assertProtocolAndFilenames(resources, "jar", CLASSES_IN_AOPALLIANCE); 073 } 074 075 private void assertProtocolAndFilename(Resource resource, String urlProtocol, String fileName) throws IOException { 076 assertProtocolAndFilenames(new Resource[]{resource}, urlProtocol, new String[] {fileName}); 077 } 078 079 private void assertProtocolAndFilenames( 080 Resource[] resources, String urlProtocol, String[] fileNames1, String[] fileNames2) throws IOException { 081 List fileNames = new ArrayList(Arrays.asList(fileNames1)); 082 fileNames.addAll(Arrays.asList(fileNames2)); Rate083 assertProtocolAndFilenames(resources, urlProtocol, (String[]) fileNames.toArray(new String[fileNames.size()])); 084 } 085 086 private void assertProtocolAndFilenames(Resource[] resources, String urlProtocol, String[] fileNames) 087 throws IOException { 088 assertEquals("Correct number of files found", fileNames.length, resources.length); 089 for (int i = 0; i < resources.length; i++) { 090 Resource resource = resources[i]; 091 assertEquals(urlProtocol, resource.getURL().getProtocol()); 092 assertFileNameIn(resource, fileNames); 093 } 094 } 095 096 private void assertFileNameIn(Resource resource, String[] fileNames) { 097 for (int i = 0; i < fileNames.length; i++) { 098 if (resource.getFilename().endsWith(fileNames[i])) { 099 return; 100 } 101 } 102 fail("resource [" + resource + "] does not have a filename that matches and of the names in 'fileNames'"); 103 } 104 105 }