|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.jpa.util; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 24 | + |
| 25 | +/** |
| 26 | + * {@link ExecutionCondition} for {@link DisabledOnHibernate @DisabledOnHibernate}. |
| 27 | + * |
| 28 | + * @see DisabledOnHibernate |
| 29 | + */ |
| 30 | +class DisabledOnHibernateCondition extends BooleanExecutionCondition<DisabledOnHibernate> { |
| 31 | + |
| 32 | + static final String ENABLED_ON_CURRENT_HIBERNATE = // |
| 33 | + "Enabled on Hibernate version: " + org.hibernate.Version.getVersionString(); |
| 34 | + |
| 35 | + static final String DISABLED_ON_CURRENT_HIBERNATE = // |
| 36 | + "Disabled on Hibernate version: " + org.hibernate.Version.getVersionString(); |
| 37 | + |
| 38 | + DisabledOnHibernateCondition() { |
| 39 | + super(DisabledOnHibernate.class, ENABLED_ON_CURRENT_HIBERNATE, DISABLED_ON_CURRENT_HIBERNATE, |
| 40 | + DisabledOnHibernate::disabledReason); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + boolean isEnabled(DisabledOnHibernate annotation) { |
| 45 | + |
| 46 | + VersionMatcher disabled = VersionMatcher.parse(annotation.value()); |
| 47 | + VersionMatcher hibernate = VersionMatcher.parse(org.hibernate.Version.getVersionString()); |
| 48 | + |
| 49 | + return !disabled.matches(hibernate); |
| 50 | + } |
| 51 | + |
| 52 | + static class VersionMatcher { |
| 53 | + |
| 54 | + private static final Pattern PATTERN = Pattern.compile("(\\d+)+"); |
| 55 | + private final int[] components; |
| 56 | + |
| 57 | + private VersionMatcher(int[] components) { |
| 58 | + this.components = components; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Parse the given version string into a {@link VersionMatcher}. |
| 63 | + * |
| 64 | + * @param version |
| 65 | + * @return |
| 66 | + */ |
| 67 | + public static VersionMatcher parse(String version) { |
| 68 | + |
| 69 | + Matcher matcher = PATTERN.matcher(version); |
| 70 | + List<Integer> ints = new ArrayList<>(); |
| 71 | + while (matcher.find()) { |
| 72 | + ints.add(Integer.parseInt(matcher.group())); |
| 73 | + } |
| 74 | + |
| 75 | + return new VersionMatcher(ints.stream().mapToInt(value -> value).toArray()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Match the given version against another VersionMatcher. This matcher's version spec controls the expected length. |
| 80 | + * If the other version is shorter, then the match returns {@code false}. |
| 81 | + * |
| 82 | + * @param version |
| 83 | + * @return |
| 84 | + */ |
| 85 | + public boolean matches(VersionMatcher version) { |
| 86 | + |
| 87 | + for (int i = 0; i < components.length; i++) { |
| 88 | + if (version.components.length <= i) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + if (components[i] != version.components[i]) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments