|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.envers.integration.basic; |
6 | 6 |
|
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
7 | 11 | import org.hibernate.annotations.Generated; |
8 | 12 | import org.hibernate.dialect.H2Dialect; |
| 13 | +import org.hibernate.envers.AuditReaderFactory; |
9 | 14 | import org.hibernate.envers.Audited; |
10 | 15 | import org.hibernate.mapping.Table; |
11 | | -import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; |
12 | | -import org.hibernate.orm.test.envers.Priority; |
13 | | - |
14 | 16 | import org.hibernate.testing.RequiresDialect; |
| 17 | +import org.hibernate.testing.envers.junit.EnversTest; |
| 18 | +import org.hibernate.testing.orm.junit.BeforeClassTemplate; |
| 19 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 20 | +import org.hibernate.testing.orm.junit.DomainModelScope; |
15 | 21 | import org.hibernate.testing.orm.junit.JiraKey; |
16 | | -import org.junit.ComparisonFailure; |
17 | | -import org.junit.Test; |
18 | | - |
19 | | -import jakarta.persistence.Column; |
20 | | -import jakarta.persistence.Entity; |
21 | | -import jakarta.persistence.GeneratedValue; |
22 | | -import jakarta.persistence.Id; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 23 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.opentest4j.AssertionFailedError; |
23 | 26 |
|
24 | 27 | import static org.hibernate.boot.model.naming.Identifier.toIdentifier; |
25 | 28 | import static org.hibernate.engine.jdbc.Size.DEFAULT_LENGTH; |
26 | 29 | import static org.hibernate.engine.jdbc.Size.DEFAULT_PRECISION; |
27 | 30 | import static org.hibernate.engine.jdbc.Size.DEFAULT_SCALE; |
28 | | -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
29 | | -import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
30 | 33 |
|
31 | 34 | /** |
32 | 35 | * This test verifies that resolving a column mapping's {@code sql-type} for HBM XML is performed |
33 | 36 | * correctly such that when a column supplies a {@code columnDefinition}, Envers properly builds |
34 | 37 | * its schema based on the right type rather than directly using the column definition as-is. |
35 | | - * |
| 38 | + * <p> |
36 | 39 | * The following illustrate some examples of expected transformations: |
37 | 40 | * |
38 | 41 | * <li>{@code @Column(columnDefinition = "varchar(10) not null")} => {@code sql-type = "varchar(255)"}</li> |
39 | 42 | * <li>{@code @Column(length = 10, columnDefinition = "varchar(10) not null")} => {@code sql-type = "varchar(10)"}</li> |
40 | 43 | * <li>{@code @Column(columnDefinition = "integer not null auto_increment")} => {@code sql-type = "integer"}</li> |
41 | | - * |
| 44 | + * <p> |
42 | 45 | * It is important to point out that resolving the sql-types length/precision/scale is all based on the |
43 | 46 | * values supplied as part of the {@link Column} annotation itself and not what is in the definition text. |
44 | 47 | * |
45 | 48 | * @author Chris Cranford |
46 | 49 | */ |
47 | 50 | @JiraKey(value = "HHH-10844") |
48 | 51 | @RequiresDialect(value = H2Dialect.class) |
49 | | -public class BasicTypeColumnDefinitionTest extends BaseEnversJPAFunctionalTestCase { |
50 | | - @Override |
51 | | - protected Class<?>[] getAnnotatedClasses() { |
52 | | - return new Class<?>[] { BasicTypeContainer.class }; |
53 | | - } |
54 | | - |
55 | | - // By reverting changes for HHH-10844 to restore columnDefinition original behavior, this implies this test will |
56 | | - // now fail because the expected sql-type will once again be identical to the base table mapping. |
57 | | - @Test(expected = ComparisonFailure.class) |
58 | | - @Priority(10) |
59 | | - public void testMetadataBindings() { |
60 | | - final Long expectedDefaultLength = new Long( DEFAULT_LENGTH ); |
61 | | - final Integer expectedDefaultPrecision = Integer.valueOf( DEFAULT_PRECISION ); |
62 | | - final Integer expectedDefaultScale = Integer.valueOf( DEFAULT_SCALE ); |
63 | | - |
64 | | - final Table auditTable = metadata().getEntityBinding( BasicTypeContainer.class.getName() + "_AUD" ).getTable(); |
65 | | - |
66 | | - final org.hibernate.mapping.Column caseNumber = auditTable.getColumn( toIdentifier( "caseNumber" ) ); |
67 | | - assertEquals( "integer", caseNumber.getSqlType() ); |
68 | | - assertEquals( expectedDefaultLength, caseNumber.getLength() ); |
69 | | - assertEquals( expectedDefaultPrecision, caseNumber.getPrecision() ); |
70 | | - assertEquals( expectedDefaultScale, caseNumber.getScale() ); |
71 | | - |
72 | | - final org.hibernate.mapping.Column colDef = auditTable.getColumn( toIdentifier( "columnWithDefinition" ) ); |
73 | | - assertEquals( "varchar(10)", colDef.getSqlType() ); |
74 | | - assertEquals( new Long( 10 ), colDef.getLength() ); |
75 | | - assertEquals( expectedDefaultPrecision, colDef.getPrecision() ); |
76 | | - assertEquals( expectedDefaultScale, colDef.getScale() ); |
77 | | - } |
78 | | - |
79 | | - @Test |
80 | | - @Priority(10) |
81 | | - public void initData() { |
82 | | - final BasicTypeContainer detachedEntity = doInJPA( this::entityManagerFactory, entityManager -> { |
| 52 | +@EnversTest |
| 53 | +@DomainModel(annotatedClasses = {BasicTypeColumnDefinitionTest.BasicTypeContainer.class}) |
| 54 | +@SessionFactory |
| 55 | +public class BasicTypeColumnDefinitionTest { |
| 56 | + @BeforeClassTemplate |
| 57 | + public void initData(SessionFactoryScope scope) { |
| 58 | + scope.inTransaction( session -> { |
83 | 59 | final BasicTypeContainer entity = new BasicTypeContainer(); |
84 | 60 | entity.setData( "test" ); |
85 | 61 | entity.setColumnWithDefinition( "1234567890" ); |
86 | | - entityManager.persist( entity ); |
87 | | - return entity; |
| 62 | + session.persist( entity ); |
88 | 63 | } ); |
89 | 64 |
|
90 | | - doInJPA( this::entityManagerFactory, entityManager -> { |
91 | | - final BasicTypeContainer entity = entityManager.find( BasicTypeContainer.class, detachedEntity.getId() ); |
| 65 | + scope.inTransaction( session -> { |
| 66 | + final BasicTypeContainer entity = session.createQuery( "from BasicTypeContainer", BasicTypeContainer.class ) |
| 67 | + .getResultList().get( 0 ); |
92 | 68 | entity.setData( "test2" ); |
93 | | - entityManager.merge( entity ); |
| 69 | + session.merge( entity ); |
| 70 | + } ); |
| 71 | + } |
| 72 | + |
| 73 | + // By reverting changes for HHH-10844 to restore columnDefinition original behavior, this implies this test will |
| 74 | + // now fail because the expected sql-type will once again be identical to the base table mapping. |
| 75 | + @Test |
| 76 | + public void testMetadataBindings(DomainModelScope scope) { |
| 77 | + final var domainModel = scope.getDomainModel(); |
| 78 | + |
| 79 | + assertThrows( AssertionFailedError.class, () -> { |
| 80 | + final Long expectedDefaultLength = new Long( DEFAULT_LENGTH ); |
| 81 | + final Integer expectedDefaultPrecision = Integer.valueOf( DEFAULT_PRECISION ); |
| 82 | + final Integer expectedDefaultScale = Integer.valueOf( DEFAULT_SCALE ); |
| 83 | + |
| 84 | + final Table auditTable = domainModel.getEntityBinding( BasicTypeContainer.class.getName() + "_AUD" ) |
| 85 | + .getTable(); |
| 86 | + |
| 87 | + final org.hibernate.mapping.Column caseNumber = auditTable.getColumn( toIdentifier( "caseNumber" ) ); |
| 88 | + assertEquals( "integer", caseNumber.getSqlType() ); |
| 89 | + assertEquals( expectedDefaultLength, caseNumber.getLength() ); |
| 90 | + assertEquals( expectedDefaultPrecision, caseNumber.getPrecision() ); |
| 91 | + assertEquals( expectedDefaultScale, caseNumber.getScale() ); |
| 92 | + |
| 93 | + final org.hibernate.mapping.Column colDef = auditTable.getColumn( toIdentifier( "columnWithDefinition" ) ); |
| 94 | + assertEquals( "varchar(10)", colDef.getSqlType() ); |
| 95 | + assertEquals( new Long( 10 ), colDef.getLength() ); |
| 96 | + assertEquals( expectedDefaultPrecision, colDef.getPrecision() ); |
| 97 | + assertEquals( expectedDefaultScale, colDef.getScale() ); |
94 | 98 | } ); |
95 | 99 | } |
96 | 100 |
|
97 | 101 | @Test |
98 | | - public void testRevisionHistory() { |
99 | | - assertEquals( 2, getAuditReader().getRevisions( BasicTypeContainer.class, 1 ).size() ); |
100 | | - |
101 | | - final BasicTypeContainer rev1 = getAuditReader().find( BasicTypeContainer.class, 1, 1 ); |
102 | | - assertEquals( "test", rev1.getData() ); |
103 | | - assertEquals( "1234567890", rev1.getColumnWithDefinition() ); |
104 | | - assertEquals( Integer.valueOf( 1 ), rev1.getCaseNumber() ); |
105 | | - |
106 | | - final BasicTypeContainer rev2 = getAuditReader().find( BasicTypeContainer.class, 1, 2 ); |
107 | | - assertEquals( "test2", rev2.getData() ); |
108 | | - assertEquals( "1234567890", rev2.getColumnWithDefinition() ); |
109 | | - assertEquals( Integer.valueOf( 1 ), rev2.getCaseNumber() ); |
| 102 | + public void testRevisionHistory(SessionFactoryScope scope) { |
| 103 | + scope.inSession( session -> { |
| 104 | + final var auditReader = AuditReaderFactory.get( session ); |
| 105 | + assertEquals( 2, auditReader.getRevisions( BasicTypeContainer.class, 1 ).size() ); |
| 106 | + |
| 107 | + final BasicTypeContainer rev1 = auditReader.find( BasicTypeContainer.class, 1, 1 ); |
| 108 | + assertEquals( "test", rev1.getData() ); |
| 109 | + assertEquals( "1234567890", rev1.getColumnWithDefinition() ); |
| 110 | + assertEquals( Integer.valueOf( 1 ), rev1.getCaseNumber() ); |
| 111 | + |
| 112 | + final BasicTypeContainer rev2 = auditReader.find( BasicTypeContainer.class, 1, 2 ); |
| 113 | + assertEquals( "test2", rev2.getData() ); |
| 114 | + assertEquals( "1234567890", rev2.getColumnWithDefinition() ); |
| 115 | + assertEquals( Integer.valueOf( 1 ), rev2.getCaseNumber() ); |
| 116 | + } ); |
110 | 117 | } |
111 | 118 |
|
112 | 119 | @Entity(name = "BasicTypeContainer") |
|
0 commit comments