|
| 1 | +/* |
| 2 | + * Copyright 2019 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.kafka.test.condition; |
| 18 | + |
| 19 | +import java.lang.reflect.AnnotatedElement; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | + |
| 26 | +import org.apache.commons.logging.LogFactory; |
| 27 | +import org.apache.logging.log4j.Level; |
| 28 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 29 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 30 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 31 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 32 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 33 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 34 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 35 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 36 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 37 | + |
| 38 | +import org.springframework.core.annotation.MergedAnnotation; |
| 39 | +import org.springframework.core.annotation.MergedAnnotations; |
| 40 | +import org.springframework.core.log.LogAccessor; |
| 41 | +import org.springframework.kafka.test.utils.JUnitUtils; |
| 42 | +import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer; |
| 43 | + |
| 44 | +/** |
| 45 | + * JUnit condition that adjusts and reverts log levels before/after each test. |
| 46 | + * |
| 47 | + * @author Gary Russell |
| 48 | + * @since 2.2 |
| 49 | + * |
| 50 | + */ |
| 51 | +public class LogLevelsCondition |
| 52 | + implements ExecutionCondition, BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback { |
| 53 | + |
| 54 | + private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(LogLevelsCondition.class)); |
| 55 | + |
| 56 | + private static final String STORE_ANNOTATION_KEY = "logLevelsAnnotation"; |
| 57 | + |
| 58 | + private static final String STORE_CONTAINER_KEY = "logLevelsContainer"; |
| 59 | + |
| 60 | + private static final ConditionEvaluationResult ENABLED = |
| 61 | + ConditionEvaluationResult.enabled("@LogLevels always enabled"); |
| 62 | + |
| 63 | + private final Map<String, Boolean> loggedMethods = new ConcurrentHashMap<>(); |
| 64 | + |
| 65 | + @Override |
| 66 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 67 | + Optional<AnnotatedElement> element = context.getElement(); |
| 68 | + MergedAnnotations annotations = MergedAnnotations.from(element.get(), |
| 69 | + MergedAnnotations.SearchStrategy.TYPE_HIERARCHY); |
| 70 | + MergedAnnotation<LogLevels> mergedAnnotation = annotations.get(LogLevels.class); |
| 71 | + if (mergedAnnotation.isPresent()) { |
| 72 | + LogLevels loglevels = mergedAnnotation.synthesize(); |
| 73 | + Store store = context.getStore(Namespace.create(getClass(), context)); |
| 74 | + store.put(STORE_ANNOTATION_KEY, loglevels); |
| 75 | + } |
| 76 | + return ENABLED; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void beforeAll(ExtensionContext context) { |
| 81 | + Store store = context.getStore(Namespace.create(getClass(), context)); |
| 82 | + LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class); |
| 83 | + if (logLevels != null) { |
| 84 | + store.put(STORE_CONTAINER_KEY, JUnitUtils.adjustLogLevels(context.getDisplayName(), |
| 85 | + Arrays.asList((logLevels.classes())), |
| 86 | + Arrays.asList(logLevels.categories()), |
| 87 | + Level.toLevel(logLevels.level()))); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void beforeEach(ExtensionContext context) { |
| 93 | + Store store = context.getStore(Namespace.create(getClass(), context)); |
| 94 | + LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class); |
| 95 | + if (logLevels != null) { // Method level annotation |
| 96 | + if (store.get(STORE_CONTAINER_KEY) == null) { |
| 97 | + store.put(STORE_CONTAINER_KEY, JUnitUtils.adjustLogLevels(context.getDisplayName(), |
| 98 | + Arrays.asList((logLevels.classes())), |
| 99 | + Arrays.asList(logLevels.categories()), |
| 100 | + Level.toLevel(logLevels.level()))); |
| 101 | + } |
| 102 | + } |
| 103 | + else { |
| 104 | + Optional<Method> testMethod = context.getTestMethod(); |
| 105 | + if (testMethod.isPresent() |
| 106 | + && this.loggedMethods.putIfAbsent(testMethod.get().getName(), Boolean.TRUE) == null) { |
| 107 | + LOGGER.info(() -> "+++++++++++++++++++++++++++++ Begin " + testMethod.get().getName()); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void afterEach(ExtensionContext context) { |
| 114 | + Store store = context.getStore(Namespace.create(getClass(), context)); |
| 115 | + LevelsContainer container = store.get(STORE_CONTAINER_KEY, LevelsContainer.class); |
| 116 | + if (container != null) { |
| 117 | + LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class); |
| 118 | + if (logLevels != null) { |
| 119 | + JUnitUtils.revertLevels(context.getDisplayName(), container); |
| 120 | + store.remove(STORE_CONTAINER_KEY); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void afterAll(ExtensionContext context) { |
| 127 | + Store store = context.getStore(Namespace.create(getClass(), context)); |
| 128 | + LogLevels logLevels = store.remove(STORE_ANNOTATION_KEY, LogLevels.class); |
| 129 | + if (logLevels != null) { |
| 130 | + LevelsContainer container = store.get(STORE_CONTAINER_KEY, LevelsContainer.class); |
| 131 | + JUnitUtils.revertLevels(context.getDisplayName(), container); |
| 132 | + store.remove(STORE_CONTAINER_KEY); |
| 133 | + } |
| 134 | + this.loggedMethods.clear(); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments