|
16 | 16 |
|
17 | 17 | package org.springframework.boot.logging.log4j2; |
18 | 18 |
|
| 19 | +import java.net.URL; |
| 20 | +import java.net.URLClassLoader; |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory; |
| 24 | +import org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory; |
19 | 25 | import org.apache.logging.log4j.core.impl.Log4jContextFactory; |
20 | 26 | import org.apache.logging.log4j.jul.Log4jBridgeHandler; |
21 | 27 | import org.apache.logging.log4j.jul.LogManager; |
22 | 28 | import org.junit.jupiter.api.Test; |
23 | 29 |
|
24 | | -import org.springframework.aot.hint.ReflectionHints; |
25 | 30 | import org.springframework.aot.hint.RuntimeHints; |
26 | | -import org.springframework.aot.hint.TypeReference; |
| 31 | +import org.springframework.aot.hint.predicate.ReflectionHintsPredicates; |
| 32 | +import org.springframework.aot.hint.predicate.ResourceHintsPredicates; |
| 33 | +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; |
27 | 34 |
|
28 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
29 | 36 |
|
30 | 37 | /** |
31 | 38 | * Tests for {@link Log4J2RuntimeHints}. |
32 | 39 | * |
33 | 40 | * @author Piotr P. Karwasz |
| 41 | + * @author Stephane Nicoll |
34 | 42 | */ |
35 | 43 | class Log4J2RuntimeHintsTests { |
36 | 44 |
|
| 45 | + private static final ReflectionHintsPredicates reflectionHints = RuntimeHintsPredicates.reflection(); |
| 46 | + |
| 47 | + private static final ResourceHintsPredicates resourceHints = RuntimeHintsPredicates.resource(); |
| 48 | + |
37 | 49 | @Test |
38 | 50 | void registersHintsForTypesCheckedByLog4J2LoggingSystem() { |
39 | | - ReflectionHints reflection = registerHints(); |
40 | | - // Once Log4j Core is reachable, GraalVM will automatically |
41 | | - // add reachability metadata embedded in the Log4j Core jar and extensions. |
42 | | - assertThat(reflection.getTypeHint(Log4jContextFactory.class)).isNotNull(); |
43 | | - assertThat(reflection.getTypeHint(Log4jBridgeHandler.class)).isNotNull(); |
44 | | - assertThat(reflection.getTypeHint(LogManager.class)).isNotNull(); |
| 51 | + RuntimeHints runtimeHints = registerHints(); |
| 52 | + assertThat(reflectionHints.onType(Log4jContextFactory.class)).accepts(runtimeHints); |
| 53 | + assertThat(reflectionHints.onType(Log4jBridgeHandler.class)).accepts(runtimeHints); |
| 54 | + assertThat(reflectionHints.onType(LogManager.class)).accepts(runtimeHints); |
| 55 | + assertThat(reflectionHints.onType(PropertiesConfigurationFactory.class)).accepts(runtimeHints); |
| 56 | + assertThat(reflectionHints.onType(YamlConfigurationFactory.class)).accepts(runtimeHints); |
45 | 57 | } |
46 | 58 |
|
47 | | - /** |
48 | | - * |
49 | | - */ |
50 | 59 | @Test |
51 | | - void registersHintsForConfigurationFileParsers() { |
52 | | - ReflectionHints reflection = registerHints(); |
53 | | - // JSON |
54 | | - assertThat(reflection.getTypeHint(TypeReference.of("com.fasterxml.jackson.databind.ObjectMapper"))).isNotNull(); |
55 | | - // YAML |
56 | | - assertThat(reflection.getTypeHint(TypeReference.of("com.fasterxml.jackson.dataformat.yaml.YAMLMapper"))) |
57 | | - .isNotNull(); |
| 60 | + void registersHintsForLog4j2DefaultConfigurationFiles() { |
| 61 | + RuntimeHints runtimeHints = registerHints(); |
| 62 | + assertThat(resourceHints.forResource("org/springframework/boot/logging/log4j2/log4j2.xml")) |
| 63 | + .accepts(runtimeHints); |
| 64 | + assertThat(resourceHints.forResource("org/springframework/boot/logging/log4j2/log4j2-file.xml")) |
| 65 | + .accepts(runtimeHints); |
58 | 66 | } |
59 | 67 |
|
60 | 68 | @Test |
61 | 69 | void doesNotRegisterHintsWhenLog4jCoreIsNotAvailable() { |
62 | | - RuntimeHints hints = new RuntimeHints(); |
63 | | - new Log4J2RuntimeHints().registerHints(hints, ClassLoader.getPlatformClassLoader()); |
64 | | - assertThat(hints.reflection().typeHints()).isEmpty(); |
| 70 | + RuntimeHints runtimeHints = new RuntimeHints(); |
| 71 | + new Log4J2RuntimeHints().registerHints(runtimeHints, new HidePackagesClassLoader("org.apache.logging.log4j")); |
| 72 | + assertThat(runtimeHints.reflection().typeHints()).isEmpty(); |
65 | 73 | } |
66 | 74 |
|
67 | | - private ReflectionHints registerHints() { |
| 75 | + private RuntimeHints registerHints() { |
68 | 76 | RuntimeHints hints = new RuntimeHints(); |
69 | 77 | new Log4J2RuntimeHints().registerHints(hints, getClass().getClassLoader()); |
70 | | - return hints.reflection(); |
| 78 | + return hints; |
| 79 | + } |
| 80 | + |
| 81 | + static final class HidePackagesClassLoader extends URLClassLoader { |
| 82 | + |
| 83 | + private final String[] hiddenPackages; |
| 84 | + |
| 85 | + HidePackagesClassLoader(String... hiddenPackages) { |
| 86 | + super(new URL[0], HidePackagesClassLoader.class.getClassLoader()); |
| 87 | + this.hiddenPackages = hiddenPackages; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 92 | + if (Arrays.stream(this.hiddenPackages).anyMatch(name::startsWith)) { |
| 93 | + throw new ClassNotFoundException(); |
| 94 | + } |
| 95 | + return super.loadClass(name, resolve); |
| 96 | + } |
| 97 | + |
71 | 98 | } |
72 | 99 |
|
73 | 100 | } |
0 commit comments