From 90942041e73acb118f8e6d4d76275e39439a3558 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Wed, 18 Sep 2013 12:21:07 +0200 Subject: [PATCH] Move TestUtils and TestWithWallet into the core package so they can be used by third party code in their unit tests. --- .../java/com/google/bitcoin/core/Block.java | 4 +++- .../java/com/google/bitcoin/core/Message.java | 5 ++++ .../java/com/google/bitcoin/core/Wallet.java | 2 +- .../com/google/bitcoin/utils}/TestUtils.java | 11 +++++---- .../google/bitcoin/utils}/TestWithWallet.java | 23 +++++++++++-------- .../google/bitcoin/core/BlockChainTest.java | 5 ++-- .../bitcoin/core/LazyParseByteCacheTest.java | 4 ++-- .../google/bitcoin/core/MemoryPoolTest.java | 1 + .../google/bitcoin/core/PeerGroupTest.java | 1 + .../com/google/bitcoin/core/PeerTest.java | 3 ++- .../com/google/bitcoin/core/WalletTest.java | 13 +++++++++-- .../channels/ChannelConnectionTest.java | 9 +++++++- .../channels/PaymentChannelStateTest.java | 14 ++++++++--- .../store/WalletProtobufSerializerTest.java | 3 ++- 14 files changed, 69 insertions(+), 29 deletions(-) rename core/src/{test/java/com/google/bitcoin/core => main/java/com/google/bitcoin/utils}/TestUtils.java (97%) rename core/src/{test/java/com/google/bitcoin/core => main/java/com/google/bitcoin/utils}/TestWithWallet.java (79%) diff --git a/core/src/main/java/com/google/bitcoin/core/Block.java b/core/src/main/java/com/google/bitcoin/core/Block.java index a40fd9ee..aa9c3a0f 100644 --- a/core/src/main/java/com/google/bitcoin/core/Block.java +++ b/core/src/main/java/com/google/bitcoin/core/Block.java @@ -18,6 +18,7 @@ package com.google.bitcoin.core; import com.google.bitcoin.script.Script; import com.google.bitcoin.script.ScriptBuilder; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import org.slf4j.Logger; @@ -974,7 +975,8 @@ public class Block extends Message { /** * Returns a solved block that builds on top of this one. This exists for unit tests. */ - Block createNextBlock(Address to, long time) { + @VisibleForTesting + public Block createNextBlock(Address to, long time) { return createNextBlock(to, null, time, EMPTY_BYTES, Utils.toNanoCoins(50, 0)); } diff --git a/core/src/main/java/com/google/bitcoin/core/Message.java b/core/src/main/java/com/google/bitcoin/core/Message.java index 1cc2e3fa..11680ba2 100644 --- a/core/src/main/java/com/google/bitcoin/core/Message.java +++ b/core/src/main/java/com/google/bitcoin/core/Message.java @@ -505,6 +505,11 @@ public abstract class Message implements Serializable { return cursor < bytes.length; } + /** Network parameters this message was created with. */ + public NetworkParameters getParams() { + return params; + } + public static class LazyParseException extends RuntimeException { private static final long serialVersionUID = 6971943053112975594L; diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index ebe33f64..7965fc97 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -739,7 +739,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi * risky it is. If this method returns true then {@link Wallet#receivePending(Transaction, java.util.List)} * will soon be called with the transactions dependencies as well. */ - boolean isPendingTransactionRelevant(Transaction tx) throws ScriptException { + public boolean isPendingTransactionRelevant(Transaction tx) throws ScriptException { lock.lock(); try { // Ignore it if we already know about this transaction. Receiving a pending transaction never moves it diff --git a/core/src/test/java/com/google/bitcoin/core/TestUtils.java b/core/src/main/java/com/google/bitcoin/utils/TestUtils.java similarity index 97% rename from core/src/test/java/com/google/bitcoin/core/TestUtils.java rename to core/src/main/java/com/google/bitcoin/utils/TestUtils.java index be1c4e25..13a5e75e 100644 --- a/core/src/test/java/com/google/bitcoin/core/TestUtils.java +++ b/core/src/main/java/com/google/bitcoin/utils/TestUtils.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package com.google.bitcoin.core; +package com.google.bitcoin.utils; +import com.google.bitcoin.core.*; import com.google.bitcoin.store.BlockStore; import com.google.bitcoin.store.BlockStoreException; @@ -146,15 +147,15 @@ public class TestUtils { } public static class BlockPair { - StoredBlock storedBlock; - Block block; + public StoredBlock storedBlock; + public Block block; } // Emulates receiving a valid block that builds on top of the chain. public static BlockPair createFakeBlock(BlockStore blockStore, long timeSeconds, Transaction... transactions) { try { Block chainHead = blockStore.getChainHead().getHeader(); - Address to = new ECKey().toAddress(chainHead.params); + Address to = new ECKey().toAddress(chainHead.getParams()); Block b = chainHead.createNextBlock(to, timeSeconds); // Coinbase tx was already added. for (Transaction tx : transactions) { @@ -186,7 +187,7 @@ public class TestUtils { } public static Block makeSolvedTestBlock(Block prev, Transaction... transactions) throws BlockStoreException { - Address to = new ECKey().toAddress(prev.params); + Address to = new ECKey().toAddress(prev.getParams()); Block b = prev.createNextBlock(to); // Coinbase tx already exists. for (Transaction tx : transactions) { diff --git a/core/src/test/java/com/google/bitcoin/core/TestWithWallet.java b/core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java similarity index 79% rename from core/src/test/java/com/google/bitcoin/core/TestWithWallet.java rename to core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java index 4a2fb6ef..d5a883be 100644 --- a/core/src/test/java/com/google/bitcoin/core/TestWithWallet.java +++ b/core/src/main/java/com/google/bitcoin/utils/TestWithWallet.java @@ -14,22 +14,27 @@ * limitations under the License. */ -package com.google.bitcoin.core; +package com.google.bitcoin.utils; +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.utils.BriefLogFormatter; -import org.junit.After; -import org.junit.Before; import java.io.IOException; import java.math.BigInteger; -import java.util.ArrayList; -import static com.google.bitcoin.core.TestUtils.createFakeBlock; -import static com.google.bitcoin.core.TestUtils.createFakeTx; +import static com.google.bitcoin.utils.TestUtils.createFakeBlock; +import static com.google.bitcoin.utils.TestUtils.createFakeTx; +/** + * A utility class that you can derive from in your unit tests. TestWithWallet sets up a wallet with a key, + * an in memory block store and a block chain object. It also provides helper methods for filling the wallet + * with money in whatever ways you wish. Note that for simplicity with amounts, this class sets the default + * fee per kilobyte to zero in setUp and back to normal in tearDown. If you are wanting to test your behaviour + * with fees (a good idea!) make sure you set the {@link Wallet.SendRequest#DEFAULT_FEE_PER_KB} value to + * {@link Transaction#REFERENCE_DEFAULT_MIN_TX_FEE} before doing so. + */ public class TestWithWallet { protected static final NetworkParameters params = UnitTestParams.get(); protected ECKey myKey; @@ -38,7 +43,6 @@ public class TestWithWallet { protected BlockChain chain; protected BlockStore blockStore; - @Before public void setUp() throws Exception { BriefLogFormatter.init(); Wallet.SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO; @@ -50,13 +54,12 @@ public class TestWithWallet { chain = new BlockChain(params, wallet, blockStore); } - @After public void tearDown() throws Exception { Wallet.SendRequest.DEFAULT_FEE_PER_KB = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; } protected Transaction sendMoneyToWallet(Wallet wallet, Transaction tx, AbstractBlockChain.NewBlockType type) - throws IOException, ProtocolException, VerificationException { + throws IOException, VerificationException { if (type == null) { // Pending/broadcast tx. if (wallet.isPendingTransactionRelevant(tx)) 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 f530ea5b..ba325525 100644 --- a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java +++ b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java @@ -23,6 +23,7 @@ import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.BlockStore; import com.google.bitcoin.store.MemoryBlockStore; 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; @@ -32,8 +33,8 @@ import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.Date; -import static com.google.bitcoin.core.TestUtils.createFakeBlock; -import static com.google.bitcoin.core.TestUtils.createFakeTx; +import static com.google.bitcoin.utils.TestUtils.createFakeBlock; +import static com.google.bitcoin.utils.TestUtils.createFakeTx; import static org.junit.Assert.*; // Handling of chain splits/reorgs are in ChainSplitTests. 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 8e6c70e7..f7aa1c09 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.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Arrays; -import static com.google.bitcoin.core.TestUtils.createFakeBlock; -import static com.google.bitcoin.core.TestUtils.createFakeTx; +import static com.google.bitcoin.utils.TestUtils.createFakeBlock; +import static com.google.bitcoin.utils.TestUtils.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 f61f2018..0d506487 100644 --- a/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java +++ b/core/src/test/java/com/google/bitcoin/core/MemoryPoolTest.java @@ -18,6 +18,7 @@ package com.google.bitcoin.core; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.utils.BriefLogFormatter; +import com.google.bitcoin.utils.TestUtils; import org.junit.Before; import org.junit.Test; 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 1a998f25..a6167e6e 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java @@ -20,6 +20,7 @@ import com.google.bitcoin.discovery.PeerDiscovery; import com.google.bitcoin.discovery.PeerDiscoveryException; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.store.MemoryBlockStore; +import com.google.bitcoin.utils.TestUtils; import com.google.bitcoin.utils.Threading; import org.junit.After; import org.junit.Before; 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 d1650270..38942f7e 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerTest.java @@ -18,6 +18,7 @@ package com.google.bitcoin.core; import com.google.bitcoin.core.Peer.PeerHandler; import com.google.bitcoin.params.TestNet3Params; +import com.google.bitcoin.utils.TestUtils; import com.google.bitcoin.utils.Threading; import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; @@ -43,7 +44,7 @@ import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import static com.google.bitcoin.core.TestUtils.*; +import static com.google.bitcoin.utils.TestUtils.*; import static org.easymock.EasyMock.*; import static org.junit.Assert.*; 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 0bedacd4..6cb8768f 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -25,6 +25,8 @@ 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.utils.Threading; import com.google.bitcoin.wallet.KeyTimeCoinSelector; import com.google.bitcoin.wallet.WalletFiles; @@ -34,6 +36,7 @@ import com.google.protobuf.ByteString; import org.bitcoinj.wallet.Protos; import org.bitcoinj.wallet.Protos.ScryptParameters; import org.bitcoinj.wallet.Protos.Wallet.EncryptionType; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -49,7 +52,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import static com.google.bitcoin.core.TestUtils.*; +import static com.google.bitcoin.utils.TestUtils.*; import static com.google.bitcoin.core.Utils.*; import static org.junit.Assert.*; @@ -94,7 +97,13 @@ public class WalletTest extends TestWithWallet { myEncryptedAddress2 = myEncryptedKey2.toAddress(params); } - @Test + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test public void basicSpending() throws Exception { basicSpendingCommon(wallet, myAddress, false); } 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 45b0faed..0680b4ae 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,6 +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.utils.Threading; import com.google.bitcoin.wallet.WalletFiles; import com.google.common.util.concurrent.ListenableFuture; @@ -93,7 +94,13 @@ public class ChannelConnectionTest extends TestWithWallet { Threading.warnOnLockCycles(); } - @After + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + + @After public void checkFail() { assertFalse(fail.get()); Threading.throwOnLockCycles(); 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 e163b83b..46e3cb12 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,9 +19,11 @@ 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.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,8 +34,8 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.LinkedBlockingQueue; -import static com.google.bitcoin.core.TestUtils.createFakeTx; -import static com.google.bitcoin.core.TestUtils.makeSolvedTestBlock; +import static com.google.bitcoin.utils.TestUtils.createFakeTx; +import static com.google.bitcoin.utils.TestUtils.makeSolvedTestBlock; import static org.junit.Assert.*; public class PaymentChannelStateTest extends TestWithWallet { @@ -84,7 +86,13 @@ public class PaymentChannelStateTest extends TestWithWallet { }; } - @Test + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test public void stateErrors() throws Exception { PaymentChannelClientState channelState = new PaymentChannelClientState(wallet, myKey, serverKey, Utils.COIN.multiply(BigInteger.TEN), 20); 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 60db8fce..6438ce8e 100644 --- a/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java +++ b/core/src/test/java/com/google/bitcoin/store/WalletProtobufSerializerTest.java @@ -6,6 +6,7 @@ import com.google.bitcoin.core.TransactionConfidence.ConfidenceType; import com.google.bitcoin.params.MainNetParams; import com.google.bitcoin.params.UnitTestParams; 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; @@ -20,7 +21,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.Set; -import static com.google.bitcoin.core.TestUtils.createFakeTx; +import static com.google.bitcoin.utils.TestUtils.createFakeTx; import static org.junit.Assert.*; public class WalletProtobufSerializerTest {