SPR-6888: ClassPathResource.getDescription() now fully qualifies resource path regardless of constructor used.
This commit is contained in:
parent
5dc59aaa7b
commit
5e19f2959e
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2010 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -45,7 +45,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
private Class clazz;
|
private Class<?> clazz;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -89,7 +89,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||||
* @param clazz the class to load resources with
|
* @param clazz the class to load resources with
|
||||||
* @see java.lang.Class#getResourceAsStream
|
* @see java.lang.Class#getResourceAsStream
|
||||||
*/
|
*/
|
||||||
public ClassPathResource(String path, Class clazz) {
|
public ClassPathResource(String path, Class<?> clazz) {
|
||||||
Assert.notNull(path, "Path must not be null");
|
Assert.notNull(path, "Path must not be null");
|
||||||
this.path = StringUtils.cleanPath(path);
|
this.path = StringUtils.cleanPath(path);
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
|
|
@ -102,7 +102,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||||
* @param classLoader the class loader to load the resource with, if any
|
* @param classLoader the class loader to load the resource with, if any
|
||||||
* @param clazz the class to load resources with, if any
|
* @param clazz the class to load resources with, if any
|
||||||
*/
|
*/
|
||||||
protected ClassPathResource(String path, ClassLoader classLoader, Class clazz) {
|
protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
|
||||||
this.path = StringUtils.cleanPath(path);
|
this.path = StringUtils.cleanPath(path);
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
|
|
@ -190,7 +190,16 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||||
* This implementation returns a description that includes the class path location.
|
* This implementation returns a description that includes the class path location.
|
||||||
*/
|
*/
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return "class path resource [" + this.path + "]";
|
StringBuilder builder = new StringBuilder("class path resource [");
|
||||||
|
|
||||||
|
if (this.clazz != null) {
|
||||||
|
builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
|
||||||
|
builder.append('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.append(this.path);
|
||||||
|
builder.append(']');
|
||||||
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2010 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.core.io;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import static org.junit.internal.matchers.StringContains.containsString;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests cornering bug SPR-6888.
|
||||||
|
*
|
||||||
|
* @author Chris Beams
|
||||||
|
*/
|
||||||
|
public class ClassPathResourceTests {
|
||||||
|
private static final String PACKAGE_PATH = "org/springframework/core/io";
|
||||||
|
private static final String RESOURCE_NAME = "notexist.xml";
|
||||||
|
private static final String FQ_RESOURCE_PATH = PACKAGE_PATH + '/' + RESOURCE_NAME;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void stringConstructorRaisesExceptionWithFullyQualifiedPath() {
|
||||||
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath() {
|
||||||
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(RESOURCE_NAME, this.getClass()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath() {
|
||||||
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH, this.getClass().getClassLoader()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertExceptionContainsFullyQualifiedPath(ClassPathResource resource) {
|
||||||
|
try {
|
||||||
|
resource.getInputStream();
|
||||||
|
fail("FileNotFoundException expected for resource: " + resource);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
assertThat(ex, instanceOf(FileNotFoundException.class));
|
||||||
|
assertThat(ex.getMessage(), containsString(FQ_RESOURCE_PATH));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue