Merge pull request #11884 from anuragkumawat/JAVA-10379

JAVA-10379 Fix tests in hibernate-mapping-2 module
This commit is contained in:
kwoyke 2022-03-10 13:21:30 +01:00 committed by GitHub
commit 64cc1089f9
3 changed files with 64 additions and 44 deletions

View File

@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>hibernate-mapping-2y</artifactId>
<artifactId>hibernate-mapping-2</artifactId>
<version>0.1-SNAPSHOT</version>
<name>hibernate-mapping-2</name>

View File

@ -45,6 +45,13 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
projects.add(new Project("Networking Project"));
session.persist(new Employee("Peter", "Oven", projects));
session.persist(new Employee("Allan", "Norman", projects));
Set<Employee> employees = new HashSet<Employee>();
employees.add(new Employee("Sam", "Curran"));
employees.add(new Employee("Tom", "Curran"));
Project project = new Project("Database Project");
project.setEmployees(employees);
session.persist(project);
}
}

View File

@ -1,7 +1,7 @@
package com.baeldung.hibernate.manytomany;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashSet;
import java.util.List;
@ -23,60 +23,73 @@ import com.baeldung.manytomany.util.HibernateUtil;
* Configured in: manytomany.cfg.xml
*/
public class HibernateManyToManyAnnotationMainIntegrationTest {
private static SessionFactory sessionFactory;
private static SessionFactory sessionFactory;
private Session session;
private Session session;
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory();
}
@BeforeClass
public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory();
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@Test
public void givenData_whenInsert_thenCreatesMtoMrelationship() {
String[] employeeData = { "Peter Oven", "Allan Norman" };
String[] projectData = { "IT Project", "Networking Project" };
Set<Project> projects = new HashSet<Project>();
for (String proj : projectData) {
projects.add(new Project(proj));
}
for (String emp : employeeData) {
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
assertEquals(0, employee.getProjects().size());
employee.setProjects(projects);
session.persist(employee);
assertNotNull(employee);
}
}
@Test
@Test
public void givenSession_whenRead_thenReturnsMtoMdata() {
prepareData();
@SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list();
@SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list();
List<Project> projectList = session.createQuery("FROM Project").list();
assertNotNull(employeeList);
assertNotNull(projectList);
assertEquals(2, employeeList.size());
assertEquals(2, projectList.size());
for(Employee employee : employeeList) {
assertNotNull(employee.getProjects());
assertEquals(2, employee.getProjects().size());
}
for(Project project : projectList) {
assertNotNull(project.getEmployees());
assertEquals(2, project.getEmployees().size());
}
}
@After
public void tearDown() {
session.getTransaction()
.commit();
session.close();
}
private void prepareData() {
String[] employeeData = { "Peter Oven", "Allan Norman" };
String[] projectData = { "IT Project", "Networking Project" };
Set<Project> projects = new HashSet<Project>();
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
for (String proj : projectData) {
projects.add(new Project(proj));
}
for (String emp : employeeData) {
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
employee.setProjects(projects);
for (Project proj : projects) {
proj.getEmployees().add(employee);
}
session.persist(employee);
}
}
@After
public void tearDown() {
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void afterTests() {
sessionFactory.close();
}
}