This is an automated email from the ASF dual-hosted git repository.
jimin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new 4aa446ee3e test: add test for SpringProxyUtils and OrderUtils (#7400)
4aa446ee3e is described below
commit 4aa446ee3ef10f5eb37b8c40b69658a8a89aa931
Author: Oscar Cheng <[email protected]>
AuthorDate: Mon Jun 9 00:21:54 2025 +0800
test: add test for SpringProxyUtils and OrderUtils (#7400)
---
.../apache/seata/spring/util/OrderUtilTest.java | 27 +-
.../seata/spring/util/SpringProxyUtilsTest.java | 302 +++++++++++++++++++++
2 files changed, 328 insertions(+), 1 deletion(-)
diff --git
a/spring/src/test/java/org/apache/seata/spring/util/OrderUtilTest.java
b/spring/src/test/java/org/apache/seata/spring/util/OrderUtilTest.java
index 9c5e281350..69096959d3 100644
--- a/spring/src/test/java/org/apache/seata/spring/util/OrderUtilTest.java
+++ b/spring/src/test/java/org/apache/seata/spring/util/OrderUtilTest.java
@@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* The type OrderUtil test
- *
*/
public class OrderUtilTest {
@@ -65,6 +64,17 @@ public class OrderUtilTest {
assertThat(OrderUtil.lowerOrEquals(advisor21, advisor11)).isTrue();
assertThat(OrderUtil.lowerOrEquals(advisor12, advisor11)).isTrue();
assertThat(OrderUtil.lowerOrEquals(advisor22, advisor21)).isTrue();
+
+ assertThat(OrderUtil.lowerThan(null, 0)).isTrue();
+ assertThat(OrderUtil.lowerThan(0, null)).isFalse();
+ assertThat(OrderUtil.lowerThan((Integer) null, null)).isFalse();
+ }
+
+ @Test
+ public void test_lowerOrEquals_nullCases() {
+ assertThat(OrderUtil.lowerOrEquals(null, 0)).isTrue(); // MAX >= 0
+ assertThat(OrderUtil.lowerOrEquals(0, null)).isFalse(); // 0 >= MAX →
false
+ assertThat(OrderUtil.lowerOrEquals((Integer) null, null)).isTrue(); //
MAX >= MAX
}
@Test
@@ -90,6 +100,18 @@ public class OrderUtilTest {
assertThat(OrderUtil.higherOrEquals(advisor11, advisor21)).isTrue();
assertThat(OrderUtil.higherOrEquals(advisor11, advisor12)).isTrue();
assertThat(OrderUtil.higherOrEquals(advisor21, advisor22)).isTrue();
+
+ assertThat(OrderUtil.higherThan(null, 0)).isFalse();
+ assertThat(OrderUtil.higherThan(0, null)).isTrue();
+ assertThat(OrderUtil.higherThan((Integer) null, null)).isFalse();
+ }
+
+
+ @Test
+ public void test_higherOrEquals_nullCases() {
+ assertThat(OrderUtil.higherOrEquals(null, 0)).isFalse(); //MAX >= 0
+ assertThat((OrderUtil.higherOrEquals(0, null))).isTrue(); // 0 <= MAX
+ assertThat(OrderUtil.higherOrEquals((Integer) null, null)).isTrue();
// MAX <= MAX
}
@Test
@@ -99,6 +121,7 @@ public class OrderUtilTest {
assertThat(OrderUtil.lower(Ordered.LOWEST_PRECEDENCE,
1)).isEqualTo(Ordered.LOWEST_PRECEDENCE);
Assertions.assertThrows(IllegalArgumentException.class, () ->
OrderUtil.lower(1, -1));
+ assertThat(OrderUtil.lower(null, 1)).isEqualTo(Integer.MAX_VALUE);
}
@Test
@@ -108,5 +131,7 @@ public class OrderUtilTest {
assertThat(OrderUtil.higher(Ordered.HIGHEST_PRECEDENCE,
1)).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
Assertions.assertThrows(IllegalArgumentException.class, () ->
OrderUtil.higher(1, -1));
+
+ assertThat(OrderUtil.higher(null, 1)).isEqualTo(Integer.MAX_VALUE - 1);
}
}
diff --git
a/spring/src/test/java/org/apache/seata/spring/util/SpringProxyUtilsTest.java
b/spring/src/test/java/org/apache/seata/spring/util/SpringProxyUtilsTest.java
new file mode 100644
index 0000000000..8b1c69c144
--- /dev/null
+++
b/spring/src/test/java/org/apache/seata/spring/util/SpringProxyUtilsTest.java
@@ -0,0 +1,302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.spring.util;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.aop.TargetSource;
+import org.springframework.aop.framework.Advised;
+import org.springframework.aop.framework.AdvisedSupport;
+import org.springframework.aop.framework.ProxyFactory;
+import org.springframework.aop.support.AopUtils;
+
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class SpringProxyUtilsTest {
+
+ interface TestService {
+ void doSomething();
+ }
+
+ static class TestServiceImpl implements TestService {
+ @Override
+ public void doSomething() {
+ // no-op
+ }
+ }
+
+ // Helper: create a “pure” JDK dynamic proxy for TestService
+ private TestService createJdkProxy() {
+ return (TestService) Proxy.newProxyInstance(
+ TestService.class.getClassLoader(),
+ new Class<?>[]{TestService.class},
+ (proxy, method, args) -> null
+ );
+ }
+
+
+ // Tests for findTargetClass
+
+ @Test
+ public void testFindTargetClass_WithNull() throws Exception {
+ assertNull(SpringProxyUtils.findTargetClass(null));
+ }
+
+ @Test
+ public void testFindTargetClass_WithPlainObject() throws Exception {
+ TestServiceImpl plain = new TestServiceImpl();
+ Class<?> result = SpringProxyUtils.findTargetClass(plain);
+ assertEquals(TestServiceImpl.class, result);
+ }
+
+ @Test
+ public void testFindTargetClass_WithJdkProxy() throws Exception {
+ TestService jdkProxy = createJdkProxy();
+ Class<?> result = SpringProxyUtils.findTargetClass(jdkProxy);
+ assertEquals(jdkProxy.getClass(), result);
+ }
+
+ @Test
+ public void testFindTargetClass_WithSpringAopProxy() throws Exception {
+ ProxyFactory factory = new ProxyFactory(new TestServiceImpl());
+ factory.addInterface(TestService.class);
+ TestService springJdkProxy = (TestService) factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(springJdkProxy) &&
AopUtils.isJdkDynamicProxy(springJdkProxy));
+
+ Class<?> result = SpringProxyUtils.findTargetClass(springJdkProxy);
+ assertEquals(TestServiceImpl.class, result);
+ }
+
+
+ // Tests for findInterfaces
+
+ @Test
+ public void testFindInterfaces_WithJdkProxy() throws Exception {
+ ProxyFactory factory = new ProxyFactory(new TestServiceImpl());
+ factory.addInterface(TestService.class);
+ TestService springJdkProxy = (TestService) factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(springJdkProxy) &&
AopUtils.isJdkDynamicProxy(springJdkProxy));
+
+ Class<?>[] ifaces = SpringProxyUtils.findInterfaces(springJdkProxy);
+ assertEquals(1, ifaces.length);
+ assertEquals(TestService.class, ifaces[0]);
+ }
+
+ @Test
+ public void testFindInterfaces_WithPlainObject() throws Exception {
+ TestServiceImpl plain = new TestServiceImpl();
+ Class<?>[] ifaces = SpringProxyUtils.findInterfaces(plain);
+ assertNotNull(ifaces);
+ assertEquals(0, ifaces.length);
+ }
+
+ // Tests for getAdvisedSupport
+
+ @Test
+ public void testGetAdvisedSupport_WithSpringJdkProxy() throws Exception {
+ TestServiceImpl target = new TestServiceImpl();
+ ProxyFactory factory = new ProxyFactory(target);
+ factory.addInterface(TestService.class);
+ TestService springProxy = (TestService) factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(springProxy));
+ assertTrue(springProxy instanceof Advised);
+
+ AdvisedSupport advisedSupport =
SpringProxyUtils.getAdvisedSupport(springProxy);
+ assertNotNull(advisedSupport);
+ Object actualTarget = advisedSupport.getTargetSource().getTarget();
+ assertTrue(actualTarget instanceof TestServiceImpl);
+ }
+
+ @Test
+ public void testGetAdvisedSupport_WithCglibProxy() throws Exception {
+ // Define a concrete class with no interfaces to force CGLIB proxy
+ class NoInterfaceTarget {
+ public void sayHello() {
+ }
+ }
+
+ NoInterfaceTarget target = new NoInterfaceTarget();
+ ProxyFactory factory = new ProxyFactory(target);
+ factory.setProxyTargetClass(true); // force CGLIB instead of JDK
dynamic proxy
+ Object cglibProxy = factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(cglibProxy));
+ assertFalse(AopUtils.isJdkDynamicProxy(cglibProxy)); // must be false
to enter CGLIB branch
+
+ AdvisedSupport advisedSupport =
SpringProxyUtils.getAdvisedSupport(cglibProxy);
+ assertNotNull(advisedSupport);
+
+ Object realTarget = advisedSupport.getTargetSource().getTarget();
+ assertTrue(realTarget instanceof NoInterfaceTarget);
+ }
+
+ // Tests for isProxy(Object)
+
+ @Test
+ public void testIsProxy_WithNull() {
+ assertFalse(SpringProxyUtils.isProxy(null));
+ }
+
+ @Test
+ public void testIsProxy_WithPlainObject() {
+ TestServiceImpl plain = new TestServiceImpl();
+ assertFalse(SpringProxyUtils.isProxy(plain));
+ }
+
+ @Test
+ public void testIsProxy_WithJdkProxy() {
+ TestService jdkProxy = createJdkProxy();
+ assertTrue(SpringProxyUtils.isProxy(jdkProxy));
+ }
+
+ @Test
+ public void testIsProxy_WithSpringAopProxy() {
+ ProxyFactory factory = new ProxyFactory(new TestServiceImpl());
+ factory.addInterface(TestService.class);
+ TestService springProxy = (TestService) factory.getProxy();
+ assertTrue(SpringProxyUtils.isProxy(springProxy));
+ }
+
+
+ // Tests for getTargetInterface
+
+ @Test
+ public void testGetTargetInterface_WithNull() {
+ assertThrows(IllegalArgumentException.class, () ->
SpringProxyUtils.getTargetInterface(null));
+ }
+
+ @Test
+ public void testGetTargetInterface_WithJdkProxy() throws Exception {
+ TestService jdkProxy = createJdkProxy();
+ Class<?> iface = SpringProxyUtils.getTargetInterface(jdkProxy);
+ assertEquals(TestService.class, iface);
+ }
+
+ @Test
+ public void testGetTargetInterface_WithPlainObject() throws Exception {
+ TestServiceImpl plain = new TestServiceImpl();
+ Class<?> clazz = SpringProxyUtils.getTargetInterface(plain);
+ assertEquals(TestServiceImpl.class, clazz);
+ }
+
+
+ // Tests for getAllInterfaces
+ @Test
+ public void testGetAllInterfaces_WithNull() {
+ Class<?>[] result = SpringProxyUtils.getAllInterfaces(null);
+ assertNotNull(result);
+ assertEquals(0, result.length);
+ }
+
+ @Test
+ public void testGetAllInterfaces_WithSingleInterface() {
+ TestServiceImpl single = new TestServiceImpl();
+ Class<?>[] ifaces = SpringProxyUtils.getAllInterfaces(single);
+ assertNotNull(ifaces);
+ assertEquals(1, ifaces.length);
+ assertEquals(TestService.class, ifaces[0]);
+ }
+
+ @Test
+ public void testGetAllInterfaces_WithMultipleInterfaces() {
+ class MultiInterfaceImpl implements TestService, Runnable {
+ @Override
+ public void doSomething() {
+ }
+ @Override
+ public void run() {
+ }
+ }
+ MultiInterfaceImpl multi = new MultiInterfaceImpl();
+ Class<?>[] ifaces = SpringProxyUtils.getAllInterfaces(multi);
+ assertNotNull(ifaces);
+ assertEquals(2, ifaces.length);
+ assertTrue(Arrays.asList(ifaces).contains(TestService.class));
+ assertTrue(Arrays.asList(ifaces).contains(Runnable.class));
+ }
+
+
+ // Tests for getTargetClass
+ @Test
+ public void testGetTargetClass_WithNull() throws Exception {
+ assertNull(SpringProxyUtils.findTargetClass(null));
+ }
+
+ @Test
+ public void testGetTargetClass_WithPlainObject() throws Exception {
+ TestServiceImpl plain = new TestServiceImpl();
+ Class<?> result = SpringProxyUtils.findTargetClass(plain);
+ assertEquals(TestServiceImpl.class, result);
+ }
+
+ @Test
+ public void testGetTargetClass_WithJdkProxy() throws Exception {
+ TestService jdkProxy = createJdkProxy();
+ Class<?> result = SpringProxyUtils.findTargetClass(jdkProxy);
+ assertEquals(jdkProxy.getClass(), result);
+ }
+
+ @Test
+ public void testGetTargetClass_WithSpringAopProxy() throws Exception {
+ ProxyFactory factory = new ProxyFactory(new TestServiceImpl());
+ factory.addInterface(TestService.class);
+ TestService springJdkProxy = (TestService) factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(springJdkProxy) &&
AopUtils.isJdkDynamicProxy(springJdkProxy));
+
+ Class<?> result = SpringProxyUtils.findTargetClass(springJdkProxy);
+ assertEquals(TestServiceImpl.class, result);
+ }
+
+ @Test
+ public void testGetTargetClass_WithNullTargetSource() throws Exception {
+ class NullTargetSource implements TargetSource {
+ @Override
+ public Class<?> getTargetClass() {
+ return TestService.class;
+ }
+ @Override
+ public boolean isStatic() {
+ return true;
+ }
+ @Override
+ public Object getTarget() {
+ return null;
+ }
+ @Override
+ public void releaseTarget(Object target) {
+ // no-op
+ }
+ }
+
+ ProxyFactory factory = new ProxyFactory();
+ factory.addInterface(TestService.class);
+ factory.setTargetSource(new NullTargetSource());
+ TestService proxyWithNullTarget = (TestService) factory.getProxy();
+
+ assertTrue(AopUtils.isAopProxy(proxyWithNullTarget));
+
+ Class<?> result =
SpringProxyUtils.findTargetClass(proxyWithNullTarget);
+ assertNull(result);
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]