|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.jpa.callbacks; |
6 | 6 |
|
7 | | -import java.util.Iterator; |
8 | 7 | import java.util.List; |
9 | | -import jakarta.persistence.EntityManager; |
10 | 8 |
|
11 | | -import org.junit.Test; |
| 9 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 10 | +import org.hibernate.testing.orm.junit.Jpa; |
| 11 | +import org.junit.jupiter.api.Test; |
12 | 12 |
|
13 | | -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; |
14 | | - |
15 | | -import static org.junit.Assert.assertEquals; |
16 | | -import static org.junit.Assert.assertTrue; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
17 | 15 |
|
18 | 16 | /** |
19 | 17 | * @author Emmanuel Bernard |
20 | 18 | */ |
21 | | -public class CallbackAndDirtyTest extends BaseEntityManagerFunctionalTestCase { |
| 19 | +@Jpa(annotatedClasses = { |
| 20 | + Customer.class, |
| 21 | + Employee.class, |
| 22 | + Person.class |
| 23 | +}) |
| 24 | +public class CallbackAndDirtyTest { |
22 | 25 | @Test |
23 | | - public void testDirtyButNotDirty() throws Exception { |
24 | | - EntityManager manager = getOrCreateEntityManager(); |
25 | | - manager.getTransaction().begin(); |
26 | | - Employee mark = new Employee(); |
27 | | - mark.setName( "Mark" ); |
28 | | - mark.setTitle( "internal sales" ); |
29 | | - mark.setSex( 'M' ); |
30 | | - mark.setAddress( "buckhead" ); |
31 | | - mark.setZip( "30305" ); |
32 | | - mark.setCountry( "USA" ); |
33 | | - |
34 | | - Customer joe = new Customer(); |
35 | | - joe.setName( "Joe" ); |
36 | | - joe.setSex( 'M' ); |
37 | | - joe.setAddress( "San Francisco" ); |
38 | | - joe.setZip( "XXXXX" ); |
39 | | - joe.setCountry( "USA" ); |
40 | | - joe.setComments( "Very demanding" ); |
41 | | - joe.setSalesperson( mark ); |
| 26 | + public void testDirtyButNotDirty(EntityManagerFactoryScope scope) { |
| 27 | + scope.inEntityManager( entityManager -> { |
| 28 | + entityManager.getTransaction().begin(); |
| 29 | + Employee mark = new Employee(); |
| 30 | + mark.setName( "Mark" ); |
| 31 | + mark.setTitle( "internal sales" ); |
| 32 | + mark.setSex( 'M' ); |
| 33 | + mark.setAddress( "buckhead" ); |
| 34 | + mark.setZip( "30305" ); |
| 35 | + mark.setCountry( "USA" ); |
42 | 36 |
|
43 | | - Person yomomma = new Person(); |
44 | | - yomomma.setName( "mum" ); |
45 | | - yomomma.setSex( 'F' ); |
| 37 | + Customer joe = new Customer(); |
| 38 | + joe.setName( "Joe" ); |
| 39 | + joe.setSex( 'M' ); |
| 40 | + joe.setAddress( "San Francisco" ); |
| 41 | + joe.setZip( "XXXXX" ); |
| 42 | + joe.setCountry( "USA" ); |
| 43 | + joe.setComments( "Very demanding" ); |
| 44 | + joe.setSalesperson( mark ); |
46 | 45 |
|
47 | | - manager.persist( mark ); |
48 | | - manager.persist( joe ); |
49 | | - manager.persist( yomomma ); |
50 | | - long[] ids = {mark.getId(), joe.getId(), yomomma.getId()}; |
51 | | - manager.getTransaction().commit(); |
| 46 | + Person yomomma = new Person(); |
| 47 | + yomomma.setName( "mum" ); |
| 48 | + yomomma.setSex( 'F' ); |
52 | 49 |
|
53 | | - manager.getTransaction().begin(); |
54 | | - assertEquals( |
55 | | - manager.createQuery( "select p.address, p.name from Person p order by p.name" ).getResultList().size(), |
56 | | - 3 |
57 | | - ); |
58 | | - assertEquals( manager.createQuery( "select p from Person p where p.class = Customer" ).getResultList().size(), 1 ); |
59 | | - manager.getTransaction().commit(); |
| 50 | + entityManager.persist( mark ); |
| 51 | + entityManager.persist( joe ); |
| 52 | + entityManager.persist( yomomma ); |
| 53 | + long[] ids = {mark.getId(), joe.getId(), yomomma.getId()}; |
| 54 | + entityManager.getTransaction().commit(); |
| 55 | + entityManager.getTransaction().begin(); |
| 56 | + assertEquals( |
| 57 | + 3, |
| 58 | + entityManager.createQuery( "select p.address, p.name from Person p order by p.name" ).getResultList().size() |
| 59 | + ); |
| 60 | + assertEquals( 1, |
| 61 | + entityManager.createQuery( "select p from Person p where p.class = Customer" ).getResultList().size() ); |
| 62 | + entityManager.getTransaction().commit(); |
60 | 63 |
|
61 | | - manager.getTransaction().begin(); |
62 | | - List customers = manager.createQuery( "select c from Customer c left join fetch c.salesperson" ).getResultList(); |
63 | | - for ( Iterator iter = customers.iterator(); iter.hasNext() ; ) { |
64 | | - Customer c = (Customer) iter.next(); |
65 | | - assertEquals( c.getSalesperson().getName(), "Mark" ); |
66 | | - } |
67 | | - assertEquals( customers.size(), 1 ); |
68 | | - manager.getTransaction().commit(); |
| 64 | + entityManager.getTransaction().begin(); |
| 65 | + List customers = entityManager.createQuery( "select c from Customer c left join fetch c.salesperson" ).getResultList(); |
| 66 | + for ( Object customer : customers ) { |
| 67 | + Customer c = (Customer) customer; |
| 68 | + assertEquals( "Mark", c.getSalesperson().getName() ); |
| 69 | + } |
| 70 | + assertEquals( 1, customers.size() ); |
| 71 | + entityManager.getTransaction().commit(); |
69 | 72 |
|
70 | | - manager.getTransaction().begin(); |
71 | | - customers = manager.createQuery( "select c from Customer c" ).getResultList(); |
72 | | - for ( Iterator iter = customers.iterator(); iter.hasNext() ; ) { |
73 | | - Customer c = (Customer) iter.next(); |
74 | | - assertEquals( c.getSalesperson().getName(), "Mark" ); |
75 | | - } |
76 | | - assertEquals( customers.size(), 1 ); |
77 | | - manager.getTransaction().commit(); |
| 73 | + entityManager.getTransaction().begin(); |
| 74 | + customers = entityManager.createQuery( "select c from Customer c" ).getResultList(); |
| 75 | + for ( Object customer : customers ) { |
| 76 | + Customer c = (Customer) customer; |
| 77 | + assertEquals( "Mark", c.getSalesperson().getName() ); |
| 78 | + } |
| 79 | + assertEquals( 1, customers.size() ); |
| 80 | + entityManager.getTransaction().commit(); |
78 | 81 |
|
79 | | - manager.getTransaction().begin(); |
80 | | - mark = manager.find( Employee.class, Long.valueOf( ids[0] ) ); |
81 | | - joe = manager.find( Customer.class, Long.valueOf( ids[1] ) ); |
82 | | - yomomma = manager.find( Person.class, Long.valueOf( ids[2] ) ); |
| 82 | + entityManager.getTransaction().begin(); |
| 83 | + mark = entityManager.find( Employee.class, ids[0] ); |
| 84 | + joe = entityManager.find( Customer.class, ids[1] ); |
| 85 | + yomomma = entityManager.find( Person.class, ids[2] ); |
83 | 86 |
|
84 | | - mark.setZip( "30306" ); |
85 | | - assertEquals( 1, manager.createQuery( "select p from Person p where p.zip = '30306'" ).getResultList().size() ); |
86 | | - manager.remove( mark ); |
87 | | - manager.remove( joe ); |
88 | | - manager.remove( yomomma ); |
89 | | - assertTrue( manager.createQuery( "select p from Person p" ).getResultList().isEmpty() ); |
90 | | - manager.getTransaction().commit(); |
91 | | - manager.close(); |
| 87 | + mark.setZip( "30306" ); |
| 88 | + assertEquals( 1, entityManager.createQuery( "select p from Person p where p.zip = '30306'" ).getResultList().size() ); |
| 89 | + entityManager.remove( mark ); |
| 90 | + entityManager.remove( joe ); |
| 91 | + entityManager.remove( yomomma ); |
| 92 | + assertTrue( entityManager.createQuery( "select p from Person p" ).getResultList().isEmpty() ); |
| 93 | + entityManager.getTransaction().commit(); |
| 94 | + } ); |
92 | 95 | } |
93 | 96 |
|
94 | | - @Override |
95 | | - public Class[] getAnnotatedClasses() { |
96 | | - return new Class[]{ |
97 | | - Customer.class, |
98 | | - Employee.class, |
99 | | - Person.class |
100 | | - }; |
101 | | - } |
102 | 97 | } |
0 commit comments