Skip to content

Commit 9c04966

Browse files
committed
Introduce conditional testing for Hibernate.
Certain test cases should be disabled based on the version of Hibernate. We should also run the entire test suite against both Hibernate 6.1 and 6.2, to ensure maxiumum support. Resolves #2921. Related: #2927.
1 parent 13bb22c commit 9c04966

File tree

8 files changed

+215
-12
lines changed

8 files changed

+215
-12
lines changed

Jenkinsfile

+17
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ pipeline {
5353
}
5454

5555
parallel {
56+
stage("test: baseline (hibernate 6.2)") {
57+
agent {
58+
label 'data'
59+
}
60+
options { timeout(time: 30, unit: 'MINUTES')}
61+
environment {
62+
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
63+
}
64+
steps {
65+
script {
66+
docker.image(p['docker.java.next.image']).inside(p['docker.java.inside.docker']) {
67+
sh 'PROFILE=all-dbs,hibernate-62 ci/test.sh'
68+
sh "ci/clean.sh"
69+
}
70+
}
71+
}
72+
}
5673
stage("test: baseline (next)") {
5774
agent {
5875
label 'data'

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454

5555
<profiles>
56+
<profile>
57+
<id>hibernate-62</id>
58+
<properties>
59+
<hibernate>6.2.1.Final</hibernate>
60+
</properties>
61+
</profile>
5662
<profile>
5763
<id>all-dbs</id>
5864
<build>

spring-data-jpa/pom.xml

+29-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
<java-module-name>spring.data.jpa</java-module-name>
2424
</properties>
2525

26+
<profiles>
27+
<profile>
28+
<id>hibernate-62</id>
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.eclipse.persistence</groupId>
32+
<artifactId>org.eclipse.persistence.jpa</artifactId>
33+
<version>${eclipselink}</version>
34+
<optional>true</optional>
35+
<exclusions> <!-- Defer to Hibernate 6.2's choice of JPA -->
36+
<exclusion>
37+
<groupId>jakarta.persistence</groupId>
38+
<artifactId>jakarta.persistence-api</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
43+
</dependencies>
44+
</profile>
45+
</profiles>
46+
2647
<dependencies>
2748

2849
<dependency>
@@ -137,6 +158,13 @@
137158

138159
<!-- Persistence providers -->
139160

161+
<dependency>
162+
<groupId>org.eclipse.persistence</groupId>
163+
<artifactId>org.eclipse.persistence.jpa</artifactId>
164+
<version>${eclipselink}</version>
165+
<optional>true</optional>
166+
</dependency>
167+
140168
<dependency>
141169
<groupId>${hibernate.groupId}.orm</groupId>
142170
<artifactId>hibernate-core</artifactId>
@@ -149,7 +177,7 @@
149177
</exclusion>
150178
</exclusions>
151179
</dependency>
152-
180+
153181
<dependency>
154182
<groupId>${hibernate.groupId}.orm</groupId>
155183
<artifactId>hibernate-jpamodelgen</artifactId>
@@ -170,13 +198,6 @@
170198
<version>${jakarta-annotation-api}</version>
171199
</dependency>
172200

173-
<dependency>
174-
<groupId>org.eclipse.persistence</groupId>
175-
<artifactId>org.eclipse.persistence.jpa</artifactId>
176-
<version>${eclipselink}</version>
177-
<optional>true</optional>
178-
</dependency>
179-
180201
<!-- QueryDsl -->
181202
<dependency>
182203
<groupId>com.querydsl</groupId>

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/procedures/PostgresStoredProcedureIntegrationTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.data.jpa.repository.procedures;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.*;
2020

2121
import jakarta.persistence.Entity;
2222
import jakarta.persistence.EntityManagerFactory;
@@ -35,7 +35,7 @@
3535

3636
import javax.sql.DataSource;
3737

38-
import org.hibernate.dialect.PostgreSQL91Dialect;
38+
import org.hibernate.dialect.PostgreSQLDialect;
3939
import org.junit.jupiter.api.Test;
4040
import org.junit.jupiter.api.extension.ExtendWith;
4141
import org.postgresql.ds.PGSimpleDataSource;
@@ -47,6 +47,7 @@
4747
import org.springframework.data.jpa.repository.JpaRepository;
4848
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4949
import org.springframework.data.jpa.repository.query.Procedure;
50+
import org.springframework.data.jpa.util.DisabledOnHibernate62;
5051
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
5152
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
5253
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
@@ -114,6 +115,7 @@ void testNamedOutputParameter() {
114115
new Employee(4, "Gabriel"));
115116
}
116117

118+
@DisabledOnHibernate62
117119
@Test // 2256
118120
void testSingleEntityFromResultSet() {
119121

@@ -201,7 +203,7 @@ static class Config {
201203
@Bean(initMethod = "start", destroyMethod = "stop")
202204
public PostgreSQLContainer<?> container() {
203205

204-
return new PostgreSQLContainer<>("postgres:10.21") //
206+
return new PostgreSQLContainer<>("postgres:9.6.12") //
205207
.withUsername("postgres");
206208
}
207209

@@ -226,7 +228,7 @@ public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSour
226228

227229
Properties properties = new Properties();
228230
properties.setProperty("hibernate.hbm2ddl.auto", "create");
229-
properties.setProperty("hibernate.dialect", PostgreSQL91Dialect.class.getCanonicalName());
231+
properties.setProperty("hibernate.dialect", PostgreSQLDialect.class.getCanonicalName());
230232
factoryBean.setJpaProperties(properties);
231233

232234
return factoryBean;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/HibernateJpaMetamodelEntityInformationIntegrationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18+
import org.junit.jupiter.api.Test;
1819
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.springframework.data.jpa.util.DisabledOnHibernate61;
1921
import org.springframework.test.context.ContextConfiguration;
2022
import org.springframework.test.context.junit.jupiter.SpringExtension;
2123

@@ -33,4 +35,25 @@ public class HibernateJpaMetamodelEntityInformationIntegrationTests
3335
String getMetadadataPersistenceUnitName() {
3436
return "metadata-id-handling";
3537
}
38+
39+
@DisabledOnHibernate61
40+
@Test
41+
@Override
42+
void correctlyDeterminesIdValueForNestedIdClassesWithNonPrimitiveNonManagedType() {
43+
super.correctlyDeterminesIdValueForNestedIdClassesWithNonPrimitiveNonManagedType();
44+
}
45+
46+
@DisabledOnHibernate61
47+
@Test
48+
@Override
49+
void prefersPrivateGetterOverFieldAccess() {
50+
super.prefersPrivateGetterOverFieldAccess();
51+
}
52+
53+
@DisabledOnHibernate61
54+
@Test
55+
@Override
56+
void findsIdClassOnMappedSuperclass() {
57+
super.findsIdClassOnMappedSuperclass();
58+
}
3659
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2015-2023 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.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
25+
/**
26+
* Annotation to flag JUnit 5 test cases to ONLY activate when Hibernate 6.2 is on the classpath.
27+
*
28+
* @author Greg Turnquist
29+
* @since 3.1
30+
*/
31+
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@ExtendWith(HibernateSupport.DisabledWhenHibernate61OnClasspath.class)
34+
public @interface DisabledOnHibernate61 {
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2015-2023 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.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
25+
/**
26+
* Annotation to flag JUnit 5 test cases to ONLY activate when Hibernate 6.1 is on the classpath.
27+
*
28+
* @author Greg Turnquist
29+
* @since 3.1
30+
*/
31+
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@ExtendWith(HibernateSupport.DisabledWhenHibernate62OnClasspath.class)
34+
public @interface DisabledOnHibernate62 {
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2015-2023 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 org.junit.jupiter.api.extension.ConditionEvaluationResult;
19+
import org.junit.jupiter.api.extension.ExecutionCondition;
20+
import org.junit.jupiter.api.extension.ExtensionContext;
21+
import org.springframework.util.ClassUtils;
22+
23+
/**
24+
* JUnit 5 utilities to support conditional test cases based upon Hibernate classpath settings.
25+
*
26+
* @author Greg Turnquist
27+
* @since 3.1
28+
*/
29+
abstract class HibernateSupport {
30+
31+
/**
32+
* {@literal org.hibernate.dialect.PostgreSQL91Dialect} is deprecated in Hibernate 6.1 and fully removed in Hibernate
33+
* 6.2, making it a perfect detector between the two.
34+
*/
35+
private static final boolean HIBERNATE_61_ON_CLASSPATH = ClassUtils
36+
.isPresent("org.hibernate.dialect.PostgreSQL91Dialect", HibernateSupport.class.getClassLoader());
37+
38+
private static final boolean HIBERNATE_62_ON_CLASSPATH = !HIBERNATE_61_ON_CLASSPATH;
39+
40+
static class DisabledWhenHibernate61OnClasspath implements ExecutionCondition {
41+
42+
@Override
43+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
44+
45+
return HibernateSupport.HIBERNATE_61_ON_CLASSPATH
46+
? ConditionEvaluationResult.disabled("Disabled because Hibernate 6.1 is on the classpath")
47+
: ConditionEvaluationResult.enabled("NOT disabled because Hibernate 6.2 is on the classpath");
48+
}
49+
}
50+
51+
static class DisabledWhenHibernate62OnClasspath implements ExecutionCondition {
52+
53+
@Override
54+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
55+
56+
return HibernateSupport.HIBERNATE_62_ON_CLASSPATH
57+
? ConditionEvaluationResult.disabled("Disabled because Hibernate 6.2 is on the classpath")
58+
: ConditionEvaluationResult.enabled("NOT disabled because Hibernate 6.1 is on the classpath");
59+
}
60+
61+
}
62+
}

0 commit comments

Comments
 (0)