|
3 | 3 | * Copyright Red Hat Inc. and Hibernate Authors |
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.jdbc; |
6 | | -import java.sql.Connection; |
7 | | -import java.sql.ResultSet; |
8 | | -import java.sql.SQLException; |
9 | | -import java.sql.Statement; |
10 | | - |
11 | | -import org.junit.Test; |
12 | 6 |
|
13 | 7 | import org.hibernate.JDBCException; |
14 | | -import org.hibernate.Session; |
15 | 8 | import org.hibernate.engine.spi.SessionImplementor; |
16 | | -import org.hibernate.jdbc.ReturningWork; |
17 | | -import org.hibernate.jdbc.Work; |
18 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.sql.Statement; |
19 | 17 |
|
20 | | -import static org.junit.Assert.assertEquals; |
21 | | -import static org.junit.Assert.fail; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.fail; |
22 | 20 |
|
23 | 21 | /** |
24 | 22 | * GeneralWorkTest implementation |
25 | 23 | * |
26 | 24 | * @author Steve Ebersole |
27 | 25 | */ |
28 | | -public class GeneralWorkTest extends BaseCoreFunctionalTestCase { |
29 | | - @Override |
30 | | - public String getBaseForMappings() { |
31 | | - return ""; |
32 | | - } |
33 | | - |
34 | | - @Override |
35 | | - public String[] getMappings() { |
36 | | - return new String[] { "org/hibernate/orm/test/jdbc/Mappings.hbm.xml" }; |
| 26 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 27 | +@DomainModel(xmlMappings = "org/hibernate/orm/test/jdbc/Mappings.hbm.xml") |
| 28 | +@SessionFactory |
| 29 | +public class GeneralWorkTest { |
| 30 | + @AfterEach |
| 31 | + void cleanup(SessionFactoryScope factoryScope) { |
| 32 | + factoryScope.dropData(); |
37 | 33 | } |
38 | 34 |
|
39 | 35 | @Test |
40 | | - public void testGeneralUsage() throws Throwable { |
41 | | - final Session session = openSession(); |
42 | | - session.beginTransaction(); |
43 | | - session.doWork( |
44 | | - new Work() { |
45 | | - public void execute(Connection connection) throws SQLException { |
46 | | - // in this current form, users must handle try/catches themselves for proper resource release |
47 | | - Statement statement = null; |
48 | | - try { |
49 | | - statement = ((SessionImplementor)session).getJdbcCoordinator().getStatementPreparer().createStatement(); |
50 | | - ResultSet resultSet = null; |
51 | | - try { |
52 | | - |
53 | | - resultSet = ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from T_JDBC_PERSON" ); |
54 | | - } |
55 | | - finally { |
56 | | - releaseQuietly( ((SessionImplementor)session), resultSet, statement ); |
57 | | - } |
58 | | - try { |
59 | | - ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from T_JDBC_BOAT" ); |
60 | | - } |
61 | | - finally { |
62 | | - releaseQuietly( ((SessionImplementor)session), resultSet, statement ); |
63 | | - } |
64 | | - } |
65 | | - finally { |
66 | | - releaseQuietly( ((SessionImplementor)session), statement ); |
67 | | - } |
68 | | - } |
| 36 | + public void testGeneralUsage(SessionFactoryScope factoryScope) { |
| 37 | + factoryScope.inTransaction( (session) -> session.doWork( (connection) -> { |
| 38 | + // in this current form, users must handle try/catches themselves for proper resource release |
| 39 | + Statement statement = null; |
| 40 | + try { |
| 41 | + statement = session.getJdbcCoordinator().getStatementPreparer().createStatement(); |
| 42 | + ResultSet resultSet = null; |
| 43 | + try { |
| 44 | + resultSet = session.getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from T_JDBC_PERSON" ); |
69 | 45 | } |
70 | | - ); |
71 | | - session.getTransaction().commit(); |
72 | | - session.close(); |
| 46 | + finally { |
| 47 | + releaseQuietly( session, resultSet, statement ); |
| 48 | + } |
| 49 | + try { |
| 50 | + session.getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from T_JDBC_BOAT" ); |
| 51 | + } |
| 52 | + finally { |
| 53 | + releaseQuietly( session, resultSet, statement ); |
| 54 | + } |
| 55 | + } |
| 56 | + finally { |
| 57 | + releaseQuietly( session, statement ); |
| 58 | + } |
| 59 | + } ) ); |
73 | 60 | } |
74 | 61 |
|
75 | 62 | @Test |
76 | | - public void testSQLExceptionThrowing() { |
77 | | - final Session session = openSession(); |
78 | | - session.beginTransaction(); |
79 | | - try { |
80 | | - session.doWork( |
81 | | - new Work() { |
82 | | - public void execute(Connection connection) throws SQLException { |
83 | | - Statement statement = null; |
84 | | - try { |
85 | | - statement = ((SessionImplementor)session).getJdbcCoordinator().getStatementPreparer().createStatement(); |
86 | | - ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from non_existent" ); |
87 | | - } |
88 | | - finally { |
89 | | - releaseQuietly( ((SessionImplementor)session), statement ); |
90 | | - } |
91 | | - } |
| 63 | + public void testSQLExceptionThrowing(SessionFactoryScope factoryScope) { |
| 64 | + factoryScope.inTransaction( (session) -> { |
| 65 | + try { |
| 66 | + session.doWork( (connection) -> { |
| 67 | + Statement statement = null; |
| 68 | + try { |
| 69 | + statement = session.getJdbcCoordinator().getStatementPreparer().createStatement(); |
| 70 | + session.getJdbcCoordinator().getResultSetReturn().extract( statement, "select * from non_existent" ); |
92 | 71 | } |
93 | | - ); |
94 | | - fail( "expecting exception" ); |
95 | | - } |
96 | | - catch ( JDBCException expected ) { |
97 | | - // expected outcome |
98 | | - } |
99 | | - session.getTransaction().commit(); |
100 | | - session.close(); |
| 72 | + finally { |
| 73 | + releaseQuietly( session, statement ); |
| 74 | + } |
| 75 | + fail( "expecting exception" ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + catch ( JDBCException expected ) { |
| 79 | + // expected outcome |
| 80 | + } |
| 81 | + } ); |
101 | 82 | } |
102 | 83 |
|
103 | 84 | @Test |
104 | | - public void testGeneralReturningUsage() throws Throwable { |
105 | | - Session session = openSession(); |
106 | | - session.beginTransaction(); |
107 | | - Person p = new Person( "Abe", "Lincoln" ); |
108 | | - session.persist( p ); |
109 | | - session.getTransaction().commit(); |
| 85 | + public void testGeneralReturningUsage(SessionFactoryScope factoryScope) { |
| 86 | + factoryScope.inTransaction( (session) -> { |
| 87 | + var p = new Person( "Abe", "Lincoln" ); |
| 88 | + session.persist( p ); |
| 89 | + } ); |
110 | 90 |
|
111 | | - final Session session2 = openSession(); |
112 | | - session2.beginTransaction(); |
113 | | - long count = session2.doReturningWork( |
114 | | - new ReturningWork<Long>() { |
115 | | - public Long execute(Connection connection) throws SQLException { |
116 | | - // in this current form, users must handle try/catches themselves for proper resource release |
117 | | - Statement statement = null; |
118 | | - long personCount = 0; |
119 | | - try { |
120 | | - statement = ((SessionImplementor)session2).getJdbcCoordinator().getStatementPreparer().createStatement(); |
121 | | - ResultSet resultSet = null; |
122 | | - try { |
123 | | - resultSet = ((SessionImplementor)session2).getJdbcCoordinator().getResultSetReturn().extract( statement, "select count(*) from T_JDBC_PERSON" ); |
124 | | - resultSet.next(); |
125 | | - personCount = resultSet.getLong( 1 ); |
126 | | - assertEquals( 1L, personCount ); |
127 | | - } |
128 | | - finally { |
129 | | - releaseQuietly( ((SessionImplementor)session2), resultSet, statement ); |
130 | | - } |
131 | | - } |
132 | | - finally { |
133 | | - releaseQuietly( ((SessionImplementor)session2), statement ); |
134 | | - } |
135 | | - return personCount; |
| 91 | + factoryScope.inTransaction( (session) -> { |
| 92 | + long count = session.doReturningWork( (connection) -> { |
| 93 | + // in this current form, users must handle try/catches themselves for proper resource release |
| 94 | + Statement statement = null; |
| 95 | + long personCount = 0; |
| 96 | + try { |
| 97 | + statement = session.getJdbcCoordinator().getStatementPreparer().createStatement(); |
| 98 | + ResultSet resultSet = null; |
| 99 | + try { |
| 100 | + resultSet = session.getJdbcCoordinator().getResultSetReturn().extract( statement, "select count(*) from T_JDBC_PERSON" ); |
| 101 | + resultSet.next(); |
| 102 | + personCount = resultSet.getLong( 1 ); |
| 103 | + assertEquals( 1L, personCount ); |
| 104 | + } |
| 105 | + finally { |
| 106 | + releaseQuietly( session, resultSet, statement ); |
136 | 107 | } |
137 | 108 | } |
138 | | - ); |
139 | | - session2.getTransaction().commit(); |
140 | | - session2.close(); |
141 | | - assertEquals( 1L, count ); |
142 | | - |
143 | | - session = openSession(); |
144 | | - session.beginTransaction(); |
145 | | - session.remove( p ); |
146 | | - session.getTransaction().commit(); |
147 | | - session.close(); |
| 109 | + finally { |
| 110 | + releaseQuietly( session, statement ); |
| 111 | + } |
| 112 | + return personCount; |
| 113 | + } ); |
| 114 | + } ); |
148 | 115 | } |
149 | 116 |
|
150 | 117 | private void releaseQuietly(SessionImplementor s, Statement statement) { |
|
0 commit comments