/** 
 * 
 * Copyright 2004 Michael Gaffney
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.service.impl;

import org.activemq.broker.Broker;
import org.activemq.service.TransactionTask;
import org.jmock.cglib.Mock;
import org.jmock.cglib.MockObjectTestCase;

import javax.transaction.xa.XAException;
import java.util.Map;

/**
 * @author <a href="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
 */
public class LocalTransactionCommandTest extends MockObjectTestCase {
    /*
    private LocalTransactionCommand txnCmd;
    private String stubTxnId;
    private Mock mockBroker;
    private Mock mockTxnMap;
    private Mock mockTxnTask;
    private Mock stubTxnTask;

    public LocalTransactionCommandTest(String name) {
        setName(name);
    }

    public void testRollbackWithPostRollbackFailure() {
        putTxnCmdInUseState();
        addPostRollbackFailure();
        expectRemoveTxnIdCalled();
        doRollbackExpectException(XAException.XAER_RMERR);
        endInFinishedState();
    }
        
    public void testRollback() {
        putTxnCmdInUseState();
        expectPostRollbackCalled();
        expectRemoveTxnIdCalled();
        doRollbackExpectSuccess();
        endInFinishedState();
    }
        
    public void testPrepare() {
        putTxnCmdInUseState();
        doPrepareExpectException(XAException.XAER_RMERR);
        endInUseState();
    }
    
    public void testCommitFromInUseStateWithPostCommitFailure() {
        putTxnCmdInUseState();
        expectPrePrepareCalled();
        doPostCommitFailure(true);
    }

    public void testCommitFromInUseStateWithPrePrepareFailure() {
        putTxnCmdInUseState();
        addPrePrepareFailure();
        expectPostRollbackCalled();
        expectRemoveTxnIdCalled();
        doCommitExpectException(true, XAException.XA_RBOTHER);
        endInFinishedState();
    }

    public void testCommitFromFinishedState() {
        putTxnCmdInFinishedState();
        doCommitExpectException(false, XAException.XAER_PROTO);
        endInFinishedState();
    }

    public void testCommitFromInUseState() {
        putTxnCmdInUseState();
        expectPrePrepareCalled();
        expectPostCommitCalled();
        doCommitExpectSuccess(true);
        endInFinishedState();
    }

    public void testCommitFromStartState() {
        putTxnCmdInStartState();
        expectPrePrepareCalled();
        expectPostCommitCalled();
        doCommitExpectSuccess(true);
        endInFinishedState();
    }

    public void testPrePrepare() throws Throwable {
        putTxnCmdInStartState();
        expectPrePrepareCalled();
        assertTxnCmdInUseState();   
        txnCmd.prePrepare();
        endInUseState();
    }

    public void testAddPrePrepare() throws Throwable {
        putTxnCmdInStartState();
        txnCmd.addPrePrepareTask((TransactionTask) mockTxnTask.proxy());
        endInUseState();
    }

    public void testAddPostCommitTask() throws Throwable {
        putTxnCmdInStartState();
        txnCmd.addPostCommitTask((TransactionTask) mockTxnTask.proxy());
        endInUseState();
    }

    public void testAddPostRollbackTask() throws Throwable {
        putTxnCmdInStartState();
        txnCmd.addPostRollbackTask((TransactionTask) mockTxnTask.proxy());
        endInUseState();
    }

    protected void setUp() throws Exception {
        setupStubs();
        setupMocks();
        setupTxnCommand();
    }

    private void setupStubs() {
        stubTxnId = new String("1:2:3");
        stubTxnTask = new Mock(TransactionTask.class);        
    }

    private void setupMocks() {
        mockBroker = new Mock(Broker.class);
        mockTxnMap = new Mock(Map.class);
        mockTxnTask = new Mock(TransactionTask.class);
    }

    private void setupTxnCommand() {
        txnCmd = new LocalTransactionCommand((Broker) mockBroker.proxy(),
                (Map) mockTxnMap.proxy(), stubTxnId);
    }

    private void verifyMocks() {
        mockBroker.verify();
        mockTxnMap.verify();
        mockTxnTask.verify();
    }

    private void doRollbackExpectSuccess() {
        try {
            txnCmd.rollback();
        } catch (XAException e) {
            fail("No XAException should be thrown");
        }
    }
        
    private void doRollbackExpectException(final int expectedErrorCode) {
        try {
            txnCmd.rollback();
            fail("An XAException should have been thrown");
        } catch (XAException e) {
            assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
        }
    }
    
    private void doPrepareExpectException(final int expectedErrorCode) {
        try {
            txnCmd.prepare();
            fail("An XAException should have been thrown");
        } catch (XAException e) {
            assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
        }
    }
    
    private void doPostCommitFailure(final boolean onePhase) {
        addPostCommitFailure();
        expectRemoveTxnIdCalled();
        doCommitExpectException(onePhase, XAException.XAER_RMERR);
        endInFinishedState();
    }

    private void doCommitExpectSuccess(final boolean onePhase) {
        expectRemoveTxnIdCalled();
        try {
            txnCmd.commit(onePhase);
        } catch (XAException e) {
            fail("No XAException should be thrown");
        }
    }

    private void doCommitExpectException(final boolean onePhase, final int expectedErrorCode) {
        try {
            txnCmd.commit(onePhase);
            fail("An XAException should have been thrown");
        } catch (XAException e) {
            assertEquals("Incorrect XAException error code", expectedErrorCode, e.errorCode);
        }
    }

    private void addPostRollbackFailure() {
        stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
        txnCmd.addPostRollbackTask((TransactionTask) stubTxnTask.proxy());
    }

    private void addPostCommitFailure() {
        stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
        txnCmd.addPostCommitTask((TransactionTask) stubTxnTask.proxy());
    }

    private void addPrePrepareFailure() {
        stubTxnTask.expects(once()).method("execute").will(throwException(new Throwable("Stub")));
        txnCmd.addPrePrepareTask((TransactionTask) stubTxnTask.proxy());
    }

    private void expectPrePrepareCalled() {
        mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
        txnCmd.addPrePrepareTask((TransactionTask) mockTxnTask.proxy());
    }

    private void expectPostCommitCalled() {
        mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
        txnCmd.addPostCommitTask((TransactionTask) mockTxnTask.proxy());
    }

    private void expectPostRollbackCalled() {
        mockTxnTask.expects(once()).method("execute").with(same(mockBroker.proxy()));
        txnCmd.addPostRollbackTask((TransactionTask) mockTxnTask.proxy());
    }

    private void expectRemoveTxnIdCalled() {
        mockTxnMap.expects(once()).method("remove").with(same(stubTxnId));
    }

    private void putTxnCmdInStartState() {
        txnCmd.setState(AbstractTransaction.START_STATE);
        assertTxnCmdInStartState();
    }

    private void putTxnCmdInUseState() {
        txnCmd.setState(AbstractTransaction.IN_USE_STATE);
        assertTxnCmdInUseState();
    }

    private void putTxnCmdInFinishedState() {
        txnCmd.setState(AbstractTransaction.FINISHED_STATE);
        assertTxnCmdInFinishedState();
    }

    private void assertTxnCmdInStartState() {
        assertEquals("Not in START_STATE", AbstractTransaction.START_STATE, txnCmd.getState());
    }

    private void assertTxnCmdInFinishedState() {
        assertEquals("Not in FINISHED_STATE", AbstractTransaction.FINISHED_STATE, txnCmd.getState());
    }

    private void assertTxnCmdInUseState() {
        assertEquals("Not in IN_USE_STATE", AbstractTransaction.IN_USE_STATE, txnCmd.getState());
    }

    private void endInFinishedState() {
        assertTxnCmdInFinishedState();
        verifyMocks();
    }
    
    private void endInUseState() {
        assertTxnCmdInUseState();
        verifyMocks();
    }
    */
    public void testX() {}
}
