diff --git a/trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java b/trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java index 1259065458..5f7b4f2c5d 100644 --- a/trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java +++ b/trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java @@ -45,7 +45,6 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import org.reflections.ReflectionUtils; @Extension(ThreadWorkerPool.TYPE) public class ThreadWorkerPool extends AbstractWorkerPool @@ -104,7 +103,7 @@ public void init() throws TRpcExtensionException { try { // Use JDK 21+ method Executors.newThreadPerTaskExecutor(ThreadFactory threadFactory) // to create a virtual thread executor service - Class executorsClazz = ReflectionUtils.forName(EXECUTORS_CLASS_NAME); + Class executorsClazz = Class.forName(EXECUTORS_CLASS_NAME); Method newThreadPerTaskExecutorMethod = executorsClazz .getDeclaredMethod(NEW_THREAD_PER_TASK_EXECUTOR_NAME, ThreadFactory.class); ThreadPerTaskExecutorWrapper wrappedThreadPool = ThreadPerTaskExecutorWrapper @@ -115,7 +114,8 @@ public void init() throws TRpcExtensionException { logger.info("Successfully created an executor that assigns each task to a " + "new virtual thread for processing"); return; - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) { + } catch (NoSuchMethodException | InvocationTargetException | + IllegalAccessException | ClassNotFoundException exception) { logger.warn("The current JDK version does not support virtual threads, please use OpenJDK 21+, " + "or remove use_virtual_thread_per_task_executor config, error: ", exception); } @@ -219,7 +219,7 @@ private ThreadFactory getThreadFactory(ThreadPoolConfig poolConfig) { // introducing the "java.lang.Thread.Builder.OfVirtual" dependency will result in an error, // so we create virtual threads through reflection, which is compatible with JDKs that do not support // virtual threads. When the JDK does not support virtual threads, it downgrades to thread. - Class threadClazz = ReflectionUtils.forName(THREAD_CLASS_NAME); + Class threadClazz = Class.forName(THREAD_CLASS_NAME); Method ofVirtualMethod = threadClazz.getDeclaredMethod(OF_VIRTUAL_NAME); Object virtual = ofVirtualMethod.invoke(threadClazz); Class virtualClazz = ofVirtualMethod.getReturnType(); @@ -238,7 +238,8 @@ && containsMethod(virtualClazz.getDeclaredMethods(), SCHEDULER_NAME)) { threadFactory = (ThreadFactory) factoryMethod.invoke(virtual); logger.info("Successfully created virtual thread factory"); return threadFactory; - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) { + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | + ClassNotFoundException exception) { logger.error("The current JDK version cannot use virtual threads, please use OpenJDK 21+ or " + "Tencent Kona JDK FIBER 8+ version, error: ", exception); } diff --git a/trpc-dependencies/trpc-dependencies-bom/pom.xml b/trpc-dependencies/trpc-dependencies-bom/pom.xml index bbc2b77a84..57588696f3 100644 --- a/trpc-dependencies/trpc-dependencies-bom/pom.xml +++ b/trpc-dependencies/trpc-dependencies-bom/pom.xml @@ -63,31 +63,31 @@ 3.24.2 2.1.0 1.14.9 - 2.9.3 + 3.1.8 0.7.0 - 1.15 + 1.17.1 4.4 - 2.10.1 + 2.11.0 2.6 - 3.12.0 - 2.3 - 2.15.1 - 1.10.0 + 3.17.0 + 2.12.0 + 2.18.0 + 1.13.0 1.9.4 0.2.12 - 5.5.0 + 5.7.1 1.4.5 3.4.2 2.10.0 2.3.32 - 2.8.9 + 2.11.0 32.1.3-jre 4.5.14 - 5.3.1 + 5.4.1 + 5.4 4.4.15 - 5.2.5 5.8.28 - 2.15.0-rc2 + 2.18.2 6.0.0 2.1.1 2.1.3 @@ -101,7 +101,7 @@ 2.7 2.4.14 5.14.2 - 3.28.0-GA + 3.30.2-GA 3.1.0 3.0.2 1.4.14 @@ -119,7 +119,7 @@ 1.4.2 2.2.1 3.3 - 4.10.0 + 4.12.0 0.33.0 12.2 3.8.0 @@ -132,24 +132,24 @@ 0.4.1 0.6.13 0.7.9 - 0.9.12 + 0.10.2 6.2.15.Final 1.8.6 1.8.6 2.0.17 2.0 - 1.1.10.4 + 1.1.10.7 6.2.7 3.5.0 4.3.0 2.12.4 - 3.8.4 + 3.9.3 1.9 1.56.0 - 2022.0.9 - 4.1.79.Final + 2023.0.11 + 4.1.115.Final diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcGatewayFilterFactory.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcGatewayFilterFactory.java index ce0f6307e6..62a7b4126b 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcGatewayFilterFactory.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcGatewayFilterFactory.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -65,7 +65,6 @@ public List shortcutFieldOrder() { @Override public GatewayFilter apply(TrpcGatewayFilterFactory.Config config) { - logger.info("config.toString():" + config.toString()); loadRequestRewriter(config); loadResponseRewriter(config); return new TrpcRoutingFilter(requestRewriter, responseRewriter, config); diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilter.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilter.java index c76c00f414..42d7a3d175 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilter.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilter.java @@ -83,7 +83,17 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); return DataBufferUtils.join(request.getBody()) .map(body -> requestRewriter.resolver(exchange, route, body)) - .map(body -> client.asyncInvoke(request, route, body.asByteBuffer().array())) + .map(body -> { + java.nio.ByteBuffer byteBuffer = body.asByteBuffer(); + byte[] bytes; + if (byteBuffer.hasArray()) { + bytes = byteBuffer.array(); + } else { + bytes = new byte[byteBuffer.remaining()]; + byteBuffer.get(bytes); + } + return client.asyncInvoke(request, route, bytes); + }) .flatMap(result -> this.responseRewriter.write(exchange, route.getMetadata(), result)); } diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/rewriter/DefaultTrpcResponseRewriter.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/rewriter/DefaultTrpcResponseRewriter.java index e5bc2a0a71..45ad5a5507 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/rewriter/DefaultTrpcResponseRewriter.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/rewriter/DefaultTrpcResponseRewriter.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -30,11 +30,25 @@ public Mono write(ServerWebExchange exchange, Mono result) { ServerHttpResponse response = exchange.getResponse(); if (result != null) { - DataBuffer dataBuffer = response.bufferFactory().wrap(result.block()); - logger.info("dataBuffer :{}", dataBuffer.toString(StandardCharsets.UTF_8)); - // Content-Type uses application/json by default - response.getHeaders().add("Content-Type", MimeTypeUtils.APPLICATION_JSON_VALUE); - return response.writeWith(Mono.justOrEmpty(dataBuffer)); + return result.flatMap(bytes -> { + if (bytes == null || bytes.length == 0) { + if (logger.isDebugEnabled()) { + logger.debug("Empty response body, skipping write"); + } + return Mono.empty(); + } + + if (logger.isDebugEnabled()) { + logger.debug("Writing response: {}", new String(bytes, StandardCharsets.UTF_8)); + } + + DataBuffer dataBuffer = response.bufferFactory().wrap(bytes); + response.getHeaders().setContentLength(bytes.length); + response.getHeaders().set("Content-Type", MimeTypeUtils.APPLICATION_JSON_VALUE); + + return response.writeWith(Mono.just(dataBuffer)) + .doOnError(error -> logger.error("Failed to write response", error)); + }); } return Mono.empty(); } diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java index d34e8aa800..b1d0eb482e 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -11,8 +11,8 @@ package com.tencent.trpc.spring.cloud.gateway.filter; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import com.tencent.trpc.spring.cloud.gateway.TrpcGatewayApplication; import java.io.IOException; @@ -22,6 +22,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.ResponseBody; import org.json.JSONException; import org.json.JSONObject; import org.junit.jupiter.api.AfterEach; @@ -34,52 +35,60 @@ public class TrpcRoutingFilterTest { private final String requestBody = "{\"msg\":\"hello gateway!\",\"id\":\"\"}"; - ConfigurableApplicationContext application; + private ConfigurableApplicationContext application; + private OkHttpClient httpClient; @BeforeEach - void setUp() { - // Start the Spring container, start the gateway, and backend services + void setUp() throws InterruptedException { application = new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(new String[0]); + TimeUnit.SECONDS.sleep(5); + + httpClient = new OkHttpClient().newBuilder() + .readTimeout(10, TimeUnit.SECONDS) + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .build(); } @AfterEach void tearDown() { - // Stop the gateway and simulated backend services - application.stop(); + if (application != null) { + application.close(); + } } @Test void filter() { - // Initiate an HTTP request and verify the normal TRPC forwarding scenario. trpcTest(); - - // Initiate an HTTP request and verify the normal HTTP forwarding scenario. httpTest(); } private void httpTest() { try { JSONObject response = gateway(getHttpRequest()); - assertEquals(response.toString(), requestBody); + assertNotNull(response); + assertEquals(requestBody, response.toString()); } catch (JSONException | IOException e) { - assertNull(e); + throw new AssertionError("httpTest failed", e); } } private void trpcTest() { try { JSONObject response = gateway(getTRPCRequest(requestBody)); - assertEquals(response.toString(), requestBody); + assertNotNull(response); + assertEquals(requestBody, response.toString()); } catch (JSONException | IOException e) { - assertNull(e); + throw new AssertionError("trpcTest failed", e); } } private JSONObject gateway(Request httpRequest) throws JSONException, IOException { - Response response = new OkHttpClient().newBuilder().readTimeout(2, TimeUnit.SECONDS).build() - .newCall(httpRequest).execute(); - // Format is as follows: {"message":"","id":""} - return new JSONObject(response.body().string()); + try (Response response = httpClient.newCall(httpRequest).execute()) { + ResponseBody body = response.body(); + assertNotNull(body); + return new JSONObject(body.string()); + } } private Request getHttpRequest() { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestClientFilter.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestClientFilter.java index d9087bcf95..2cdbf99a4b 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestClientFilter.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestClientFilter.java @@ -17,8 +17,9 @@ import com.tencent.trpc.core.rpc.Response; import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.TestService; import java.util.concurrent.CompletionStage; -import javax.annotation.Resource; +import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; public class AutoInjectTestClientFilter implements Filter { @@ -31,6 +32,7 @@ public TestService getMyTestService1() { } @Autowired + @Qualifier("myTestService1") public void setMyTestService1(TestService myTestService1) { this.myTestService1 = myTestService1; } diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestServerFilter.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestServerFilter.java index 2923dd4df4..9a9ff2ac10 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestServerFilter.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/AutoInjectTestServerFilter.java @@ -18,7 +18,7 @@ import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.InjectByFieldBean; import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.InjectBySetterBean; import java.util.concurrent.CompletionStage; -import javax.annotation.Resource; +import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; public class AutoInjectTestServerFilter implements Filter { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigAutoRegistryTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigAutoRegistryTest.java index e9ca53fcaa..700ae14200 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigAutoRegistryTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigAutoRegistryTest.java @@ -25,10 +25,9 @@ import com.tencent.trpc.spring.context.configuration.TRpcConfigManagerCustomizer; import com.tencent.trpc.spring.test.TRpcConfigManagerTestUtils; import com.tencent.trpc.spring.test.TestSpringApplication; -import javax.annotation.Resource; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -46,9 +45,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = {TRpcConfigAutoRegistryTestConfiguration.class, TRpcConfiguration.class}, initializers = TRpcConfigAutoRegistryTestInitializer.class) @@ -83,16 +80,16 @@ public class TRpcConfigAutoRegistryTest { @Test public void testTRpcClientAutoRegistryByField() { // test field - Assert.assertNotNull(injectByFieldBean.getMyTestService()); - Assert.assertNotNull(injectByFieldBean.getMyTestService1()); - Assert.assertNotNull(injectByFieldBean.getMyTestService2()); - Assert.assertNotNull(injectByFieldBean.getMyTestServiceByResource()); - Assert.assertNotNull(injectByFieldBean.getTestService1()); - Assert.assertNotNull(injectByFieldBean.getTestService2()); - Assert.assertSame(injectByFieldBean.getTestService1(), injectByFieldBean.getMyTestService()); - Assert.assertSame(injectByFieldBean.getTestService1(), injectByFieldBean.getMyTestService1()); - Assert.assertSame(injectByFieldBean.getTestService2(), injectByFieldBean.getMyTestService2()); - Assert.assertSame(injectByFieldBean.getTestService2(), injectByFieldBean.getMyTestServiceByResource()); + Assertions.assertNotNull(injectByFieldBean.getMyTestService()); + Assertions.assertNotNull(injectByFieldBean.getMyTestService1()); + Assertions.assertNotNull(injectByFieldBean.getMyTestService2()); + Assertions.assertNotNull(injectByFieldBean.getMyTestServiceByResource()); + Assertions.assertNotNull(injectByFieldBean.getTestService1()); + Assertions.assertNotNull(injectByFieldBean.getTestService2()); + Assertions.assertSame(injectByFieldBean.getTestService1(), injectByFieldBean.getMyTestService()); + Assertions.assertSame(injectByFieldBean.getTestService1(), injectByFieldBean.getMyTestService1()); + Assertions.assertSame(injectByFieldBean.getTestService2(), injectByFieldBean.getMyTestService2()); + Assertions.assertSame(injectByFieldBean.getTestService2(), injectByFieldBean.getMyTestServiceByResource()); } /** @@ -101,14 +98,14 @@ public void testTRpcClientAutoRegistryByField() { @Test public void testTRpcClientAutoRegistry() { // test constructor - Assert.assertNotNull(injectByConstructorBean.getMyTestService()); - Assert.assertNotNull(injectByConstructorBean.getMyTestService1()); - Assert.assertNotNull(injectByConstructorBean.getMyTestService2()); - Assert.assertNotNull(injectByConstructorBean.getTestService1()); - Assert.assertNotNull(injectByConstructorBean.getTestService2()); - Assert.assertSame(injectByConstructorBean.getTestService1(), injectByConstructorBean.getMyTestService()); - Assert.assertSame(injectByConstructorBean.getTestService1(), injectByConstructorBean.getMyTestService1()); - Assert.assertSame(injectByConstructorBean.getTestService2(), injectByConstructorBean.getMyTestService2()); + Assertions.assertNotNull(injectByConstructorBean.getMyTestService()); + Assertions.assertNotNull(injectByConstructorBean.getMyTestService1()); + Assertions.assertNotNull(injectByConstructorBean.getMyTestService2()); + Assertions.assertNotNull(injectByConstructorBean.getTestService1()); + Assertions.assertNotNull(injectByConstructorBean.getTestService2()); + Assertions.assertSame(injectByConstructorBean.getTestService1(), injectByConstructorBean.getMyTestService()); + Assertions.assertSame(injectByConstructorBean.getTestService1(), injectByConstructorBean.getMyTestService1()); + Assertions.assertSame(injectByConstructorBean.getTestService2(), injectByConstructorBean.getMyTestService2()); } /** @@ -117,15 +114,15 @@ public void testTRpcClientAutoRegistry() { @Test public void testTRpcClientAutoRegistryBySetter() { // test setter - Assert.assertNotNull(injectBySetterBean.getMyTestService()); - Assert.assertNotNull(injectBySetterBean.getMyTestService1()); - Assert.assertNotNull(injectBySetterBean.getMyTestService2()); - Assert.assertNotNull(injectBySetterBean.getTestService1()); - Assert.assertNotNull(injectBySetterBean.getTestService2()); - Assert.assertSame(injectBySetterBean.getTestService1(), injectBySetterBean.getMyTestService()); - Assert.assertSame(injectBySetterBean.getTestService1(), injectBySetterBean.getMyTestService1()); - Assert.assertSame(injectBySetterBean.getTestService2(), injectBySetterBean.getMyTestService2()); - Assert.assertSame(injectBySetterBean.getTestService2(), injectBySetterBean.getMyTestServiceByResource()); + Assertions.assertNotNull(injectBySetterBean.getMyTestService()); + Assertions.assertNotNull(injectBySetterBean.getMyTestService1()); + Assertions.assertNotNull(injectBySetterBean.getMyTestService2()); + Assertions.assertNotNull(injectBySetterBean.getTestService1()); + Assertions.assertNotNull(injectBySetterBean.getTestService2()); + Assertions.assertSame(injectBySetterBean.getTestService1(), injectBySetterBean.getMyTestService()); + Assertions.assertSame(injectBySetterBean.getTestService1(), injectBySetterBean.getMyTestService1()); + Assertions.assertSame(injectBySetterBean.getTestService2(), injectBySetterBean.getMyTestService2()); + Assertions.assertSame(injectBySetterBean.getTestService2(), injectBySetterBean.getMyTestServiceByResource()); } /** @@ -133,12 +130,12 @@ public void testTRpcClientAutoRegistryBySetter() { */ @Test public void testNotAutoRegistry() { - Assert.assertNull(customizedMyService); - Assert.assertTrue(beanFactory.containsBean("myTRpcConfigManagerCustomizer")); - Assert.assertFalse(beanFactory.containsBean("my.trpc.customized.client")); - Assert.assertEquals(1, beanFactory.getBeanProvider(MyService.class).stream().count()); - Assert.assertTrue(myTRpcConfigManagerCustomizer.isEnabled()); - Assert.assertEquals("add", myTRpcConfigManagerCustomizer.getFlag()); + Assertions.assertNull(customizedMyService); + Assertions.assertTrue(beanFactory.containsBean("myTRpcConfigManagerCustomizer")); + Assertions.assertFalse(beanFactory.containsBean("my.trpc.customized.client")); + Assertions.assertEquals(1, beanFactory.getBeanProvider(MyService.class).stream().count()); + Assertions.assertTrue(myTRpcConfigManagerCustomizer.isEnabled()); + Assertions.assertEquals("add", myTRpcConfigManagerCustomizer.getFlag()); } /** @@ -148,20 +145,20 @@ public void testNotAutoRegistry() { public void testTRpcFilterAutoRegistry() { AutoInjectTestClientFilter clientFilter = (AutoInjectTestClientFilter) FilterManager.get( "auto_inject_test_client_filter"); - Assert.assertNotNull(clientFilter.getMyTestService1()); - Assert.assertNotNull(clientFilter.getMyTestService2()); - Assert.assertSame(clientFilter.getMyTestService1(), myTestService1); - Assert.assertSame(clientFilter.getMyTestService2(), myTestService2); + Assertions.assertNotNull(clientFilter.getMyTestService1()); + Assertions.assertNotNull(clientFilter.getMyTestService2()); + Assertions.assertSame(clientFilter.getMyTestService1(), myTestService1); + Assertions.assertSame(clientFilter.getMyTestService2(), myTestService2); AutoInjectTestServerFilter serverFilter = (AutoInjectTestServerFilter) FilterManager.get( "auto_inject_test_server_filter"); - Assert.assertSame(serverFilter, autoInjectTestServerFilter); - Assert.assertNotNull(serverFilter.getInjectByFieldBean()); - Assert.assertNotNull(serverFilter.getAutowiredByFieldBean()); - Assert.assertNotNull(serverFilter.getInjectBySetterBean()); - Assert.assertSame(serverFilter.getInjectByFieldBean(), injectByFieldBean); - Assert.assertSame(serverFilter.getAutowiredByFieldBean(), injectByFieldBean); - Assert.assertSame(serverFilter.getInjectBySetterBean(), injectBySetterBean); + Assertions.assertSame(serverFilter, autoInjectTestServerFilter); + Assertions.assertNotNull(serverFilter.getInjectByFieldBean()); + Assertions.assertNotNull(serverFilter.getAutowiredByFieldBean()); + Assertions.assertNotNull(serverFilter.getInjectBySetterBean()); + Assertions.assertSame(serverFilter.getInjectByFieldBean(), injectByFieldBean); + Assertions.assertSame(serverFilter.getAutowiredByFieldBean(), injectByFieldBean); + Assertions.assertSame(serverFilter.getInjectBySetterBean(), injectBySetterBean); } @TRpcService(name = "test_trpc_config_registry") @@ -350,7 +347,8 @@ public InjectByConstructorBean(TestService testService1, TestService testService } @Autowired - public InjectByConstructorBean(TestService myTestService1, TestService myTestService2, + public InjectByConstructorBean(@Qualifier("myTestService1") TestService myTestService1, + @Qualifier("myTestService2") TestService myTestService2, @Qualifier("myTestService1") TestService myTestService) { this.myTestService1 = myTestService1; this.myTestService2 = myTestService2; @@ -415,6 +413,7 @@ public TestService getMyTestService1() { } @Autowired + @Qualifier("myTestService1") public void setMyTestService1(TestService myTestService1) { this.myTestService1 = myTestService1; } @@ -424,6 +423,7 @@ public TestService getMyTestService2() { } @Autowired + @Qualifier("myTestService2") public void setMyTestService2(TestService myTestService2) { this.myTestService2 = myTestService2; } diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigManagerInitializerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigManagerInitializerTest.java index 2f0a235eaf..76ca9a5a52 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigManagerInitializerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/TRpcConfigManagerInitializerTest.java @@ -24,9 +24,8 @@ import com.tencent.trpc.spring.context.configuration.TRpcConfigManagerCustomizer; import com.tencent.trpc.spring.test.TRpcConfigManagerTestUtils; import com.tencent.trpc.spring.test.TestSpringApplication; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; @@ -35,9 +34,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = {BeanConfiguration.class, TRpcConfiguration.class}, initializers = InitConfigManagerApplicationContextInitializer.class) @@ -60,14 +57,14 @@ public class TRpcConfigManagerInitializerTest { public void test() { BackendConfig backendConfig = ConfigManager.getInstance().getClientConfig().getBackendConfigMap() .get(CLIENT_ID); - Assert.assertNotNull(backendConfig); - Assert.assertEquals(backendConfig.getNamingUrl(), CUSTOMIZED_NAMING_URL); - Assert.assertFalse(backendConfig.getBatchDecoder()); - Assert.assertTrue(backendConfig.getFilters().contains(ADDITIONAL_CLIENT_FILTER)); + Assertions.assertNotNull(backendConfig); + Assertions.assertEquals(backendConfig.getNamingUrl(), CUSTOMIZED_NAMING_URL); + Assertions.assertFalse(backendConfig.getBatchDecoder()); + Assertions.assertTrue(backendConfig.getFilters().contains(ADDITIONAL_CLIENT_FILTER)); ServiceConfig serviceConfig = ConfigManager.getInstance().getServerConfig().getServiceMap().get(SERVICE_ID); - Assert.assertNotNull(serviceConfig); - Assert.assertTrue(serviceConfig.getFilters().contains(ADDITIONAL_SERVER_FILTER)); + Assertions.assertNotNull(serviceConfig); + Assertions.assertTrue(serviceConfig.getFilters().contains(ADDITIONAL_SERVER_FILTER)); } @TRpcService(name = SERVICE_ID) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index 123448802a..6b09f0613f 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -18,9 +18,9 @@ import com.tencent.trpc.core.common.config.ServiceConfig; import java.util.Arrays; import java.util.List; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class AddFilterTRpcConfigManagerCustomizerTest { @@ -48,28 +48,28 @@ public class AddFilterTRpcConfigManagerCustomizerTest { private static AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer; - @Before + @BeforeEach public void setUp() { addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); } @Test public void testConstructor() { - Assert.assertNotNull(addFilterTRpcConfigManagerCustomizer); + Assertions.assertNotNull(addFilterTRpcConfigManagerCustomizer); } @Test public void testAddClientFilters() { AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters( FILTER_ONE, FILTER_TWO); - Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); + Assertions.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); } @Test public void testAddServerFilters() { AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( FILTER_ONE, FILTER_TWO); - Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); + Assertions.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); } @Test @@ -82,15 +82,15 @@ public void testCustomize() { addFilterTRpcConfigManagerCustomizer.customize(instance); List filters = instance.getClientConfig().getFilters(); - Assert.assertEquals(filters.size(), list.size()); - Assert.assertEquals(list, instance.getClientConfig().getFilters()); + Assertions.assertEquals(filters.size(), list.size()); + Assertions.assertEquals(list, instance.getClientConfig().getFilters()); } @Test public void testGetOrder() { - Assert.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); + Assertions.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); addFilterTRpcConfigManagerCustomizer = new TestAddFilterTRpcConfigManagerCustomizer(); - Assert.assertEquals((long) ORDER_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); + Assertions.assertEquals((long) ORDER_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); } static final class TestAddFilterTRpcConfigManagerCustomizer extends AddFilterTRpcConfigManagerCustomizer { @@ -124,11 +124,11 @@ public void testConstructorWithNullEmpty() { addFilterTRpcConfigManagerCustomizer.customize(configManager); List clientExpected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); // backendConfig filters test - Assert.assertEquals(clientExpected.size(), backendConfig.getFilters().size()); + Assertions.assertEquals(clientExpected.size(), backendConfig.getFilters().size()); // serviceConfig filters test List serverExpected = Arrays.asList(SERVER_FILTER_ONE, SERVER_FILTER_TWO, SERVICE_FILTER_THREE, SERVICE_FILTER_FOUR); - Assert.assertEquals(serverExpected.size(), serviceConfig.getFilters().size()); + Assertions.assertEquals(serverExpected.size(), serviceConfig.getFilters().size()); } @Test @@ -149,9 +149,9 @@ public void testMerge() { addFilterTRpcConfigManagerCustomizer.customize(configManager); List expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); - Assert.assertEquals(expected, backendConfig.getFilters()); + Assertions.assertEquals(expected, backendConfig.getFilters()); expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE); - Assert.assertNotEquals(expected, backendConfig.getFilters()); + Assertions.assertNotEquals(expected, backendConfig.getFilters()); } } diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/SchemaTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/SchemaTest.java index 11a3a84285..e9b0f2159d 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/SchemaTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/SchemaTest.java @@ -23,8 +23,8 @@ import java.io.IOException; import java.util.List; import java.util.Map; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.env.YamlPropertySourceLoader; @@ -56,10 +56,10 @@ public void test() { private void assertBound(Binder binder, String prefix, Class clazz) { Object bound = bind(binder, prefix, clazz); - Assert.assertNotNull(bound); + Assertions.assertNotNull(bound); Map map = MAP_CONVERTER.convertValue(bound, MAP_TYPE); - Assert.assertTrue(map.size() > 0); + Assertions.assertTrue(map.size() > 0); } private Binder getBinder() { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServerSchemaTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServerSchemaTest.java index a893abb808..543ef4c5e4 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServerSchemaTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServerSchemaTest.java @@ -1,7 +1,7 @@ package com.tencent.trpc.spring.context.configuration.schema.server; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class ServerSchemaTest { @@ -10,7 +10,7 @@ public void testSetMethod() { ServerSchema serverSchema = new ServerSchema(); serverSchema.setCloseTimeout(2000L); serverSchema.setWaitTimeout(2000L); - Assert.assertEquals(Long.valueOf(2000), serverSchema.getWaitTimeout()); - Assert.assertEquals(Long.valueOf(2000), serverSchema.getCloseTimeout()); + Assertions.assertEquals(Long.valueOf(2000), serverSchema.getWaitTimeout()); + Assertions.assertEquals(Long.valueOf(2000), serverSchema.getCloseTimeout()); } } diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServiceProviderSchemaTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServiceProviderSchemaTest.java index 7244a5f345..8129477e58 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServiceProviderSchemaTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/schema/server/ServiceProviderSchemaTest.java @@ -13,15 +13,15 @@ import java.util.Collections; import org.assertj.core.util.Lists; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class ServiceProviderSchemaTest { private ServiceProviderSchema serviceProviderSchema; - @Before + @BeforeEach public void setUp() throws Exception { serviceProviderSchema = new ServiceProviderSchema(); serviceProviderSchema.setImpl("a"); @@ -33,61 +33,61 @@ public void setUp() throws Exception { @Test public void testGetImpl() { - Assert.assertEquals("a", serviceProviderSchema.getImpl()); + Assertions.assertEquals("a", serviceProviderSchema.getImpl()); } @Test public void testGetRequestTimeout() { - Assert.assertEquals(20, serviceProviderSchema.getRequestTimeout().intValue()); + Assertions.assertEquals(20, serviceProviderSchema.getRequestTimeout().intValue()); } @Test public void testGetWorkerPool() { - Assert.assertEquals("pool", serviceProviderSchema.getWorkerPool()); + Assertions.assertEquals("pool", serviceProviderSchema.getWorkerPool()); } @Test public void testGetFilters() { - Assert.assertEquals("b", serviceProviderSchema.getFilters().get(0)); + Assertions.assertEquals("b", serviceProviderSchema.getFilters().get(0)); } @Test public void testGetEnableLinkTimeout() { - Assert.assertFalse(serviceProviderSchema.getEnableLinkTimeout()); + Assertions.assertFalse(serviceProviderSchema.getEnableLinkTimeout()); } @Test public void testToString() { - Assert.assertNotNull(serviceProviderSchema.toString()); + Assertions.assertNotNull(serviceProviderSchema.toString()); } @Test public void testSetImpl() { serviceProviderSchema.setImpl(""); - Assert.assertEquals("", serviceProviderSchema.getImpl()); + Assertions.assertEquals("", serviceProviderSchema.getImpl()); } @Test public void testSetRequestTimeout() { serviceProviderSchema.setRequestTimeout(100); - Assert.assertEquals(100, serviceProviderSchema.getRequestTimeout().intValue()); + Assertions.assertEquals(100, serviceProviderSchema.getRequestTimeout().intValue()); } @Test public void testSetWorkerPool() { serviceProviderSchema.setWorkerPool("worker"); - Assert.assertEquals("worker", serviceProviderSchema.getWorkerPool()); + Assertions.assertEquals("worker", serviceProviderSchema.getWorkerPool()); } @Test public void testSetFilters() { serviceProviderSchema.setFilters(Collections.emptyList()); - Assert.assertTrue(serviceProviderSchema.getFilters().isEmpty()); + Assertions.assertTrue(serviceProviderSchema.getFilters().isEmpty()); } @Test public void testSetEnableLinkTimeout() { serviceProviderSchema.setEnableLinkTimeout(Boolean.TRUE); - Assert.assertTrue(serviceProviderSchema.getEnableLinkTimeout()); + Assertions.assertTrue(serviceProviderSchema.getEnableLinkTimeout()); } -} \ No newline at end of file +} diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomBeanHandleExceptionAnnotationTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomBeanHandleExceptionAnnotationTest.java index 8161d9bf65..16770b9381 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomBeanHandleExceptionAnnotationTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomBeanHandleExceptionAnnotationTest.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -33,10 +33,9 @@ import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.RandomUtils; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; @@ -44,9 +43,7 @@ import org.springframework.context.annotation.Lazy; import org.springframework.core.annotation.Order; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = CustomBeanHandleExceptionAnnotationTestConfiguration.class) public class CustomBeanHandleExceptionAnnotationTest { @@ -58,7 +55,7 @@ public static RpcClientContext newContext() { return new RpcClientContext(); } - @Before + @BeforeEach public void setUp() throws Exception { CustomBeanHandleExceptionAnnotationTestConfiguration.STATE = 0; } @@ -70,9 +67,9 @@ public void setUp() throws Exception { public void testHandleException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("1").setName(code).build()); - Assert.assertEquals("12345", response.getResult()); - Assert.assertEquals("customHandleException", response.getResInfo()); - Assert.assertEquals(2, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("12345", response.getResult()); + Assertions.assertEquals("customHandleException", response.getResInfo()); + Assertions.assertEquals(2, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); } /** @@ -82,9 +79,9 @@ public void testHandleException() { public void testHandleNullPointerException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("2").setName(code).build()); - Assert.assertEquals("98765", response.getResult()); - Assert.assertEquals("{name=NullPointerException}", response.getResInfo()); - Assert.assertEquals(1, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("98765", response.getResult()); + Assertions.assertEquals("{name=NullPointerException}", response.getResInfo()); + Assertions.assertEquals(1, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); } /** @@ -94,19 +91,22 @@ public void testHandleNullPointerException() { public void testHandleIndexOutOfBoundsException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("3").setInfo(code).build()); - Assert.assertEquals("1111", response.getResult()); - Assert.assertEquals("local IndexOutOfBoundsException " + code, response.getResInfo()); - Assert.assertEquals(1, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("1111", response.getResult()); + Assertions.assertEquals("local IndexOutOfBoundsException " + code, response.getResInfo()); + Assertions.assertEquals(1, CustomBeanHandleExceptionAnnotationTestConfiguration.STATE); } /** * Test for IllegalArgumentException handling */ - @Test(expected = IllegalArgumentException.class) + @Test public void testHandleIllegalArgumentException() { - String code = String.valueOf(RandomUtils.nextInt()); - Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("4").setName(code).build()); - Assert.fail(); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + String code = String.valueOf(RandomUtils.nextInt()); + Response response = testServiceApi.test(newContext(), + Request.newBuilder().setId("4").setName(code).build()); + Assertions.fail(); + }); } /** @@ -116,8 +116,8 @@ public void testHandleIllegalArgumentException() { public void testUnsupportedOperationException() { String code = String.valueOf(RandomUtils.nextInt()); Resp response = testServiceApi.call(newContext(), Req.newBuilder().setResult("2").setInfo(code).build()); - Assert.assertEquals("54321", response.getRetCode()); - Assert.assertEquals("{name=UnsupportedOperationException}", response.getRetMsg()); + Assertions.assertEquals("54321", response.getRetCode()); + Assertions.assertEquals("{name=UnsupportedOperationException}", response.getRetMsg()); } @Configuration diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomConfigHandleExceptionAnnotationTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomConfigHandleExceptionAnnotationTest.java index f70a9f2aa7..d77c9e188f 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomConfigHandleExceptionAnnotationTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/CustomConfigHandleExceptionAnnotationTest.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, @@ -31,18 +31,15 @@ import com.tencent.trpc.spring.test.TestSpringApplication; import java.lang.reflect.Method; import org.apache.commons.lang3.RandomUtils; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = CustomConfigHandleExceptionAnnotationTestConfiguration.class) public class CustomConfigHandleExceptionAnnotationTest { @@ -54,7 +51,7 @@ public static RpcClientContext newContext() { return new RpcClientContext(); } - @Before + @BeforeEach public void setUp() throws Exception { CustomConfigHandleExceptionAnnotationTestConfiguration.STATE = 0; } @@ -66,9 +63,9 @@ public void setUp() throws Exception { public void testHandleException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("1").setName(code).build()); - Assert.assertEquals("12345", response.getResult()); - Assert.assertEquals("custom", response.getResInfo()); - Assert.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("12345", response.getResult()); + Assertions.assertEquals("custom", response.getResInfo()); + Assertions.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); } /** @@ -78,9 +75,9 @@ public void testHandleException() { public void testHandleNullPointerException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("2").setName(code).build()); - Assert.assertEquals("12345", response.getResult()); - Assert.assertEquals("custom", response.getResInfo()); - Assert.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("12345", response.getResult()); + Assertions.assertEquals("custom", response.getResInfo()); + Assertions.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); } /** @@ -90,33 +87,35 @@ public void testHandleNullPointerException() { public void testHandleIndexOutOfBoundsException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("3").setName(code).build()); - Assert.assertEquals("12345", response.getResult()); - Assert.assertEquals("custom", response.getResInfo()); - Assert.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("12345", response.getResult()); + Assertions.assertEquals("custom", response.getResInfo()); + Assertions.assertEquals(1, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); } /** * Test for IllegalArgumentException handling */ - @Test(expected = IllegalArgumentException.class) + @Test public void testHandleIllegalArgumentException() { - String code = String.valueOf(RandomUtils.nextInt()); - Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("4").setName(code).build()); - Assert.fail(); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + String code = String.valueOf(RandomUtils.nextInt()); + Response response = testServiceApi.test(newContext(), + Request.newBuilder().setId("4").setName(code).build()); + Assertions.fail(); + }); } /** * Test for handle result type error */ - @Test(expected = ClassCastException.class) + @Test public void testClassCastException() { - String code = String.valueOf(RandomUtils.nextInt()); - try { + Assertions.assertThrows(ClassCastException.class, () -> { + String code = String.valueOf(RandomUtils.nextInt()); Resp response = testServiceApi.call(newContext(), Req.newBuilder().setResult("2").setInfo(code).build()); - Assert.fail(); - } finally { - Assert.assertEquals(3, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); - } + Assertions.fail(); + }); + Assertions.assertEquals(3, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); } /** @@ -126,9 +125,9 @@ public void testClassCastException() { public void testSpecificExceptionHandler() { String code = String.valueOf(RandomUtils.nextInt()); Resp response = testServiceApi.call(newContext(), Req.newBuilder().setResult("1").setInfo(code).build()); - Assert.assertEquals("666666", response.getRetCode()); - Assert.assertEquals("myHandler", response.getRetMsg()); - Assert.assertEquals(3, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); + Assertions.assertEquals("666666", response.getRetCode()); + Assertions.assertEquals("myHandler", response.getRetMsg()); + Assertions.assertEquals(3, CustomConfigHandleExceptionAnnotationTestConfiguration.STATE); } @Configuration diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/HandleExceptionAnnotationTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/HandleExceptionAnnotationTest.java index 4b9c5efb66..25f5e6cde5 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/HandleExceptionAnnotationTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/HandleExceptionAnnotationTest.java @@ -30,20 +30,17 @@ import com.tencent.trpc.spring.test.TestSpringApplication; import java.lang.reflect.Method; import org.apache.commons.lang3.RandomUtils; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; /** * Test for exception-handling */ -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = TestHandleExceptionConfiguration.class) public class HandleExceptionAnnotationTest { @@ -62,8 +59,8 @@ public static RpcClientContext newContext() { public void testHandleException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("1").setName(code).build()); - Assert.assertEquals("999", response.getResult()); - Assert.assertEquals("test_Request", response.getResInfo()); + Assertions.assertEquals("999", response.getResult()); + Assertions.assertEquals("test_Request", response.getResInfo()); } /** @@ -74,8 +71,8 @@ public void testHandleMyException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("3").setName(code).setInfo("testHandleMyException").build()); - Assert.assertEquals(code, response.getResult()); - Assert.assertEquals("testHandleMyException", response.getResInfo()); + Assertions.assertEquals(code, response.getResult()); + Assertions.assertEquals("testHandleMyException", response.getResInfo()); } /** @@ -86,8 +83,8 @@ public void testHandleSonException() { Response response = testServiceApi .test(newContext(), Request.newBuilder().setId("2").setName("testHandleSonException") .setInfo("testHandleSonExceptiontestHandleSonException").build()); - Assert.assertEquals("testHandleSonException", response.getResult()); - Assert.assertEquals("testHandleSonExceptiontestHandleSonException", response.getResInfo()); + Assertions.assertEquals("testHandleSonException", response.getResult()); + Assertions.assertEquals("testHandleSonExceptiontestHandleSonException", response.getResInfo()); } /** @@ -98,45 +95,53 @@ public void testHandleTRpcException() { String code = String.valueOf(RandomUtils.nextInt()); Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("4").setName(code).setInfo("testHandleException").build()); - Assert.assertEquals(code, response.getResult()); - Assert.assertEquals("handleTRpcException testHandleException", response.getResInfo()); + Assertions.assertEquals(code, response.getResult()); + Assertions.assertEquals("handleTRpcException testHandleException", response.getResInfo()); } /** * Test for unhandled exception type 'Error' */ - @Test(expected = Error.class) + @Test public void testNotHandleError() { - Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("5").build()); - Assert.fail(); + Assertions.assertThrows(Error.class, () -> { + Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("5").build()); + Assertions.fail(); + }); } /** * Test for Excluded exception IllegalArgumentException */ - @Test(expected = IllegalArgumentException.class) + @Test public void testExcludeHandleException() { - Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("6").build()); - Assert.fail(); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("6").build()); + Assertions.fail(); + }); } /** * Test for throw another exception in handler */ - @Test(expected = IllegalMonitorStateException.class) + @Test public void testThrowIllegalStateException() { - Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("7").build()); - Assert.fail(); + Assertions.assertThrows(IllegalMonitorStateException.class, () -> { + Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("7").build()); + Assertions.fail(); + }); } /** * Exception thrown by non-tRPC method shouldn't be handled */ - @Test(expected = NullPointerException.class) + @Test public void testCustomHandleException() { - String code = String.valueOf(RandomUtils.nextInt()); - Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("8").build()); - Assert.fail(); + Assertions.assertThrows(NullPointerException.class, () -> { + String code = String.valueOf(RandomUtils.nextInt()); + Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("8").build()); + Assertions.fail(); + }); } /** @@ -145,7 +150,7 @@ public void testCustomHandleException() { @Test public void testHandlerReturnNull() { Response response = testServiceApi.test(newContext(), Request.newBuilder().setId("9").build()); - Assert.assertNull(response); + Assertions.assertNull(response); } /** @@ -156,8 +161,8 @@ public void testLocalExceptionHandler() { String random = String.valueOf(System.nanoTime()); Response response = testServiceApi .test(newContext(), Request.newBuilder().setId("10").setInfo(random).build()); - Assert.assertEquals("1111", response.getResult()); - Assert.assertEquals("local IndexOutOfBoundsException " + random, response.getResInfo()); + Assertions.assertEquals("1111", response.getResult()); + Assertions.assertEquals("local IndexOutOfBoundsException " + random, response.getResInfo()); } /** @@ -168,26 +173,30 @@ public void testSpecificHandleResultTransform() { String random = String.valueOf(System.nanoTime()); Resp response = testServiceApi.call(newContext(), Req.newBuilder().setInfo(random).setResult("testSpecificHandleResultTransform").build()); - Assert.assertEquals("8888", response.getRetCode()); - Assert.assertEquals(random + "_testSpecificHandleResultTransform999", response.getRetMsg()); + Assertions.assertEquals("8888", response.getRetCode()); + Assertions.assertEquals(random + "_testSpecificHandleResultTransform999", response.getRetMsg()); } /** * Exception thrown by non-tRPC method shouldn't be handled */ - @Test(expected = MyException.class) + @Test public void testUselessForNotTRpcServiceMethod() { - Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("3").build()); - Assert.fail(); + Assertions.assertThrows(MyException.class, () -> { + Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("3").build()); + Assertions.fail(); + }); } /** * Exception thrown by non-tRPC method shouldn't be handled */ - @Test(expected = IllegalMonitorStateException.class) + @Test public void testIllegalMonitorStateException() { - Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("11").build()); - Assert.fail(); + Assertions.assertThrows(IllegalMonitorStateException.class, () -> { + Response response = testServiceApi.ex(newContext(), Request.newBuilder().setId("11").build()); + Assertions.fail(); + }); } public static class MyException extends RuntimeException { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/AnnotationExceptionHandlerResolverTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/AnnotationExceptionHandlerResolverTest.java index 087fae6857..1688f7dd0a 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/AnnotationExceptionHandlerResolverTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/AnnotationExceptionHandlerResolverTest.java @@ -22,8 +22,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.ArrayUtils; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Tests for AnnotationExceptionHandlerResolver @@ -33,11 +33,13 @@ public class AnnotationExceptionHandlerResolverTest { /** * Ambiguous ExceptionHandlers */ - @Test(expected = IllegalStateException.class) + @Test public void testDetectExceptionHandlers() { - MyAnnotationExceptionHandlerResolver resolver = new MyAnnotationExceptionHandlerResolver(); - resolver.detectExceptionHandlers(new MyHandlerBean()); - Assert.fail(); + Assertions.assertThrows(IllegalStateException.class, () -> { + MyAnnotationExceptionHandlerResolver resolver = new MyAnnotationExceptionHandlerResolver(); + resolver.detectExceptionHandlers(new MyHandlerBean()); + Assertions.fail(); + }); } /** @@ -50,7 +52,7 @@ public void testNullPointerException() throws Throwable { resolver.detectExceptionHandlers(new MyHandlerBean()); ExceptionHandler handler = resolver.resolveExceptionHandler(new NullPointerException(), null, null); Object result = handler.handle(new NullPointerException(), null, null); - Assert.assertEquals(true, result.toString().contains("handle")); + Assertions.assertEquals(true, result.toString().contains("handle")); } /** @@ -63,7 +65,7 @@ public void testIllegalArgumentException() throws Throwable { resolver.detectExceptionHandlers(new MyHandlerBean()); ExceptionHandler handler = resolver.resolveExceptionHandler(new IllegalArgumentException(), null, null); Object result = handler.handle(new IllegalArgumentException(), null, null); - Assert.assertEquals(true, result.toString().contains("handle")); + Assertions.assertEquals(true, result.toString().contains("handle")); } /** @@ -76,7 +78,7 @@ public void testIllegalStateException() throws Throwable { resolver.detectExceptionHandlers(new MyHandlerBean()); ExceptionHandler handler = resolver.resolveExceptionHandler(new IllegalStateException(), null, null); Object result = handler.handle(new IllegalStateException(), null, null); - Assert.assertEquals("handle3", result); + Assertions.assertEquals("handle3", result); } @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/DefaultExceptionResultTransformerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/DefaultExceptionResultTransformerTest.java index 8dc8e7f315..d078d8d161 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/DefaultExceptionResultTransformerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/DefaultExceptionResultTransformerTest.java @@ -15,8 +15,8 @@ import com.tencent.trpc.spring.exception.TestMsg.Resp; import com.tencent.trpc.spring.exception.TestMsg.Response; import java.util.List; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * Test for exception-handling result transformer @@ -31,8 +31,8 @@ public void testObjectToMessage() { DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); MyResponse myResponse = new MyResponse("testObjectToMessage", "123456"); Response response = (Response) transform.transform(myResponse, Response.class); - Assert.assertEquals(myResponse.getResult(), response.getResult()); - Assert.assertEquals(myResponse.getResInfo(), response.getResInfo()); + Assertions.assertEquals(myResponse.getResult(), response.getResult()); + Assertions.assertEquals(myResponse.getResInfo(), response.getResInfo()); } /** @@ -43,8 +43,8 @@ public void testObjectToObject() { DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); MyResponse myResponse = new MyResponse("testObjectToObject", "123456"); MyResp response = (MyResp) transform.transform(myResponse, MyResp.class); - Assert.assertEquals(myResponse.getResult(), response.getResult()); - Assert.assertEquals(myResponse.getResInfo(), response.getResInfo()); + Assertions.assertEquals(myResponse.getResult(), response.getResult()); + Assertions.assertEquals(myResponse.getResInfo(), response.getResInfo()); } /** @@ -55,8 +55,8 @@ public void testMessageToObject() { DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); Response response = Response.newBuilder().setResult("testMessageToObject").setResInfo("123456").build(); MyResponse myResponse = (MyResponse) transform.transform(response, MyResponse.class); - Assert.assertEquals(response.getResult(), myResponse.getResult()); - Assert.assertEquals(response.getResInfo(), myResponse.getResInfo()); + Assertions.assertEquals(response.getResult(), myResponse.getResult()); + Assertions.assertEquals(response.getResInfo(), myResponse.getResInfo()); } /** @@ -68,22 +68,24 @@ public void testMessageToMessage() { Response response = Response.newBuilder().setResult("testMessageToMessage").setResInfo("123456") .addMessage(Message.newBuilder().setKey("myKey").setValue("myValue").build()).build(); Resp resp = (Resp) transform.transform(response, Resp.class); - Assert.assertEquals("", resp.getRetCode()); - Assert.assertEquals("", resp.getRetMsg()); - Assert.assertEquals(1, resp.getMessageCount()); - Assert.assertEquals("myKey", resp.getMessage(0).getKey()); - Assert.assertEquals("myValue", resp.getMessage(0).getValue()); + Assertions.assertEquals("", resp.getRetCode()); + Assertions.assertEquals("", resp.getRetMsg()); + Assertions.assertEquals(1, resp.getMessageCount()); + Assertions.assertEquals("myKey", resp.getMessage(0).getKey()); + Assertions.assertEquals("myValue", resp.getMessage(0).getValue()); } /** * Test unsupported result type */ - @Test(expected = UnsupportedOperationException.class) + @Test public void testUnsupported() { - DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); - MyResponse myResponse = new MyResponse("testObjectToMessage", "123456"); - Object result = transform.transform(myResponse, List.class); - Assert.fail(); + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); + MyResponse myResponse = new MyResponse("testObjectToMessage", "123456"); + Object result = transform.transform(myResponse, List.class); + Assertions.fail(); + }); } /** @@ -93,7 +95,7 @@ public void testUnsupported() { public void testTransformNull() { DefaultExceptionResultTransformer transform = new DefaultExceptionResultTransformer(); Object result = transform.transform(null, null); - Assert.assertNull(result); + Assertions.assertNull(result); } public static class MyResponse { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/InvocableExceptionHandlerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/InvocableExceptionHandlerTest.java index 2854c55e0e..4e5aefc24b 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/InvocableExceptionHandlerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/exception/support/InvocableExceptionHandlerTest.java @@ -13,8 +13,8 @@ import java.lang.reflect.Method; import java.util.HashSet; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.core.MethodParameter; /** @@ -34,12 +34,12 @@ public void testInvocableExceptionHandler() throws Exception { InvocableExceptionHandler handler2 = new InvocableExceptionHandler(bean, method); HashSet set = new HashSet<>(); set.add(handler1); - Assert.assertEquals(handler1.hashCode(), handler2.hashCode()); - Assert.assertEquals(true, handler1.equals(handler1)); - Assert.assertEquals(true, handler1.equals(handler2)); - Assert.assertEquals(false, handler1.equals(bean)); - Assert.assertEquals(true, set.contains(handler2)); - Assert.assertEquals(MyExceptionHandle.class, handler1.getTargetType()); + Assertions.assertEquals(handler1.hashCode(), handler2.hashCode()); + Assertions.assertEquals(true, handler1.equals(handler1)); + Assertions.assertEquals(true, handler1.equals(handler2)); + Assertions.assertEquals(false, handler1.equals(bean)); + Assertions.assertEquals(true, set.contains(handler2)); + Assertions.assertEquals(MyExceptionHandle.class, handler1.getTargetType()); } /** @@ -52,16 +52,15 @@ public void testHandleException() throws Throwable { .getDeclaredMethod("handle", MyBean.class, RuntimeException.class, Method.class); MyExceptionHandle bean = new MyExceptionHandle(); InvocableExceptionHandler handler = new InvocableExceptionHandler(bean, invokeMethod); - handler.setParameterNameDiscoverer(null); MethodParameter[] methodParameters = handler.getMethodParameters(); Object result = handler.handle(new IllegalArgumentException(), targetMethod, new Object[]{new MyBean("name1"), new MyBean("name2")}); - Assert.assertEquals(3, methodParameters.length); - Assert.assertEquals("myBean1", methodParameters[0].getParameterName()); - Assert.assertEquals("e", methodParameters[1].getParameterName()); - Assert.assertEquals("method11111", methodParameters[2].getParameterName()); - Assert.assertEquals(true, String.class.isAssignableFrom(result.getClass())); - Assert.assertEquals("name1_IllegalArgumentException_targetMethod", result); + Assertions.assertEquals(3, methodParameters.length); + Assertions.assertEquals(MyBean.class, methodParameters[0].getParameterType()); + Assertions.assertEquals(RuntimeException.class, methodParameters[1].getParameterType()); + Assertions.assertEquals(Method.class, methodParameters[2].getParameterType()); + Assertions.assertEquals(true, String.class.isAssignableFrom(result.getClass())); + Assertions.assertEquals("name1_IllegalArgumentException_targetMethod", result); } public static class MyExceptionHandle { diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/util/TRpcSpringUtilsTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/util/TRpcSpringUtilsTest.java index 66416f7e68..2a725585e6 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/util/TRpcSpringUtilsTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/util/TRpcSpringUtilsTest.java @@ -17,9 +17,8 @@ import com.tencent.trpc.core.rpc.anno.TRpcService; import com.tencent.trpc.spring.test.TestSpringApplication; import com.tencent.trpc.spring.util.TRpcSpringUtilsTest.BeanConfiguration; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.context.SpringBootTest; @@ -28,9 +27,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest(classes = TestSpringApplication.class) @ContextConfiguration(classes = BeanConfiguration.class) public class TRpcSpringUtilsTest { @@ -51,8 +48,8 @@ public void testSetRef() throws Exception { TRpcSpringUtils.setRef(context, config); - Assert.assertEquals(TestService.class, config.getServiceInterface()); - Assert.assertEquals(context.getBean(TestService.class), config.getRef()); + Assertions.assertEquals(TestService.class, config.getServiceInterface()); + Assertions.assertEquals(context.getBean(TestService.class), config.getRef()); } @TRpcService(name = "testService") diff --git a/trpc-spring-support/trpc-springmvc/pom.xml b/trpc-spring-support/trpc-springmvc/pom.xml index f59438074f..4a1cad0530 100644 --- a/trpc-spring-support/trpc-springmvc/pom.xml +++ b/trpc-spring-support/trpc-springmvc/pom.xml @@ -12,10 +12,6 @@ trpc-springmvc trpc-springmvc - - 2.2.2.RELEASE - - com.tencent.trpc @@ -58,5 +54,17 @@ test + + org.apache.httpcomponents.client5 + httpclient5 + + + org.apache.httpcomponents.core5 + httpcore5 + + + org.apache.httpcomponents.core5 + httpcore5-h2 + \ No newline at end of file diff --git a/trpc-spring-support/trpc-springmvc/src/test/java/com/tencent/trpc/springmvc/TRpcHttpTests.java b/trpc-spring-support/trpc-springmvc/src/test/java/com/tencent/trpc/springmvc/TRpcHttpTests.java index 8b1f45a9a5..7ab793ffd6 100644 --- a/trpc-spring-support/trpc-springmvc/src/test/java/com/tencent/trpc/springmvc/TRpcHttpTests.java +++ b/trpc-spring-support/trpc-springmvc/src/test/java/com/tencent/trpc/springmvc/TRpcHttpTests.java @@ -27,22 +27,19 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.client.RestTemplate; import tests.TrpcSpringmvcDemoApplication; import tests.proto.HelloRequestProtocol; -@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = TrpcSpringmvcDemoApplication.class, - webEnvironment = WebEnvironment.RANDOM_PORT) + webEnvironment = WebEnvironment.DEFINED_PORT) public class TRpcHttpTests { private static final Logger logger = LoggerFactory.getLogger(TRpcHttpTests.class); @@ -63,7 +60,7 @@ public void testNotFoundService() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(404, responseCode); + Assertions.assertEquals(404, responseCode); } finally { if (connection != null) { connection.disconnect(); @@ -86,7 +83,7 @@ public void testNotSpecifiedRouter() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(404, responseCode); + Assertions.assertEquals(404, responseCode); } finally { if (connection != null) { connection.disconnect(); @@ -109,7 +106,7 @@ public void testNotSpecifiedEnoughRouter() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(404, responseCode); + Assertions.assertEquals(404, responseCode); } finally { if (connection != null) { connection.disconnect(); @@ -151,7 +148,7 @@ private void testPostByJson(String baseUrl, String service, int expectStatus) th int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(expectStatus, responseCode); + Assertions.assertEquals(expectStatus, responseCode); if (expectStatus == 404) { return; } @@ -204,7 +201,7 @@ public void testPostByJsonWithRest() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -256,7 +253,7 @@ public void testPostByPB() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(responseCode, 200); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -307,7 +304,7 @@ public void testPostMapByJson() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -352,7 +349,7 @@ public void testGet() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -393,7 +390,7 @@ public void testGetWithDefaultBasePath() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -434,7 +431,7 @@ public void testGetWithJavaBean() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -475,7 +472,7 @@ public void testGetMap() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(responseCode, 200); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); @@ -515,7 +512,7 @@ public void testUnknownMethod() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(404, responseCode); + Assertions.assertEquals(404, responseCode); } finally { if (connection != null) { connection.disconnect(); @@ -561,7 +558,7 @@ public void testGetWithTrpcHeader() throws Exception { int responseCode = connection.getResponseCode(); logger.info("response code is {}", responseCode); - Assert.assertEquals(408, responseCode); + Assertions.assertEquals(408, responseCode); } finally { if (connection != null) { connection.disconnect(); @@ -577,7 +574,7 @@ public void testControllerPostPbByJson() throws Exception { String responseStr = restTemplate .postForObject("http://localhost:12347/test", body, String.class); - Assert.assertEquals("{\"message\":\"testControllerPostPbByJsontest\"}", responseStr); + Assertions.assertEquals("{\"message\":\"testControllerPostPbByJsontest\"}", responseStr); List> messageConverters = restTemplate.getMessageConverters(); messageConverters.add(0, new ProtoJsonHttpMessageConverter<>()); @@ -585,7 +582,7 @@ public void testControllerPostPbByJson() throws Exception { HelloRequestProtocol.HelloResponse response = restTemplate .postForObject("http://localhost:12347/test", body, HelloRequestProtocol.HelloResponse.class); - Assert.assertEquals("testControllerPostPbByJsontest", response.getMessage()); + Assertions.assertEquals("testControllerPostPbByJsontest", response.getMessage()); URL url = new URL("http://localhost:12347/test"); HttpURLConnection connection = null; @@ -606,13 +603,13 @@ public void testControllerPostPbByJson() throws Exception { out.close(); int responseCode = connection.getResponseCode(); - Assert.assertEquals(200, responseCode); + Assertions.assertEquals(200, responseCode); in = connection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamUtils.copy(in, bos); - Assert.assertEquals("{\"message\":\"你好TRpc-Java!test\"}", + Assertions.assertEquals("{\"message\":\"你好TRpc-Java!test\"}", new String(bos.toByteArray(), StandardCharsets.UTF_8)); } finally { if (out != null) { diff --git a/trpc-spring-support/trpc-springmvc/src/test/java/tests/TrpcSpringmvcDemoApplication.java b/trpc-spring-support/trpc-springmvc/src/test/java/tests/TrpcSpringmvcDemoApplication.java index f99ac2622a..0e4f89711c 100644 --- a/trpc-spring-support/trpc-springmvc/src/test/java/tests/TrpcSpringmvcDemoApplication.java +++ b/trpc-spring-support/trpc-springmvc/src/test/java/tests/TrpcSpringmvcDemoApplication.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * Copyright (C) 2023 THL A29 Limited, a Tencent company. * All rights reserved. * * If you have downloaded a copy of the tRPC source code from Tencent, diff --git a/trpc-spring-support/trpc-springmvc/src/test/resources/META-INF/trpc/trpc_java.yaml b/trpc-spring-support/trpc-springmvc/src/test/resources/META-INF/trpc/trpc_java.yaml index 4bbb326379..cdc0020c29 100644 --- a/trpc-spring-support/trpc-springmvc/src/test/resources/META-INF/trpc/trpc_java.yaml +++ b/trpc-spring-support/trpc-springmvc/src/test/resources/META-INF/trpc/trpc_java.yaml @@ -14,7 +14,7 @@ server: impls: - tests.service.impl.GreeterServiceImpl #service协议接口实现类 ip: 127.0.0.1 - port: 12345 #port + port: 12445 #port network: tcp #tcp/udp - name: trpc.TestApp.TestServer.Greeter2Naming #service的id,附带naming的含义 impls: