This is an automated email from the ASF dual-hosted git repository.

menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new ba2237a  Revise for #9824 and #9789 (#9830)
ba2237a is described below

commit ba2237a5dd34dd82067cd026ac12bfc488a61129
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Mar 26 14:07:25 2021 +0800

    Revise for #9824 and #9789 (#9830)
    
    * Revise #9824
    
    * Add OrderedServicesCacheTest
---
 .../infra/spi/ordered/OrderedSPIRegistryTest.java  | 71 +++++++---------------
 .../ordered/cache/OrderedServicesCacheTest.java    | 59 +++++++++++-------
 2 files changed, 60 insertions(+), 70 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistryTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistryTest.java
index c4203a2..021fadd 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistryTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistryTest.java
@@ -17,84 +17,59 @@
 
 package org.apache.shardingsphere.infra.spi.ordered;
 
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterface;
 import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterfaceImpl;
 import org.apache.shardingsphere.infra.spi.fixture.OrderedSPIFixture;
 import org.apache.shardingsphere.infra.spi.fixture.OrderedSPIFixtureImpl;
-import org.apache.shardingsphere.infra.spi.ordered.cache.CachedOrderedServices;
 import org.apache.shardingsphere.infra.spi.ordered.cache.OrderedServicesCache;
 import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
+import java.util.Collections;
 import java.util.Map;
-import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public final class OrderedSPIRegistryTest {
-
-    private Collection<FixtureCustomInterface> customInterfaceCollection;
-
-    private FixtureCustomInterface fixtureCustomInterface;
-
-    private OrderedSPIFixture cacheOrderedSPIFixture;
-
-    @Before
-    public void init() {
+    
+    static {
         ShardingSphereServiceLoader.register(OrderedSPIFixture.class);
-
-        customInterfaceCollection = new LinkedList<>();
-        fixtureCustomInterface = new FixtureCustomInterfaceImpl();
-        customInterfaceCollection.add(fixtureCustomInterface);
-        cacheOrderedSPIFixture = new OrderedSPIFixtureImpl();
-        Map<FixtureCustomInterface, OrderedSPIFixture> result = new 
LinkedHashMap<>(customInterfaceCollection.size(), 1);
-        result.put(fixtureCustomInterface, cacheOrderedSPIFixture);
-        OrderedServicesCache.cacheServices(customInterfaceCollection, 
OrderedSPIFixture.class, result);
     }
-
-    @Test
-    public void assertGetRegisteredServicesByCache() {
-        Optional<CachedOrderedServices> actual = 
OrderedServicesCache.findCachedServices(customInterfaceCollection, 
OrderedSPIFixture.class);
-        assertThat(cacheOrderedSPIFixture, 
is(actual.get().getServices().get(fixtureCustomInterface)));
+    
+    @After
+    public void cleanCache() throws NoSuchFieldException, 
IllegalAccessException {
+        Field field = 
OrderedServicesCache.class.getDeclaredField("CACHED_SERVICES");
+        field.setAccessible(true);
+        Field modifiers = Field.class.getDeclaredField("modifiers");
+        modifiers.setAccessible(true);
+        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+        field.set(null, new ConcurrentHashMap<>());
     }
     
     @Test
     public void assertGetRegisteredServicesByClass() {
-        Collection<FixtureCustomInterface> customInterfaceCollection = new 
LinkedList<>();
-        customInterfaceCollection.add(new FixtureCustomInterfaceImpl());
-        Collection<Class<?>> collection = 
customInterfaceCollection.stream().map(Object::getClass).collect(Collectors.toList());
-        Map<Class<?>, OrderedSPIFixture> actual = 
OrderedSPIRegistry.getRegisteredServicesByClass(collection, 
OrderedSPIFixture.class);
+        Map<Class<?>, OrderedSPIFixture> actual = 
OrderedSPIRegistry.getRegisteredServicesByClass(Collections.singleton(FixtureCustomInterfaceImpl.class),
 OrderedSPIFixture.class);
         assertThat(actual.size(), is(1));
+        assertThat(actual.get(FixtureCustomInterfaceImpl.class), 
instanceOf(OrderedSPIFixtureImpl.class));
     }
     
     @Test
     public void assertGetRegisteredServices() {
-        Collection<FixtureCustomInterface> collection = new LinkedList<>();
-        collection.add(new FixtureCustomInterfaceImpl());
-        Map<FixtureCustomInterface, OrderedSPIFixture> actual = 
OrderedSPIRegistry.getRegisteredServices(collection, OrderedSPIFixture.class);
+        FixtureCustomInterfaceImpl key = new FixtureCustomInterfaceImpl();
+        Map<FixtureCustomInterfaceImpl, OrderedSPIFixture> actual = 
OrderedSPIRegistry.getRegisteredServices(Collections.singleton(key), 
OrderedSPIFixture.class);
         assertThat(actual.size(), is(1));
+        assertThat(actual.get(key), instanceOf(OrderedSPIFixtureImpl.class));
     }
 
-    @After
-    @SneakyThrows
-    public void clean() {
-        Field field = 
OrderedServicesCache.class.getDeclaredField("CACHED_SERVICES");
-        field.setAccessible(true);
-        Field modifiers = Field.class.getDeclaredField("modifiers");
-        modifiers.setAccessible(true);
-        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
-        field.set(null, new ConcurrentHashMap<>());
+    @Test
+    public void assertGetRegisteredServicesFromCache() {
+        FixtureCustomInterfaceImpl key = new FixtureCustomInterfaceImpl();
+        
assertThat(OrderedSPIRegistry.getRegisteredServices(Collections.singleton(key), 
OrderedSPIFixture.class), 
+                
is(OrderedSPIRegistry.getRegisteredServices(Collections.singleton(key), 
OrderedSPIFixture.class)));
     }
-
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCacheTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCacheTest.java
old mode 100755
new mode 100644
index 0299411..7a46110
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCacheTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCacheTest.java
@@ -21,40 +21,55 @@ import 
org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterface;
 import org.apache.shardingsphere.infra.spi.fixture.FixtureCustomInterfaceImpl;
 import org.apache.shardingsphere.infra.spi.fixture.OrderedSPIFixture;
-import org.junit.Before;
+import org.apache.shardingsphere.infra.spi.fixture.OrderedSPIFixtureImpl;
+import org.junit.After;
 import org.junit.Test;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Collection;
-import java.util.LinkedList;
+import java.util.concurrent.ConcurrentHashMap;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
-public class OrderedServicesCacheTest {
-
-    @Before
-    public void init() {
+public final class OrderedServicesCacheTest {
+    
+    static {
         ShardingSphereServiceLoader.register(OrderedSPIFixture.class);
     }
-
+    
+    @After
+    public void cleanCache() throws NoSuchFieldException, 
IllegalAccessException {
+        Field field = 
OrderedServicesCache.class.getDeclaredField("CACHED_SERVICES");
+        field.setAccessible(true);
+        Field modifiers = Field.class.getDeclaredField("modifiers");
+        modifiers.setAccessible(true);
+        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+        field.set(null, new ConcurrentHashMap<>());
+    }
+    
     @Test
     public void assertFindCachedServices() {
-        Collection<FixtureCustomInterface> types = new LinkedList<>();
-        types.add(new FixtureCustomInterfaceImpl());
-        Collection<OrderedSPIFixture> registeredServices = 
ShardingSphereServiceLoader.getSingletonServiceInstances(OrderedSPIFixture.class);
-        Map<FixtureCustomInterface, OrderedSPIFixture> services = new 
LinkedHashMap<>(registeredServices.size(), 1);
-        for (OrderedSPIFixture each : registeredServices) {
-            types.stream().filter(type -> each.getTypeClass() == 
type.getClass()).forEach(type -> services.put(type, each));
-        }
-        OrderedServicesCache.cacheServices(types, OrderedSPIFixture.class, 
services);
-        Optional<CachedOrderedServices> cachedServices = 
OrderedServicesCache.findCachedServices(types, OrderedSPIFixture.class);
-        if (cachedServices.isPresent()) {
-            assertThat(cachedServices.get().getTypes().size(), is(1));
-            assertThat(cachedServices.get().getServices().size(), is(1));
-        }
+        FixtureCustomInterface fixtureCustomInterface = new 
FixtureCustomInterfaceImpl();
+        Collection<FixtureCustomInterface> customInterfaces = 
Collections.singleton(fixtureCustomInterface);
+        OrderedSPIFixture<?> cacheOrderedSPIFixture = new 
OrderedSPIFixtureImpl();
+        Map<FixtureCustomInterface, OrderedSPIFixture> cachedOrderedServices = 
new LinkedHashMap<>(customInterfaces.size(), 1);
+        cachedOrderedServices.put(fixtureCustomInterface, 
cacheOrderedSPIFixture);
+        OrderedServicesCache.cacheServices(customInterfaces, 
OrderedSPIFixture.class, cachedOrderedServices);
+        Optional<CachedOrderedServices> actual = 
OrderedServicesCache.findCachedServices(customInterfaces, 
OrderedSPIFixture.class);
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getServices().get(fixtureCustomInterface), 
is(cacheOrderedSPIFixture));
+    }
+    
+    @Test
+    public void assertNotFindCachedServices() {
+        
assertFalse(OrderedServicesCache.findCachedServices(Collections.singleton(new 
FixtureCustomInterfaceImpl()), OrderedSPIFixture.class).isPresent());
     }
-
 }

Reply via email to