Skip to content

Commit

Permalink
test: add unit test for common module (#5335)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqiufeng committed Feb 13, 2023
1 parent 51f6a09 commit 024ed11
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.0.0.md
Expand Up @@ -55,6 +55,7 @@ The version is updated as follows:
### test:
- [[#5308](https://github.com/seata/seata/pull/5308)] add unit test [FileLoader, ObjectHolder, StringUtils]
- [[#5309](https://github.com/seata/seata/pull/5309)] add unit test [ArrayUtils, ConfigTools, MapUtil]
- [[#5335](https://github.com/seata/seata/pull/5335)] add unit test [EnhancedServiceLoader,ExtensionDefinition,SizeUtilTest,ReflectionUtil,LowerCaseLinkHashMap,FileLoader,ObjectHolder]


### Contributors:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.0.0.md
Expand Up @@ -57,6 +57,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
### test:
- [[#5308](https://github.com/seata/seata/pull/5308)] 添加单元测试用例 [FileLoader, ObjectHolder, StringUtils]
- [[#5309](https://github.com/seata/seata/pull/5309)] 添加单元测试用例 [ArrayUtils, ConfigTools, MapUtil]
- [[#5335](https://github.com/seata/seata/pull/5335)] 添加单元测试用例 [EnhancedServiceLoader,ExtensionDefinition,SizeUtilTest,ReflectionUtil,LowerCaseLinkHashMap,FileLoader,ObjectHolder]


### Contributors:
Expand Down
Expand Up @@ -16,6 +16,7 @@

package io.seata.common.holder;

import io.seata.common.exception.ShouldNeverHappenException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -37,13 +38,16 @@ void tearDown() {

@Test
public void testGetObjectByName() {
Object object = ObjectHolder.INSTANCE.getObject("objectHolderTest");
Assertions.assertNotNull(object);
Assertions.assertNotNull(ObjectHolder.INSTANCE.getObject("objectHolderTest"));
// object not exist in ObjectHolder.INSTANCE
Assertions.assertNull(ObjectHolder.INSTANCE.getObject("objectHolder"));
}

@Test
public void testGetObjectByClass() {
Object object = ObjectHolder.INSTANCE.getObject(ObjectHolderTest.class);
Assertions.assertNotNull(object);
Assertions.assertNotNull(ObjectHolder.INSTANCE.getObject(ObjectHolderTest.class));
// object not exist in ObjectHolder.INSTANCE
Assertions.assertThrows(ShouldNeverHappenException.class,
() -> ObjectHolder.INSTANCE.getObject(ObjectHolder.class));
}
}
5 changes: 5 additions & 0 deletions common/src/test/java/io/seata/common/io/FileLoaderTest.java
Expand Up @@ -37,4 +37,9 @@ public void testLoadNotExistFile() {
File file = FileLoader.load("io/NotExistFile.txt");
Assertions.assertTrue(file == null || !file.exists());
}

@Test
public void testLoadException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> FileLoader.load(null));
}
}
Expand Up @@ -15,8 +15,11 @@
*/
package io.seata.common.loader;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import io.seata.common.util.CollectionUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -133,4 +136,69 @@ public void classCastExceptionTest() {
});
}

@Test
public void testLoadByClassAndActivateNameAndArgs() {
Hello2 load = EnhancedServiceLoader.load(Hello2.class, "JapaneseHello", new Object[] {"msg"});
assertThat(load).isInstanceOf(Hello2.class);
}

@Test
public void testLoadByClassAndActivateNameAndArgsTypeAndArgs() {
Hello2 load = EnhancedServiceLoader
.load(Hello2.class, "JapaneseHello", new Class[] {String.class}, new Object[] {"msg"});
assertThat(load).isInstanceOf(Hello2.class);
}

@Test
public void testUnloadAll() throws NoSuchFieldException, IllegalAccessException {
Hello hello = EnhancedServiceLoader.load(Hello.class);
assertThat(hello).isInstanceOf(Hello.class);
Hello2 hello2 = EnhancedServiceLoader.load(Hello2.class, "JapaneseHello", new Object[]{"msg"});
assertThat(hello2).isInstanceOf(Hello2.class);

EnhancedServiceLoader.unloadAll();

Class<EnhancedServiceLoader> clazz = EnhancedServiceLoader.class;
Field serviceLoadersField = clazz.getDeclaredField("SERVICE_LOADERS");
serviceLoadersField.setAccessible(true);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>)serviceLoadersField.get(null);
assertThat(CollectionUtils.isEmpty(serviceLoaders)).isTrue();
}

@Test
public void testUnloadByClass() throws NoSuchFieldException, IllegalAccessException {
Hello load = EnhancedServiceLoader.load(Hello.class);
assertThat(load).isInstanceOf(Hello.class);

EnhancedServiceLoader.unload(Hello.class);
// get serviceLoaders
Class<EnhancedServiceLoader> clazz = EnhancedServiceLoader.class;
Field serviceLoadersField = clazz.getDeclaredField("SERVICE_LOADERS");
serviceLoadersField.setAccessible(true);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>)serviceLoadersField.get(null);

assertThat(serviceLoaders.get(Hello.class)).isNull();
}

// FIXME: 2023/2/11 wait fix EnhancedServiceLoader.unload(Class<S> service, String activateName)
// @Test
public void testUnloadByClassAndActivateName() throws NoSuchFieldException, IllegalAccessException {
Hello englishHello = EnhancedServiceLoader.load(Hello.class, "EnglishHello");
assertThat(englishHello.say()).isEqualTo("hello!");

EnhancedServiceLoader.unload(Hello.class, "EnglishHello");
// get serviceLoaders
Class<EnhancedServiceLoader> clazz = EnhancedServiceLoader.class;
Field serviceLoadersField = clazz.getDeclaredField("SERVICE_LOADERS");
serviceLoadersField.setAccessible(true);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>)serviceLoadersField.get(null);
//get innerEnhancedServiceLoader.classToDefinitionMap
Object innerEnhancedServiceLoader = serviceLoaders.get(Hello.class);
Field classToDefinitionMapField = innerEnhancedServiceLoader.getClass().getDeclaredField("classToDefinitionMap");
classToDefinitionMapField.setAccessible(true);
Map<Class<?>, Object> classToDefinitionMap = (Map<Class<?>, Object>) classToDefinitionMapField.get(innerEnhancedServiceLoader);

assertThat(classToDefinitionMap.get(EnglishHello.class)).isNull();
}

}
@@ -0,0 +1,35 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author liuqiufeng
*/
public class ExtensionDefinitionTest {

@Test
public void testEquals() {
ExtensionDefinition<ChineseHello> definition
= new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
ExtensionDefinition<ChineseHello> definition2
= new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
Assertions.assertEquals(definition2, definition);
}
}
23 changes: 23 additions & 0 deletions common/src/test/java/io/seata/common/loader/Hello2.java
@@ -0,0 +1,23 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

/**
* @author liuqiufeng
*/
public interface Hello2 {
}
30 changes: 30 additions & 0 deletions common/src/test/java/io/seata/common/loader/JapaneseHello.java
@@ -0,0 +1,30 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

/**
* @author liuqiufeng
*/
@LoadLevel(name = "JapaneseHello", order = Integer.MAX_VALUE)
public class JapaneseHello implements Hello2{

private final String msg;

public JapaneseHello(String msg) {
this.msg = msg;
}
}

0 comments on commit 024ed11

Please sign in to comment.