Skip to content

Commit

Permalink
test: add unit test[FileLoader, ObjectHolder, StringUtils] (apache#5308)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqiufeng committed Feb 7, 2023
1 parent 3b5ec35 commit 42693c6
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
3 changes: 2 additions & 1 deletion changes/en-us/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The version is updated as follows:
- [[#4880](https://github.com/seata/seata/pull/4880)] optimize logs when RM's commit return an exception and returns false

### test:
- [[#1234](https://github.com/seata/seata/pull/1234)] Please delete the sample later
- [[#5308](https://github.com/seata/seata/pull/5308)] add unit test [FileLoader, ObjectHolder, StringUtils]


### Contributors:
Expand All @@ -46,6 +46,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [a364176773](https://github.com/a364176773)
- [isharpever](https://github.com/isharpever)
- [mxsm](https://github.com/mxsm)
- [liuqiufeng](https://github.com/liuqiufeng)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.

Expand Down
3 changes: 2 additions & 1 deletion changes/zh-cn/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#4880](https://github.com/seata/seata/pull/4880)] 优化提交和回滚遇到异常时的日志输出,避免误解

### test:
- [[#1234](https://github.com/seata/seata/pull/1234)] 样例,后续请删除
- [[#5308](https://github.com/seata/seata/pull/5308)] 添加单元测试用例 [FileLoader, ObjectHolder, StringUtils]


### Contributors:
Expand All @@ -46,6 +46,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [a364176773](https://github.com/a364176773)
- [isharpever](https://github.com/isharpever)
- [mxsm](https://github.com/mxsm)
- [liuqiufeng](https://github.com/liuqiufeng)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。

Expand Down
49 changes: 49 additions & 0 deletions common/src/test/java/io/seata/common/holder/ObjectHolderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.holder;

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

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

@BeforeEach
void setUp() {
ObjectHolder.INSTANCE.setObject("objectHolderTest", this);
}

@AfterEach
void tearDown() {
}

@Test
public void testGetObjectByName() {
Object object = ObjectHolder.INSTANCE.getObject("objectHolderTest");
Assertions.assertNotNull(object);
}

@Test
public void testGetObjectByClass() {
Object object = ObjectHolder.INSTANCE.getObject(ObjectHolderTest.class);
Assertions.assertNotNull(object);
}
}
40 changes: 40 additions & 0 deletions common/src/test/java/io/seata/common/io/FileLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.io;

import java.io.File;

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

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

@Test
public void testLoadExistFile() {
File file = FileLoader.load("io/TestFile.txt");
Assertions.assertTrue(file != null && file.exists());
}

@Test
public void testLoadNotExistFile() {
File file = FileLoader.load("io/NotExistFile.txt");
Assertions.assertTrue(file == null || !file.exists());
}
}
48 changes: 48 additions & 0 deletions common/src/test/java/io/seata/common/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,54 @@ public void testIsNullOrEmpty() {
assertThat(StringUtils.isNullOrEmpty(" ")).isFalse();
}

@Test
public void testIsBlank() {
assertThat(StringUtils.isBlank(null)).isTrue();
assertThat(StringUtils.isBlank("abc")).isFalse();
assertThat(StringUtils.isBlank("")).isTrue();
assertThat(StringUtils.isBlank(" ")).isTrue();
}

@Test
public void testIsNotBlank() {
assertThat(StringUtils.isNotBlank(null)).isFalse();
assertThat(StringUtils.isNotBlank("abc")).isTrue();
assertThat(StringUtils.isNotBlank("")).isFalse();
assertThat(StringUtils.isNotBlank(" ")).isFalse();
}

@Test
public void testTrimToNull() {
assertThat(StringUtils.trimToNull(null)).isNull();
assertThat(StringUtils.trimToNull("abc")).isEqualTo("abc");
assertThat(StringUtils.trimToNull("")).isNull();
assertThat(StringUtils.trimToNull(" ")).isNull();
}

@Test
public void testTrim() {
assertThat(StringUtils.trim(null)).isNull();
assertThat(StringUtils.trim("abc")).isEqualTo("abc");
assertThat(StringUtils.trim("")).isEqualTo("");
assertThat(StringUtils.trim(" ")).isEqualTo("");
}

@Test
public void testIsEmpty() {
assertThat(StringUtils.isEmpty(null)).isTrue();
assertThat(StringUtils.isEmpty("abc")).isFalse();
assertThat(StringUtils.isEmpty("")).isTrue();
assertThat(StringUtils.isEmpty(" ")).isFalse();
}

@Test
public void testIsNotEmpty() {
assertThat(StringUtils.isNotEmpty(null)).isFalse();
assertThat(StringUtils.isNotEmpty("abc")).isTrue();
assertThat(StringUtils.isNotEmpty("")).isFalse();
assertThat(StringUtils.isNotEmpty(" ")).isTrue();
}

@Test
public void testHump2Line(){
assertThat(StringUtils.hump2Line("abc-d").equals("abcD")).isTrue();
Expand Down
15 changes: 15 additions & 0 deletions common/src/test/resources/io/TestFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

0 comments on commit 42693c6

Please sign in to comment.