You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/08 13:30:34 UTC
[shardingsphere] branch master updated: Fix issue#6594 (#6704)
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 59cd403 Fix issue#6594 (#6704)
59cd403 is described below
commit 59cd403c0903b463efb1008e779a7e9e07f1fa6f
Author: Serendipity <ja...@163.com>
AuthorDate: Sat Aug 8 21:30:25 2020 +0800
Fix issue#6594 (#6704)
* add test cases for infra.spi
* add test case for OrderedSPIRegistry
* add test case for SingletonServiceLoader
* add test case for SingletonServieLoader
* add a new line in the end of META-INF/services/OderedSPIFixture
* add test case for TypedSPIRegistry
* add test case for ShardingSphereServiceLoader
* Fix CI Check
---
.../infra/spi/ShardingSphereServiceLoaderTest.java | 29 +++++++++++++++++--
.../TypedSPIRegistryTest.java} | 33 ++++++++++++----------
2 files changed, 45 insertions(+), 17 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java
index a8a4c47..aef98ba 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java
@@ -17,11 +17,13 @@
package org.apache.shardingsphere.infra.spi;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import org.apache.shardingsphere.infra.spi.exception.ServiceLoaderInstantiationException;
import org.apache.shardingsphere.infra.spi.fixture.TypedSPIFixture;
import org.junit.Test;
-import java.util.Collection;
-
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -41,4 +43,27 @@ public final class ShardingSphereServiceLoaderTest {
Collection collection = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
assertThat(collection.size(), is(1));
}
+
+ @Test
+ public void assertRegisterTwice() {
+ ShardingSphereServiceLoader.register(TypedSPIFixture.class);
+ Collection actualFirstRegister = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
+ assertThat(actualFirstRegister.size(), is(1));
+ ShardingSphereServiceLoader.register(TypedSPIFixture.class);
+ Collection actualSecondRegister = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
+ assertThat(actualSecondRegister.size(), is(actualFirstRegister.size()));
+ }
+
+ @Test
+ public void assertNewInstanceError() throws NoSuchMethodException, IllegalAccessException {
+ Method method = ShardingSphereServiceLoader.class.getDeclaredMethod("newServiceInstance", Class.class);
+ method.setAccessible(true);
+ Throwable targetException = null;
+ try {
+ method.invoke(null, TypedSPIFixture.class);
+ } catch (InvocationTargetException ex) {
+ targetException = ex.getTargetException();
+ }
+ assertTrue("expected throw ServiceLoaderInstantiationException", targetException instanceof ServiceLoaderInstantiationException);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/type/TypedSPIRegistryTest.java
similarity index 58%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/type/TypedSPIRegistryTest.java
index a8a4c47..5b95f17 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/ShardingSphereServiceLoaderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/spi/type/TypedSPIRegistryTest.java
@@ -15,30 +15,33 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.spi;
+package org.apache.shardingsphere.infra.spi.type;
+import java.util.Properties;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.fixture.TypedSPIFixture;
+import org.junit.Before;
import org.junit.Test;
-import java.util.Collection;
+import static org.junit.Assert.assertNotNull;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class ShardingSphereServiceLoaderTest {
+public final class TypedSPIRegistryTest {
+
+ @Before
+ public void init() {
+ ShardingSphereServiceLoader.register(TypedSPIFixture.class);
+ }
@Test
- public void assertNewServiceInstanceWhenIsNotExist() {
- ShardingSphereServiceLoader.register(Collection.class);
- Collection collection = ShardingSphereServiceLoader.newServiceInstances(Collection.class);
- assertTrue(collection.isEmpty());
+ public void assertGetRegisteredService() {
+ String type = "FIXTURE";
+ TypedSPIFixture actual = TypedSPIRegistry.getRegisteredService(TypedSPIFixture.class, type, new Properties());
+ assertNotNull(actual);
}
@Test
- public void assertNewServiceInstanceWhenIsExist() {
- ShardingSphereServiceLoader.register(TypedSPIFixture.class);
- Collection collection = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
- assertThat(collection.size(), is(1));
+ public void assertGetRegisteredServiceBySPIClass() {
+ TypedSPIFixture actual = TypedSPIRegistry.getRegisteredService(TypedSPIFixture.class);
+ assertNotNull(actual);
}
}