Merge pull request #5 from proxyco/fix/crypto-coin-crash

CryptoCoinInfo crash fix for Ethereum
This commit is contained in:
craigraw 2022-06-07 09:23:14 +02:00 committed by GitHub
commit 8f6cb0a662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 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,37 @@
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 testBitcoinTypeCoinInfo() {
CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.BITCOIN, CryptoCoinInfo.Network.MAINNET);
Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.BITCOIN);
}
@Test
public void testEthereumTypeCoinInfo() {
CryptoCoinInfo coinInfo = new CryptoCoinInfo(CryptoCoinInfo.Type.ETHEREUM, CryptoCoinInfo.Network.MAINNET);
Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.ETHEREUM);
}
@Test
public void testNullTypeCoinInfo() {
CryptoCoinInfo coinInfo = new CryptoCoinInfo(null, CryptoCoinInfo.Network.MAINNET);
Assert.assertSame(coinInfo.getType(), CryptoCoinInfo.Type.BITCOIN);
}
}