YongGoose commented on code in PR #7398: URL: https://github.com/apache/incubator-seata/pull/7398#discussion_r2118471509
########## spring/src/test/java/org/apache/seata/spring/tcc/TccAnnotationProcessorTest.java: ########## @@ -0,0 +1,171 @@ +/* + * 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.tcc; + +import org.apache.seata.integration.tx.api.util.ProxyUtil; +import org.apache.seata.rm.tcc.api.TwoPhaseBusinessAction; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.junit.jupiter.api.Assertions.*; + +public class TccAnnotationProcessorTest { + + private TccAnnotationProcessor processor; + + @BeforeEach + public void setUp() { + processor = new TccAnnotationProcessor(); + clearStaticFields(); + } + + private void clearStaticFields() { + try { + Field proxiedField = TccAnnotationProcessor.class.getDeclaredField("PROXIED_SET"); + proxiedField.setAccessible(true); + ((Set<String>) proxiedField.get(null)).clear(); + + Field annotationsField = TccAnnotationProcessor.class.getDeclaredField("ANNOTATIONS"); + annotationsField.setAccessible(true); + ((java.util.List<Class<? extends Annotation>>) annotationsField.get(null)).clear(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @interface MockReference { + } + + static class MockTccService { + @TwoPhaseBusinessAction(name = "testAction") + public void tryMethod() { + } + } + + static class TestBean { + @MockReference + public MockTccService tccService = new MockTccService(); + + @MockReference + public MockTccService nullService = null; + } + + @Test + public void testAddTccAdviseCreatesProxy() throws Exception { + TestBean bean = new TestBean(); + Field field = TestBean.class.getField("tccService"); + + Object originalValue = field.get(bean); + + try (MockedStatic<ProxyUtil> mockedStatic = Mockito.mockStatic(ProxyUtil.class)) { + mockedStatic.when(() -> ProxyUtil.createProxy(bean, "testBean")) + .thenAnswer(invocation -> { + Object arg = invocation.getArgument(0); + if (arg instanceof TestBean) { + return Mockito.spy(new MockTccService()); + } + return arg; + }); + + processor.addTccAdvise(bean, "testBean", field, MockTccService.class); + } + + Object newValue = field.get(bean); + assertNotEquals(originalValue, newValue, "Proxy should replace original field"); + } + + @Test + public void testAddTccAdviseFieldValueNull() throws Exception { + TestBean bean = new TestBean(); + Field nullField = TestBean.class.getField("nullService"); + // 应该安全返回,不抛异常 + processor.addTccAdvise(bean, "testBean", nullField, MockTccService.class); + assertNull(nullField.get(bean)); Review Comment: Since fieldValue is null, I assume the log won’t be printed. It might also be a good idea to add a test to verify that the log is not printed in this case!! ########## spring/src/test/java/org/apache/seata/spring/tcc/TccAnnotationProcessorTest.java: ########## @@ -0,0 +1,171 @@ +/* + * 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.tcc; + +import org.apache.seata.integration.tx.api.util.ProxyUtil; +import org.apache.seata.rm.tcc.api.TwoPhaseBusinessAction; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.junit.jupiter.api.Assertions.*; + +public class TccAnnotationProcessorTest { + + private TccAnnotationProcessor processor; + + @BeforeEach + public void setUp() { + processor = new TccAnnotationProcessor(); + clearStaticFields(); + } + + private void clearStaticFields() { + try { + Field proxiedField = TccAnnotationProcessor.class.getDeclaredField("PROXIED_SET"); + proxiedField.setAccessible(true); + ((Set<String>) proxiedField.get(null)).clear(); + + Field annotationsField = TccAnnotationProcessor.class.getDeclaredField("ANNOTATIONS"); + annotationsField.setAccessible(true); + ((java.util.List<Class<? extends Annotation>>) annotationsField.get(null)).clear(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @interface MockReference { + } + + static class MockTccService { + @TwoPhaseBusinessAction(name = "testAction") + public void tryMethod() { + } + } + + static class TestBean { + @MockReference + public MockTccService tccService = new MockTccService(); + + @MockReference + public MockTccService nullService = null; + } + + @Test + public void testAddTccAdviseCreatesProxy() throws Exception { + TestBean bean = new TestBean(); + Field field = TestBean.class.getField("tccService"); + + Object originalValue = field.get(bean); + + try (MockedStatic<ProxyUtil> mockedStatic = Mockito.mockStatic(ProxyUtil.class)) { + mockedStatic.when(() -> ProxyUtil.createProxy(bean, "testBean")) + .thenAnswer(invocation -> { + Object arg = invocation.getArgument(0); + if (arg instanceof TestBean) { + return Mockito.spy(new MockTccService()); + } + return arg; + }); + + processor.addTccAdvise(bean, "testBean", field, MockTccService.class); + } + + Object newValue = field.get(bean); + assertNotEquals(originalValue, newValue, "Proxy should replace original field"); + } + + @Test + public void testAddTccAdviseFieldValueNull() throws Exception { + TestBean bean = new TestBean(); + Field nullField = TestBean.class.getField("nullService"); + // 应该安全返回,不抛异常 + processor.addTccAdvise(bean, "testBean", nullField, MockTccService.class); + assertNull(nullField.get(bean)); + } + + @Test + public void testProcessWithNullAnnotation() throws Exception { + processor.process(new TestBean(), "testBean", null); + Field proxiedField = TccAnnotationProcessor.class.getDeclaredField("PROXIED_SET"); + proxiedField.setAccessible(true); + Set<String> proxied = (Set<String>) proxiedField.get(null); Review Comment: What do you think about extracting proxied as a global variable? We could assign it in a `@BeforeEach` and reset it in an `@AfterEach` — that might help improve clarity and maintainability. ########## spring/src/test/java/org/apache/seata/spring/tcc/TccAnnotationProcessorTest.java: ########## @@ -0,0 +1,171 @@ +/* + * 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.tcc; + +import org.apache.seata.integration.tx.api.util.ProxyUtil; +import org.apache.seata.rm.tcc.api.TwoPhaseBusinessAction; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.junit.jupiter.api.Assertions.*; + +public class TccAnnotationProcessorTest { + + private TccAnnotationProcessor processor; + + @BeforeEach + public void setUp() { + processor = new TccAnnotationProcessor(); + clearStaticFields(); + } + + private void clearStaticFields() { + try { + Field proxiedField = TccAnnotationProcessor.class.getDeclaredField("PROXIED_SET"); + proxiedField.setAccessible(true); + ((Set<String>) proxiedField.get(null)).clear(); + + Field annotationsField = TccAnnotationProcessor.class.getDeclaredField("ANNOTATIONS"); + annotationsField.setAccessible(true); + ((java.util.List<Class<? extends Annotation>>) annotationsField.get(null)).clear(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @interface MockReference { + } + + static class MockTccService { + @TwoPhaseBusinessAction(name = "testAction") + public void tryMethod() { + } + } + + static class TestBean { + @MockReference + public MockTccService tccService = new MockTccService(); + + @MockReference + public MockTccService nullService = null; + } + + @Test + public void testAddTccAdviseCreatesProxy() throws Exception { + TestBean bean = new TestBean(); + Field field = TestBean.class.getField("tccService"); + + Object originalValue = field.get(bean); + + try (MockedStatic<ProxyUtil> mockedStatic = Mockito.mockStatic(ProxyUtil.class)) { + mockedStatic.when(() -> ProxyUtil.createProxy(bean, "testBean")) + .thenAnswer(invocation -> { + Object arg = invocation.getArgument(0); + if (arg instanceof TestBean) { + return Mockito.spy(new MockTccService()); + } + return arg; + }); + + processor.addTccAdvise(bean, "testBean", field, MockTccService.class); + } + + Object newValue = field.get(bean); + assertNotEquals(originalValue, newValue, "Proxy should replace original field"); Review Comment: I think it might also be helpful to check whether the logs are being printed correctly. WDYT? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org