Move registerResourceIfNecessary() to ResourceHints

See gh-29083
This commit is contained in:
Sam Brannen
2022-09-06 15:22:18 +02:00
parent 5c2859ffa7
commit 8fbd2141b7
6 changed files with 57 additions and 59 deletions

View File

@@ -21,9 +21,12 @@ import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.ResourceHintsTests.Nested.Inner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DescriptiveResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -33,6 +36,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
* Tests for {@link ResourceHints}.
*
* @author Stephane Nicoll
* @author Sam Brannen
*/
class ResourceHintsTests {
@@ -110,6 +114,39 @@ class ResourceHintsTests {
verifyNoInteractions(hintBuilder);
}
@Test
void registerResourceIfNecessaryWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus");
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithNonexistentClassPathResource() {
ClassPathResource resource = new ClassPathResource("bogus", getClass());
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithExistingClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource(path);
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}
@Disabled("Disabled since ClassPathResource.getPath() does not honor its contract for relative resources")
@Test
void registerResourceIfNecessaryWithExistingRelativeClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class);
this.resourceHints.registerResourceIfNecessary(resource);
// This unfortunately fails since ClassPathResource.getPath() returns
// "support" instead of "org/springframework/aot/hint/support".
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}
@Test
void registerResourceBundle() {
this.resourceHints.registerResourceBundle("com.example.message");

View File

@@ -20,7 +20,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.function.Consumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.JdkProxyHint;
@@ -29,11 +28,8 @@ import org.springframework.aot.hint.TypeReference;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DescriptiveResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
/**
* Tests for {@link RuntimeHintsUtils}.
@@ -45,39 +41,6 @@ class RuntimeHintsUtilsTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void registerResourceIfNecessaryWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus");
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(this.hints.resources().resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithNonexistentClassPathResource() {
ClassPathResource resource = new ClassPathResource("bogus", getClass());
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(this.hints.resources().resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithExistingClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource(path);
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(resource().forResource(path)).accepts(this.hints);
}
@Disabled("Disabled since ClassPathResource.getPath() does not honor its contract for relative resources")
@Test
void registerResourceIfNecessaryWithExistingRelativeClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class);
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
// This unfortunately fails since ClassPathResource.getPath() returns
// "support" instead of "org/springframework/aot/hint/support".
assertThat(resource().forResource(path)).accepts(this.hints);
}
@Test
@SuppressWarnings("deprecation")
void registerSynthesizedAnnotation() {