}
public void testRemotePrepareTransaction() throws Exception
{
TreeCache cache = createCacheWithListener();
Interceptor txInterceptor = new TxInterceptor();
txInterceptor.setCache(cache);
Interceptor replicationInterceptor = new OptimisticReplicationInterceptor();
replicationInterceptor.setCache(cache);
Interceptor createInterceptor = new OptimisticCreateIfNotExistsInterceptor();
createInterceptor.setCache(cache);
Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
nodeInterceptor.setCache(cache);
MockInterceptor dummy = new MockInterceptor();
dummy.setCache(cache);
txInterceptor.setNext(replicationInterceptor);
replicationInterceptor.setNext(createInterceptor);
createInterceptor.setNext(nodeInterceptor);
nodeInterceptor.setNext(dummy);
cache.setInterceptorChain(txInterceptor);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
//start local transaction
mgr.begin();
Transaction tx = mgr.getTransaction();
//this sets
cache.getCurrentTransaction(tx);
SamplePojo pojo = new SamplePojo(21, "test");
cache.put("/one/two", "key1", pojo);
GlobalTransaction gtx = cache.getCurrentTransaction(tx);
TransactionTable table = cache.getTransactionTable();
OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
assertNotNull(mgr.getTransaction());
mgr.commit();
GlobalTransaction remoteGtx = new GlobalTransaction();
remoteGtx.setAddress(new Address()
{
public int compareTo(Object arg0)
{
return 0;
}
public void readFrom(DataInputStream arg0) throws IOException,
IllegalAccessException, InstantiationException
{
}
public void writeTo(DataOutputStream arg0) throws IOException
{
}
public void readExternal(ObjectInput arg0) throws IOException,
ClassNotFoundException
{
}
public void writeExternal(ObjectOutput arg0) throws IOException
{
}
public int size()
{
return 0;
}
public boolean isMulticastAddress()
{
return false;
}
});
//hack the method call to make it have the remote gtx
MethodCall meth = (MethodCall) entry.getModifications().get(0);
meth.getArgs()[0] = remoteGtx;
//call our remote method
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object[]{remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE});
try
{
cache._replicate(prepareMethod);
}
catch (Throwable t)
{
fail();
}
//our thread should be null
assertNull(mgr.getTransaction());
// there should be a registration for the remote gtx
assertNotNull(table.get(remoteGtx));
assertNotNull(table.getLocalTransaction(remoteGtx));
//assert that this is populated
assertEquals(1, table.get(remoteGtx).getModifications().size());
//assert that the remote prepare has populated the local workspace
OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx);
assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
assertEquals(1, entry.getModifications().size());
List calls = dummy.getAllCalled();
assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
destroyCache(cache);
}