From 9d848a5649d84cdfeb564fd610ade769a6917a53 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 13 Sep 2026 11:04:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9A=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=BD=93=E8=AF=BB=E5=8F=96=E5=92=8C=E4=BD=99?= =?UTF-8?q?=E9=A2=9D=E6=9F=A5=E8=AF=A2=E5=BC=82=E5=B8=B8=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=9B=9E=E5=BD=92=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rtnerTransferServiceImplExceptionTest.java | 38 +++++++++ .../wxpay/util/RequestUtilsTest.java | 85 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PartnerTransferServiceImplExceptionTest.java create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/util/RequestUtilsTest.java diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PartnerTransferServiceImplExceptionTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PartnerTransferServiceImplExceptionTest.java new file mode 100644 index 000000000..f408ad7da --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/PartnerTransferServiceImplExceptionTest.java @@ -0,0 +1,38 @@ +package com.github.binarywang.wxpay.service.impl; + +import com.github.binarywang.wxpay.bean.ecommerce.FundBalanceResult; +import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum; +import com.github.binarywang.wxpay.exception.WxPayException; +import com.github.binarywang.wxpay.service.EcommerceService; +import com.github.binarywang.wxpay.service.WxPayService; +import me.chanjar.weixin.common.error.WxRuntimeException; +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.testng.Assert.assertEquals; + +public class PartnerTransferServiceImplExceptionTest { + + @Test + public void testSpDayEndBalancePreservesPayException() { + WxPayException failure = new WxPayException("balance query failed"); + WxPayService payService = new WxPayServiceImpl() { + @Override + public EcommerceService getEcommerceService() { + return new EcommerceServiceImpl(this) { + @Override + public FundBalanceResult spDayEndBalance(SpAccountTypeEnum accountType, String date) + throws WxPayException { + assertEquals(accountType, SpAccountTypeEnum.BASIC); + assertEquals(date, "2020-09-11"); + throw failure; + } + }; + } + }; + + assertThatThrownBy(() -> new PartnerTransferServiceImpl(payService) + .spDayEndBalance(SpAccountTypeEnum.BASIC, "2020-09-11")) + .isInstanceOf(WxRuntimeException.class).hasCause(failure); + } +} diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/util/RequestUtilsTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/util/RequestUtilsTest.java new file mode 100644 index 000000000..f0702741d --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/util/RequestUtilsTest.java @@ -0,0 +1,85 @@ +package com.github.binarywang.wxpay.util; + +import me.chanjar.weixin.common.error.WxRuntimeException; +import org.testng.annotations.Test; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Proxy; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.testng.Assert.assertTrue; + +public class RequestUtilsTest { + + @Test + public void testGetReaderFailure() { + IOException failure = new IOException("getReader failed"); + HttpServletRequest request = (HttpServletRequest) Proxy.newProxyInstance( + HttpServletRequest.class.getClassLoader(), new Class[]{HttpServletRequest.class}, + (proxy, method, args) -> { + if ("getReader".equals(method.getName())) { + throw failure; + } + throw new UnsupportedOperationException(method.getName()); + }); + + assertThatThrownBy(() -> RequestUtils.readData(request)) + .isInstanceOf(WxRuntimeException.class).hasCause(failure); + } + + @Test + public void testPartialReadFailureClosesReader() { + IOException failure = new IOException("read failed"); + boolean[] closed = {false}; + BufferedReader reader = new BufferedReader(new StringReader("")) { + private int reads; + + @Override + public String readLine() throws IOException { + if (reads++ == 0) { + return "partial body"; + } + throw failure; + } + + @Override + public void close() throws IOException { + closed[0] = true; + super.close(); + } + }; + + assertThatThrownBy(() -> RequestUtils.readData(requestWithReader(reader))) + .isInstanceOf(WxRuntimeException.class).hasCause(failure); + assertTrue(closed[0], "Reader must be closed after a read failure"); + } + + @Test + public void testCloseFailure() { + IOException failure = new IOException("close failed"); + BufferedReader reader = new BufferedReader(new StringReader("complete body")) { + @Override + public void close() throws IOException { + super.close(); + throw failure; + } + }; + + assertThatThrownBy(() -> RequestUtils.readData(requestWithReader(reader))) + .isInstanceOf(WxRuntimeException.class).hasCause(failure); + } + + private HttpServletRequest requestWithReader(BufferedReader reader) { + return (HttpServletRequest) Proxy.newProxyInstance( + HttpServletRequest.class.getClassLoader(), new Class[]{HttpServletRequest.class}, + (proxy, method, args) -> { + if ("getReader".equals(method.getName())) { + return reader; + } + throw new UnsupportedOperationException(method.getName()); + }); + } +}