|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.logging.log4j2; |
| 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; |
| 25 | +import org.apache.logging.log4j.core.impl.Log4jContextFactory; |
| 26 | +import org.apache.logging.log4j.jul.Log4jBridgeHandler; |
| 27 | +import org.apache.logging.log4j.jul.LogManager; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.aot.hint.RuntimeHints; |
| 31 | +import org.springframework.aot.hint.predicate.ReflectionHintsPredicates; |
| 32 | +import org.springframework.aot.hint.predicate.ResourceHintsPredicates; |
| 33 | +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link Log4J2RuntimeHints}. |
| 39 | + * |
| 40 | + * @author Piotr P. Karwasz |
| 41 | + * @author Stephane Nicoll |
| 42 | + */ |
| 43 | +class Log4J2RuntimeHintsTests { |
| 44 | + |
| 45 | + private static final ReflectionHintsPredicates reflectionHints = RuntimeHintsPredicates.reflection(); |
| 46 | + |
| 47 | + private static final ResourceHintsPredicates resourceHints = RuntimeHintsPredicates.resource(); |
| 48 | + |
| 49 | + @Test |
| 50 | + void registersHintsForTypesCheckedByLog4J2LoggingSystem() { |
| 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); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 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); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void doesNotRegisterHintsWhenLog4jCoreIsNotAvailable() { |
| 70 | + RuntimeHints runtimeHints = new RuntimeHints(); |
| 71 | + new Log4J2RuntimeHints().registerHints(runtimeHints, new HidePackagesClassLoader("org.apache.logging.log4j")); |
| 72 | + assertThat(runtimeHints.reflection().typeHints()).isEmpty(); |
| 73 | + } |
| 74 | + |
| 75 | + private RuntimeHints registerHints() { |
| 76 | + RuntimeHints hints = new RuntimeHints(); |
| 77 | + new Log4J2RuntimeHints().registerHints(hints, getClass().getClassLoader()); |
| 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 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments