CryptoCoinInfo fix to avoid crashes and to support ETHEREUM value in Type enum

This commit is contained in:
Jorge Rego 2022-06-06 17:00:09 +02:00
parent 286d9843df
commit 236f91a7e0
2 changed files with 44 additions and 3 deletions

View file

@ -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 {

View file

@ -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);
}
}