Skip to content

Commit fc3e80e

Browse files
halibobo1205claude
andcommitted
fix: strengthen all servlet test verify matchers and enforce checkstyle
Replace weak any(ContractClass) matchers with argThat() field-level validation across all 30 servlet tests, ensuring JSON deserialization correctness is verified (addresses, amounts, IDs, etc.). Add test task dependency on checkstyle. Fix import ordering in BaseHttpTest and remove unused assertFalse/assertTrue imports from 29 test files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 44d011e commit fc3e80e

31 files changed

Lines changed: 159 additions & 86 deletions

framework/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ tasks.register('lint', Checkstyle) {
9292

9393
tasks.matching { it instanceof Test }.all {
9494
testLogging.events = ["failed", "passed", "skipped"]
95+
dependsOn tasks.matching { it instanceof Checkstyle }
9596
}
9697

9798
if (project.hasProperty("mainClass")) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -48,7 +47,13 @@ public void testAccountPermissionUpdate() throws Exception {
4847
MockHttpServletResponse response = newResponse();
4948
servlet.doPost(request, response);
5049
verify(wallet).createTransactionCapsule(
51-
any(AccountContract.AccountPermissionUpdateContract.class),
50+
argThat(c -> c instanceof AccountContract.AccountPermissionUpdateContract
51+
&& addressEquals(((AccountContract.AccountPermissionUpdateContract) c)
52+
.getOwnerAddress(), ownerAddr)
53+
&& ((AccountContract.AccountPermissionUpdateContract) c)
54+
.getOwner().getThreshold() == 1
55+
&& ((AccountContract.AccountPermissionUpdateContract) c)
56+
.getActivesCount() == 1),
5257
eq(Protocol.Transaction.Contract.ContractType.AccountPermissionUpdateContract));
5358
assertTransactionResponse(response);
5459
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.google.protobuf.ByteString;
88
import java.lang.reflect.Field;
99
import javax.servlet.http.HttpServlet;
10-
import org.tron.common.utils.ByteArray;
1110
import org.junit.After;
1211
import org.junit.AfterClass;
1312
import org.junit.Before;
@@ -17,6 +16,7 @@
1716
import org.springframework.mock.web.MockHttpServletRequest;
1817
import org.springframework.mock.web.MockHttpServletResponse;
1918
import org.tron.common.TestConstants;
19+
import org.tron.common.utils.ByteArray;
2020
import org.tron.core.Wallet;
2121
import org.tron.core.config.args.Args;
2222
import org.tron.protos.Protocol.Transaction;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -39,7 +38,9 @@ public void testCancelAllUnfreezeV2() throws Exception {
3938
MockHttpServletResponse response = newResponse();
4039
servlet.doPost(request, response);
4140
verify(wallet).createTransactionCapsule(
42-
any(BalanceContract.CancelAllUnfreezeV2Contract.class),
41+
argThat(c -> c instanceof BalanceContract.CancelAllUnfreezeV2Contract
42+
&& addressEquals(((BalanceContract.CancelAllUnfreezeV2Contract) c)
43+
.getOwnerAddress(), ownerAddr)),
4344
eq(Protocol.Transaction.Contract.ContractType.CancelAllUnfreezeV2Contract));
4445
assertTransactionResponse(response);
4546
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -45,7 +44,12 @@ public void testDelegateResource() throws Exception {
4544
MockHttpServletResponse response = newResponse();
4645
servlet.doPost(request, response);
4746
verify(wallet).createTransactionCapsule(
48-
any(BalanceContract.DelegateResourceContract.class),
47+
argThat(c -> c instanceof BalanceContract.DelegateResourceContract
48+
&& addressEquals(((BalanceContract.DelegateResourceContract) c)
49+
.getOwnerAddress(), ownerAddr)
50+
&& addressEquals(((BalanceContract.DelegateResourceContract) c)
51+
.getReceiverAddress(), receiverAddr)
52+
&& ((BalanceContract.DelegateResourceContract) c).getBalance() == 1000000),
4953
eq(Protocol.Transaction.Contract.ContractType.DelegateResourceContract));
5054
assertTransactionResponse(response);
5155
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -44,7 +43,12 @@ public void testExchangeCreate() throws Exception {
4443
MockHttpServletResponse response = newResponse();
4544
servlet.doPost(request, response);
4645
verify(wallet).createTransactionCapsule(
47-
any(ExchangeCreateContract.class), eq(ContractType.ExchangeCreateContract));
46+
argThat(c -> c instanceof ExchangeCreateContract
47+
&& addressEquals(((ExchangeCreateContract) c)
48+
.getOwnerAddress(), ownerAddr)
49+
&& ((ExchangeCreateContract) c).getFirstTokenBalance() == 100
50+
&& ((ExchangeCreateContract) c).getSecondTokenBalance() == 200),
51+
eq(ContractType.ExchangeCreateContract));
4852
assertTransactionResponse(response);
4953
}
5054
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -44,7 +43,11 @@ public void testExchangeInject() throws Exception {
4443
MockHttpServletResponse response = newResponse();
4544
servlet.doPost(request, response);
4645
verify(wallet).createTransactionCapsule(
47-
any(ExchangeContract.ExchangeInjectContract.class),
46+
argThat(c -> c instanceof ExchangeContract.ExchangeInjectContract
47+
&& addressEquals(((ExchangeContract.ExchangeInjectContract) c)
48+
.getOwnerAddress(), ownerAddr)
49+
&& ((ExchangeContract.ExchangeInjectContract) c).getExchangeId() == 1
50+
&& ((ExchangeContract.ExchangeInjectContract) c).getQuant() == 100),
4851
eq(Protocol.Transaction.Contract.ContractType.ExchangeInjectContract));
4952
assertTransactionResponse(response);
5053
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -45,7 +44,12 @@ public void testExchangeTransaction() throws Exception {
4544
MockHttpServletResponse response = newResponse();
4645
servlet.doPost(request, response);
4746
verify(wallet).createTransactionCapsule(
48-
any(ExchangeContract.ExchangeTransactionContract.class),
47+
argThat(c -> c instanceof ExchangeContract.ExchangeTransactionContract
48+
&& addressEquals(((ExchangeContract.ExchangeTransactionContract) c)
49+
.getOwnerAddress(), ownerAddr)
50+
&& ((ExchangeContract.ExchangeTransactionContract) c).getExchangeId() == 1
51+
&& ((ExchangeContract.ExchangeTransactionContract) c).getQuant() == 100
52+
&& ((ExchangeContract.ExchangeTransactionContract) c).getExpected() == 10),
4953
eq(Protocol.Transaction.Contract.ContractType.ExchangeTransactionContract));
5054
assertTransactionResponse(response);
5155
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -44,7 +43,11 @@ public void testExchangeWithdraw() throws Exception {
4443
MockHttpServletResponse response = newResponse();
4544
servlet.doPost(request, response);
4645
verify(wallet).createTransactionCapsule(
47-
any(ExchangeContract.ExchangeWithdrawContract.class),
46+
argThat(c -> c instanceof ExchangeContract.ExchangeWithdrawContract
47+
&& addressEquals(((ExchangeContract.ExchangeWithdrawContract) c)
48+
.getOwnerAddress(), ownerAddr)
49+
&& ((ExchangeContract.ExchangeWithdrawContract) c).getExchangeId() == 1
50+
&& ((ExchangeContract.ExchangeWithdrawContract) c).getQuant() == 50),
4851
eq(Protocol.Transaction.Contract.ContractType.ExchangeWithdrawContract));
4952
assertTransactionResponse(response);
5053
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.tron.core.services.http;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
53
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.argThat;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -43,7 +42,11 @@ public void testFreezeBalance() throws Exception {
4342
MockHttpServletResponse response = newResponse();
4443
servlet.doPost(request, response);
4544
verify(wallet).createTransactionCapsule(
46-
any(BalanceContract.FreezeBalanceContract.class),
45+
argThat(c -> c instanceof BalanceContract.FreezeBalanceContract
46+
&& addressEquals(((BalanceContract.FreezeBalanceContract) c)
47+
.getOwnerAddress(), ownerAddr)
48+
&& ((BalanceContract.FreezeBalanceContract) c).getFrozenBalance() == 1000000
49+
&& ((BalanceContract.FreezeBalanceContract) c).getFrozenDuration() == 3),
4750
eq(Protocol.Transaction.Contract.ContractType.FreezeBalanceContract));
4851
assertTransactionResponse(response);
4952
}

0 commit comments

Comments
 (0)