33import com .github .binarywang .wxpay .bean .profitsharing .request .ProfitSharingReceiverV3Request ;
44import com .github .binarywang .wxpay .bean .profitsharing .request .ProfitSharingV3Request ;
55import com .github .binarywang .wxpay .exception .WxPayException ;
6+ import com .github .binarywang .wxpay .v3 .SpecEncrypt ;
67import org .testng .annotations .Test ;
78
8- import java .security . cert . X509Certificate ;
9+ import java .lang . reflect . Field ;
910import java .util .ArrayList ;
1011import java .util .List ;
1112
1617 */
1718public class RsaCryptoUtilTest {
1819
20+ /**
21+ * 测试反射能否找到嵌套类中的 @SpecEncrypt 注解字段
22+ */
23+ @ Test
24+ public void testFindAnnotatedFieldsInNestedClass () {
25+ // 创建 Receiver 对象
26+ ProfitSharingV3Request .Receiver receiver = new ProfitSharingV3Request .Receiver ();
27+ receiver .setName ("测试姓名" );
28+
29+ // 使用反射查找带有 @SpecEncrypt 注解的字段
30+ Class <?> receiverClass = receiver .getClass ();
31+ Field [] fields = receiverClass .getDeclaredFields ();
32+
33+ System .out .println ("=== Receiver 类中的所有字段 ===" );
34+ boolean foundNameField = false ;
35+ boolean nameFieldHasAnnotation = false ;
36+
37+ for (Field field : fields ) {
38+ System .out .println ("字段名: " + field .getName () + ", 类型: " + field .getType ().getName ());
39+ if (field .getName ().equals ("name" )) {
40+ foundNameField = true ;
41+ if (field .isAnnotationPresent (SpecEncrypt .class )) {
42+ nameFieldHasAnnotation = true ;
43+ System .out .println (" -> name 字段有 @SpecEncrypt 注解" );
44+ } else {
45+ System .out .println (" -> name 字段没有 @SpecEncrypt 注解" );
46+ }
47+ }
48+ }
49+
50+ // 验证能够找到 name 字段并且它有 @SpecEncrypt 注解
51+ assertTrue (foundNameField , "应该能找到 name 字段" );
52+ assertTrue (nameFieldHasAnnotation , "name 字段应该有 @SpecEncrypt 注解" );
53+ }
54+
1955 /**
2056 * 测试嵌套对象中的字段加密
21- * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确加密
57+ * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确找到和处理
2258 */
2359 @ Test
24- public void testEncryptFieldsWithNestedObjects () throws WxPayException {
25- // 由于需要真实的证书才能加密,这里只是验证递归逻辑是否正确
60+ public void testEncryptFieldsWithNestedObjects () {
2661 // 创建测试对象
27- ProfitSharingV3Request request = new ProfitSharingV3Request ();
62+ ProfitSharingV3Request request = ProfitSharingV3Request .newBuilder ()
63+ .appid ("test-appid" )
64+ .subMchId ("test-submchid" )
65+ .transactionId ("test-transaction" )
66+ .outOrderNo ("test-order-no" )
67+ .unfreezeUnsplit (true )
68+ .build ();
2869
2970 List <ProfitSharingV3Request .Receiver > receivers = new ArrayList <>();
3071 ProfitSharingV3Request .Receiver receiver = new ProfitSharingV3Request .Receiver ();
3172 receiver .setName ("张三" ); // 设置需要加密的字段
3273 receiver .setAccount ("test-account" );
3374 receiver .setType ("PERSONAL_OPENID" );
3475 receiver .setAmount (100 );
76+ receiver .setRelationType ("STORE" );
77+ receiver .setDescription ("测试分账" );
3578
3679 receivers .add (receiver );
3780 request .setReceivers (receivers );
3881
39- // 注意:这个测试需要有效的证书才能真正执行加密
40- // 这里只是演示如何设置测试数据
41- // 如果没有证书,会在实际加密时抛出异常
82+ // 验证 receivers 字段有 @SpecEncrypt 注解
83+ try {
84+ Field receiversField = ProfitSharingV3Request .class .getDeclaredField ("receivers" );
85+ boolean hasAnnotation = receiversField .isAnnotationPresent (SpecEncrypt .class );
86+ System .out .println ("ProfitSharingV3Request.receivers 字段有 @SpecEncrypt 注解: " + hasAnnotation );
87+ assertTrue (hasAnnotation , "receivers 字段应该有 @SpecEncrypt 注解" );
88+ } catch (NoSuchFieldException e ) {
89+ fail ("应该能找到 receivers 字段" );
90+ }
4291
4392 System .out .println ("测试对象创建成功,name字段: " + receiver .getName ());
4493 // 验证name字段不为null
@@ -48,15 +97,29 @@ public void testEncryptFieldsWithNestedObjects() throws WxPayException {
4897
4998 /**
5099 * 测试单个对象中的字段加密
51- * 验证直接在对象上的 @SpecEncrypt 字段是否能被正确加密
100+ * 验证直接在对象上的 @SpecEncrypt 字段是否能被正确找到
52101 */
53102 @ Test
54- public void testEncryptFieldsWithDirectField () throws WxPayException {
103+ public void testEncryptFieldsWithDirectField () {
55104 // 创建测试对象
56- ProfitSharingReceiverV3Request request = new ProfitSharingReceiverV3Request ();
57- request .setName ("李四" ); // 设置需要加密的字段
58- request .setAccount ("test-account" );
59- request .setType ("PERSONAL_OPENID" );
105+ ProfitSharingReceiverV3Request request = ProfitSharingReceiverV3Request .newBuilder ()
106+ .appid ("test-appid" )
107+ .subMchId ("test-submchid" )
108+ .type ("PERSONAL_OPENID" )
109+ .account ("test-account" )
110+ .name ("李四" )
111+ .relationType ("STORE" )
112+ .build ();
113+
114+ // 验证 name 字段有 @SpecEncrypt 注解
115+ try {
116+ Field nameField = ProfitSharingReceiverV3Request .class .getDeclaredField ("name" );
117+ boolean hasAnnotation = nameField .isAnnotationPresent (SpecEncrypt .class );
118+ System .out .println ("ProfitSharingReceiverV3Request.name 字段有 @SpecEncrypt 注解: " + hasAnnotation );
119+ assertTrue (hasAnnotation , "name 字段应该有 @SpecEncrypt 注解" );
120+ } catch (NoSuchFieldException e ) {
121+ fail ("应该能找到 name 字段" );
122+ }
60123
61124 System .out .println ("测试对象创建成功,name字段: " + request .getName ());
62125 // 验证name字段不为null
0 commit comments