Package org.jboss.cache.optimistic

Source Code of org.jboss.cache.optimistic.NodeInterceptorGetKeysTest

package org.jboss.cache.optimistic;

import org.jboss.cache.*;
import org.jboss.cache.interceptors.Interceptor;
import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
import org.jboss.cache.loader.SamplePojo;
import org.jboss.cache.transaction.DummyTransactionManager;

import javax.transaction.Transaction;
import java.util.Iterator;

/**
* @author xenephon
*/
public class NodeInterceptorGetKeysTest extends AbstractOptimisticTestCase
{


    /**
     * @param name
     */
    public NodeInterceptorGetKeysTest(String name)
    {
        super(name);
    }


    public void testTransactionGetKeysMethod() throws Exception
    {

        TestListener listener = new TestListener();
        final TreeCache cache = createCacheWithListener(listener);

        Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
        interceptor.setCache(cache);
        Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
        nodeInterceptor.setCache(cache);
        MockInterceptor dummy = new MockInterceptor();
        dummy.setCache(cache);

        interceptor.setNext(nodeInterceptor);
        nodeInterceptor.setNext(dummy);

        cache.setInterceptorChain(interceptor);

//     first set up a node with a pojo
        DummyTransactionManager mgr = DummyTransactionManager.getInstance();
        mgr.begin();
        Transaction tx = mgr.getTransaction();

        // inject InvocationContext
        cache.getInvocationContext().setTransaction(tx);
        cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));

        SamplePojo pojo = new SamplePojo(21, "test");

        cache.put("/one/two", "key1", pojo);

        assertEquals(null, dummy.getCalled());
        TransactionTable table = cache.getTransactionTable();

        GlobalTransaction gtx = table.get(tx);

        OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);

        TransactionWorkspace workspace = entry.getTransactionWorkSpace();

        //assert we can see this with a key value get in the transaction
        assertEquals(1, cache.getKeys("/one/two").size());
        mgr.commit();

        //assert what should be the results of our call
        assertEquals(3, workspace.getNodes().size());
        assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
        assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
        assertTrue(entry.getLocks().isEmpty());
        assertEquals(1, entry.getModifications().size());
        assertTrue(!cache.exists("/one/two"));
        assertEquals(null, dummy.getCalled());

        //assert that we cannot see the change if we have not put it into the cache
        // we need to do this as we do not have the tx interceptor in this stack
        mgr.begin();

        Transaction tx2 = mgr.getTransaction();

        // inject InvocationContext
        cache.getInvocationContext().setTransaction(tx2);
        cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));

        assertNull(cache.get("/one/two", "key1"));
        mgr.commit();
        cache.stopService();
    }


    public void testTransactionGetNoKeysMethod() throws Exception
    {

        TestListener listener = new TestListener();
        final TreeCache cache = createCacheWithListener(listener);

        Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
        interceptor.setCache(cache);
        Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
        nodeInterceptor.setCache(cache);
        MockInterceptor dummy = new MockInterceptor();
        dummy.setCache(cache);

        interceptor.setNext(nodeInterceptor);
        nodeInterceptor.setNext(dummy);

        cache.setInterceptorChain(interceptor);

//     first set up a node with a pojo
        DummyTransactionManager mgr = DummyTransactionManager.getInstance();
        mgr.begin();
        Transaction tx = mgr.getTransaction();

        // inject InvocationContext
        cache.getInvocationContext().setTransaction(tx);
        cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));


        assertEquals(null, dummy.getCalled());
        TransactionTable table = cache.getTransactionTable();

        GlobalTransaction gtx = table.get(tx);

        OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);

        //assert we can see this with a key value get in the transaction
        assertEquals(0, cache.getKeys("/").size());
        mgr.commit();


        assertTrue(entry.getLocks().isEmpty());
        assertEquals(0, entry.getModifications().size());
        assertTrue(!cache.exists("/one/two"));
        assertEquals(null, dummy.getCalled());


        cache.stopService();
    }

    public void testTransactionGetKeysIteratorMethod() throws Exception
    {

        TestListener listener = new TestListener();
        final TreeCache cache = createCacheWithListener(listener);

        Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
        interceptor.setCache(cache);
        Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
        nodeInterceptor.setCache(cache);
        MockInterceptor dummy = new MockInterceptor();
        dummy.setCache(cache);

        interceptor.setNext(nodeInterceptor);
        nodeInterceptor.setNext(dummy);

        cache.setInterceptorChain(interceptor);

//     first set up a node with a pojo
        DummyTransactionManager mgr = DummyTransactionManager.getInstance();
        mgr.begin();
        Transaction tx = mgr.getTransaction();

        // inject InvocationContext
        cache.getInvocationContext().setTransaction(tx);
        cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));


        SamplePojo pojo = new SamplePojo(21, "test");

        cache.put("/one/two", "key1", pojo);

        assertEquals(null, dummy.getCalled());
        TransactionTable table = cache.getTransactionTable();

        GlobalTransaction gtx = table.get(tx);

        OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);

        //assert we can see this with a key value get in the transaction
        assertEquals(1, cache.getKeys("/one/two").size());

        for (Iterator it = cache.getKeys("/one/two").iterator(); it.hasNext();)
        {
            it.next();
            it.remove();
        }
        assertEquals(0, cache.getKeys("/one/two").size());
        mgr.commit();


        assertTrue(entry.getLocks().isEmpty());
        assertEquals(1, entry.getModifications().size());
        assertTrue(!cache.exists("/one/two"));
        assertEquals(null, dummy.getCalled());


        cache.stopService();
    }


}
TOP

Related Classes of org.jboss.cache.optimistic.NodeInterceptorGetKeysTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.