PathMatchingResourcePatternResolver supports "classpath*" searches in jar file roots as well

Issue: SPR-12095
This commit is contained in:
Juergen Hoeller
2014-08-19 20:34:45 +02:00
parent 0c32d66cbd
commit 8c76381d95
3 changed files with 103 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,6 @@
package org.springframework.core.io.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
@@ -27,8 +24,11 @@ import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.Resource;
import static org.junit.Assert.*;
/**
* If this test case fails, uncomment diagnostics in
* {@code assertProtocolAndFilenames} method.
@@ -91,13 +91,13 @@ public class PathMatchingResourcePatternResolverTests {
Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class");
// Have to exclude Clover-generated class files here,
// as we might be running as part of a Clover test run.
List noCloverResources = new ArrayList();
for (int i = 0; i < resources.length; i++) {
if (resources[i].getFilename().indexOf("$__CLOVER_") == -1) {
noCloverResources.add(resources[i]);
List<Resource> noCloverResources = new ArrayList<Resource>();
for (Resource resource : resources) {
if (!resource.getFilename().contains("$__CLOVER_")) {
noCloverResources.add(resource);
}
}
resources = (Resource[]) noCloverResources.toArray(new Resource[noCloverResources.size()]);
resources = noCloverResources.toArray(new Resource[noCloverResources.size()]);
assertProtocolAndFilenames(resources, "file", CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT);
}
@@ -113,15 +113,29 @@ public class PathMatchingResourcePatternResolverTests {
assertProtocolAndFilenames(resources, "jar", CLASSES_IN_COMMONSLOGGING);
}
@Test
public void testRootPatternRetrievalInJarFiles() throws IOException {
Resource[] resources = resolver.getResources("classpath*:*.dtd");
boolean found = false;
for (Resource resource : resources) {
if (resource.getFilename().equals("aspectj_1_5_0.dtd")) {
found = true;
}
}
assertTrue("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar", found);
}
private void assertProtocolAndFilename(Resource resource, String urlProtocol, String fileName) throws IOException {
assertProtocolAndFilenames(new Resource[] {resource}, urlProtocol, new String[] {fileName});
}
private void assertProtocolAndFilenames(
Resource[] resources, String urlProtocol, String[] fileNames1, String[] fileNames2) throws IOException {
List fileNames = new ArrayList(Arrays.asList(fileNames1));
List<String> fileNames = new ArrayList<String>(Arrays.asList(fileNames1));
fileNames.addAll(Arrays.asList(fileNames2));
assertProtocolAndFilenames(resources, urlProtocol, (String[]) fileNames.toArray(new String[fileNames.size()]));
assertProtocolAndFilenames(resources, urlProtocol, fileNames.toArray(new String[fileNames.size()]));
}
private void assertProtocolAndFilenames(Resource[] resources, String urlProtocol, String[] fileNames)
@@ -146,16 +160,15 @@ public class PathMatchingResourcePatternResolverTests {
// }
assertEquals("Correct number of files found", fileNames.length, resources.length);
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
for (Resource resource : resources) {
assertEquals(urlProtocol, resource.getURL().getProtocol());
assertFilenameIn(resource, fileNames);
}
}
private void assertFilenameIn(Resource resource, String[] fileNames) {
for (int i = 0; i < fileNames.length; i++) {
if (resource.getFilename().endsWith(fileNames[i])) {
for (String fileName : fileNames) {
if (resource.getFilename().endsWith(fileName)) {
return;
}
}