Skip to content

Commit

Permalink
improve unit test TransferAssetActuatorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
xiechang33 committed May 9, 2018
1 parent b09475a commit 81c42c8
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions src/test/java/org/tron/core/actuator/TransferAssetActuatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public class TransferAssetActuatorTest {
private static final String NOT_EXIT_ADDRESS =
Wallet.getAddressPreFixString() + "B56446E617E924805E4D6CA021D341FEF6E2013B";
private static final long OWNER_ASSET_BALANCE = 99999;
private static final String ownerAsset_ADDRESS =
Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049010";
private static final String ownerASSET_NAME = "trxtest";
private static final long OWNER_ASSET_Test_BALANCE = 99999;

private static final String OWNER_ADDRESS_INVALIATE = "cccc";
private static final String TO_ADDRESS_INVALIATE = "dddd";
private static final long TOTAL_SUPPLY = 10L;
private static final int TRX_NUM = 10;
private static final int NUM = 1;
Expand Down Expand Up @@ -391,6 +397,149 @@ public void addOverflowTest() {
}
}

@Test
/**
* transfer asset to yourself,result is error
*/
public void transferToYouself() {
TransferAssetActuator actuator = new TransferAssetActuator(
getContract(100L, OWNER_ADDRESS, OWNER_ADDRESS), dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();
try {
actuator.validate();
actuator.execute(ret);
fail("Cannot transfer asset to yourself.");

} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Cannot transfer asset to yourself.", e.getMessage());
AccountCapsule owner = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule toAccount = dbManager.getAccountStore()
.get(ByteArray.fromHexString(TO_ADDRESS));
Assert.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE);
Assert.assertTrue(isNullOrZero(toAccount.getAssetMap().get(ASSET_NAME)));
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

}

@Test
/**
* Invalidate ownerAddress,result is error
*/
public void iniviateOwnerAddress() {
TransferAssetActuator actuator = new TransferAssetActuator(
getContract(100L, OWNER_ADDRESS_INVALIATE, TO_ADDRESS), dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();
try {
actuator.validate();
actuator.execute(ret);
fail("Invalidate ownerAddress");

} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Invalidate ownerAddress", e.getMessage());
AccountCapsule owner = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule toAccount = dbManager.getAccountStore()
.get(ByteArray.fromHexString(TO_ADDRESS));
Assert.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE);
Assert.assertTrue(isNullOrZero(toAccount.getAssetMap().get(ASSET_NAME)));

} catch (ContractExeException e) {
Assert.assertTrue(e instanceof ContractExeException);
}

}

@Test
/**
* Invalidate ToAddress,result is error
*/
public void iniviateToAddress() {
TransferAssetActuator actuator = new TransferAssetActuator(
getContract(100L, OWNER_ADDRESS, TO_ADDRESS_INVALIATE), dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();
try {
actuator.validate();
actuator.execute(ret);
fail("Invalidate toAddress");

} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("Invalidate toAddress", e.getMessage());
AccountCapsule owner = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule toAccount = dbManager.getAccountStore()
.get(ByteArray.fromHexString(TO_ADDRESS));
Assert.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE);
Assert.assertTrue(isNullOrZero(toAccount.getAssetMap().get(ASSET_NAME)));
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

}

@Test
/**
* Account do not have this asset,Transfer this Asset result is failed
*/
public void ownerNoThisAsset() {
AccountCapsule ownerAssetCapsule =
new AccountCapsule(
ByteString.copyFrom(ByteArray.fromHexString(ownerAsset_ADDRESS)),
ByteString.copyFromUtf8("ownerAsset"),
AccountType.AssetIssue);
ownerAssetCapsule.addAsset(ownerASSET_NAME, OWNER_ASSET_Test_BALANCE);
AssetIssueContract assetIssueTestContract =
AssetIssueContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ownerAsset_ADDRESS)))
.setName(ByteString.copyFrom(ByteArray.fromString(ownerASSET_NAME)))
.setTotalSupply(TOTAL_SUPPLY)
.setTrxNum(TRX_NUM)
.setNum(NUM)
.setStartTime(START_TIME)
.setEndTime(END_TIME)
.setDecayRatio(DECAY_RATIO)
.setVoteScore(VOTE_SCORE)
.setDescription(ByteString.copyFrom(ByteArray.fromString(DESCRIPTION)))
.setUrl(ByteString.copyFrom(ByteArray.fromString(URL)))
.build();
AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueTestContract);
dbManager.getAccountStore()
.put(ownerAssetCapsule.getAddress().toByteArray(), ownerAssetCapsule);
dbManager
.getAssetIssueStore()
.put(assetIssueCapsule.getName().toByteArray(), assetIssueCapsule);
TransferAssetActuator actuator = new TransferAssetActuator(getContract(1, ownerASSET_NAME),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();
try {
actuator.validate();
actuator.execute(ret);
Assert.assertTrue(false);
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertTrue("assetBalance must greater than 0.".equals(e.getMessage()));
AccountCapsule owner =
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule toAccount =
dbManager.getAccountStore().get(ByteArray.fromHexString(TO_ADDRESS));
Assert.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), OWNER_ASSET_BALANCE);
Assert.assertTrue(isNullOrZero(toAccount.getAssetMap().get(ASSET_NAME)));
AccountCapsule ownerAsset =
dbManager.getAccountStore().get(ByteArray.fromHexString(ownerAsset_ADDRESS));
Assert.assertEquals(ownerAsset.getAssetMap().get(ownerASSET_NAME).longValue(),
OWNER_ASSET_Test_BALANCE);

} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}

}

private boolean isNullOrZero(Long value) {
if (null == value || value == 0) {
return true;
Expand Down

0 comments on commit 81c42c8

Please sign in to comment.