Skip to content

Commit

Permalink
test: increase common module unit test coverage (#6157)
Browse files Browse the repository at this point in the history
  • Loading branch information
l81893521 authored Jan 2, 2024
1 parent d2dbbff commit c1e611e
Show file tree
Hide file tree
Showing 21 changed files with 613 additions and 48 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6081](https://github.com/apache/incubator-seata/pull/6081)] add `test-os.yml` for testing the OS
- [[#PR_NO](https://github.com/apache/incubator-seata/pull/PR_NO)] A brief and accurate description of PR
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage

Thanks to these contributors for their code commits. Please report an unintended omission.

Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
### test:
- [[#6081](https://github.com/apache/incubator-seata/pull/6081)] 添加 `test-os.yml` 用于测试seata在各种操作系统下的运行情况
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率

非常感谢以下 contributors 的代码贡献。若有无意遗漏,请报告。

Expand Down
22 changes: 0 additions & 22 deletions common/src/main/java/io/seata/common/util/ConfigTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;

/**
Expand Down Expand Up @@ -118,25 +117,4 @@ public static byte[] base642Byte(String base64Key) {
return Base64.getDecoder().decode(base64Key);
}

public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
KeyPair keyPair = getKeyPair();
String publicKeyStr = ConfigTools.getPublicKey(keyPair);
String privateKeyStr = ConfigTools.getPrivateKey(keyPair);
System.out.println("publicKeyStr:\n" + publicKeyStr);
System.out.println("privateKeyStr:\n" + privateKeyStr);
System.out.println(
"after the key is generated, please keep your key pair properly, if you need to encrypt, please enter your database password");
System.out.println("input 'q' exit");
while (scan.hasNextLine()) {
String password = scan.nextLine();
if (StringUtils.isNotBlank(password) && !"q".equalsIgnoreCase(password)) {
String byte2Base64 = ConfigTools.privateEncrypt(password, privateKeyStr);
System.out.println("encryption completed: \n" + byte2Base64);
}
break;
}
scan.close();
}

}
11 changes: 11 additions & 0 deletions common/src/test/java/io/seata/common/BranchDO.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class BranchDO {
private Integer status;
private Double test;
private Date gmtCreate;
public static String msg;
private final String message = "t";
private Byte testByte;

public String getXid() {
return xid;
Expand All @@ -48,6 +51,14 @@ public Date getGmtCreate() {
return gmtCreate;
}

public String getMessage() {
return message;
}

public Byte getTestByte() {
return testByte;
}

public BranchDO() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.exception;

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

/**
*/
public class AuthenticationFailedExceptionTest {

@Test
public void testAuthenticationFailedException() {
Assertions.assertThrowsExactly(AuthenticationFailedException.class, () -> {
throw new AuthenticationFailedException();
});
Assertions.assertThrowsExactly(AuthenticationFailedException.class, () -> {
throw new AuthenticationFailedException("error");
});
Assertions.assertThrowsExactly(AuthenticationFailedException.class, () -> {
throw new AuthenticationFailedException(new Throwable("error"));
});
Assertions.assertThrowsExactly(AuthenticationFailedException.class, () -> {
throw new AuthenticationFailedException("error", new Throwable("error"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.exception;

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

/**
*/
public class JsonParseExceptionTest {

@Test
public void testJsonParseException() {
Assertions.assertThrowsExactly(JsonParseException.class, () -> {
throw new JsonParseException("error");
});
Assertions.assertThrowsExactly(JsonParseException.class, () -> {
throw new JsonParseException("error", new Throwable("error"));
});
Assertions.assertThrowsExactly(JsonParseException.class, () -> {
throw new JsonParseException(new Throwable("error"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.exception;

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

/**
*/
public class RedisExceptionTest {

@Test
public void testRedisException() {
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException(FrameworkErrorCode.UnknownAppError);
});
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException("error");
});
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException("error", FrameworkErrorCode.UnknownAppError);
});
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException(new Throwable("error"), "error", FrameworkErrorCode.UnknownAppError);
});
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException(new Throwable("error"));
});
Assertions.assertThrowsExactly(RedisException.class, () -> {
throw new RedisException(new Throwable("error"), "error");
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.exception;

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

/**
*/
public class RetryableExceptionTest {

@Test
public void testRetryableException() {
Assertions.assertThrowsExactly(RetryableException.class, () -> {
throw new RetryableException();
});
Assertions.assertThrowsExactly(RetryableException.class, () -> {
throw new RetryableException("error");
});
Assertions.assertThrowsExactly(RetryableException.class, () -> {
throw new RetryableException(new Throwable("error"));
});
Assertions.assertThrowsExactly(RetryableException.class, () -> {
throw new RetryableException("error", new Throwable("error"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,12 @@ public void testUnloadByClassAndActivateName() throws NoSuchFieldException, Ille
assertThat(classToDefinitionMap.get(EnglishHello.class)).isNull();
}

@Test
public void testUnload() {
Assertions.assertThrows(IllegalArgumentException.class, () -> EnhancedServiceLoader.unload(Hello.class, null));
Hello load = EnhancedServiceLoader.load(Hello.class, "FrenchHello");
Assertions.assertDoesNotThrow(() -> EnhancedServiceLoader.unload(Hello.class, "FrenchHello"));
}


}
34 changes: 34 additions & 0 deletions common/src/test/java/io/seata/common/metadata/ClusterRoleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.metadata;

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

/**
*/
public class ClusterRoleTest {

@Test
public void testClusterRole() {
Assertions.assertEquals(0, ClusterRole.LEADER.getRoleCode());
Assertions.assertEquals(1, ClusterRole.FOLLOWER.getRoleCode());
Assertions.assertEquals(2, ClusterRole.LEARNER.getRoleCode());
Assertions.assertEquals(3, ClusterRole.MEMBER.getRoleCode());
Assertions.assertDoesNotThrow(() -> ClusterRole.MEMBER.setRoleCode(4));
}
}
Loading

0 comments on commit c1e611e

Please sign in to comment.