forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/test/java/org/tron/core/capsule/VotesCapsuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.tron.core.capsule; | ||
|
||
import com.google.protobuf.ByteString; | ||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.tron.common.utils.FileUtil; | ||
import org.tron.core.Constant; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.protos.Protocol.Vote; | ||
import org.tron.protos.Protocol.AccountType; | ||
|
||
public class AccountCapsuleTest { | ||
|
||
private static String dbPath = "output_accountCapsule_test"; | ||
static AccountCapsule accountCapsuleTest; | ||
static AccountCapsule accountCapsule; | ||
|
||
@BeforeClass | ||
public static void init() { | ||
Args.setParam(new String[]{"-d", dbPath, "-w"}, | ||
Constant.TEST_CONF); | ||
ByteString accountName = ByteString.copyFrom(AccountCapsuleTest.randomBytes(16)); | ||
ByteString address = ByteString.copyFrom(AccountCapsuleTest.randomBytes(32)); | ||
AccountType accountType = AccountType.forNumber(1); | ||
accountCapsuleTest = new AccountCapsule(accountName, address, accountType); | ||
byte[] accountByte = accountCapsuleTest.getData(); | ||
accountCapsule = new AccountCapsule(accountByte); | ||
accountCapsuleTest.setBalance(1111L); | ||
} | ||
|
||
@AfterClass | ||
public static void removeDb() { | ||
Args.clearParam(); | ||
FileUtil.deleteDir(new File(dbPath)); | ||
} | ||
|
||
@Test | ||
public void getDataTest() { | ||
//test AccountCapsule onstructed function | ||
Assert.assertEquals(accountCapsule.getInstance().getAccountName(), | ||
accountCapsuleTest.getInstance().getAccountName()); | ||
Assert.assertEquals(accountCapsule.getInstance().getType(), | ||
accountCapsuleTest.getInstance().getType()); | ||
Assert.assertEquals(1111, accountCapsuleTest.getBalance()); | ||
} | ||
|
||
@Test | ||
public void addVotesTest() { | ||
//test addVote and getVotesList function | ||
ByteString voteAddress = ByteString.copyFrom(AccountCapsuleTest.randomBytes(32)); | ||
long voteAdd = 10L; | ||
accountCapsuleTest.addVotes(voteAddress, voteAdd); | ||
List<Vote> votesList = accountCapsuleTest.getVotesList(); | ||
for (Vote vote : | ||
votesList) { | ||
Assert.assertEquals(voteAddress, vote.getVoteAddress()); | ||
Assert.assertEquals(voteAdd, vote.getVoteCount()); | ||
} | ||
} | ||
|
||
@Test | ||
public void AssetAmountTest() { | ||
//test AssetAmount ,addAsset and reduceAssetAmount function | ||
|
||
String nameAdd = "TokenX"; | ||
long amountAdd = 222L; | ||
boolean addBoolean = accountCapsuleTest | ||
.addAssetAmount(ByteString.copyFromUtf8(nameAdd), amountAdd); | ||
|
||
Assert.assertTrue(addBoolean); | ||
|
||
Map<String, Long> assetMap = accountCapsuleTest.getAssetMap(); | ||
for (Map.Entry<String, Long> entry : assetMap.entrySet()) { | ||
Assert.assertEquals(nameAdd, entry.getKey()); | ||
Assert.assertEquals(amountAdd, entry.getValue().longValue()); | ||
} | ||
long amountReduce = 22L; | ||
|
||
boolean reduceBoolean = accountCapsuleTest | ||
.reduceAssetAmount(ByteString.copyFromUtf8("TokenX"), amountReduce); | ||
Assert.assertTrue(reduceBoolean); | ||
|
||
Map<String, Long> assetMapAfter = accountCapsuleTest.getAssetMap(); | ||
for (Map.Entry<String, Long> entry : assetMapAfter.entrySet()) { | ||
Assert.assertEquals(nameAdd, entry.getKey()); | ||
Assert.assertEquals(amountAdd - amountReduce, entry.getValue().longValue()); | ||
} | ||
String key = nameAdd; | ||
long value = 11L; | ||
boolean addAsssetBoolean = accountCapsuleTest.addAsset(key, value); | ||
Assert.assertFalse(addAsssetBoolean); | ||
|
||
String keyName = "TokenTest"; | ||
long amountValue = 33L; | ||
boolean addAsssetTrue = accountCapsuleTest.addAsset(keyName, amountValue); | ||
Assert.assertTrue(addAsssetTrue); | ||
} | ||
|
||
|
||
public static byte[] randomBytes(int length) { | ||
//generate the random number | ||
byte[] result = new byte[length]; | ||
new Random().nextBytes(result); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.tron.core.db; | ||
|
||
import com.google.protobuf.ByteString; | ||
import java.io.File; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.tron.common.utils.FileUtil; | ||
import org.tron.core.Constant; | ||
import org.tron.core.capsule.WitnessCapsule; | ||
import org.tron.core.config.DefaultConfig; | ||
import org.tron.core.config.args.Args; | ||
|
||
@Slf4j | ||
public class WitnessStoreTest { | ||
|
||
private static final String dbPath = "output-witnessStore-test"; | ||
private static AnnotationConfigApplicationContext context; | ||
WitnessStore witnessStore; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF); | ||
context = new AnnotationConfigApplicationContext(DefaultConfig.class); | ||
} | ||
|
||
@Before | ||
public void initDb() { | ||
this.witnessStore = context.getBean(WitnessStore.class); | ||
} | ||
|
||
@AfterClass | ||
public static void destroy() { | ||
Args.clearParam(); | ||
FileUtil.deleteDir(new File(dbPath)); | ||
WitnessStore.destory(); | ||
context.destroy(); | ||
} | ||
|
||
@Test | ||
public void putAndGetWitness() { | ||
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFromUtf8("100000000x"), 100L, | ||
""); | ||
|
||
this.witnessStore.put(witnessCapsule.getAddress().toByteArray(), witnessCapsule); | ||
WitnessCapsule witnessSource = this.witnessStore | ||
.get(ByteString.copyFromUtf8("100000000x").toByteArray()); | ||
Assert.assertEquals(witnessCapsule.getAddress(), witnessSource.getAddress()); | ||
Assert.assertEquals(witnessCapsule.getVoteCount(), witnessSource.getVoteCount()); | ||
|
||
Assert.assertEquals(ByteString.copyFromUtf8("100000000x"), witnessSource.getAddress()); | ||
Assert.assertEquals(100L, witnessSource.getVoteCount()); | ||
|
||
witnessCapsule = new WitnessCapsule(ByteString.copyFromUtf8(""), 100L, ""); | ||
|
||
this.witnessStore.put(witnessCapsule.getAddress().toByteArray(), witnessCapsule); | ||
witnessSource = this.witnessStore.get(ByteString.copyFromUtf8("").toByteArray()); | ||
Assert.assertEquals(witnessCapsule.getAddress(), witnessSource.getAddress()); | ||
Assert.assertEquals(witnessCapsule.getVoteCount(), witnessSource.getVoteCount()); | ||
|
||
Assert.assertEquals(ByteString.copyFromUtf8(""), witnessSource.getAddress()); | ||
Assert.assertEquals(100L, witnessSource.getVoteCount()); | ||
} | ||
|
||
|
||
} |