From e1d6707626070c8ac7ce3c35804c2862a3357cf2 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 22 Apr 2014 16:48:49 +0200 Subject: [PATCH] Move some testing utilities to a new testing subpackage and rename TextUtils to reflect what it actually does. --- .../bitcoin/core/PeerSocketHandler.java | 2 +- .../FakeTxBuilder.java} | 5 ++--- .../testing}/InboundMessageQueuer.java | 11 +++++----- .../MockTransactionBroadcaster.java | 3 ++- .../testing}/TestWithNetworkConnections.java | 11 +++++----- .../{utils => testing}/TestWithWallet.java | 10 +++++---- .../google/bitcoin/core/BlockChainTest.java | 8 +++---- .../google/bitcoin/core/ChainSplitTest.java | 12 +++++----- ...ilteredBlockAndPartialMerkleTreeTests.java | 1 + .../bitcoin/core/LazyParseByteCacheTest.java | 4 ++-- .../google/bitcoin/core/MemoryPoolTest.java | 4 ++-- .../google/bitcoin/core/PeerGroupTest.java | 21 +++++++++--------- .../com/google/bitcoin/core/PeerTest.java | 22 ++++++++++--------- .../bitcoin/core/TestWithPeerGroup.java | 2 ++ .../core/TransactionBroadcastTest.java | 9 ++++---- .../com/google/bitcoin/core/WalletTest.java | 10 ++++----- .../channels/ChannelConnectionTest.java | 4 ++-- .../channels/PaymentChannelStateTest.java | 6 ++--- .../store/WalletProtobufSerializerTest.java | 6 ++--- .../wallet/DefaultCoinSelectorTest.java | 6 ++--- 20 files changed, 84 insertions(+), 73 deletions(-) rename core/src/main/java/com/google/bitcoin/{utils/TestUtils.java => testing/FakeTxBuilder.java} (98%) rename core/src/{test/java/com/google/bitcoin/core => main/java/com/google/bitcoin/testing}/InboundMessageQueuer.java (72%) rename core/src/main/java/com/google/bitcoin/{utils => testing}/MockTransactionBroadcaster.java (97%) rename core/src/{test/java/com/google/bitcoin/core => main/java/com/google/bitcoin/testing}/TestWithNetworkConnections.java (96%) rename core/src/main/java/com/google/bitcoin/{utils => testing}/TestWithWallet.java (92%) diff --git a/core/src/main/java/com/google/bitcoin/core/PeerSocketHandler.java b/core/src/main/java/com/google/bitcoin/core/PeerSocketHandler.java index d9e262fa..a2e59558 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerSocketHandler.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerSocketHandler.java @@ -47,7 +47,7 @@ public abstract class PeerSocketHandler extends AbstractTimeoutHandler implement // If we close() before we know our writeTarget, set this to true to call writeTarget.closeConnection() right away. private boolean closePending = false; // writeTarget will be thread-safe, and may call into PeerGroup, which calls us, so we should call it unlocked - @VisibleForTesting MessageWriteTarget writeTarget = null; + @VisibleForTesting protected MessageWriteTarget writeTarget = null; // The ByteBuffers passed to us from the writeTarget are static in size, and usually smaller than some messages we // will receive. For SPV clients, this should be rare (ie we're mostly dealing with small transactions), but for diff --git a/core/src/main/java/com/google/bitcoin/utils/TestUtils.java b/core/src/main/java/com/google/bitcoin/testing/FakeTxBuilder.java similarity index 98% rename from core/src/main/java/com/google/bitcoin/utils/TestUtils.java rename to core/src/main/java/com/google/bitcoin/testing/FakeTxBuilder.java index e582adc2..83123435 100644 --- a/core/src/main/java/com/google/bitcoin/utils/TestUtils.java +++ b/core/src/main/java/com/google/bitcoin/testing/FakeTxBuilder.java @@ -14,19 +14,18 @@ * limitations under the License. */ -package com.google.bitcoin.utils; +package com.google.bitcoin.testing; import com.google.bitcoin.core.*; import com.google.bitcoin.store.BlockStore; import com.google.bitcoin.store.BlockStoreException; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; -public class TestUtils { +public class FakeTxBuilder { public static Transaction createFakeTxWithChangeAddress(NetworkParameters params, BigInteger nanocoins, Address to, Address changeOutput) throws IOException, ProtocolException { // Create a fake TX of sufficient realism to exercise the unit tests. Two outputs, one to us, one to somewhere diff --git a/core/src/test/java/com/google/bitcoin/core/InboundMessageQueuer.java b/core/src/main/java/com/google/bitcoin/testing/InboundMessageQueuer.java similarity index 72% rename from core/src/test/java/com/google/bitcoin/core/InboundMessageQueuer.java rename to core/src/main/java/com/google/bitcoin/testing/InboundMessageQueuer.java index 27089f8e..05c16f95 100644 --- a/core/src/test/java/com/google/bitcoin/core/InboundMessageQueuer.java +++ b/core/src/main/java/com/google/bitcoin/testing/InboundMessageQueuer.java @@ -1,5 +1,6 @@ -package com.google.bitcoin.core; +package com.google.bitcoin.testing; +import com.google.bitcoin.core.*; import com.google.common.util.concurrent.SettableFuture; import java.net.InetSocketAddress; @@ -9,11 +10,11 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; /** - * An extension of {@link PeerSocketHandler} that keeps inbound messages in a queue for later processing + * An extension of {@link com.google.bitcoin.core.PeerSocketHandler} that keeps inbound messages in a queue for later processing */ public abstract class InboundMessageQueuer extends PeerSocketHandler { - final BlockingQueue inboundMessages = new ArrayBlockingQueue(1000); - final Map> mapPingFutures = new HashMap>(); + public final BlockingQueue inboundMessages = new ArrayBlockingQueue(1000); + public final Map> mapPingFutures = new HashMap>(); public Peer peer; public BloomFilter lastReceivedFilter; @@ -33,7 +34,7 @@ public abstract class InboundMessageQueuer extends PeerSocketHandler { @Override protected void processMessage(Message m) throws Exception { if (m instanceof Ping) { - SettableFuture future = mapPingFutures.get(((Ping)m).getNonce()); + SettableFuture future = mapPingFutures.get(((Ping) m).getNonce()); if (future != null) { future.set(null); return; diff --git a/core/src/main/java/com/google/bitcoin/utils/MockTransactionBroadcaster.java b/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java similarity index 97% rename from core/src/main/java/com/google/bitcoin/utils/MockTransactionBroadcaster.java rename to core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java index 59829e5d..7f2a4790 100644 --- a/core/src/main/java/com/google/bitcoin/utils/MockTransactionBroadcaster.java +++ b/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package com.google.bitcoin.utils; +package com.google.bitcoin.testing; import com.google.bitcoin.core.Transaction; import com.google.bitcoin.core.TransactionBroadcaster; import com.google.bitcoin.core.VerificationException; import com.google.bitcoin.core.Wallet; +import com.google.bitcoin.utils.Threading; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.SettableFuture; diff --git a/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java b/core/src/main/java/com/google/bitcoin/testing/TestWithNetworkConnections.java similarity index 96% rename from core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java rename to core/src/main/java/com/google/bitcoin/testing/TestWithNetworkConnections.java index 0a67c1ee..d312cf08 100644 --- a/core/src/test/java/com/google/bitcoin/core/TestWithNetworkConnections.java +++ b/core/src/main/java/com/google/bitcoin/testing/TestWithNetworkConnections.java @@ -15,8 +15,9 @@ * limitations under the License. */ -package com.google.bitcoin.core; +package com.google.bitcoin.testing; +import com.google.bitcoin.core.*; import com.google.bitcoin.net.*; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.BlockStore; @@ -37,7 +38,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; import static com.google.common.base.Preconditions.checkArgument; -import static org.junit.Assert.assertTrue; +import static com.google.common.base.Preconditions.checkState; /** * Utility class that makes it easy to work with mock NetworkConnections. @@ -56,7 +57,7 @@ public class TestWithNetworkConnections { private final ClientConnectionManager channels; protected final BlockingQueue newPeerWriteTargetQueue = new LinkedBlockingQueue(); - enum ClientType { + public enum ClientType { NIO_CLIENT_MANAGER, BLOCKING_CLIENT_MANAGER, NIO_CLIENT, @@ -168,8 +169,8 @@ public class TestWithNetworkConnections { writeTarget.sendMessage(versionMessage); writeTarget.sendMessage(new VersionAck()); try { - assertTrue(writeTarget.nextMessageBlocking() instanceof VersionMessage); - assertTrue(writeTarget.nextMessageBlocking() instanceof VersionAck); + checkState(writeTarget.nextMessageBlocking() instanceof VersionMessage); + checkState(writeTarget.nextMessageBlocking() instanceof VersionAck); synchronized (doneConnecting) { doneConnecting.set(true); } diff --git a/core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java b/core/src/main/java/com/google/bitcoin/testing/TestWithWallet.java similarity index 92% rename from core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java rename to core/src/main/java/com/google/bitcoin/testing/TestWithWallet.java index 5c4a4779..186a0aff 100644 --- a/core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java +++ b/core/src/main/java/com/google/bitcoin/testing/TestWithWallet.java @@ -14,19 +14,21 @@ * limitations under the License. */ -package com.google.bitcoin.utils; +package com.google.bitcoin.testing; import com.google.bitcoin.core.*; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.BlockStore; import com.google.bitcoin.store.MemoryBlockStore; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.utils.BriefLogFormatter; import javax.annotation.Nullable; import java.io.IOException; import java.math.BigInteger; -import static com.google.bitcoin.utils.TestUtils.createFakeBlock; -import static com.google.bitcoin.utils.TestUtils.createFakeTx; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx; // TODO: This needs to be somewhat rewritten - the "sendMoneyToWallet" methods aren't sending via the block chain object @@ -69,7 +71,7 @@ public class TestWithWallet { if (wallet.isPendingTransactionRelevant(tx)) wallet.receivePending(tx, null); } else { - TestUtils.BlockPair bp = createFakeBlock(blockStore, tx); + FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, tx); wallet.receiveFromBlock(tx, bp.storedBlock, type, 0); if (type == AbstractBlockChain.NewBlockType.BEST_CHAIN) wallet.notifyNewBestBlock(bp.storedBlock); diff --git a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java index dcfa9fa0..cb2d1633 100644 --- a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java +++ b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java @@ -22,8 +22,8 @@ import com.google.bitcoin.params.TestNet2Params; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.BlockStore; import com.google.bitcoin.store.MemoryBlockStore; +import com.google.bitcoin.testing.FakeTxBuilder; import com.google.bitcoin.utils.BriefLogFormatter; -import com.google.bitcoin.utils.TestUtils; import com.google.common.util.concurrent.ListenableFuture; import org.junit.After; import org.junit.Before; @@ -33,8 +33,8 @@ import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.Date; -import static com.google.bitcoin.utils.TestUtils.createFakeBlock; -import static com.google.bitcoin.utils.TestUtils.createFakeTx; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx; import static org.junit.Assert.*; // Handling of chain splits/reorgs are in ChainSplitTests. @@ -272,7 +272,7 @@ public class BlockChainTest { wallet.addKey(key); Address addr = key.toAddress(unitTestParams); // Create a tx that gives us some coins, and another that spends it to someone else in the same block. - Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), addr); + Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), addr); Transaction t2 = new Transaction(unitTestParams); t2.addInput(t1.getOutputs().get(0)); t2.addOutput(Utils.toNanoCoins(2, 0), somebodyElse); diff --git a/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java b/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java index 4a920353..d57756d4 100644 --- a/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java +++ b/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java @@ -19,8 +19,8 @@ package com.google.bitcoin.core; import com.google.bitcoin.core.TransactionConfidence.ConfidenceType; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.MemoryBlockStore; +import com.google.bitcoin.testing.FakeTxBuilder; import com.google.bitcoin.utils.BriefLogFormatter; -import com.google.bitcoin.utils.TestUtils; import com.google.bitcoin.utils.Threading; import org.junit.Before; import org.junit.Test; @@ -540,8 +540,8 @@ public class ChainSplitTest { // This covers issue 468. // Receive some money to the wallet. - Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.COIN, coinsTo); - final Block b1 = TestUtils.makeSolvedTestBlock(unitTestParams.genesisBlock, t1); + Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.COIN, coinsTo); + final Block b1 = FakeTxBuilder.makeSolvedTestBlock(unitTestParams.genesisBlock, t1); chain.add(b1); // Send a couple of payments one after the other (so the second depends on the change output of the first). @@ -550,7 +550,7 @@ public class ChainSplitTest { wallet.commitTx(t2); Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(unitTestParams), Utils.CENT)); wallet.commitTx(t3); - chain.add(TestUtils.makeSolvedTestBlock(b1, t2, t3)); + chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3)); final BigInteger coins0point98 = Utils.COIN.subtract(Utils.CENT).subtract(Utils.CENT); assertEquals(coins0point98, wallet.getBalance()); @@ -559,8 +559,8 @@ public class ChainSplitTest { ByteArrayOutputStream bos = new ByteArrayOutputStream(); wallet.saveToFileStream(bos); wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray())); - final Block b2 = TestUtils.makeSolvedTestBlock(b1, t2, t3); - final Block b3 = TestUtils.makeSolvedTestBlock(b2); + final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3); + final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2); chain.add(b2); chain.add(b3); diff --git a/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java b/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java index 314a52dc..dfd65aa4 100644 --- a/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java +++ b/core/src/test/java/com/google/bitcoin/core/FilteredBlockAndPartialMerkleTreeTests.java @@ -20,6 +20,7 @@ package com.google.bitcoin.core; import com.google.bitcoin.core.TransactionConfidence.ConfidenceType; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.MemoryBlockStore; +import com.google.bitcoin.testing.InboundMessageQueuer; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; diff --git a/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java b/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java index 9b03f51d..2259b52e 100644 --- a/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java +++ b/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java @@ -28,8 +28,8 @@ import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.util.Arrays; -import static com.google.bitcoin.utils.TestUtils.createFakeBlock; -import static com.google.bitcoin.utils.TestUtils.createFakeTx; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx; import static org.junit.Assert.*; public class LazyParseByteCacheTest { diff --git a/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java b/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java index 0d506487..c4d0bbff 100644 --- a/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java +++ b/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java @@ -17,8 +17,8 @@ package com.google.bitcoin.core; import com.google.bitcoin.params.UnitTestParams; +import com.google.bitcoin.testing.FakeTxBuilder; import com.google.bitcoin.utils.BriefLogFormatter; -import com.google.bitcoin.utils.TestUtils; import org.junit.Before; import org.junit.Test; @@ -35,7 +35,7 @@ public class MemoryPoolTest { @Before public void setup() throws Exception { BriefLogFormatter.init(); - tx1 = TestUtils.createFakeTx(params, Utils.toNanoCoins(1, 0), new ECKey().toAddress(params)); + tx1 = FakeTxBuilder.createFakeTx(params, Utils.toNanoCoins(1, 0), new ECKey().toAddress(params)); tx2 = new Transaction(params, tx1.bitcoinSerialize()); address1 = new PeerAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })); diff --git a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java index d0f79a72..bf0e8077 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java @@ -21,7 +21,8 @@ import com.google.bitcoin.net.discovery.PeerDiscovery; import com.google.bitcoin.net.discovery.PeerDiscoveryException; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.MemoryBlockStore; -import com.google.bitcoin.utils.TestUtils; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.testing.InboundMessageQueuer; import com.google.bitcoin.utils.Threading; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -177,7 +178,7 @@ public class PeerGroupTest extends TestWithPeerGroup { assertEquals(tmp, expectedPeers); BigInteger value = Utils.toNanoCoins(1, 0); - Transaction t1 = TestUtils.createFakeTx(unitTestParams, value, address); + Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address); InventoryMessage inv = new InventoryMessage(unitTestParams); inv.addTransaction(t1); @@ -212,10 +213,10 @@ public class PeerGroupTest extends TestWithPeerGroup { // Set up a little block chain. We heard about b1 but not b2 (it is pending download). b3 is solved whilst we // are downloading the chain. - Block b1 = TestUtils.createFakeBlock(blockStore).block; + Block b1 = FakeTxBuilder.createFakeBlock(blockStore).block; blockChain.add(b1); - Block b2 = TestUtils.makeSolvedTestBlock(b1); - Block b3 = TestUtils.makeSolvedTestBlock(b2); + Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1); + Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2); // Peer 1 and 2 receives an inv advertising a newly solved block. InventoryMessage inv = new InventoryMessage(params); @@ -254,9 +255,9 @@ public class PeerGroupTest extends TestWithPeerGroup { InboundMessageQueuer p1 = connectPeer(1); // Set up a little block chain. - Block b1 = TestUtils.createFakeBlock(blockStore).block; - Block b2 = TestUtils.makeSolvedTestBlock(b1); - Block b3 = TestUtils.makeSolvedTestBlock(b2); + Block b1 = FakeTxBuilder.createFakeBlock(blockStore).block; + Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1); + Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2); // Expect a zero hash getblocks on p1. This is how the process starts. peerGroup.startBlockChainDownload(new AbstractPeerEventListener() { @@ -299,7 +300,7 @@ public class PeerGroupTest extends TestWithPeerGroup { InboundMessageQueuer p2 = connectPeer(2); InboundMessageQueuer p3 = connectPeer(3); - Transaction tx = TestUtils.createFakeTx(params, Utils.toNanoCoins(20, 0), address); + Transaction tx = FakeTxBuilder.createFakeTx(params, Utils.toNanoCoins(20, 0), address); InventoryMessage inv = new InventoryMessage(params); inv.addTransaction(tx); @@ -534,7 +535,7 @@ public class PeerGroupTest extends TestWithPeerGroup { InboundMessageQueuer p1 = connectPeer(1); InboundMessageQueuer p2 = connectPeer(2); // Create a pay to pubkey tx. - Transaction tx = TestUtils.createFakeTx(params, Utils.COIN, key); + Transaction tx = FakeTxBuilder.createFakeTx(params, Utils.COIN, key); Transaction tx2 = new Transaction(params); tx2.addInput(tx.getOutput(0)); TransactionOutPoint outpoint = tx2.getInput(0).getOutpoint(); diff --git a/core/src/test/java/com/google/bitcoin/core/PeerTest.java b/core/src/test/java/com/google/bitcoin/core/PeerTest.java index 5caa15d9..0476e32a 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerTest.java @@ -17,7 +17,9 @@ package com.google.bitcoin.core; import com.google.bitcoin.params.TestNet3Params; -import com.google.bitcoin.utils.TestUtils; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.testing.InboundMessageQueuer; +import com.google.bitcoin.testing.TestWithNetworkConnections; import com.google.bitcoin.utils.Threading; import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; @@ -46,7 +48,7 @@ import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -import static com.google.bitcoin.utils.TestUtils.*; +import static com.google.bitcoin.testing.FakeTxBuilder.*; import static org.junit.Assert.*; @RunWith(value = Parameterized.class) @@ -541,9 +543,9 @@ public class PeerTest extends TestWithNetworkConnections { // -> [t7] // -> [t8] // The ones in brackets are assumed to be in the chain and are represented only by hashes. - Transaction t2 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), to); + Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), to); Sha256Hash t5 = t2.getInput(0).getOutpoint().getHash(); - Transaction t4 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), new ECKey()); + Transaction t4 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), new ECKey()); Sha256Hash t6 = t4.getInput(0).getOutpoint().getHash(); t4.addOutput(Utils.toNanoCoins(1, 0), new ECKey()); Transaction t3 = new Transaction(unitTestParams); @@ -557,10 +559,10 @@ public class PeerTest extends TestWithNetworkConnections { Sha256Hash anotherHash = new Sha256Hash("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6"); t1.addInput(new TransactionInput(unitTestParams, t1, new byte[]{}, new TransactionOutPoint(unitTestParams, 1, anotherHash))); t1.addOutput(Utils.toNanoCoins(1, 0), to); - t1 = TestUtils.roundTripTransaction(unitTestParams, t1); - t2 = TestUtils.roundTripTransaction(unitTestParams, t2); - t3 = TestUtils.roundTripTransaction(unitTestParams, t3); - t4 = TestUtils.roundTripTransaction(unitTestParams, t4); + t1 = FakeTxBuilder.roundTripTransaction(unitTestParams, t1); + t2 = FakeTxBuilder.roundTripTransaction(unitTestParams, t2); + t3 = FakeTxBuilder.roundTripTransaction(unitTestParams, t3); + t4 = FakeTxBuilder.roundTripTransaction(unitTestParams, t4); // Announce the first one. Wait for it to be downloaded. InventoryMessage inv = new InventoryMessage(unitTestParams); @@ -663,7 +665,7 @@ public class PeerTest extends TestWithNetworkConnections { } }); // Send a normal relevant transaction, it's received correctly. - Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), key); + Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), key); inbound(writeTarget, t1); GetDataMessage getdata = (GetDataMessage) outbound(writeTarget); if (useNotFound) { @@ -676,7 +678,7 @@ public class PeerTest extends TestWithNetworkConnections { assertNotNull(vtx[0]); vtx[0] = null; // Send a timelocked transaction, nothing happens. - Transaction t2 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(2, 0), key); + Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(2, 0), key); t2.setLockTime(999999); inbound(writeTarget, t2); Threading.waitForUserCode(); diff --git a/core/src/test/java/com/google/bitcoin/core/TestWithPeerGroup.java b/core/src/test/java/com/google/bitcoin/core/TestWithPeerGroup.java index a6bbf8a2..573c292c 100644 --- a/core/src/test/java/com/google/bitcoin/core/TestWithPeerGroup.java +++ b/core/src/test/java/com/google/bitcoin/core/TestWithPeerGroup.java @@ -20,6 +20,8 @@ import com.google.bitcoin.net.BlockingClientManager; import com.google.bitcoin.net.NioClientManager; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.BlockStore; +import com.google.bitcoin.testing.InboundMessageQueuer; +import com.google.bitcoin.testing.TestWithNetworkConnections; import com.google.common.base.Preconditions; import java.net.InetSocketAddress; diff --git a/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java b/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java index 8eb687cb..a3bc5cc1 100644 --- a/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java +++ b/core/src/test/java/com/google/bitcoin/core/TransactionBroadcastTest.java @@ -19,7 +19,8 @@ package com.google.bitcoin.core; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.MemoryBlockStore; -import com.google.bitcoin.utils.TestUtils; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.testing.InboundMessageQueuer; import com.google.bitcoin.utils.Threading; import com.google.common.util.concurrent.ListenableFuture; import org.junit.After; @@ -106,7 +107,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup { connectPeer(2); // Send ourselves a bit of money. - Block b1 = TestUtils.makeSolvedTestBlock(blockStore, address); + Block b1 = FakeTxBuilder.makeSolvedTestBlock(blockStore, address); inbound(p1, b1); assertNull(outbound(p1)); assertEquals(Utils.toNanoCoins(50, 0), wallet.getBalance()); @@ -140,7 +141,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup { InboundMessageQueuer p2 = connectPeer(2); // Send ourselves a bit of money. - Block b1 = TestUtils.makeSolvedTestBlock(blockStore, address); + Block b1 = FakeTxBuilder.makeSolvedTestBlock(blockStore, address); inbound(p1, b1); pingAndWait(p1); assertNull(outbound(p1)); @@ -178,7 +179,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup { assertEquals(transactions[0], sendResult.tx); assertEquals(1, transactions[0].getConfidence().numBroadcastPeers()); // Confirm it. - Block b2 = TestUtils.createFakeBlock(blockStore, t1).block; + Block b2 = FakeTxBuilder.createFakeBlock(blockStore, t1).block; inbound(p1, b2); pingAndWait(p1); assertNull(outbound(p1)); diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index b682f096..65740eb5 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -24,9 +24,9 @@ import com.google.bitcoin.crypto.KeyCrypterException; import com.google.bitcoin.crypto.KeyCrypterScrypt; import com.google.bitcoin.crypto.TransactionSignature; import com.google.bitcoin.store.WalletProtobufSerializer; -import com.google.bitcoin.utils.MockTransactionBroadcaster; -import com.google.bitcoin.utils.TestUtils; -import com.google.bitcoin.utils.TestWithWallet; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.testing.MockTransactionBroadcaster; +import com.google.bitcoin.testing.TestWithWallet; import com.google.bitcoin.utils.Threading; import com.google.bitcoin.wallet.*; import com.google.bitcoin.wallet.WalletTransaction.Pool; @@ -54,7 +54,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static com.google.bitcoin.core.Utils.*; -import static com.google.bitcoin.utils.TestUtils.*; +import static com.google.bitcoin.testing.FakeTxBuilder.*; import static com.google.common.base.Preconditions.checkNotNull; import static org.junit.Assert.*; @@ -771,7 +771,7 @@ public class WalletTest extends TestWithWallet { send1.getConfidence().getConfidenceType()); assertEquals(send2, received.getOutput(0).getSpentBy().getParentTransaction()); - TestUtils.DoubleSpends doubleSpends = TestUtils.createFakeDoubleSpendTxns(params, myAddress); + FakeTxBuilder.DoubleSpends doubleSpends = FakeTxBuilder.createFakeDoubleSpendTxns(params, myAddress); // t1 spends to our wallet. t2 double spends somewhere else. wallet.receivePending(doubleSpends.t1, null); assertEquals(TransactionConfidence.ConfidenceType.PENDING, diff --git a/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java b/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java index 0bd9b863..117e913c 100644 --- a/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java +++ b/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java @@ -18,7 +18,7 @@ package com.google.bitcoin.protocols.channels; import com.google.bitcoin.core.*; import com.google.bitcoin.store.WalletProtobufSerializer; -import com.google.bitcoin.utils.TestWithWallet; +import com.google.bitcoin.testing.TestWithWallet; import com.google.bitcoin.utils.Threading; import com.google.bitcoin.wallet.WalletFiles; import com.google.common.util.concurrent.ListenableFuture; @@ -40,7 +40,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import static com.google.bitcoin.protocols.channels.PaymentChannelCloseException.CloseReason; -import static com.google.bitcoin.utils.TestUtils.createFakeBlock; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock; import static org.bitcoin.paymentchannel.Protos.TwoWayChannelMessage.MessageType; import static org.junit.Assert.*; diff --git a/core/src/test/java/com/google/bitcoin/protocols/channels/PaymentChannelStateTest.java b/core/src/test/java/com/google/bitcoin/protocols/channels/PaymentChannelStateTest.java index 7e39b8ca..67e55db8 100644 --- a/core/src/test/java/com/google/bitcoin/protocols/channels/PaymentChannelStateTest.java +++ b/core/src/test/java/com/google/bitcoin/protocols/channels/PaymentChannelStateTest.java @@ -19,7 +19,7 @@ package com.google.bitcoin.protocols.channels; import com.google.bitcoin.core.*; import com.google.bitcoin.script.Script; import com.google.bitcoin.script.ScriptBuilder; -import com.google.bitcoin.utils.TestWithWallet; +import com.google.bitcoin.testing.TestWithWallet; import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; @@ -34,8 +34,8 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.LinkedBlockingQueue; -import static com.google.bitcoin.utils.TestUtils.createFakeTx; -import static com.google.bitcoin.utils.TestUtils.makeSolvedTestBlock; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx; +import static com.google.bitcoin.testing.FakeTxBuilder.makeSolvedTestBlock; import static org.junit.Assert.*; public class PaymentChannelStateTest extends TestWithWallet { diff --git a/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java b/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java index 9899b9bc..ec1a3389 100644 --- a/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java +++ b/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java @@ -23,8 +23,8 @@ import com.google.bitcoin.core.TransactionConfidence.ConfidenceType; import com.google.bitcoin.params.MainNetParams; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.script.ScriptBuilder; +import com.google.bitcoin.testing.FakeTxBuilder; import com.google.bitcoin.utils.BriefLogFormatter; -import com.google.bitcoin.utils.TestUtils; import com.google.bitcoin.utils.Threading; import com.google.protobuf.ByteString; import org.bitcoinj.wallet.Protos; @@ -40,7 +40,7 @@ import java.util.Date; import java.util.Iterator; import java.util.Set; -import static com.google.bitcoin.utils.TestUtils.createFakeTx; +import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx; import static org.junit.Assert.*; public class WalletProtobufSerializerTest { @@ -125,7 +125,7 @@ public class WalletProtobufSerializerTest { @Test public void doubleSpend() throws Exception { // Check that we can serialize double spends correctly, as this is a slightly tricky case. - TestUtils.DoubleSpends doubleSpends = TestUtils.createFakeDoubleSpendTxns(params, myAddress); + FakeTxBuilder.DoubleSpends doubleSpends = FakeTxBuilder.createFakeDoubleSpendTxns(params, myAddress); // t1 spends to our wallet. myWallet.receivePending(doubleSpends.t1, null); // t2 rolls back t1 and spends somewhere else. diff --git a/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java b/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java index cff9d6f3..f6d00b9e 100644 --- a/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java +++ b/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java @@ -19,8 +19,8 @@ package com.google.bitcoin.wallet; import com.google.bitcoin.core.*; import com.google.bitcoin.params.RegTestParams; import com.google.bitcoin.params.UnitTestParams; -import com.google.bitcoin.utils.TestUtils; -import com.google.bitcoin.utils.TestWithWallet; +import com.google.bitcoin.testing.FakeTxBuilder; +import com.google.bitcoin.testing.TestWithWallet; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -96,7 +96,7 @@ public class DefaultCoinSelectorTest extends TestWithWallet { // and t3=0.01. Transaction t1 = checkNotNull(sendMoneyToWallet(Utils.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN)); // Padding block. - wallet.notifyNewBestBlock(TestUtils.createFakeBlock(blockStore).storedBlock); + wallet.notifyNewBestBlock(FakeTxBuilder.createFakeBlock(blockStore).storedBlock); final BigInteger TWO_COINS = Utils.COIN.multiply(BigInteger.valueOf(2)); Transaction t2 = checkNotNull(sendMoneyToWallet(TWO_COINS, AbstractBlockChain.NewBlockType.BEST_CHAIN)); Transaction t3 = checkNotNull(sendMoneyToWallet(Utils.CENT, AbstractBlockChain.NewBlockType.BEST_CHAIN));