Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes committed Jan 15, 2025
2 parents e4a1a7a + 6799dc9 commit 603555e
Show file tree
Hide file tree
Showing 18 changed files with 1,000 additions and 82 deletions.
5 changes: 4 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Add changes here for all PR submitted to the 2.x branch.

### bugfix:

- [[#PR_NO](https://github.com/apache/incubator-seata/pull/#PR_NO)] fix XXX
- [[#7104](https://github.com/apache/incubator-seata/pull/7104)] fix impl of supportsSourceType is not defined


### optimize:

Expand All @@ -27,6 +28,7 @@ Add changes here for all PR submitted to the 2.x branch.
### test:

- [[#7092](https://github.com/apache/incubator-seata/pull/7092)] fix the issue of NacosMockTest failing to run
- [[#7098](https://github.com/apache/incubator-seata/pull/7098)] Add unit tests for the `seata-common` module

### refactor:

Expand All @@ -42,6 +44,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [PeppaO](https://github.com/PeppaO)
- [funky-eyes](https://github.com/funky-eyes)
- [psxjoy](https://github.com/psxjoy)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
4 changes: 3 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

### bugfix:

- [[#PR_NO](https://github.com/apache/incubator-seata/pull/#PR_NO)] 修复XXX
- [[#7104](https://github.com/apache/incubator-seata/pull/7104)] 修复SeataApplicationListener在低版本springboot未实现supportsSourceType方法的问题

### optimize:

Expand All @@ -27,6 +27,7 @@
### test:

- [[#7092](https://github.com/apache/incubator-seata/pull/7092)] 修复NacosMockTest测试方法并行导致测试结果被干扰失败的问题
- [[#7098](https://github.com/apache/incubator-seata/pull/7098)] 增加 `seata-common` 模块的测试用例

### refactor:

Expand All @@ -42,5 +43,6 @@
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [PeppaO](https://github.com/PeppaO)
- [funky-eyes](https://github.com/funky-eyes)
- [psxjoy](https://github.com/psxjoy)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
51 changes: 51 additions & 0 deletions common/src/test/java/org/apache/seata/common/code/CodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 org.apache.seata.common.code;

import org.apache.seata.common.result.Code;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class CodeTest {

@Test
public void testGetErrorMsgWithValidCodeReturnsExpectedMsg() {
// Test case for SUCCESS
assertEquals("ok", Code.SUCCESS.getMsg());
// Test case for ERROR
assertEquals("Server error", Code.ERROR.getMsg());
// Test case for LOGIN_FAILED
assertEquals("Login failed", Code.LOGIN_FAILED.getMsg());
}

@Test
public void testGetErrorMsgWithInvalidCodeReturnsNull() {
// Test case for non-existing code
assertNull(Code.getErrorMsg("404"));
}

@Test
public void testSetCodeAndMsgUpdatesValuesCorrectly() {
// Test case to check if setCode and setMsg are working as expected
Code.SUCCESS.setCode("201");
Code.SUCCESS.setMsg("Created");
assertEquals("201", Code.SUCCESS.getCode());
assertEquals("Created", Code.SUCCESS.getMsg());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
*/
package org.apache.seata.common.exception;

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

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
*
*/
class ExceptionUtilTest {
private Exception exception;

@Test
public void unwrap() {
Expand All @@ -38,4 +35,32 @@ public void unwrap() {
RuntimeException runtimeException = new RuntimeException("runtime");
Assertions.assertInstanceOf(RuntimeException.class, ExceptionUtil.unwrap(runtimeException));
}

@Test
public void unwrapInvocationTargetException() {
InvocationTargetException ite = new InvocationTargetException(exception, "test");
Throwable result = ExceptionUtil.unwrap(ite);
Assertions.assertSame(exception, result, "Expected the unwrapped exception to be the cause of InvocationTargetException.");
}

@Test
public void unwrapUndeclaredThrowableException() {
UndeclaredThrowableException ute = new UndeclaredThrowableException(exception, "test");
Throwable result = ExceptionUtil.unwrap(ute);
Assertions.assertSame(exception, result, "Expected the unwrapped exception to be the cause of UndeclaredThrowableException.");
}

@Test
public void unwrapNestedInvocationTargetException() {
Exception rootCause = new Exception();
InvocationTargetException ite = new InvocationTargetException(new UndeclaredThrowableException(rootCause, "test"), "test");
Throwable result = ExceptionUtil.unwrap(ite);
Assertions.assertSame(rootCause, result, "Expected the unwrapped exception to be the root cause.");
}

@Test
public void unwrapNotWrappedException() {
Throwable result = ExceptionUtil.unwrap(exception);
Assertions.assertSame(exception, result, "Expected the unwrapped exception to be the same as the input when no wrapping is present.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 org.apache.seata.common.exception;

import java.sql.SQLException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class SeataRuntimeExceptionTest {

private ErrorCode errorCode;
private String[] params;

@BeforeEach
void setUp() {
errorCode = ErrorCode.ERR_CONFIG;
params = new String[] {"param1", "param2"};
}

@Test
void testConstructorWithErrorCodeCauseAndParams() {
SQLException cause = new SQLException("SQL Error", "S0001", 1000);
SeataRuntimeException exception = new SeataRuntimeException(errorCode, cause, params);
assertNotNull(exception);
assertEquals(errorCode.getMessage(params), exception.getMessage());
assertEquals("S0001", exception.getSqlState());
assertEquals(1000, exception.getVendorCode());
}

@Test
void testToStringShouldReturnLocalizedMessage() {
SeataRuntimeException exception = new SeataRuntimeException(errorCode, params);
assertEquals(exception.getLocalizedMessage(), exception.toString());
}

@Test
void testGetVendorCodeWithSQLExceptionCause() {
SQLException cause = new SQLException("SQL Error", "S0001", 1000);
SeataRuntimeException exception = new SeataRuntimeException(errorCode, cause, params);
assertEquals(1000, exception.getVendorCode());
}

@Test
void testGetSqlStateWithSQLExceptionCause() {
SQLException cause = new SQLException("SQL Error", "S0001", 1000);
SeataRuntimeException exception = new SeataRuntimeException(errorCode, cause, params);
assertEquals("S0001", exception.getSqlState());
}

@Test
void testGetVendorCodeWithSeataRuntimeExceptionCause() {
SQLException innerCause = new SQLException("SQL Error", "S0001", 1000);
SeataRuntimeException cause = new SeataRuntimeException(errorCode, innerCause, params);
SeataRuntimeException exception = new SeataRuntimeException(errorCode, cause, params);
assertEquals(1000, exception.getVendorCode());
}

@Test
void testGetSqlStateWithSeataRuntimeExceptionCause() {
SQLException innerCause = new SQLException("SQL Error", "S0001", 1000);
SeataRuntimeException cause = new SeataRuntimeException(errorCode, innerCause, params);
SeataRuntimeException exception = new SeataRuntimeException(errorCode, cause, params);
assertEquals("S0001", exception.getSqlState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.apache.seata.common.util.CollectionUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -28,7 +27,6 @@

/**
* The type Enhanced service loader test.
*
*/
public class EnhancedServiceLoaderTest {

Expand Down Expand Up @@ -75,14 +73,14 @@ public void testLoadByClassAndActivateName() {
@Test
public void testLoadByClassAndClassLoaderAndActivateName() {
Hello englishHello = EnhancedServiceLoader
.load(Hello.class, "EnglishHello", EnhancedServiceLoaderTest.class.getClassLoader());
.load(Hello.class, "EnglishHello", EnhancedServiceLoaderTest.class.getClassLoader());
assertThat(englishHello.say()).isEqualTo("hello!");
}

/**
* Gets all extension class.
*/
@Test
@SuppressWarnings("checkstyle:UnnecessaryParentheses") @Test
public void getAllExtensionClass() {
List<Class<Hello>> allExtensionClass = EnhancedServiceLoader.getAllExtensionClass(Hello.class);
assertThat(allExtensionClass.get(3).getSimpleName()).isEqualTo((LatinHello.class.getSimpleName()));
Expand All @@ -97,33 +95,32 @@ public void getAllExtensionClass() {
@Test
public void getAllExtensionClass1() {
List<Class<Hello>> allExtensionClass = EnhancedServiceLoader
.getAllExtensionClass(Hello.class, ClassLoader.getSystemClassLoader());
.getAllExtensionClass(Hello.class, ClassLoader.getSystemClassLoader());
assertThat(allExtensionClass).isNotEmpty();
}

@Test
public void getSingletonExtensionInstance(){
public void getSingletonExtensionInstance() {
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
assertThat(hello1 == hello2).isTrue();
}

@Test
public void getMultipleExtensionInstance(){
public void getMultipleExtensionInstance() {
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
assertThat(hello1 == hello2).isFalse();
}

@Test
public void getAllInstances(){
public void getAllInstances() {
List<Hello> hellows1 = EnhancedServiceLoader.loadAll(Hello.class);
List<Hello> hellows2 = EnhancedServiceLoader.loadAll(Hello.class);
for (Hello hello : hellows1){
for (Hello hello : hellows1) {
if (!hello.say().equals("Olá.")) {
assertThat(hellows2.contains(hello)).isTrue();
}
else{
} else {
assertThat(hellows2.contains(hello)).isFalse();
}
}
Expand All @@ -145,23 +142,23 @@ public void testLoadByClassAndActivateNameAndArgs() {
@Test
public void testLoadByClassAndActivateNameAndArgsTypeAndArgs() {
Hello2 load = EnhancedServiceLoader
.load(Hello2.class, "JapaneseHello", new Class[] {String.class}, new Object[] {"msg"});
.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"});
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);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>) serviceLoadersField.get(null);
assertThat(CollectionUtils.isEmpty(serviceLoaders)).isTrue();
}

Expand All @@ -175,7 +172,7 @@ public void testUnloadByClass() throws NoSuchFieldException, IllegalAccessExcept
Class<EnhancedServiceLoader> clazz = EnhancedServiceLoader.class;
Field serviceLoadersField = clazz.getDeclaredField("SERVICE_LOADERS");
serviceLoadersField.setAccessible(true);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>)serviceLoadersField.get(null);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>) serviceLoadersField.get(null);

assertThat(serviceLoaders.get(Hello.class)).isNull();
}
Expand All @@ -191,7 +188,7 @@ public void testUnloadByClassAndActivateName() throws NoSuchFieldException, Ille
Class<EnhancedServiceLoader> clazz = EnhancedServiceLoader.class;
Field serviceLoadersField = clazz.getDeclaredField("SERVICE_LOADERS");
serviceLoadersField.setAccessible(true);
Map<Class<?>, Object> serviceLoaders = (Map<Class<?>, Object>)serviceLoadersField.get(null);
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");
Expand All @@ -208,5 +205,4 @@ public void testUnload() {
Assertions.assertDoesNotThrow(() -> EnhancedServiceLoader.unload(Hello.class, "FrenchHello"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


/**
* The type Extension definition test.
*/
public class ExtensionDefinitionTest {

@Test
Expand All @@ -30,4 +32,23 @@ public void testEquals() {
= new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
Assertions.assertEquals(definition2, definition);
}

@Test
public void testConstructorAndGetters() {
ExtensionDefinition<ChineseHello> definition =
new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
Assertions.assertEquals("abc", definition.getName());
Assertions.assertEquals(1, definition.getOrder());
Assertions.assertEquals(Scope.PROTOTYPE, definition.getScope());
Assertions.assertEquals(ChineseHello.class, definition.getServiceClass());
}

@Test
public void testHashCode() {
ExtensionDefinition<ChineseHello> definition =
new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
ExtensionDefinition<ChineseHello> definition2 =
new ExtensionDefinition<>("abc", 1, Scope.PROTOTYPE, ChineseHello.class);
Assertions.assertEquals(definition.hashCode(), definition2.hashCode());
}
}
Loading

0 comments on commit 603555e

Please sign in to comment.