This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 3f8af76 add test cases for OrderedServicesCache (#9824)
3f8af76 is described below
commit 3f8af76f5107b6ab7a4c7e67a6ea71302cec447c
Author: totalo <[email protected]>
AuthorDate: Fri Mar 26 12:57:49 2021 +0800
add test cases for OrderedServicesCache (#9824)
* fixed #9789
* checkstyle
---
.../infra/spi/ordered/OrderedSPIRegistryTest.java | 44 +++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
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 178aef8..c4203a2 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,26 +17,56 @@
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.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
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() {
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)));
}
@Test
@@ -55,4 +85,16 @@ public final class OrderedSPIRegistryTest {
Map<FixtureCustomInterface, OrderedSPIFixture> actual =
OrderedSPIRegistry.getRegisteredServices(collection, OrderedSPIFixture.class);
assertThat(actual.size(), is(1));
}
+
+ @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<>());
+ }
+
}