Skip to content

Commit

Permalink
minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
nanfengpo committed May 9, 2018
1 parent 195b54a commit 8c88f63
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/tron/core/db/VotesStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class VotesStore extends TronStoreWithRevoking<VotesCapsule> {

@Autowired
public VotesStore(@Qualifier("vote") String dbName) {
public VotesStore(@Qualifier("votes") String dbName) {
super(dbName);
}

Expand Down Expand Up @@ -61,7 +61,7 @@ public void put(byte[] key, VotesCapsule item) {
}

/**
* get all accounts.
* get all votes.
*/
public List<VotesCapsule> getAllVotes() {
return dbSource
Expand Down
112 changes: 112 additions & 0 deletions src/test/java/org/tron/core/capsule/VotesCapsuleTest.java
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;
}
}
68 changes: 68 additions & 0 deletions src/test/java/org/tron/core/db/VotesStoreTest.java
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());
}


}

0 comments on commit 8c88f63

Please sign in to comment.