|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, PostgreSQL Global Development Group |
| 3 | + * See the LICENSE file in the project root for more information. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.postgresql.test.impl; |
| 7 | + |
| 8 | +import org.postgresql.core.ServerVersion; |
| 9 | +import org.postgresql.core.Version; |
| 10 | +import org.postgresql.test.TestUtil; |
| 11 | +import org.postgresql.test.annotations.DisabledIfServerVersionGreater; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 14 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 15 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 16 | +import org.junit.platform.commons.support.AnnotationSupport; |
| 17 | + |
| 18 | +import java.lang.reflect.AnnotatedElement; |
| 19 | +import java.sql.Connection; |
| 20 | + |
| 21 | +public class ServerVersionGreaterCondition implements ExecutionCondition { |
| 22 | + private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled( |
| 23 | + "@DisabledIfServerVersionGreater is not present"); |
| 24 | + |
| 25 | + @Override |
| 26 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 27 | + return context.getElement() |
| 28 | + .flatMap(element -> |
| 29 | + AnnotationSupport.findAnnotation(element, DisabledIfServerVersionGreater.class) |
| 30 | + .map(annotation -> ServerVersionGreaterCondition.toResult(element, annotation)) |
| 31 | + ).orElse(ENABLED); |
| 32 | + } |
| 33 | + |
| 34 | + private static ConditionEvaluationResult toResult(AnnotatedElement element, |
| 35 | + DisabledIfServerVersionGreater annotation) { |
| 36 | + Version maxVersion = ServerVersion.from(annotation.value()); |
| 37 | + if (maxVersion.getVersionNum() <= 0) { |
| 38 | + throw new IllegalArgumentException( |
| 39 | + "Server version " + annotation.value() + " not valid for " |
| 40 | + + element); |
| 41 | + } |
| 42 | + |
| 43 | + try (Connection con = TestUtil.openDB()) { |
| 44 | + String dbVersionNumber = con.getMetaData().getDatabaseProductVersion(); |
| 45 | + Version actualVersion = ServerVersion.from(dbVersionNumber); |
| 46 | + if (actualVersion.getVersionNum() >= maxVersion.getVersionNum()) { |
| 47 | + return ConditionEvaluationResult.disabled( |
| 48 | + "Test requires version <= " + maxVersion |
| 49 | + + ", but the server version is " + actualVersion); |
| 50 | + } |
| 51 | + return ConditionEvaluationResult.enabled( |
| 52 | + "Test requires version <= " + maxVersion |
| 53 | + + ", and the server version is " + actualVersion); |
| 54 | + } catch (Exception e) { |
| 55 | + throw new IllegalStateException("Not available open connection", e); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments