Skip to content

Commit 58ca710

Browse files
authored
Merge pull request #5169 from lxcmyf/release_v4.7.2
fix(test): optimize unit test
2 parents 4fd7b49 + 73a7419 commit 58ca710

9 files changed

Lines changed: 885 additions & 310 deletions

File tree

framework/src/main/java/org/tron/common/client/DatabaseGrpcClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public class DatabaseGrpcClient {
1515

1616
public DatabaseGrpcClient(String host, int port) {
1717
channel = ManagedChannelBuilder.forAddress(host, port)
18-
.usePlaintext(true)
18+
.usePlaintext()
1919
.build();
2020
databaseBlockingStub = DatabaseGrpc.newBlockingStub(channel);
2121
}
2222

2323
public DatabaseGrpcClient(String host) {
2424
channel = ManagedChannelBuilder.forTarget(host)
25-
.usePlaintext(true)
25+
.usePlaintext()
2626
.build();
2727
databaseBlockingStub = DatabaseGrpc.newBlockingStub(channel);
2828
}

framework/src/main/java/org/tron/common/client/WalletGrpcClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public class WalletGrpcClient {
2929

3030
public WalletGrpcClient(String host, int port) {
3131
channel = ManagedChannelBuilder.forAddress(host, port)
32-
.usePlaintext(true)
32+
.usePlaintext()
3333
.build();
3434
walletBlockingStub = WalletGrpc.newBlockingStub(channel);
3535
}
3636

3737
public WalletGrpcClient(String host) {
3838
channel = ManagedChannelBuilder.forTarget(host)
39-
.usePlaintext(true)
39+
.usePlaintext()
4040
.build();
4141
walletBlockingStub = WalletGrpc.newBlockingStub(channel);
4242
}

framework/src/main/java/org/tron/program/SolidityNode.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.extern.slf4j.Slf4j;
88
import org.apache.commons.lang3.BooleanUtils;
99
import org.springframework.context.ApplicationContext;
10+
import org.springframework.util.ObjectUtils;
1011
import org.springframework.util.StringUtils;
1112
import org.tron.common.application.Application;
1213
import org.tron.common.application.ApplicationFactory;
@@ -37,7 +38,7 @@ public class SolidityNode {
3738

3839
private AtomicLong remoteBlockNum = new AtomicLong();
3940

40-
private LinkedBlockingDeque<Block> blockQueue = new LinkedBlockingDeque(100);
41+
private LinkedBlockingDeque<Block> blockQueue = new LinkedBlockingDeque<>(100);
4142

4243
private int exceptionSleepTime = 1000;
4344

@@ -48,7 +49,7 @@ public SolidityNode(Manager dbManager) {
4849
this.chainBaseManager = dbManager.getChainBaseManager();
4950
resolveCompatibilityIssueIfUsingFullNodeDatabase();
5051
ID.set(chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum());
51-
databaseGrpcClient = new DatabaseGrpcClient(Args.getInstance().getTrustNodeAddr());
52+
databaseGrpcClient = new DatabaseGrpcClient(CommonParameter.getInstance().getTrustNodeAddr());
5253
remoteBlockNum.set(getLastSolidityBlockNum());
5354
}
5455

@@ -58,13 +59,13 @@ public SolidityNode(Manager dbManager) {
5859
public static void main(String[] args) {
5960
logger.info("Solidity node is running.");
6061
Args.setParam(args, Constant.TESTNET_CONF);
61-
CommonParameter parameter = Args.getInstance();
62+
CommonParameter parameter = CommonParameter.getInstance();
6263

6364
logger.info("index switch is {}",
6465
BooleanUtils.toStringOnOff(BooleanUtils
6566
.toBoolean(parameter.getStorage().getIndexSwitch())));
6667

67-
if (StringUtils.isEmpty(parameter.getTrustNodeAddr())) {
68+
if (ObjectUtils.isEmpty(parameter.getTrustNodeAddr())) {
6869
logger.error("Trust node is not set.");
6970
return;
7071
}
@@ -102,13 +103,13 @@ public static void main(String[] args) {
102103

103104
private void start() {
104105
try {
105-
new Thread(() -> getBlock()).start();
106-
new Thread(() -> processBlock()).start();
106+
new Thread(this::getBlock).start();
107+
new Thread(this::processBlock).start();
107108
logger.info("Success to start solid node, ID: {}, remoteBlockNum: {}.", ID.get(),
108109
remoteBlockNum);
109110
} catch (Exception e) {
110-
logger
111-
.error("Failed to start solid node, address: {}.", Args.getInstance().getTrustNodeAddr());
111+
logger.error("Failed to start solid node, address: {}.",
112+
CommonParameter.getInstance().getTrustNodeAddr());
112113
System.exit(0);
113114
}
114115
}

framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void tearDown() {
8181
}
8282

8383
@Test
84-
public void testDoPost() throws IOException {
84+
public void doPostTest() throws IOException {
8585
URLStreamHandlerFactory urlStreamHandlerFactory = mock(URLStreamHandlerFactory.class);
8686
URL.setURLStreamHandlerFactory(urlStreamHandlerFactory);
8787

@@ -139,6 +139,7 @@ public void testDoPost() throws IOException {
139139
while ((line = in.readLine()) != null) {
140140
result.append(line).append("\n");
141141
}
142+
Assert.assertNotNull(result);
142143
in.close();
143144
writer.flush();
144145
FileInputStream fileInputStream = new FileInputStream("temp.txt");

framework/src/test/java/org/tron/core/services/http/HttpServletTest.java

Lines changed: 531 additions & 0 deletions
Large diffs are not rendered by default.

framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.extern.slf4j.Slf4j;
66
import org.apache.http.HttpResponse;
77
import org.bouncycastle.util.encoders.Hex;
8+
import org.junit.Assert;
89
import org.junit.Before;
910
import org.junit.BeforeClass;
1011
import org.junit.Test;
@@ -80,9 +81,12 @@ public void testNormalCall() {
8081
parameter.addProperty("owner_address", ByteArray.toHexString(ownerAddr));
8182
parameter.addProperty("contract_address", ByteArray.toHexString(contractAddr));
8283
parameter.addProperty("function_selector", "test()");
83-
invokeToLocal("triggersmartcontract", parameter);
84-
invokeToLocal("triggerconstantcontract", parameter);
85-
invokeToLocal("estimateenergy", parameter);
84+
HttpResponse triggersmartcontract1 = invokeToLocal("triggersmartcontract", parameter);
85+
HttpResponse triggersmartcontract2 = invokeToLocal("triggerconstantcontract", parameter);
86+
HttpResponse triggersmartcontract3 = invokeToLocal("estimateenergy", parameter);
87+
Assert.assertNotNull(triggersmartcontract1);
88+
Assert.assertNotNull(triggersmartcontract2);
89+
Assert.assertNotNull(triggersmartcontract3);
8690
}
8791

8892
public static HttpResponse invokeToLocal(

framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void init() {
5959
*/
6060

6161
@Before
62-
public void setUp() throws InterruptedException {
62+
public void setUp() {
6363
getTransactionByIdSolidityServlet = new GetTransactionByIdSolidityServlet();
6464
this.request = mock(HttpServletRequest.class);
6565
this.response = mock(HttpServletResponse.class);
@@ -96,7 +96,7 @@ public void doPostTest() throws IOException {
9696
httpUrlConnection.setDoOutput(true);
9797
String postData = "{\"value\": \"309b6fa3d01353e46f57dd8a8f27611f98e392b50d035cef21"
9898
+ "3f2c55225a8bd2\"}";
99-
httpUrlConnection.setRequestProperty("Content-Length", "" + postData.length());
99+
httpUrlConnection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
100100

101101
when(httpUrlConnection.getOutputStream()).thenReturn(outContent);
102102
OutputStreamWriter out = new OutputStreamWriter(httpUrlConnection.getOutputStream(),
@@ -121,14 +121,15 @@ public void doPostTest() throws IOException {
121121
while ((line = in.readLine()) != null) {
122122
result.append(line).append("\n");
123123
}
124+
Assert.assertNotNull(result);
124125
in.close();
125126
writer.flush();
126127
FileInputStream fileInputStream = new FileInputStream("temp.txt");
127128
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
128129
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
129130

130-
StringBuffer sb = new StringBuffer();
131-
String text = null;
131+
StringBuilder sb = new StringBuilder();
132+
String text;
132133
while ((text = bufferedReader.readLine()) != null) {
133134
sb.append(text);
134135
}
@@ -150,7 +151,7 @@ public void doGetTest() throws IOException {
150151
httpUrlConnection.setDoOutput(true);
151152
String postData = "{\"value\": \"309b6fa3d01353e46f57dd8a8f27611f98e392b50d035cef21"
152153
+ "3f2c55225a8bd2\"}";
153-
httpUrlConnection.setRequestProperty("Content-Length", "" + postData.length());
154+
httpUrlConnection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
154155

155156
when(httpUrlConnection.getOutputStream()).thenReturn(outContent);
156157
OutputStreamWriter out = new OutputStreamWriter(httpUrlConnection.getOutputStream(),
@@ -175,14 +176,15 @@ public void doGetTest() throws IOException {
175176
while ((line = in.readLine()) != null) {
176177
result.append(line).append("\n");
177178
}
179+
Assert.assertNotNull(result);
178180
in.close();
179181
writer.flush();
180182
FileInputStream fileInputStream = new FileInputStream("temp.txt");
181183
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
182184
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
183185

184-
StringBuffer sb = new StringBuffer();
185-
String text = null;
186+
StringBuilder sb = new StringBuilder();
187+
String text;
186188
while ((text = bufferedReader.readLine()) != null) {
187189
sb.append(text);
188190
}

0 commit comments

Comments
 (0)