|
| 1 | +/* |
| 2 | + * Copyright 2015-2023 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junit.jupiter.api.condition; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.mockito.Mockito.doReturn; |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 23 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 24 | +import org.junit.platform.commons.test.TestClassLoader; |
| 25 | +import org.junit.platform.commons.util.ReflectionUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests for {@link DisabledIfCondition} using custom {@link ClassLoader} arrangements. |
| 29 | + * |
| 30 | + * @since 5.10 |
| 31 | + */ |
| 32 | +public class DisabledIfConditionClassLoaderTests { |
| 33 | + |
| 34 | + @Test |
| 35 | + // No need to introduce a "disabled" version of this test, since it would simply be the |
| 36 | + // logical inverse of this method and would therefore not provide any further benefit. |
| 37 | + void enabledWithStaticMethodInTypeFromDifferentClassLoader() throws Exception { |
| 38 | + try (var testClassLoader = TestClassLoader.forClasses(getClass(), StaticConditionMethods.class)) { |
| 39 | + var testClass = testClassLoader.loadClass(getClass().getName()); |
| 40 | + assertThat(testClass.getClassLoader()).isSameAs(testClassLoader); |
| 41 | + |
| 42 | + ExtensionContext context = mock(); |
| 43 | + Method annotatedMethod = ReflectionUtils.findMethod(getClass(), "enabledMethod").get(); |
| 44 | + when(context.getElement()).thenReturn(Optional.of(annotatedMethod)); |
| 45 | + doReturn(testClass).when(context).getRequiredTestClass(); |
| 46 | + |
| 47 | + DisabledIfCondition condition = new DisabledIfCondition(); |
| 48 | + ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); |
| 49 | + assertThat(result).isNotNull(); |
| 50 | + assertThat(result.isDisabled()).isFalse(); |
| 51 | + |
| 52 | + Method conditionMethod = condition.getConditionMethod( |
| 53 | + "org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse", context); |
| 54 | + assertThat(conditionMethod).isNotNull(); |
| 55 | + Class<?> declaringClass = conditionMethod.getDeclaringClass(); |
| 56 | + assertThat(declaringClass.getClassLoader()).isSameAs(testClassLoader); |
| 57 | + assertThat(declaringClass.getName()).isEqualTo(StaticConditionMethods.class.getName()); |
| 58 | + assertThat(declaringClass).isNotEqualTo(StaticConditionMethods.class); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse") |
| 63 | + private void enabledMethod() { |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments