|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.test.context.bean.override.mockito; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.AnnotatedElement; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.function.Predicate; |
| 24 | + |
| 25 | +import org.springframework.util.ReflectionUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility class that detects {@code org.mockito} annotations as well as the |
| 29 | + * annotations in this package (like {@link MockitoBeanSettings @MockitoBeanSettings}). |
| 30 | + * |
| 31 | + * @author Simon Baslé |
| 32 | + * @author Sam Brannen |
| 33 | + */ |
| 34 | +abstract class MockitoAnnotationDetector { |
| 35 | + |
| 36 | + private static final String MOCKITO_BEAN_PACKAGE = MockitoBeanSettings.class.getPackageName(); |
| 37 | + |
| 38 | + private static final String ORG_MOCKITO_PACKAGE = "org.mockito"; |
| 39 | + |
| 40 | + private static final Predicate<Annotation> isMockitoAnnotation = annotation -> { |
| 41 | + String packageName = annotation.annotationType().getPackageName(); |
| 42 | + return (packageName.startsWith(MOCKITO_BEAN_PACKAGE) || |
| 43 | + packageName.startsWith(ORG_MOCKITO_PACKAGE)); |
| 44 | + }; |
| 45 | + |
| 46 | + static boolean hasMockitoAnnotations(Class<?> testClass) { |
| 47 | + if (isAnnotated(testClass)) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + // TODO Ideally we should short-circuit the search once we've found a Mockito annotation, |
| 51 | + // since there's no need to continue searching additional fields or further up the class |
| 52 | + // hierarchy; however, that is not possible with ReflectionUtils#doWithFields. Plus, the |
| 53 | + // previous invocation of isAnnotated(testClass) only finds annotations declared directly |
| 54 | + // on the test class. So, we'll likely need a completely different approach that combines |
| 55 | + // the "test class/interface is annotated?" and "field is annotated?" checks in a single |
| 56 | + // search algorithm. |
| 57 | + AtomicBoolean found = new AtomicBoolean(); |
| 58 | + ReflectionUtils.doWithFields(testClass, field -> found.set(true), MockitoAnnotationDetector::isAnnotated); |
| 59 | + return found.get(); |
| 60 | + } |
| 61 | + |
| 62 | + private static boolean isAnnotated(AnnotatedElement annotatedElement) { |
| 63 | + return Arrays.stream(annotatedElement.getAnnotations()).anyMatch(isMockitoAnnotation); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments