This commit is contained in:
Looly 2023-04-16 01:50:31 +08:00
parent bc9d665036
commit 1fd105dab5
5 changed files with 75 additions and 7 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.spi;
import org.dromara.hutool.core.exceptions.UtilException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ListServiceLoaderTest {
@Test
void loadFirstAvailableTest() {
final ListServiceLoader<TestSPI1> serviceLoader = ListServiceLoader.of(TestSPI1.class);
final TestSPI1 testSPI1 = SpiUtil.loadFirstAvailable(serviceLoader);
Assertions.assertNotNull(testSPI1);
Assertions.assertEquals("test service 1", testSPI1.doSth());
}
@Test
void getServiceClassTest() {
final ListServiceLoader<TestSPI1> serviceLoader = ListServiceLoader.of(TestSPI1.class);
final Class<TestSPI1> service1 = serviceLoader.getServiceClass(1);
Assertions.assertEquals(TestService1.class, service1);
}
@Test
void getServiceClassNotExistTest() {
final ListServiceLoader<TestSPI1> serviceLoader = ListServiceLoader.of(TestSPI1.class);
Assertions.assertThrows(UtilException.class, ()->{
serviceLoader.getServiceClass(0);
});
}
public interface TestSPI1{
String doSth();
}
public static class TestService1 implements TestSPI1 {
@Override
public String doSth() {
return "test service 1";
}
}
}

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.exceptions.UtilException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SpiUtilTest {
public class MapServiceLoaderTest {
@Test
void loadFirstAvailableTest() {

View File

@ -0,0 +1,6 @@
# 无服务
serviceEmpty =
# 正常服务
service1 = org.dromara.hutool.core.spi.MapServiceLoaderTest$TestService1
# 此服务类未定义
service2 = org.dromara.hutool.core.spi.MapServiceLoaderTest$TestService2

View File

@ -1,6 +0,0 @@
# 无服务
serviceEmpty =
# 正常服务
service1 = org.dromara.hutool.core.spi.SpiUtilTest$TestService1
# 此服务类未定义
service2 = org.dromara.hutool.core.spi.SpiUtilTest$TestService2

View File

@ -0,0 +1,14 @@
#
# Copyright (c) 2023 looly(loolly@aliyun.com)
# Hutool is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#
org.dromara.hutool.core.spi.ListServiceLoaderTest$TestService2
org.dromara.hutool.core.spi.ListServiceLoaderTest$TestService1