use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class CacheTransactionManagerCreationTest method shouldBeMockable.
@Test
public void shouldBeMockable() throws Exception {
CacheTransactionManagerCreation mockCacheTransactionManagerCreation = mock(CacheTransactionManagerCreation.class);
TransactionListener mockTransactionListener = mock(TransactionListener.class);
TransactionWriter mockTransactionWriter = mock(TransactionWriter.class);
when(mockCacheTransactionManagerCreation.getListener()).thenReturn(mockTransactionListener);
mockCacheTransactionManagerCreation.setWriter(mockTransactionWriter);
verify(mockCacheTransactionManagerCreation, times(1)).setWriter(mockTransactionWriter);
assertThat(mockCacheTransactionManagerCreation.getListener()).isSameAs(mockTransactionListener);
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrowWithJTA.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testAfterCommitFailedOnTransactionWriterThrowWithJTA() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
UserTransaction userTx = (UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
userTx.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
userTx.commit();
fail("Commit should have thrown RollbackException");
} catch (RollbackException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() + " is not a SynchronizationCommitConflictException", expected.getCause() instanceof SynchronizationCommitConflictException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(1, this.afterRollbacks);
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class TXWriterJUnitTest method testNoCallbacksOnTransactionWriterThrow.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testNoCallbacksOnTransactionWriterThrow() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException cce) {
assertNotNull(cce.getCause());
assertTrue(cce.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.cbCount = 0;
this.region.create("key1", "value1");
// do a sanity check to make sure callbacks are installed
// 2 -> 1writer + 1listener
assertEquals(2, this.cbCount);
this.txMgr.begin();
this.region.put("key1", "value2");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.localDestroy("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.destroy("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.localInvalidate("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnThrowNPE.
@Test
public void testAfterCommitFailedOnThrowNPE() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new NullPointerException("this is expected!");
}
public void close() {
}
});
installTransactionListener();
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof NullPointerException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(0, this.afterRollbacks);
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class CacheXmlParser method endTransactionWriter.
/**
* Create a <code>transaction-writer</code> using the declarable interface and set the transaction
* manager with the newly instantiated writer.
*/
private void endTransactionWriter() {
Declarable d = createDeclarable();
if (!(d instanceof TransactionWriter)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_TRANSACTION_WRITER.toLocalizedString(d.getClass().getName()));
}
CacheTransactionManagerCreation txMgrCreation = (CacheTransactionManagerCreation) stack.peek();
txMgrCreation.setWriter((TransactionWriter) d);
}
Aggregations