Polish ModuleResource[Tests]

This commit is contained in:
Sam Brannen 2023-07-01 16:49:32 +02:00
parent f075120675
commit 9ac2443b78
2 changed files with 37 additions and 23 deletions

View File

@ -101,7 +101,7 @@ public class ModuleResource extends AbstractResource {
@Override @Override
public String getDescription() { public String getDescription() {
return "module resource [" + this.path + "]" + return "module resource [" + this.path + "]" +
(this.module.isNamed() ? " from module '" + this.module.getName() + "'" : ""); (this.module.isNamed() ? " from module [" + this.module.getName() + "]" : "");
} }

View File

@ -29,36 +29,48 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Unit tests for the {@link ModuleResource} class. * Unit tests for the {@link ModuleResource} class.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
* @since 6.1 * @since 6.1
*/ */
class ModuleResourceTests { class ModuleResourceTests {
@Test private static final String existingPath = "java/beans/Introspector.class";
void existingResource() throws IOException { private static final String nonExistingPath = "org/example/NonExistingClass.class";
ModuleResource mr = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class");
assertThat(mr.exists()).isTrue();
assertThat(mr.isReadable()).isTrue(); @Test
assertThat(mr.isOpen()).isFalse(); void existingClassFileResource() throws IOException {
assertThat(mr.isFile()).isFalse(); // Check expected behavior of ClassPathResource first.
assertThat(mr.getFilename()).isEqualTo("Introspector.class"); ClassPathResource cpr = new ClassPathResource(existingPath);
assertThat(mr.getDescription()).contains(mr.getModule().getName()); assertExistingResource(cpr);
assertThat(mr.getDescription()).contains(mr.getPath()); 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.getContentAsByteArray()).isEqualTo(cpr.getContentAsByteArray());
assertThat(mr.contentLength()).isEqualTo(cpr.contentLength()); 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 @Test
void nonExistingResource() { 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.exists()).isFalse();
assertThat(mr.isReadable()).isFalse(); assertThat(mr.isReadable()).isFalse();
assertThat(mr.isOpen()).isFalse(); assertThat(mr.isOpen()).isFalse();
assertThat(mr.isFile()).isFalse(); assertThat(mr.isFile()).isFalse();
assertThat(mr.getFilename()).isEqualTo("Introspecter.class"); assertThat(mr.getFilename()).isEqualTo("NonExistingClass.class");
assertThat(mr.getDescription()).contains(mr.getModule().getName()); assertThat(mr.getDescription()).startsWith("module resource").contains(mr.getModule().getName(), mr.getPath());
assertThat(mr.getDescription()).contains(mr.getPath());
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::getContentAsByteArray); assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::getContentAsByteArray);
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::contentLength); assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(mr::contentLength);
@ -66,13 +78,15 @@ class ModuleResourceTests {
@Test @Test
void equalsAndHashCode() { void equalsAndHashCode() {
Resource mr1 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class"); Resource resource1 = new ModuleResource(Introspector.class.getModule(), existingPath);
Resource mr2 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspector.class"); Resource resource2 = new ModuleResource(Introspector.class.getModule(), existingPath);
Resource mr3 = new ModuleResource(Introspector.class.getModule(), "java/beans/Introspecter.class"); Resource resource3 = new ModuleResource(Introspector.class.getModule(), nonExistingPath);
assertThat(mr1).isEqualTo(mr2); assertThat(resource1).isEqualTo(resource1);
assertThat(mr1).isNotEqualTo(mr3); assertThat(resource1).isEqualTo(resource2);
assertThat(mr1).hasSameHashCodeAs(mr2); assertThat(resource2).isEqualTo(resource1);
assertThat(mr1).doesNotHaveSameHashCodeAs(mr3); assertThat(resource1).isNotEqualTo(resource3);
assertThat(resource1).hasSameHashCodeAs(resource2);
assertThat(resource1).doesNotHaveSameHashCodeAs(resource3);
} }
} }