|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | + |
| 8 | +//$Id$ |
| 9 | + |
| 10 | +package org.hibernate.jpa.test; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | +import javax.persistence.Query; |
| 14 | + |
| 15 | +import org.hibernate.cfg.Environment; |
| 16 | +import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; |
| 17 | +import org.hibernate.resource.transaction.spi.TransactionCoordinator; |
| 18 | +import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; |
| 19 | + |
| 20 | +import org.hibernate.testing.TestForIssue; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import org.mockito.Mockito; |
| 24 | + |
| 25 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 29 | +import static org.mockito.Mockito.doNothing; |
| 30 | +import static org.mockito.Mockito.doThrow; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Vlad Mihalcea |
| 35 | + */ |
| 36 | +public class NamedQueryTransactionFailureTest extends BaseEntityManagerFunctionalTestCase { |
| 37 | +@Override |
| 38 | +public Class[] getAnnotatedClasses() { |
| 39 | +return new Class[] { |
| 40 | +Item.class, |
| 41 | +Distributor.class, |
| 42 | +Wallet.class |
| 43 | +}; |
| 44 | +} |
| 45 | + |
| 46 | +private TransactionCoordinator transactionCoordinator; |
| 47 | + |
| 48 | +@SuppressWarnings( {"unchecked"}) |
| 49 | +protected void addConfigOptions(Map options) { |
| 50 | +TransactionCoordinatorBuilder transactionCoordinatorBuilder = Mockito.mock( TransactionCoordinatorBuilder.class); |
| 51 | +when(transactionCoordinatorBuilder.getDefaultConnectionHandlingMode()) |
| 52 | +.thenReturn( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_HOLD ); |
| 53 | + |
| 54 | +when(transactionCoordinatorBuilder.isJta()) |
| 55 | +.thenReturn( false ); |
| 56 | + |
| 57 | +transactionCoordinator = Mockito.mock( TransactionCoordinator.class); |
| 58 | + |
| 59 | +when(transactionCoordinatorBuilder.buildTransactionCoordinator(any(), any( TransactionCoordinatorBuilder.Options.class))) |
| 60 | +.thenReturn( transactionCoordinator ); |
| 61 | + |
| 62 | +when( transactionCoordinator.getTransactionCoordinatorBuilder() ).thenReturn( transactionCoordinatorBuilder ); |
| 63 | + |
| 64 | +TransactionCoordinator.TransactionDriver transactionDriver = Mockito.mock( TransactionCoordinator.TransactionDriver.class); |
| 65 | +when( transactionCoordinator.getTransactionDriverControl() ).thenReturn( transactionDriver ); |
| 66 | +when( transactionCoordinator.isActive() ).thenReturn( true ); |
| 67 | +when( transactionDriver.isActive( anyBoolean() ) ).thenReturn( false ); |
| 68 | + |
| 69 | +doNothing().when( transactionCoordinator ).pulse(); |
| 70 | + |
| 71 | +options.put( Environment.TRANSACTION_COORDINATOR_STRATEGY, transactionCoordinatorBuilder ); |
| 72 | +} |
| 73 | + |
| 74 | +@Override |
| 75 | +protected boolean createSchema() { |
| 76 | +return false; |
| 77 | +} |
| 78 | + |
| 79 | +@Test |
| 80 | +@TestForIssue( jiraKey = "HHH-11997" ) |
| 81 | +public void testNamedQueryWithTransactionSynchStatus() { |
| 82 | +try { |
| 83 | +doInJPA( this::entityManagerFactory, entityManager -> { |
| 84 | +try { |
| 85 | +Mockito.reset( transactionCoordinator ); |
| 86 | +doThrow(IllegalStateException.class).when( transactionCoordinator ).pulse(); |
| 87 | + |
| 88 | +entityManager.createNamedQuery( "NamedQuery" ); |
| 89 | +} |
| 90 | +catch (Exception e) { |
| 91 | +assertEquals(IllegalArgumentException.class, e.getClass()); |
| 92 | +assertEquals(IllegalStateException.class, e.getCause().getClass()); |
| 93 | +} |
| 94 | +}); |
| 95 | +} |
| 96 | +catch (Exception ignore) { |
| 97 | +} |
| 98 | +} |
| 99 | + |
| 100 | +@Test |
| 101 | +@TestForIssue( jiraKey = "HHH-11997" ) |
| 102 | +public void testNamedQueryWithMarkForRollbackOnlyFailure() { |
| 103 | +try { |
| 104 | +doInJPA( this::entityManagerFactory, entityManager -> { |
| 105 | +try { |
| 106 | +Mockito.reset( transactionCoordinator ); |
| 107 | +doNothing(). |
| 108 | +doThrow(IllegalStateException.class).when( transactionCoordinator ).pulse(); |
| 109 | + |
| 110 | +entityManager.createNamedQuery( "NamedQuery" ); |
| 111 | +} |
| 112 | +catch (Exception e) { |
| 113 | +assertEquals(IllegalArgumentException.class, e.getClass()); |
| 114 | +assertEquals(IllegalStateException.class, e.getCause().getClass()); |
| 115 | +} |
| 116 | +}); |
| 117 | +} |
| 118 | +catch (Exception ignore) { |
| 119 | +} |
| 120 | +} |
| 121 | +} |
0 commit comments