From 236f91a7e0b2e21a347293cb8a53c22d5f229479 Mon Sep 17 00:00:00 2001 From: Jorge Rego Date: Mon, 6 Jun 2022 17:00:09 +0200 Subject: [PATCH] CryptoCoinInfo fix to avoid crashes and to support ETHEREUM value in Type enum --- .../hummingbird/registry/CryptoCoinInfo.java | 22 +++++++++++++--- .../registry/CryptoCoinInfoTest.java | 25 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java index a78591b..1ce35be 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfo.java @@ -17,12 +17,12 @@ public class CryptoCoinInfo extends RegistryItem { } public CryptoCoinInfo(Type type, Network network) { - this.type = (type != null ? type.ordinal() : null); + this.type = (type != null ? type.typeValue : null); this.network = (network != null ? network.ordinal() : null); } public Type getType() { - return type == null ? Type.BITCOIN : Type.values()[type]; + return type == null ? Type.BITCOIN : Type.getTypeFromValue(type); } public Network getNetwork() { @@ -65,7 +65,23 @@ public class CryptoCoinInfo extends RegistryItem { } public enum Type { - BITCOIN + BITCOIN(0), ETHEREUM(60); + + Integer typeValue; + + Type(Integer typeValue) { + this.typeValue = typeValue; + } + + static Type getTypeFromValue(int value) { + for (int i = 0; i < Type.values().length; i++) { + Type current = Type.values()[i]; + if(value == current.typeValue) { + return current; + } + } + return null; + } } public enum Network { diff --git a/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java new file mode 100644 index 0000000..a89331d --- /dev/null +++ b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoCoinInfoTest.java @@ -0,0 +1,25 @@ +package com.sparrowwallet.hummingbird.registry; + +import org.junit.Assert; +import org.junit.Test; + +public class CryptoCoinInfoTest { + + @Test + public void testBitcoinCoinInfo() { + CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.MAINNET); + Assert.assertSame(coinInfo.getType().typeValue, CryptoCoinInfo.Type.BITCOIN.typeValue); + } + + @Test + public void testEthereumCoinInfo() { + CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.MAINNET); + Assert.assertSame(coinInfo.getType().typeValue, CryptoCoinInfo.Type.ETHEREUM.typeValue); + } + + @Test + public void testNullTypeCoinInfo() { + CryptoCoinInfo coinInfo = new CryptoCoinInfo(null, CryptoCoinInfo.Network.MAINNET); + Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.BITCOIN); + } +}