Skip to content

Commit

Permalink
add:unit test for seata-core
Browse files Browse the repository at this point in the history
  • Loading branch information
lightClouds917 committed Dec 20, 2023
1 parent 9e1abf3 commit 7a7cec1
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.core.compressor;

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

/**
* The CompressorType Test
*
* @author zhongxiang.wang
*/
public class CompressorTypeTest {
@Test
public void testGetByCode() {
int code = 1;
CompressorType expectedType = CompressorType.GZIP;

CompressorType actualType = CompressorType.getByCode(code);

// Assert the returned type matches the expected type
Assertions.assertEquals(expectedType, actualType);
}

@Test
public void testGetByName() {
String name = "gzip";
CompressorType expectedType = CompressorType.GZIP;

CompressorType actualType = CompressorType.getByName(name);

// Assert the returned type matches the expected type
Assertions.assertEquals(expectedType, actualType);
}
}
43 changes: 43 additions & 0 deletions core/src/test/java/io/seata/core/constants/DBTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.core.constants;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Fail.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* The DBType Test
*
* @author zhongxiang.wang
*/
public class DBTypeTest {
@Test
public void testValueOf() {
// Test valid DBType values
assertEquals(DBType.MYSQL, DBType.valueof("MYSQL"));
assertEquals(DBType.ORACLE, DBType.valueof("ORACLE"));
assertEquals(DBType.DB2, DBType.valueof("DB2"));

// Test invalid DBType value
try {
DBType.valueof("INVALID");
fail("Expected IllegalArgumentException to be thrown");
} catch (IllegalArgumentException e) {
assertEquals("unknown dbtype:INVALID", e.getMessage());
}
}
}
18 changes: 18 additions & 0 deletions core/src/test/java/io/seata/core/context/ContextCoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -68,6 +70,22 @@ public void testGet() {
load.remove(NOT_EXIST_KEY);
}

/**
* Test entries.
*/
@Test
public void testEntries() {
ContextCore load = ContextCoreLoader.load();
load.put(FIRST_KEY, FIRST_VALUE);
load.put(SECOND_KEY, FIRST_VALUE);
Map<String, Object> entries = load.entries();
assertThat(entries.get(FIRST_KEY)).isEqualTo(FIRST_VALUE);
assertThat(entries.get(SECOND_KEY)).isEqualTo(FIRST_VALUE);
load.remove(FIRST_KEY);
load.remove(SECOND_KEY);
load.remove(NOT_EXIST_KEY);
}

/**
* Test remove.
*/
Expand Down
66 changes: 66 additions & 0 deletions core/src/test/java/io/seata/core/context/RootContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -57,6 +59,70 @@ public void testGetXID() {
assertThat(RootContext.getXID()).isNull();
}

/**
* Test set timeout.
*/
@Test
public void testSetTimeout() {
RootContext.setTimeout(100);
assertThat(RootContext.getTimeout()).isEqualTo(100);
RootContext.setTimeout(null);
assertThat(RootContext.getTimeout()).isEqualTo(null);
}

/**
* Test get timeout.
*/
@Test
public void testGetTimeout() {
RootContext.setTimeout(100);
assertThat(RootContext.getTimeout()).isEqualTo(100);
RootContext.setTimeout(null);
assertThat(RootContext.getTimeout()).isEqualTo(null);
}

/**
* Test bind global lock flag.
*/
@Test
public void testBindGlobalLockFlag() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
}

/**
* Test unbind global lock flag.
*/
@Test
public void testUnBindGlobalLockFlag() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
RootContext.unbindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(false);
}

/**
* Test require global lock.
*/
@Test
public void testRequireGlobalLock() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
RootContext.unbindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(false);
}

/**
* Test entries.
*/
@Test
public void testEntries() {
RootContext.bind(DEFAULT_XID);
Map<String, Object> entries = RootContext.entries();
assertThat(entries.get(RootContext.KEY_XID)).isEqualTo(DEFAULT_XID);
RootContext.unbind();
}

/**
* Test bind and unbind branchType.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.core.context;

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

import static org.junit.jupiter.api.Assertions.*;

/**
* The type Thread local context core test.
*
* @author zhongxiang.wang
*/
public class ThreadLocalContextCoreTest {
private static ThreadLocalContextCore contextCore ;


@BeforeAll
public static void setUp() {
contextCore = new ThreadLocalContextCore();
}
@Test
public void testPutAndGet() {
// Test putting and getting a value
contextCore.put("key", "value");
assertEquals("value", contextCore.get("key"));
contextCore.remove("key");
}

@Test
public void testRemove() {
// Test putting and removing a value
contextCore.put("key", "value");
assertEquals("value", contextCore.remove("key"));
assertNull(contextCore.get("key"));
}

@Test
public void testEntries() {
// Test getting all entries
contextCore.put("key1", "value1");
contextCore.put("key2", "value2");
contextCore.put("key3", "value3");
assertEquals(3, contextCore.entries().size());
assertTrue(contextCore.entries().containsKey("key1"));
assertTrue(contextCore.entries().containsKey("key2"));
assertTrue(contextCore.entries().containsKey("key3"));
contextCore.remove("key1");
contextCore.remove("key2");
contextCore.remove("key3");
assertNull(contextCore.get("key1"));
assertNull(contextCore.get("key2"));
assertNull(contextCore.get("key3"));
}
}
36 changes: 36 additions & 0 deletions core/src/test/java/io/seata/core/event/ExceptionEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.core.event;

import org.junit.jupiter.api.Test;

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

/**
* The ExceptionEvent Test
*
* @author zhongxiang.wang
*/
public class ExceptionEventTest {
@Test
public void testGetName() {
// Create an ExceptionEvent with a code
ExceptionEvent exceptionEvent = new ExceptionEvent("CODE123");

// Test the getName method
assertEquals("CODE123", exceptionEvent.getName());
}
}
Loading

0 comments on commit 7a7cec1

Please sign in to comment.