|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2017 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. |
|
|
|
@ -47,26 +47,6 @@ public class ClassPathResourceTests { |
|
|
|
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("^class path resource \\[(.+?)\\]$"); |
|
|
|
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("^class path resource \\[(.+?)\\]$"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) { |
|
|
|
|
|
|
|
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription()); |
|
|
|
|
|
|
|
assertTrue(matcher.matches()); |
|
|
|
|
|
|
|
assertEquals(1, matcher.groupCount()); |
|
|
|
|
|
|
|
String match = matcher.group(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedPath, match); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void stringConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
public void stringConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH)); |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH)); |
|
|
|
@ -74,13 +54,12 @@ public class ClassPathResourceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, this.getClass())); |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath() { |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH, |
|
|
|
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader())); |
|
|
|
this.getClass().getClassLoader())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -91,31 +70,64 @@ public class ClassPathResourceTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getDescriptionWithStringConstructorAndLeadingSlash() { |
|
|
|
public void getDescriptionWithStringConstructorAndLeadingSlash() { |
|
|
|
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH), |
|
|
|
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH), |
|
|
|
FQ_RESOURCE_PATH); |
|
|
|
FQ_RESOURCE_PATH); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getDescriptionWithClassLiteralConstructor() { |
|
|
|
public void getDescriptionWithClassLiteralConstructor() { |
|
|
|
assertDescriptionContainsExpectedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, this.getClass()), |
|
|
|
assertDescriptionContainsExpectedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()), |
|
|
|
FQ_RESOURCE_PATH); |
|
|
|
FQ_RESOURCE_PATH); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getDescriptionWithClassLiteralConstructorAndLeadingSlash() { |
|
|
|
public void getDescriptionWithClassLiteralConstructorAndLeadingSlash() { |
|
|
|
assertDescriptionContainsExpectedPath( |
|
|
|
assertDescriptionContainsExpectedPath( |
|
|
|
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, this.getClass()), FQ_RESOURCE_PATH); |
|
|
|
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass()), FQ_RESOURCE_PATH); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getDescriptionWithClassLoaderConstructor() { |
|
|
|
public void getDescriptionWithClassLoaderConstructor() { |
|
|
|
assertDescriptionContainsExpectedPath( |
|
|
|
assertDescriptionContainsExpectedPath( |
|
|
|
new ClassPathResource(FQ_RESOURCE_PATH, this.getClass().getClassLoader()), FQ_RESOURCE_PATH); |
|
|
|
new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()), FQ_RESOURCE_PATH); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getDescriptionWithClassLoaderConstructorAndLeadingSlash() { |
|
|
|
public void getDescriptionWithClassLoaderConstructorAndLeadingSlash() { |
|
|
|
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, |
|
|
|
assertDescriptionContainsExpectedPath( |
|
|
|
this.getClass().getClassLoader()), FQ_RESOURCE_PATH); |
|
|
|
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass().getClassLoader()), FQ_RESOURCE_PATH); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void dropLeadingSlashForClassLoaderAccess() { |
|
|
|
|
|
|
|
assertEquals("test.html", new ClassPathResource("/test.html").getPath()); |
|
|
|
|
|
|
|
assertEquals("test.html", ((ClassPathResource) new ClassPathResource("").createRelative("/test.html")).getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void preserveLeadingSlashForClassRelativeAccess() { |
|
|
|
|
|
|
|
assertEquals("/test.html", new ClassPathResource("/test.html", getClass()).getPath()); |
|
|
|
|
|
|
|
assertEquals("/test.html", ((ClassPathResource) new ClassPathResource("", getClass()).createRelative("/test.html")).getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) { |
|
|
|
|
|
|
|
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription()); |
|
|
|
|
|
|
|
assertTrue(matcher.matches()); |
|
|
|
|
|
|
|
assertEquals(1, matcher.groupCount()); |
|
|
|
|
|
|
|
String match = matcher.group(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedPath, match); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|