Skip to content

Commit f3690c1

Browse files
authored
Fix tests for hibernate 7 (#13890)
1 parent 2addc6e commit f3690c1

File tree

22 files changed

+1314
-12
lines changed

22 files changed

+1314
-12
lines changed

instrumentation/hibernate/hibernate-6.0/javaagent/build.gradle.kts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,67 @@ dependencies {
2828
testImplementation("com.sun.xml.bind:jaxb-impl:2.2.11")
2929
testImplementation("javax.activation:activation:1.1.1")
3030
testImplementation("org.hsqldb:hsqldb:2.0.0")
31-
testImplementation("org.springframework.data:spring-data-jpa:3.0.0")
31+
testLibrary("org.springframework.data:spring-data-jpa:3.0.0")
3232
}
3333

3434
otelJava {
3535
minJavaVersionSupported.set(JavaVersion.VERSION_11)
3636
}
3737

38+
val latestDepTest = findProperty("testLatestDeps") as Boolean
39+
40+
testing {
41+
suites {
42+
val hibernate6Test by registering(JvmTestSuite::class) {
43+
dependencies {
44+
implementation("com.h2database:h2:1.4.197")
45+
implementation("org.hsqldb:hsqldb:2.0.0")
46+
if (latestDepTest) {
47+
implementation("org.hibernate:hibernate-core:6.+")
48+
} else {
49+
implementation("org.hibernate:hibernate-core:6.0.0.Final")
50+
}
51+
}
52+
}
53+
54+
val hibernate7Test by registering(JvmTestSuite::class) {
55+
dependencies {
56+
implementation("com.h2database:h2:1.4.197")
57+
implementation("org.hsqldb:hsqldb:2.0.0")
58+
if (latestDepTest) {
59+
implementation("org.hibernate:hibernate-core:7.+")
60+
} else {
61+
implementation("org.hibernate:hibernate-core:7.0.0.Final")
62+
}
63+
}
64+
}
65+
}
66+
}
67+
3868
tasks {
3969
withType<Test>().configureEach {
4070
// TODO run tests both with and without experimental span attributes
4171
jvmArgs("-Dotel.instrumentation.hibernate.experimental-span-attributes=true")
4272
}
4373

74+
named("compileHibernate7TestJava", JavaCompile::class).configure {
75+
options.release.set(17)
76+
}
77+
val testJavaVersion =
78+
gradle.startParameter.projectProperties.get("testJavaVersion")?.let(JavaVersion::toVersion)
79+
?: JavaVersion.current()
80+
if (!testJavaVersion.isCompatibleWith(JavaVersion.VERSION_17)) {
81+
named("hibernate7Test", Test::class).configure {
82+
enabled = false
83+
}
84+
}
85+
4486
val testStableSemconv by registering(Test::class) {
4587
jvmArgs("-Dotel.semconv-stability.opt-in=database")
4688
}
4789

4890
check {
4991
dependsOn(testStableSemconv)
92+
dependsOn(testing.suites)
5093
}
5194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.hibernate.v6_0;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
9+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import org.hibernate.Session;
13+
import org.hibernate.SessionFactory;
14+
import org.hibernate.cfg.Configuration;
15+
import org.junit.jupiter.api.AfterAll;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.extension.RegisterExtension;
18+
19+
abstract class AbstractHibernateTest {
20+
protected static SessionFactory sessionFactory;
21+
protected static List<Value> prepopulated;
22+
23+
@RegisterExtension
24+
protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
25+
26+
@BeforeAll
27+
protected static void setup() {
28+
sessionFactory = new Configuration().configure().buildSessionFactory();
29+
// Pre-populate the DB, so delete/update can be tested.
30+
Session writer = sessionFactory.openSession();
31+
writer.beginTransaction();
32+
prepopulated = new ArrayList<>();
33+
for (int i = 0; i < 5; i++) {
34+
prepopulated.add(new Value("Hello :) " + i));
35+
writer.persist(prepopulated.get(i));
36+
}
37+
writer.getTransaction().commit();
38+
writer.close();
39+
}
40+
41+
@AfterAll
42+
protected static void cleanup() {
43+
if (sessionFactory != null) {
44+
sessionFactory.close();
45+
}
46+
}
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.junit.jupiter.api.Test;
3939
import org.junit.jupiter.api.extension.RegisterExtension;
4040

41-
public class ProcedureCallTest {
41+
class ProcedureCallTest {
4242
protected static SessionFactory sessionFactory;
4343
protected static List<Value> prepopulated;
4444

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.junit.jupiter.params.provider.MethodSource;
4646

4747
@SuppressWarnings("deprecation") // testing instrumentation of deprecated class
48-
public class SessionTest extends AbstractHibernateTest {
48+
class SessionTest extends AbstractHibernateTest {
4949
@ParameterizedTest(name = "{index}: {0}")
5050
@MethodSource("provideHibernateActionParameters")
5151
void testHibernateAction(Parameter parameter) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.hibernate.v6_0;
7+
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.Table;
12+
import org.hibernate.annotations.GenericGenerator;
13+
import org.hibernate.annotations.NamedQuery;
14+
15+
@Entity
16+
@Table
17+
@NamedQuery(name = "TestNamedQuery", query = "from Value")
18+
public class Value {
19+
20+
private Long id;
21+
private String name;
22+
23+
public Value() {}
24+
25+
public Value(String name) {
26+
this.name = name;
27+
}
28+
29+
@Id
30+
@GeneratedValue(generator = "increment")
31+
@GenericGenerator(name = "increment", strategy = "increment")
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String title) {
45+
name = title;
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5+
6+
<hibernate-configuration>
7+
8+
<session-factory>
9+
10+
<!-- Use in-memory DB for testing -->
11+
<property name="connection.driver_class">org.h2.Driver</property>
12+
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
13+
<property name="connection.username">sa</property>
14+
<property name="connection.password"/>
15+
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
16+
17+
<property name="connection.pool_size">3</property>
18+
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
19+
<property name="show_sql">true</property>
20+
21+
<!-- Reset the DB each test -->
22+
<property name="hbm2ddl.auto">create</property>
23+
24+
<!-- Objects -->
25+
<mapping class="io.opentelemetry.javaagent.instrumentation.hibernate.v6_0.Value"/>
26+
27+
</session-factory>
28+
29+
</hibernate-configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.hibernate.v7_0;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
9+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import org.hibernate.Session;
13+
import org.hibernate.SessionFactory;
14+
import org.hibernate.cfg.Configuration;
15+
import org.junit.jupiter.api.AfterAll;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.extension.RegisterExtension;
18+
19+
abstract class AbstractHibernateTest {
20+
protected static SessionFactory sessionFactory;
21+
protected static List<Value> prepopulated;
22+
23+
@RegisterExtension
24+
protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
25+
26+
@BeforeAll
27+
protected static void setup() {
28+
sessionFactory = new Configuration().configure().buildSessionFactory();
29+
// Pre-populate the DB, so delete/update can be tested.
30+
Session writer = sessionFactory.openSession();
31+
writer.beginTransaction();
32+
prepopulated = new ArrayList<>();
33+
for (int i = 0; i < 5; i++) {
34+
prepopulated.add(new Value("Hello :) " + i));
35+
writer.persist(prepopulated.get(i));
36+
}
37+
writer.getTransaction().commit();
38+
writer.close();
39+
}
40+
41+
@AfterAll
42+
protected static void cleanup() {
43+
if (sessionFactory != null) {
44+
sessionFactory.close();
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)