@Test
public void sortTxnsByDependency() throws Exception {

    CoinSelector originalCoinSelector = wallet.getCoinSelector();
    try {
        wallet.allowSpendingUnconfirmedTransactions();
        Transaction send1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
        Transaction send1a = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
        wallet.commitTx(send1a);
        Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)));
        wallet.commitTx(send1b);
        Transaction send1c = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 25)));
        wallet.commitTx(send1c);
        Transaction send1d = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 12)));
        wallet.commitTx(send1d);
        Transaction send1e = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 06)));
        wallet.commitTx(send1e);
        Transaction send2 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(200, 0));
        SendRequest req2a = SendRequest.to(OTHER_ADDRESS, valueOf(100, 0));
        req2a.tx.addInput(send2.getOutput(0));
        req2a.shuffleOutputs = false;
        wallet.completeTx(req2a);
        Transaction send2a = req2a.tx;
        SendRequest req2b = SendRequest.to(OTHER_ADDRESS, valueOf(50, 0));
        req2b.tx.addInput(send2a.getOutput(1));
        req2b.shuffleOutputs = false;
        wallet.completeTx(req2b);
        Transaction send2b = req2b.tx;
        SendRequest req2c = SendRequest.to(OTHER_ADDRESS, valueOf(25, 0));
        req2c.tx.addInput(send2b.getOutput(1));
        req2c.shuffleOutputs = false;
        wallet.completeTx(req2c);
        Transaction send2c = req2c.tx;
        Set<Transaction> unsortedTxns = new HashSet<Transaction>();
        unsortedTxns.add(send1a);
        unsortedTxns.add(send1b);
        unsortedTxns.add(send1c);
        unsortedTxns.add(send1d);
        unsortedTxns.add(send1e);
        unsortedTxns.add(send2a);
        unsortedTxns.add(send2b);
        unsortedTxns.add(send2c);
        List<Transaction> sortedTxns = wallet.sortTxnsByDependency(unsortedTxns);
        assertEquals(8, sortedTxns.size());
        assertTrue(sortedTxns.indexOf(send1a) < sortedTxns.indexOf(send1b));
        assertTrue(sortedTxns.indexOf(send1b) < sortedTxns.indexOf(send1c));
        assertTrue(sortedTxns.indexOf(send1c) < sortedTxns.indexOf(send1d));
        assertTrue(sortedTxns.indexOf(send1d) < sortedTxns.indexOf(send1e));
        assertTrue(sortedTxns.indexOf(send2a) < sortedTxns.indexOf(send2b));
        assertTrue(sortedTxns.indexOf(send2b) < sortedTxns.indexOf(send2c));
    } finally {
        wallet.setCoinSelector(originalCoinSelector);
    }
}