-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
PostgreSQL81DialectTestCase.java
101 lines (86 loc) · 3.99 KB
/
PostgreSQL81DialectTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.dialect;
import java.sql.BatchUpdateException;
import java.sql.CallableStatement;
import java.sql.SQLException;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import org.mockito.Mockito;
import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Testing of patched support for PostgreSQL Lock error detection. HHH-7251
*
* @author Bryan Varner
*/
public class PostgreSQL81DialectTestCase extends BaseUnitTestCase {
@Test
public void testDeadlockException() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
assertNotNull(delegate);
JDBCException exception = delegate.convert(new SQLException("Deadlock Detected", "40P01"), "", "");
assertTrue(exception instanceof LockAcquisitionException);
}
@Test
public void testTimeoutException() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
assertNotNull(delegate);
JDBCException exception = delegate.convert(new SQLException("Lock Not Available", "55P03"), "", "");
assertTrue(exception instanceof PessimisticLockException);
}
/**
* Tests that getForUpdateString(String aliases, LockOptions lockOptions) will return a String
* that will effect the SELECT ... FOR UPDATE OF tableAlias1, ..., tableAliasN
*/
@TestForIssue( jiraKey = "HHH-5654" )
public void testGetForUpdateStringWithAliasesAndLockOptions() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
LockOptions lockOptions = new LockOptions();
lockOptions.setAliasSpecificLockMode("tableAlias1", LockMode.PESSIMISTIC_WRITE);
String forUpdateClause = dialect.getForUpdateString("tableAlias1", lockOptions);
assertTrue("for update of tableAlias1".equals(forUpdateClause));
lockOptions.setAliasSpecificLockMode("tableAlias2", LockMode.PESSIMISTIC_WRITE);
forUpdateClause = dialect.getForUpdateString("tableAlias1,tableAlias2", lockOptions);
assertTrue("for update of tableAlias1,tableAlias2".equals(forUpdateClause));
}
@Test
public void testExtractConstraintName() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLException psqlException = new java.sql.SQLException("ERROR: duplicate key value violates unique constraint \"uk_4bm1x2ultdmq63y3h5r3eg0ej\" Detail: Key (username, server_config)=(user, 1) already exists.", "23505");
BatchUpdateException batchUpdateException = new BatchUpdateException("Concurrent Error", "23505", null);
batchUpdateException.setNextException(psqlException);
String constraintName = dialect.getViolatedConstraintNameExtractor().extractConstraintName(batchUpdateException);
assertThat(constraintName, is("uk_4bm1x2ultdmq63y3h5r3eg0ej"));
}
@Test
@TestForIssue(jiraKey = "HHH-8687")
public void testMessageException() throws SQLException {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
try {
dialect.getResultSet( Mockito.mock( CallableStatement.class), "abc" );
fail( "Expected UnsupportedOperationException" );
}
catch (Exception e) {
assertTrue( e instanceof UnsupportedOperationException );
assertEquals( "PostgreSQL only supports accessing REF_CURSOR parameters by position", e.getMessage() );
}
}
}