/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.optimistic;

import org.jboss.cache.TreeCache;

import javax.transaction.TransactionManager;
import javax.transaction.Transaction;

/**
 * Tests a failure in validating a concurrently updated node
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
public class ValidationFailureTest extends AbstractOptimisticTestCase
{
    public ValidationFailureTest(String name)
    {
        super(name);
    }

    public void testValidationFailureLockRelease() throws Exception
    {
        TreeCache cache = createCache();

        TransactionManager mgr = cache.getTransactionManager();

        mgr.begin();
        cache.put("/a", "key", "value");
        mgr.commit();

        // should succeed, 0 locks left over
        assertEquals("value", cache.get("/a", "key"));
        assertEquals(0, cache.getNumberOfLocksHeld());

        mgr.begin();
        cache.put("/b", "key", "value");
        Transaction tx1 = mgr.suspend();

        mgr.begin();
        cache.put("/b", "key", "value2");
        mgr.commit();

        mgr.resume(tx1);
        try
        {
            mgr.commit();
            assertTrue("Should have failed", false);
        }
        catch(Exception e)
        {
            assertTrue("Expecting this to fail", true);
        }

        // nothing should have been locked.
        assertEquals(0, cache.getNumberOfLocksHeld());

        destroyCache(cache);
    }
}
