|
6 | 6 |
|
7 | 7 | import java.math.BigDecimal; |
8 | 8 | import java.math.BigInteger; |
9 | | -import jakarta.persistence.EntityManager; |
| 9 | + |
10 | 10 | import jakarta.persistence.criteria.CriteriaBuilder; |
11 | 11 | import jakarta.persistence.criteria.CriteriaQuery; |
12 | 12 | import jakarta.persistence.criteria.Root; |
13 | 13 |
|
14 | | -import org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest; |
15 | 14 | import org.hibernate.orm.test.jpa.metamodel.Product; |
16 | 15 | import org.hibernate.orm.test.jpa.metamodel.Product_; |
17 | 16 |
|
| 17 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 18 | +import org.hibernate.testing.orm.junit.Jpa; |
18 | 19 | import org.junit.jupiter.api.AfterEach; |
19 | 20 | import org.junit.jupiter.api.BeforeEach; |
20 | 21 | import org.junit.jupiter.api.Test; |
|
24 | 25 | /** |
25 | 26 | * @author Steve Ebersole |
26 | 27 | */ |
27 | | -public class AggregationResultTest extends AbstractMetamodelSpecificTest { |
| 28 | +@Jpa(annotatedClasses = {Product.class}) |
| 29 | +public class AggregationResultTest { |
28 | 30 | private CriteriaBuilder builder; |
29 | 31 |
|
30 | 32 | @BeforeEach |
31 | | - public void createTestData() { |
32 | | - builder = entityManagerFactory().getCriteriaBuilder(); |
33 | | - |
34 | | - EntityManager em = getOrCreateEntityManager(); |
35 | | - em.getTransaction().begin(); |
36 | | - Product product = new Product(); |
37 | | - product.setId( "product1" ); |
38 | | - product.setPrice( 1.23d ); |
39 | | - product.setQuantity( 1000 ); |
40 | | - product.setPartNumber( ( (long) Integer.MAX_VALUE ) + 1 ); |
41 | | - product.setRating( 1.999f ); |
42 | | - product.setSomeBigInteger( BigInteger.valueOf( 987654321 ) ); |
43 | | - product.setSomeBigDecimal( BigDecimal.valueOf( 987654.32 ) ); |
44 | | - em.persist( product ); |
45 | | - em.getTransaction().commit(); |
46 | | - em.close(); |
| 33 | + public void createTestData(EntityManagerFactoryScope scope) { |
| 34 | + builder = scope.getEntityManagerFactory().getCriteriaBuilder(); |
| 35 | + |
| 36 | + scope.inTransaction( entityManager -> { |
| 37 | + Product product = new Product(); |
| 38 | + product.setId( "product1" ); |
| 39 | + product.setPrice( 1.23d ); |
| 40 | + product.setQuantity( 1000 ); |
| 41 | + product.setPartNumber( ((long) Integer.MAX_VALUE) + 1 ); |
| 42 | + product.setRating( 1.999f ); |
| 43 | + product.setSomeBigInteger( BigInteger.valueOf( 987654321 ) ); |
| 44 | + product.setSomeBigDecimal( BigDecimal.valueOf( 987654.32 ) ); |
| 45 | + entityManager.persist( product ); |
| 46 | + } ); |
47 | 47 | } |
48 | 48 |
|
49 | 49 | @AfterEach |
50 | | - public void cleanUpTestData() throws Exception { |
51 | | - EntityManager em = getOrCreateEntityManager(); |
52 | | - em.getTransaction().begin(); |
53 | | - em.createQuery( "delete Product" ).executeUpdate(); |
54 | | - em.getTransaction().commit(); |
55 | | - em.close(); |
| 50 | + public void cleanUpTestData(EntityManagerFactoryScope scope) { |
| 51 | + scope.getEntityManagerFactory().getSchemaManager().truncate(); |
56 | 52 | } |
57 | 53 |
|
58 | 54 | /** |
59 | 55 | * Sum of Longs should return a Long |
60 | 56 | */ |
61 | 57 | @Test |
62 | | - public void testSumOfLongs() { |
63 | | - EntityManager em = getOrCreateEntityManager(); |
64 | | - em.getTransaction().begin(); |
65 | | - CriteriaQuery<Long> criteria = builder.createQuery( Long.class ); |
66 | | - Root<Product> productRoot = criteria.from( Product.class ); |
67 | | - criteria.select( builder.sum( productRoot.get( Product_.partNumber ) ) ); |
68 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
69 | | - assertReturnType( Long.class, sumResult ); |
70 | | - em.getTransaction().commit(); |
71 | | - em.close(); |
| 58 | + public void testSumOfLongs(EntityManagerFactoryScope scope) { |
| 59 | + scope.inTransaction( entityManager -> { |
| 60 | + CriteriaQuery<Long> criteria = builder.createQuery( Long.class ); |
| 61 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 62 | + criteria.select( builder.sum( productRoot.get( Product_.partNumber ) ) ); |
| 63 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 64 | + assertReturnType( Long.class, sumResult ); |
| 65 | + } ); |
72 | 66 | } |
73 | 67 |
|
74 | 68 | /** |
75 | | - * Sum of Integers should return an Integer; note that this is distinctly different than JPAQL |
| 69 | + * Sum of Integers should return an Integer; note that this is distinctly different from JPAQL |
76 | 70 | */ |
77 | 71 | @Test |
78 | | - public void testSumOfIntegers() { |
79 | | - EntityManager em = getOrCreateEntityManager(); |
80 | | - em.getTransaction().begin(); |
81 | | - CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class ); |
82 | | - Root<Product> productRoot = criteria.from( Product.class ); |
83 | | - criteria.select( builder.sum( productRoot.get( Product_.quantity ) ) ); |
84 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
85 | | - assertReturnType( Integer.class, sumResult ); |
86 | | - em.getTransaction().commit(); |
87 | | - em.close(); |
| 72 | + public void testSumOfIntegers(EntityManagerFactoryScope scope) { |
| 73 | + scope.inTransaction( entityManager -> { |
| 74 | + CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class ); |
| 75 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 76 | + criteria.select( builder.sum( productRoot.get( Product_.quantity ) ) ); |
| 77 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 78 | + assertReturnType( Integer.class, sumResult ); |
| 79 | + } ); |
88 | 80 | } |
89 | 81 |
|
90 | 82 | /** |
91 | 83 | * Sum of Doubles should return a Double |
92 | 84 | */ |
93 | 85 | @Test |
94 | | - public void testSumOfDoubles() { |
95 | | - EntityManager em = getOrCreateEntityManager(); |
96 | | - em.getTransaction().begin(); |
97 | | - CriteriaQuery<Double> criteria = builder.createQuery( Double.class ); |
98 | | - Root<Product> productRoot = criteria.from( Product.class ); |
99 | | - criteria.select( builder.sum( productRoot.get( Product_.price ) ) ); |
100 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
101 | | - assertReturnType( Double.class, sumResult ); |
102 | | - em.getTransaction().commit(); |
103 | | - em.close(); |
| 86 | + public void testSumOfDoubles(EntityManagerFactoryScope scope) { |
| 87 | + scope.inTransaction( entityManager -> { |
| 88 | + CriteriaQuery<Double> criteria = builder.createQuery( Double.class ); |
| 89 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 90 | + criteria.select( builder.sum( productRoot.get( Product_.price ) ) ); |
| 91 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 92 | + assertReturnType( Double.class, sumResult ); |
| 93 | + } ); |
104 | 94 | } |
105 | 95 |
|
106 | 96 | /** |
107 | | - * Sum of Floats should return a Float; note that this is distinctly different than JPAQL |
| 97 | + * Sum of Floats should return a Float; note that this is distinctly different from JPAQL |
108 | 98 | */ |
109 | 99 | @Test |
110 | | - public void testSumOfFloats() { |
111 | | - EntityManager em = getOrCreateEntityManager(); |
112 | | - em.getTransaction().begin(); |
113 | | - CriteriaQuery<Float> criteria = builder.createQuery( Float.class ); |
114 | | - Root<Product> productRoot = criteria.from( Product.class ); |
115 | | - criteria.select( builder.sum( productRoot.get( Product_.rating ) ) ); |
116 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
117 | | - assertReturnType( Float.class, sumResult ); |
118 | | - em.getTransaction().commit(); |
119 | | - em.close(); |
| 100 | + public void testSumOfFloats(EntityManagerFactoryScope scope) { |
| 101 | + scope.inTransaction( entityManager -> { |
| 102 | + CriteriaQuery<Float> criteria = builder.createQuery( Float.class ); |
| 103 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 104 | + criteria.select( builder.sum( productRoot.get( Product_.rating ) ) ); |
| 105 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 106 | + assertReturnType( Float.class, sumResult ); |
| 107 | + } ); |
120 | 108 | } |
121 | 109 |
|
122 | 110 | /** |
123 | 111 | * Sum of BigInteger should return a BigInteger |
124 | 112 | */ |
125 | 113 | @Test |
126 | | - public void testSumOfBigIntegers() { |
127 | | - EntityManager em = getOrCreateEntityManager(); |
128 | | - em.getTransaction().begin(); |
129 | | - CriteriaQuery<BigInteger> criteria = builder.createQuery( BigInteger.class ); |
130 | | - Root<Product> productRoot = criteria.from( Product.class ); |
131 | | - criteria.select( builder.sum( productRoot.get( Product_.someBigInteger ) ) ); |
132 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
133 | | - assertReturnType( BigInteger.class, sumResult ); |
134 | | - em.getTransaction().commit(); |
135 | | - em.close(); |
| 114 | + public void testSumOfBigIntegers(EntityManagerFactoryScope scope) { |
| 115 | + scope.inTransaction( entityManager -> { |
| 116 | + CriteriaQuery<BigInteger> criteria = builder.createQuery( BigInteger.class ); |
| 117 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 118 | + criteria.select( builder.sum( productRoot.get( Product_.someBigInteger ) ) ); |
| 119 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 120 | + assertReturnType( BigInteger.class, sumResult ); |
| 121 | + } ); |
136 | 122 | } |
137 | 123 |
|
138 | 124 | /** |
139 | 125 | * Sum of BigDecimal should return a BigDecimal |
140 | 126 | */ |
141 | 127 | @Test |
142 | | - public void testSumOfBigDecimals() { |
143 | | - EntityManager em = getOrCreateEntityManager(); |
144 | | - em.getTransaction().begin(); |
145 | | - CriteriaQuery<BigDecimal> criteria = builder.createQuery( BigDecimal.class ); |
146 | | - Root<Product> productRoot = criteria.from( Product.class ); |
147 | | - criteria.select( builder.sum( productRoot.get( Product_.someBigDecimal ) ) ); |
148 | | - Object sumResult = em.createQuery( criteria ).getSingleResult(); |
149 | | - assertReturnType( BigDecimal.class, sumResult ); |
150 | | - em.getTransaction().commit(); |
151 | | - em.close(); |
| 128 | + public void testSumOfBigDecimals(EntityManagerFactoryScope scope) { |
| 129 | + scope.inTransaction( entityManager -> { |
| 130 | + CriteriaQuery<BigDecimal> criteria = builder.createQuery( BigDecimal.class ); |
| 131 | + Root<Product> productRoot = criteria.from( Product.class ); |
| 132 | + criteria.select( builder.sum( productRoot.get( Product_.someBigDecimal ) ) ); |
| 133 | + Object sumResult = entityManager.createQuery( criteria ).getSingleResult(); |
| 134 | + assertReturnType( BigDecimal.class, sumResult ); |
| 135 | + } ); |
152 | 136 | } |
153 | 137 |
|
154 | | - private void assertReturnType(Class expectedType, Object value) { |
| 138 | + private void assertReturnType(Class<?> expectedType, Object value) { |
155 | 139 | if ( value != null && ! expectedType.isInstance( value ) ) { |
156 | 140 | fail( |
157 | 141 | "Result value was not of expected type: expected [" + expectedType.getName() |
|
0 commit comments