From 620a9857874adca7b260fa7fdeb5735e3d964651 Mon Sep 17 00:00:00 2001
From: Jim Simon <jim.simon@workiva.com>
Date: Wed, 15 Jan 2025 19:42:35 -0500
Subject: [PATCH] HHH-19017: Adds Assertions to Unit Test

---
 .../LazyOneToOneWithEntityGraphTest.java      | 162 ++++++++++--------
 1 file changed, 92 insertions(+), 70 deletions(-)

diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/lazyonetoone/LazyOneToOneWithEntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/lazyonetoone/LazyOneToOneWithEntityGraphTest.java
index b68785deb92f..cb692b87e810 100644
--- a/hibernate-core/src/test/java/org/hibernate/orm/test/lazyonetoone/LazyOneToOneWithEntityGraphTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/orm/test/lazyonetoone/LazyOneToOneWithEntityGraphTest.java
@@ -1,3 +1,7 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ * Copyright Red Hat Inc. and Hibernate Authors
+ */
 package org.hibernate.orm.test.lazyonetoone;
 
 import java.util.List;
@@ -14,80 +18,98 @@
 import org.hibernate.testing.orm.junit.DomainModel;
 import org.hibernate.testing.orm.junit.SessionFactory;
 import org.hibernate.testing.orm.junit.SessionFactoryScope;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
+import static org.hibernate.Hibernate.isInitialized;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 @DomainModel(
-    annotatedClasses = {
-        LazyOneToOneWithEntityGraphTest.Company.class,
-        LazyOneToOneWithEntityGraphTest.Employee.class,
-        LazyOneToOneWithEntityGraphTest.Project.class
-    }
+	annotatedClasses = {
+		LazyOneToOneWithEntityGraphTest.Company.class,
+		LazyOneToOneWithEntityGraphTest.Employee.class,
+		LazyOneToOneWithEntityGraphTest.Project.class
+	}
 )
 @SessionFactory
 @BytecodeEnhanced(runNotEnhancedAsWell = true)
 public class LazyOneToOneWithEntityGraphTest {
-  @Test
-  void reproducerTest(SessionFactoryScope scope) {
-    scope.inTransaction(session -> {
-      // Create company
-      Company company = new Company();
-      company.id = 1L;
-      company.name = "Hibernate";
-      session.persist(company);
-
-      // Create project
-      Project project = new Project();
-      project.id = 1L;
-      session.persist(project);
-
-      // Create employee
-      Employee employee = new Employee();
-      employee.id = 1L;
-      employee.company = company;
-      employee.projects = List.of(project);
-      session.persist(employee);
-    });
-
-    scope.inTransaction(session -> {
-      // Load employee using entity graph
-      Employee employee = session.createQuery(
-              "select e from Employee e where e.id = :id", Employee.class)
-          .setParameter("id", 1L)
-          .setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects"))
-          .getSingleResult();
-    });
-
-    // No assertions here, because this test trigger an exception with Hibernate 6.6.4
-  }
-
-  @Entity(name = "Company")
-  public static class Company {
-    @Id
-    private Long id;
-
-    private String name;
-  }
-
-  @Entity(name = "Employee")
-  @NamedEntityGraph(
-      name = "employee.projects",
-      attributeNodes = @NamedAttributeNode("projects")
-  )
-  public static class Employee {
-    @Id
-    private Long id;
-
-    @OneToOne
-    @JoinColumn(name = "company_name", referencedColumnName = "name")
-    private Company company;
-
-    @OneToMany(fetch = FetchType.LAZY)
-    private List<Project> projects;
-  }
-
-  @Entity(name = "Project")
-  public static class Project {
-    @Id
-    private Long id;
-  }
-}
\ No newline at end of file
+	@BeforeAll
+	void setUp(SessionFactoryScope scope) {
+		scope.inTransaction(session -> {
+			// Create company
+			Company company = new Company();
+			company.id = 1L;
+			company.name = "Hibernate";
+			session.persist(company);
+
+			// Create project
+			Project project = new Project();
+			project.id = 1L;
+			session.persist(project);
+
+			// Create employee
+			Employee employee = new Employee();
+			employee.id = 1L;
+			employee.company = company;
+			employee.projects = List.of(project);
+			session.persist(employee);
+		});
+	}
+
+	@AfterAll
+	void tearDown(SessionFactoryScope scope) {
+		scope.inTransaction(session -> {
+			scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
+		});
+	}
+
+
+	@Test
+	void reproducerTest(SessionFactoryScope scope) {
+		scope.inTransaction(session -> {
+		// Load employee using entity graph
+		Employee employee = session.createQuery(
+				"select e from Employee e where e.id = :id", Employee.class)
+			.setParameter("id", 1L)
+			.setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects"))
+			.getSingleResult();
+
+		assertTrue(isInitialized(employee.projects));
+		assertEquals("Hibernate", employee.company.name);
+		});
+	}
+
+	@Entity(name = "Company")
+	public static class Company {
+		@Id
+		private Long id;
+
+		private String name;
+	}
+
+	@Entity(name = "Employee")
+	@NamedEntityGraph(
+		name = "employee.projects",
+		attributeNodes = @NamedAttributeNode("projects")
+	)
+	public static class Employee {
+		@Id
+		private Long id;
+
+		@OneToOne
+		@JoinColumn(name = "company_name", referencedColumnName = "name")
+		private Company company;
+
+		@OneToMany(fetch = FetchType.LAZY)
+		private List<Project> projects;
+	}
+
+	@Entity(name = "Project")
+	public static class Project {
+		@Id
+		private Long id;
+	}
+}