|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.slf4j; |
| 18 | + |
| 19 | +import ch.qos.logback.classic.Logger; |
| 20 | +import ch.qos.logback.classic.LoggerContext; |
| 21 | +import ch.qos.logback.classic.joran.JoranConfigurator; |
| 22 | +import ch.qos.logback.core.joran.spi.JoranException; |
| 23 | +import java.net.URL; |
| 24 | +import org.apache.logging.log4j.test.junit.ExtensionContextAnchor; |
| 25 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 26 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 27 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 28 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContextException; |
| 30 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 31 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 32 | +import org.junit.jupiter.api.extension.support.TypeBasedParameterResolver; |
| 33 | +import org.junit.platform.commons.support.AnnotationSupport; |
| 34 | +import org.junit.platform.commons.support.HierarchyTraversalMode; |
| 35 | +import org.junit.platform.commons.support.ModifierSupport; |
| 36 | +import org.junit.platform.commons.support.ReflectionSupport; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +class LoggerContextResolver extends TypeBasedParameterResolver<LoggerContext> |
| 40 | + implements BeforeAllCallback, BeforeEachCallback { |
| 41 | + |
| 42 | + private static final Object KEY = LoggerContextHolder.class; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void beforeEach(ExtensionContext extensionContext) throws Exception { |
| 46 | + final Class<?> testClass = extensionContext.getRequiredTestClass(); |
| 47 | + if (AnnotationSupport.isAnnotated(testClass, LoggerContextSource.class)) { |
| 48 | + final LoggerContextHolder holder = |
| 49 | + ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); |
| 50 | + if (holder == null) { |
| 51 | + throw new IllegalStateException( |
| 52 | + "Specified @LoggerContextSource but no LoggerContext found for test class " |
| 53 | + + testClass.getCanonicalName()); |
| 54 | + } |
| 55 | + } |
| 56 | + AnnotationSupport.findAnnotation(extensionContext.getRequiredTestMethod(), LoggerContextSource.class) |
| 57 | + .ifPresent(source -> { |
| 58 | + final LoggerContextHolder holder = new LoggerContextHolder(source, extensionContext); |
| 59 | + ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext); |
| 60 | + }); |
| 61 | + final LoggerContextHolder holder = |
| 62 | + ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); |
| 63 | + if (holder != null) { |
| 64 | + ReflectionSupport.findFields( |
| 65 | + extensionContext.getRequiredTestClass(), |
| 66 | + f -> ModifierSupport.isNotStatic(f) && f.getType().equals(LoggerContext.class), |
| 67 | + HierarchyTraversalMode.TOP_DOWN) |
| 68 | + .forEach(f -> { |
| 69 | + try { |
| 70 | + f.setAccessible(true); |
| 71 | + f.set(extensionContext.getRequiredTestInstance(), holder.getLoggerContext()); |
| 72 | + } catch (ReflectiveOperationException e) { |
| 73 | + throw new ExtensionContextException("Failed to inject field " + f, e); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void beforeAll(ExtensionContext extensionContext) throws Exception { |
| 81 | + final Class<?> testClass = extensionContext.getRequiredTestClass(); |
| 82 | + AnnotationSupport.findAnnotation(testClass, LoggerContextSource.class).ifPresent(testSource -> { |
| 83 | + final LoggerContextHolder holder = new LoggerContextHolder(testSource, extensionContext); |
| 84 | + ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext); |
| 85 | + }); |
| 86 | + final LoggerContextHolder holder = |
| 87 | + ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); |
| 88 | + if (holder != null) { |
| 89 | + ReflectionSupport.findFields( |
| 90 | + extensionContext.getRequiredTestClass(), |
| 91 | + f -> ModifierSupport.isStatic(f) && f.getType().equals(LoggerContext.class), |
| 92 | + HierarchyTraversalMode.TOP_DOWN) |
| 93 | + .forEach(f -> { |
| 94 | + try { |
| 95 | + f.setAccessible(true); |
| 96 | + f.set(null, holder.getLoggerContext()); |
| 97 | + } catch (ReflectiveOperationException e) { |
| 98 | + throw new ExtensionContextException("Failed to inject field " + f, e); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public LoggerContext resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 106 | + throws ParameterResolutionException { |
| 107 | + return ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext) |
| 108 | + .getLoggerContext(); |
| 109 | + } |
| 110 | + |
| 111 | + static final class LoggerContextHolder implements Store.CloseableResource { |
| 112 | + |
| 113 | + private final LoggerContext context; |
| 114 | + private final Logger logger; |
| 115 | + |
| 116 | + private LoggerContextHolder(final LoggerContextSource source, final ExtensionContext extensionContext) { |
| 117 | + this.context = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 118 | + Class<?> clazz = extensionContext.getRequiredTestClass(); |
| 119 | + this.logger = context.getLogger(clazz); |
| 120 | + |
| 121 | + final JoranConfigurator configurator = new JoranConfigurator(); |
| 122 | + final URL configLocation = getConfigLocation(source, extensionContext); |
| 123 | + configurator.setContext(context); |
| 124 | + try { |
| 125 | + configurator.doConfigure(configLocation); |
| 126 | + } catch (final JoranException e) { |
| 127 | + throw new ExtensionContextException("Failed to initialize Logback logger context for " + clazz, e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static URL getConfigLocation( |
| 132 | + final LoggerContextSource source, final ExtensionContext extensionContext) { |
| 133 | + final String value = source.value(); |
| 134 | + Class<?> clazz = extensionContext.getRequiredTestClass(); |
| 135 | + URL url = null; |
| 136 | + if (value.isEmpty()) { |
| 137 | + while (clazz != null) { |
| 138 | + url = clazz.getResource(clazz.getSimpleName() + ".xml"); |
| 139 | + if (url != null) { |
| 140 | + break; |
| 141 | + } |
| 142 | + clazz = clazz.getSuperclass(); |
| 143 | + } |
| 144 | + } else { |
| 145 | + url = clazz.getClassLoader().getResource(value); |
| 146 | + } |
| 147 | + if (url != null) { |
| 148 | + return url; |
| 149 | + } |
| 150 | + throw new ExtensionContextException("Failed to find a default configuration for " + clazz); |
| 151 | + } |
| 152 | + |
| 153 | + public LoggerContext getLoggerContext() { |
| 154 | + return context; |
| 155 | + } |
| 156 | + |
| 157 | + public Logger getLogger() { |
| 158 | + return logger; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void close() { |
| 163 | + context.stop(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments