Skip to content

Commit 39d2690

Browse files
lxcmyfzhang0125
authored andcommitted
fix(unitTest): replace context with spring test config
1 parent 0b05977 commit 39d2690

64 files changed

Lines changed: 913 additions & 2382 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Map;
1717
import java.util.Objects;
1818
import java.util.concurrent.ExecutionException;
19+
import java.util.concurrent.ExecutorService;
1920
import java.util.concurrent.Executors;
2021
import java.util.concurrent.Future;
2122
import java.util.concurrent.ScheduledExecutorService;
@@ -122,6 +123,7 @@ public void close() {
122123
exitThread.interrupt();
123124
// help GC
124125
exitThread = null;
126+
flushServices.values().forEach(ExecutorService::shutdown);
125127
} catch (Exception e) {
126128
logger.warn("exitThread interrupt error", e);
127129
}

framework/src/test/java/org/tron/common/BaseTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import javax.annotation.Resource;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.apache.commons.lang3.StringUtils;
78
import org.junit.AfterClass;
89
import org.junit.runner.RunWith;
910
import org.springframework.test.annotation.DirtiesContext;
@@ -28,7 +29,6 @@
2829
public abstract class BaseTest {
2930

3031
protected static String dbPath;
31-
3232
@Resource
3333
protected Manager dbManager;
3434
@Resource
@@ -37,7 +37,7 @@ public abstract class BaseTest {
3737
@AfterClass
3838
public static void destroy() {
3939
Args.clearParam();
40-
if (FileUtil.deleteDir(new File(dbPath))) {
40+
if (StringUtils.isNotEmpty(dbPath) && FileUtil.deleteDir(new File(dbPath))) {
4141
logger.info("Release resources successful.");
4242
} else {
4343
logger.info("Release resources failure.");
@@ -81,5 +81,4 @@ public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] priva
8181

8282
return block.toBuilder().setBlockHeader(blockHeader).build();
8383
}
84-
8584
}

framework/src/test/java/org/tron/common/runtime/InheritanceTest.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package org.tron.common.runtime;
22

3-
import java.io.File;
43
import lombok.extern.slf4j.Slf4j;
54
import org.bouncycastle.util.encoders.Hex;
6-
import org.junit.AfterClass;
7-
import org.junit.BeforeClass;
5+
import org.junit.Before;
86
import org.junit.Test;
97
import org.testng.Assert;
10-
import org.tron.common.application.Application;
11-
import org.tron.common.application.ApplicationFactory;
12-
import org.tron.common.application.TronApplicationContext;
13-
import org.tron.common.utils.FileUtil;
8+
import org.tron.common.BaseTest;
149
import org.tron.core.Constant;
1510
import org.tron.core.Wallet;
16-
import org.tron.core.config.DefaultConfig;
1711
import org.tron.core.config.args.Args;
18-
import org.tron.core.db.Manager;
1912
import org.tron.core.exception.ContractExeException;
2013
import org.tron.core.exception.ContractValidateException;
2114
import org.tron.core.exception.ReceiptCheckErrException;
@@ -25,54 +18,31 @@
2518
import org.tron.protos.Protocol.AccountType;
2619

2720
@Slf4j
28-
public class InheritanceTest {
21+
public class InheritanceTest extends BaseTest {
2922

30-
private static final String dbPath = "output_InheritanceTest";
3123
private static final String OWNER_ADDRESS;
32-
private static Runtime runtime;
33-
private static Manager dbManager;
34-
private static TronApplicationContext context;
35-
private static Application appT;
36-
private static RepositoryImpl repository;
24+
private RepositoryImpl repository;
3725

3826
static {
27+
dbPath = "output_InheritanceTest";
3928
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, Constant.TEST_CONF);
40-
context = new TronApplicationContext(DefaultConfig.class);
41-
appT = ApplicationFactory.create(context);
4229
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
4330
}
4431

4532
/**
4633
* Init data.
4734
*/
48-
@BeforeClass
49-
public static void init() {
50-
dbManager = context.getBean(Manager.class);
35+
@Before
36+
public void init() {
5137
repository = RepositoryImpl.createRoot(StoreFactory.getInstance());
5238
repository.createAccount(Hex.decode(OWNER_ADDRESS), AccountType.Normal);
5339
repository.addBalance(Hex.decode(OWNER_ADDRESS), 100000000);
5440
}
5541

56-
/**
57-
* Release resources.
58-
*/
59-
@AfterClass
60-
public static void destroy() {
61-
Args.clearParam();
62-
context.destroy();
63-
if (FileUtil.deleteDir(new File(dbPath))) {
64-
logger.info("Release resources successful.");
65-
} else {
66-
logger.info("Release resources failure.");
67-
}
68-
}
69-
7042
/**
7143
* pragma solidity ^0.4.19;
72-
*
7344
* contract foo { uint256 public id=10; function getNumber() returns (uint256){return 100;}
7445
* function getName() returns (string){ return "foo"; } }
75-
*
7646
* contract bar is foo { function getName() returns (string) { return "bar"; } function getId()
7747
* returns(uint256){return id;} }
7848
*/
@@ -119,7 +89,7 @@ public void inheritanceTest()
11989

12090
/* ========================== CALL getName() return child value ============================= */
12191
byte[] triggerData1 = TvmTestUtils.parseAbi("getName()", "");
122-
runtime = TvmTestUtils
92+
Runtime runtime = TvmTestUtils
12393
.triggerContractWholeProcessReturnContractAddress(callerAddress, contractAddress,
12494
triggerData1, 0, 1000000, repository, null);
12595

framework/src/test/java/org/tron/common/runtime/InternalTransactionComplexTest.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package org.tron.common.runtime;
22

3-
import java.io.File;
43
import lombok.extern.slf4j.Slf4j;
54
import org.bouncycastle.util.encoders.Hex;
6-
import org.junit.AfterClass;
7-
import org.junit.BeforeClass;
5+
import org.junit.Before;
86
import org.junit.Test;
97
import org.testng.Assert;
10-
import org.tron.common.application.Application;
11-
import org.tron.common.application.ApplicationFactory;
12-
import org.tron.common.application.TronApplicationContext;
8+
import org.tron.common.BaseTest;
139
import org.tron.common.runtime.vm.DataWord;
14-
import org.tron.common.utils.FileUtil;
1510
import org.tron.core.Constant;
1611
import org.tron.core.Wallet;
17-
import org.tron.core.config.DefaultConfig;
1812
import org.tron.core.config.args.Args;
19-
import org.tron.core.db.Manager;
2013
import org.tron.core.exception.ContractExeException;
2114
import org.tron.core.exception.ContractValidateException;
2215
import org.tron.core.exception.ReceiptCheckErrException;
@@ -26,55 +19,33 @@
2619
import org.tron.protos.Protocol.AccountType;
2720

2821
@Slf4j
29-
public class InternalTransactionComplexTest {
22+
public class InternalTransactionComplexTest extends BaseTest {
3023

31-
private static final String dbPath = "output_InternalTransactionComplexTest";
3224
private static final String OWNER_ADDRESS;
3325
private static Runtime runtime;
34-
private static Manager dbManager;
35-
private static TronApplicationContext context;
36-
private static Application appT;
3726
private static RepositoryImpl repository;
3827

3928
static {
29+
dbPath = "output_InternalTransactionComplexTest";
4030
Args.setParam(new String[]{"--output-directory", dbPath, "--debug", "--support-constant"},
4131
Constant.TEST_CONF);
42-
context = new TronApplicationContext(DefaultConfig.class);
43-
appT = ApplicationFactory.create(context);
4432
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
4533
}
4634

4735
/**
4836
* Init data.
4937
*/
50-
@BeforeClass
51-
public static void init() {
52-
dbManager = context.getBean(Manager.class);
38+
@Before
39+
public void init() {
5340
repository = RepositoryImpl.createRoot(StoreFactory.getInstance());
5441
repository.createAccount(Hex.decode(OWNER_ADDRESS), AccountType.Normal);
5542
repository.addBalance(Hex.decode(OWNER_ADDRESS), 100000000);
5643
}
5744

58-
/**
59-
* Release resources.
60-
*/
61-
@AfterClass
62-
public static void destroy() {
63-
Args.clearParam();
64-
context.destroy();
65-
if (FileUtil.deleteDir(new File(dbPath))) {
66-
logger.info("Release resources successful.");
67-
} else {
68-
logger.info("Release resources failure.");
69-
}
70-
}
71-
7245
/**
7346
* pragma solidity 0.4.24;
74-
*
7547
* // this is to test wither the TVM is returning vars from one contract calling another //
7648
* contract's functions.
77-
*
7849
* contract callerContract { // lets set up our instance of the new contract calledContract
7950
* CALLED_INSTANCE; // lets set the contract instance address in the constructor
8051
* constructor(address _addr) public { CALLED_INSTANCE = calledContract(_addr); } // lets create a
@@ -85,7 +56,6 @@ public static void destroy() {
8556
* in to temp vars (bool _bool, uint256 _uint, bytes32 _bytes32) = CALLED_INSTANCE.testReturns();
8657
* // lets write those temp vars to state testCallbackReturns_.someBool = _bool;
8758
* testCallbackReturns_.someUint = _uint; testCallbackReturns_.someBytes32 = _bytes32; } }
88-
*
8959
* contract calledContract { function testReturns() external pure returns(bool, uint256, bytes32)
9060
* { return(true, 314159, 0x123456); } }
9161
*/

0 commit comments

Comments
 (0)