Copilot commented on code in PR #7423:
URL: https://github.com/apache/incubator-seata/pull/7423#discussion_r2134693720


##########
spring/src/test/java/org/apache/seata/spring/annotation/scannercheckers/PackageScannerCheckerTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.annotation.scannercheckers;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import 
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+
+import java.lang.reflect.Field;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Test for {@link PackageScannerChecker} class
+ */
+public class PackageScannerCheckerTest {
+
+    private final PackageScannerChecker checker = new PackageScannerChecker();
+    private final ConfigurableListableBeanFactory beanFactory = null;
+    private Set<String> originalPackages;
+
+    /**
+     * Setup before each test - save original packages and clear the set
+     */
+    @BeforeEach
+    public void setUp() throws Exception {
+        // Save original packages
+        originalPackages = getScannablePackageSet();
+        
+        // Clear the package set for testing
+        clearScannablePackageSet();
+    }
+
+    /**
+     * Cleanup after each test - restore original packages
+     */
+    @AfterEach
+    public void tearDown() throws Exception {
+        // Restore original packages
+        clearScannablePackageSet();
+        if (originalPackages != null) {
+            for (String pkg : originalPackages) {
+                PackageScannerChecker.addScannablePackages(pkg);
+            }
+        }
+    }
+
+    /**
+     * Test check method when package set is empty
+     */
+    @Test
+    public void testCheckWhenPackageSetEmpty() throws Exception {
+        // When package set is empty, should return true for any bean
+        Object testBean = new Object();
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when package set is 
empty");
+    }
+
+    /**
+     * Test check method with matching package
+     */
+    @Test
+    public void testCheckWithMatchingPackage() throws Exception {
+        // Add test packages
+        PackageScannerChecker.addScannablePackages("org.apache.seata");
+        
+        // Test with bean in the scannable package
+        TestBean testBean = new TestBean();
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when bean's package 
matches scannable package");
+    }
+
+    /**
+     * Test check method with non-matching package
+     */
+    @Test
+    public void testCheckWithNonMatchingPackage() throws Exception {
+        // Add test packages
+        PackageScannerChecker.addScannablePackages("com.example");
+        
+        // Test with bean not in the scannable package
+        TestBean testBean = new TestBean();
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertFalse(result, "Should return false when bean's 
package doesn't match scannable package");
+    }
+
+    /**
+     * Test addScannablePackages method
+     */
+    @Test
+    public void testAddScannablePackages() throws Exception {
+        // Add test packages
+        PackageScannerChecker.addScannablePackages("com.example", 
"org.apache.seata");
+        
+        // Verify packages were added
+        Set<String> packages = getScannablePackageSet();
+        Assertions.assertEquals(2, packages.size(), "Should have 2 packages in 
the set");
+        Assertions.assertTrue(packages.contains("com.example"), "Should 
contain 'com.example'");
+        Assertions.assertTrue(packages.contains("org.apache.seata"), "Should 
contain 'org.apache.seata'");
+    }
+
+    /**
+     * Test addScannablePackages with blank packages
+     */
+    @Test
+    public void testAddScannablePackagesWithBlankPackages() throws Exception {
+        // Add test packages including blank ones
+        PackageScannerChecker.addScannablePackages("com.example", "", "  ", 
null);
+        
+        // Verify only non-blank packages were added
+        Set<String> packages = getScannablePackageSet();
+        Assertions.assertEquals(1, packages.size(), "Should have 1 package in 
the set");
+        Assertions.assertTrue(packages.contains("com.example"), "Should 
contain 'com.example'");
+    }
+
+    /**
+     * Test addScannablePackages with null
+     */
+    @Test
+    public void testAddScannablePackagesWithNull() throws Exception {
+        // Add null packages
+        PackageScannerChecker.addScannablePackages((String[])null);
+        
+        // Verify no packages were added
+        Set<String> packages = getScannablePackageSet();
+        Assertions.assertTrue(packages.isEmpty(), "Package set should be 
empty");
+    }
+
+    /**
+     * Test case sensitivity in package names
+     */
+    @Test
+    public void testPackageNameCaseSensitivity() throws Exception {
+        // Add test packages with mixed case
+        PackageScannerChecker.addScannablePackages("Com.Example");
+        
+        // Verify package was added in lowercase
+        Set<String> packages = getScannablePackageSet();
+        Assertions.assertEquals(1, packages.size(), "Should have 1 package in 
the set");
+        Assertions.assertTrue(packages.contains("com.example"), "Should 
contain lowercase 'com.example'");
+        Assertions.assertFalse(packages.contains("Com.Example"), "Should not 
contain 'Com.Example' with original case");
+    }
+
+    /**
+     * Helper method to get the SCANNABLE_PACKAGE_SET field value
+     */
+    @SuppressWarnings("unchecked")
+    private Set<String> getScannablePackageSet() throws Exception {

Review Comment:
   Consider extracting the reflection-based helper methods for field access 
into a shared utility class for improved maintainability and to reduce 
duplication across test files.



##########
spring/src/test/java/org/apache/seata/spring/annotation/scannercheckers/ScopeBeansScannerCheckerTest.java:
##########
@@ -0,0 +1,278 @@
+/*
+ * 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.annotation.scannercheckers;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
+import org.springframework.beans.factory.config.BeanDefinition;
+import 
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.context.annotation.Scope;
+import org.springframework.core.type.AnnotationMetadata;
+import org.springframework.core.type.MethodMetadata;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.lang.reflect.Field;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Test for {@link ScopeBeansScannerChecker} class
+ */
+public class ScopeBeansScannerCheckerTest {
+
+    private final ScopeBeansScannerChecker checker = new 
ScopeBeansScannerChecker();
+    private final Object testBean = new Object();
+    private Set<String> originalExcludeScopes;
+
+    /**
+     * Setup before each test - save original exclude scopes
+     */
+    @BeforeEach
+    public void setUp() throws Exception {
+        // Save original exclude scopes
+        originalExcludeScopes = getExcludeScopeSet();
+    }
+
+    /**
+     * Cleanup after each test - restore original exclude scopes
+     */
+    @AfterEach
+    public void tearDown() throws Exception {
+        // Restore original exclude scopes
+        setExcludeScopeSet(originalExcludeScopes);
+    }
+
+    /**
+     * Test check method when beanFactory is null
+     */
+    @Test
+    public void testCheckWhenBeanFactoryIsNull() throws Exception {
+        boolean result = checker.check(testBean, "testBean", null);
+        Assertions.assertTrue(result, "Should return true when beanFactory is 
null");
+    }
+
+    /**
+     * Test check method when bean definition is not found
+     */
+    @Test
+    public void testCheckWhenBeanDefinitionNotFound() throws Exception {
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString()))
+               .thenThrow(new NoSuchBeanDefinitionException("testBean"));
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when bean definition 
is not found");
+    }
+
+    /**
+     * Test check method when bean definition is not an AnnotatedBeanDefinition
+     */
+    @Test
+    public void testCheckWhenBeanDefinitionIsNotAnnotated() throws Exception {
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        BeanDefinition beanDefinition = Mockito.mock(BeanDefinition.class);
+        
+        
Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString())).thenReturn(beanDefinition);
+        
Mockito.when(beanDefinition.getOriginatingBeanDefinition()).thenReturn(null);
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when bean definition 
is not an AnnotatedBeanDefinition");
+    }
+
+    /**
+     * Test check method with non-excluded scope
+     */
+    @Test
+    public void testCheckWithNonExcludedScope() throws Exception {
+        // Mock objects
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        AnnotatedBeanDefinition beanDefinition = 
Mockito.mock(AnnotatedBeanDefinition.class);
+        AnnotationMetadata metadata = Mockito.mock(AnnotationMetadata.class);
+        
+        // Setup scope attributes with a non-excluded scope
+        MultiValueMap<String, Object> scopeAttributes = new 
LinkedMultiValueMap<>();
+        scopeAttributes.add("scopeName", "singleton");
+        
+        // Setup mock behavior
+        
Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString())).thenReturn(beanDefinition);
+        Mockito.when(beanDefinition.getMetadata()).thenReturn(metadata);
+        
Mockito.when(beanDefinition.getFactoryMethodMetadata()).thenReturn(null);
+        
Mockito.when(metadata.getAllAnnotationAttributes(Mockito.eq(Scope.class.getName()))).thenReturn(scopeAttributes);
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when scope is not 
excluded");
+    }
+
+    /**
+     * Test check method with excluded scope
+     */
+    @Test
+    public void testCheckWithExcludedScope() throws Exception {
+        // Mock objects
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        AnnotatedBeanDefinition beanDefinition = 
Mockito.mock(AnnotatedBeanDefinition.class);
+        AnnotationMetadata metadata = Mockito.mock(AnnotationMetadata.class);
+        
+        // Setup scope attributes with an excluded scope (request)
+        MultiValueMap<String, Object> scopeAttributes = new 
LinkedMultiValueMap<>();
+        scopeAttributes.add("scopeName", 
ScopeBeansScannerChecker.REQUEST_SCOPE_NAME);
+        
+        // Setup mock behavior
+        
Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString())).thenReturn(beanDefinition);
+        Mockito.when(beanDefinition.getMetadata()).thenReturn(metadata);
+        
Mockito.when(beanDefinition.getFactoryMethodMetadata()).thenReturn(null);
+        
Mockito.when(metadata.getAllAnnotationAttributes(Mockito.eq(Scope.class.getName()))).thenReturn(scopeAttributes);
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertFalse(result, "Should return false when scope is 
excluded");
+    }
+
+    /**
+     * Test check method with factory method metadata having excluded scope
+     */
+    @Test
+    public void testCheckWithFactoryMethodHavingExcludedScope() throws 
Exception {
+        // Mock objects
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        AnnotatedBeanDefinition beanDefinition = 
Mockito.mock(AnnotatedBeanDefinition.class);
+        AnnotationMetadata metadata = Mockito.mock(AnnotationMetadata.class);
+        MethodMetadata factoryMethodMetadata = 
Mockito.mock(MethodMetadata.class);
+        
+        // Setup factory method metadata with an excluded scope (session)
+        MultiValueMap<String, Object> factoryScopeAttributes = new 
LinkedMultiValueMap<>();
+        factoryScopeAttributes.add("scopeName", 
ScopeBeansScannerChecker.SESSION_SCOPE_NAME);
+        
+        // Setup mock behavior
+        
Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString())).thenReturn(beanDefinition);
+        Mockito.when(beanDefinition.getMetadata()).thenReturn(metadata);
+        
Mockito.when(beanDefinition.getFactoryMethodMetadata()).thenReturn(factoryMethodMetadata);
+        
Mockito.when(factoryMethodMetadata.getAllAnnotationAttributes(Mockito.eq(Scope.class.getName())))
+               .thenReturn(factoryScopeAttributes);
+        
Mockito.when(metadata.getAllAnnotationAttributes(Mockito.eq(Scope.class.getName()))).thenReturn(null);
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertFalse(result, "Should return false when factory 
method has excluded scope");
+    }
+
+    /**
+     * Test check method with no scope annotation
+     */
+    @Test
+    public void testCheckWithNoScopeAnnotation() throws Exception {
+        // Mock objects
+        ConfigurableListableBeanFactory beanFactory = 
Mockito.mock(ConfigurableListableBeanFactory.class);
+        AnnotatedBeanDefinition beanDefinition = 
Mockito.mock(AnnotatedBeanDefinition.class);
+        AnnotationMetadata metadata = Mockito.mock(AnnotationMetadata.class);
+        
+        // Setup mock behavior
+        
Mockito.when(beanFactory.getBeanDefinition(Mockito.anyString())).thenReturn(beanDefinition);
+        Mockito.when(beanDefinition.getMetadata()).thenReturn(metadata);
+        
Mockito.when(beanDefinition.getFactoryMethodMetadata()).thenReturn(null);
+        
Mockito.when(metadata.getAllAnnotationAttributes(Mockito.eq(Scope.class.getName()))).thenReturn(null);
+        
+        boolean result = checker.check(testBean, "testBean", beanFactory);
+        Assertions.assertTrue(result, "Should return true when no scope 
annotation is present");
+    }
+
+    /**
+     * Test addExcludeScopes method
+     */
+    @Test
+    public void testAddExcludeScopes() throws Exception {
+        // Clear exclude scopes first
+        Set<String> originalSet = getExcludeScopeSet();
+        setExcludeScopeSet(new HashSet<>());
+        
+        // Add exclude scopes
+        ScopeBeansScannerChecker.addExcludeScopes("custom1", "custom2");
+        
+        // Verify scopes were added
+        Set<String> excludeScopes = getExcludeScopeSet();
+        Assertions.assertEquals(2, excludeScopes.size(), "Should have 2 
exclude scopes");
+        Assertions.assertTrue(excludeScopes.contains("custom1"), "Should 
contain 'custom1'");
+        Assertions.assertTrue(excludeScopes.contains("custom2"), "Should 
contain 'custom2'");
+        
+        // Restore original set
+        setExcludeScopeSet(originalSet);
+    }
+
+    /**
+     * Test addExcludeScopes with blank scopes
+     */
+    @Test
+    public void testAddExcludeScopesWithBlankScopes() throws Exception {
+        // Clear exclude scopes first
+        Set<String> originalSet = getExcludeScopeSet();
+        setExcludeScopeSet(new HashSet<>());
+        
+        // Add exclude scopes including blank ones
+        ScopeBeansScannerChecker.addExcludeScopes("custom", "", "  ", null);
+        
+        // Verify only non-blank scopes were added
+        Set<String> excludeScopes = getExcludeScopeSet();
+        Assertions.assertEquals(1, excludeScopes.size(), "Should have 1 
exclude scope");
+        Assertions.assertTrue(excludeScopes.contains("custom"), "Should 
contain 'custom'");
+        
+        // Restore original set
+        setExcludeScopeSet(originalSet);
+    }
+
+    /**
+     * Test default exclude scopes
+     */
+    @Test
+    public void testDefaultExcludeScopes() throws Exception {
+        Set<String> excludeScopes = getExcludeScopeSet();
+        
Assertions.assertTrue(excludeScopes.contains(ScopeBeansScannerChecker.REQUEST_SCOPE_NAME),
 
+                             "Should contain 'request' scope by default");
+        
Assertions.assertTrue(excludeScopes.contains(ScopeBeansScannerChecker.SESSION_SCOPE_NAME),
 
+                             "Should contain 'session' scope by default");
+        
Assertions.assertTrue(excludeScopes.contains(ScopeBeansScannerChecker.JOB_SCOPE_NAME),
 
+                             "Should contain 'job' scope by default");
+        
Assertions.assertTrue(excludeScopes.contains(ScopeBeansScannerChecker.STEP_SCOPE_NAME),
 
+                             "Should contain 'step' scope by default");
+    }
+
+    /**
+     * Helper method to get the EXCLUDE_SCOPE_SET field value
+     */
+    @SuppressWarnings("unchecked")
+    private Set<String> getExcludeScopeSet() throws Exception {

Review Comment:
   Consider refactoring the reflection-based helper methods into a common test 
utility to reduce code duplication and improve maintainability.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to