Polish ModuleResource[Tests]
This commit is contained in:
parent
f075120675
commit
9ac2443b78
|
@ -101,7 +101,7 @@ public class ModuleResource extends AbstractResource {
|
|||
@Override
|
||||
public String getDescription() {
|
||||
return "module resource [" + this.path + "]" +
|
||||
(this.module.isNamed() ? " from module '" + this.module.getName() + "'" : "");
|
||||
(this.module.isNamed() ? " from module [" + this.module.getName() + "]" : "");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -29,36 +29,48 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||
* Unit tests for the {@link ModuleResource} class.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
*/
|
||||
class ModuleResourceTests {
|
||||
|
||||
@Test
|
||||
void existingResource() throws IOException {
|
||||
ModuleResource mr = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class");
|
||||
assertThat(mr.exists()).isTrue();
|
||||
assertThat(mr.isReadable()).isTrue();
|
||||
assertThat(mr.isOpen()).isFalse();
|
||||
assertThat(mr.isFile()).isFalse();
|
||||
assertThat(mr.getFilename()).isEqualTo("Introspector.class");
|
||||
assertThat(mr.getDescription()).contains(mr.getModule().getName());
|
||||
assertThat(mr.getDescription()).contains(mr.getPath());
|
||||
private static final String existingPath = "java/beans/Introspector.class";
|
||||
private static final String nonExistingPath = "org/example/NonExistingClass.class";
|
||||
|
||||
|
||||
@Test
|
||||
void existingClassFileResource() throws IOException {
|
||||
// Check expected behavior of ClassPathResource first.
|
||||
ClassPathResource cpr = new ClassPathResource(existingPath);
|
||||
assertExistingResource(cpr);
|
||||
assertThat(cpr.getDescription()).startsWith("class path resource").contains(cpr.getPath());
|
||||
|
||||
ModuleResource mr = new ModuleResource(Introspector.class.getModule(), existingPath);
|
||||
assertExistingResource(mr);
|
||||
assertThat(mr.getDescription()).startsWith("module resource").contains(mr.getModule().getName(), mr.getPath());
|
||||
System.err.println(mr.getDescription());
|
||||
|
||||
Resource cpr = new ClassPathResource("java/beans/Introspector.class");
|
||||
assertThat(mr.getContentAsByteArray()).isEqualTo(cpr.getContentAsByteArray());
|
||||
assertThat(mr.contentLength()).isEqualTo(cpr.contentLength());
|
||||
}
|
||||
|
||||
private static void assertExistingResource(Resource resource) {
|
||||
assertThat(resource.exists()).isTrue();
|
||||
assertThat(resource.isReadable()).isTrue();
|
||||
assertThat(resource.isOpen()).isFalse();
|
||||
assertThat(resource.isFile()).isFalse();
|
||||
assertThat(resource.getFilename()).isEqualTo("Introspector.class");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonExistingResource() {
|
||||
ModuleResource mr = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspecter.class");
|
||||
ModuleResource mr = new ModuleResource(Introspector.class.getModule(), nonExistingPath);
|
||||
assertThat(mr.exists()).isFalse();
|
||||
assertThat(mr.isReadable()).isFalse();
|
||||
assertThat(mr.isOpen()).isFalse();
|
||||
assertThat(mr.isFile()).isFalse();
|
||||
assertThat(mr.getFilename()).isEqualTo("Introspecter.class");
|
||||
assertThat(mr.getDescription()).contains(mr.getModule().getName());
|
||||
assertThat(mr.getDescription()).contains(mr.getPath());
|
||||
assertThat(mr.getFilename()).isEqualTo("NonExistingClass.class");
|
||||
assertThat(mr.getDescription()).startsWith("module resource").contains(mr.getModule().getName(), mr.getPath());
|
||||
|
||||
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::getContentAsByteArray);
|
||||
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::contentLength);
|
||||
|
@ -66,13 +78,15 @@ class ModuleResourceTests {
|
|||
|
||||
@Test
|
||||
void equalsAndHashCode() {
|
||||
Resource mr1 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class");
|
||||
Resource mr2 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class");
|
||||
Resource mr3 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspecter.class");
|
||||
assertThat(mr1).isEqualTo(mr2);
|
||||
assertThat(mr1).isNotEqualTo(mr3);
|
||||
assertThat(mr1).hasSameHashCodeAs(mr2);
|
||||
assertThat(mr1).doesNotHaveSameHashCodeAs(mr3);
|
||||
Resource resource1 = new ModuleResource(Introspector.class.getModule(), existingPath);
|
||||
Resource resource2 = new ModuleResource(Introspector.class.getModule(), existingPath);
|
||||
Resource resource3 = new ModuleResource(Introspector.class.getModule(), nonExistingPath);
|
||||
assertThat(resource1).isEqualTo(resource1);
|
||||
assertThat(resource1).isEqualTo(resource2);
|
||||
assertThat(resource2).isEqualTo(resource1);
|
||||
assertThat(resource1).isNotEqualTo(resource3);
|
||||
assertThat(resource1).hasSameHashCodeAs(resource2);
|
||||
assertThat(resource1).doesNotHaveSameHashCodeAs(resource3);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue