This is an automated email from the ASF dual-hosted git repository. jimin pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push: new d58fa2d0c5 test: add UT for rm datasource module (#7275) d58fa2d0c5 is described below commit d58fa2d0c512dcf0d857a332d560212381e51960 Author: charlhhhh <97687881+charlh...@users.noreply.github.com> AuthorDate: Sun Apr 27 08:11:28 2025 -0400 test: add UT for rm datasource module (#7275) --- .../seata/rm/BaseDataSourceResourceTest.java | 156 +++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/BaseDataSourceResourceTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/BaseDataSourceResourceTest.java new file mode 100644 index 0000000000..62202d1558 --- /dev/null +++ b/rm-datasource/src/test/java/org/apache/seata/rm/BaseDataSourceResourceTest.java @@ -0,0 +1,156 @@ +/* + * 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.rm; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.PrintWriter; +import java.sql.SQLException; +import java.util.Map; +import org.apache.seata.common.exception.ShouldNeverHappenException; +import org.apache.seata.core.model.BranchStatus; +import org.apache.seata.rm.datasource.xa.Holdable; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BaseDataSourceResourceTest { + + static class DummyHoldable implements Holdable { + + private boolean held = false; + + @Override + public boolean isHeld() { + return held; + } + + @Override + public void setHeld(boolean held) { + this.held = held; + } + + @Override + public boolean shouldBeHeld() { + return true; + } + } + + static class DummyResource extends BaseDataSourceResource<DummyHoldable> { + + @Override + public void setLogWriter(PrintWriter out) { + } + + @Override + public java.sql.Connection getConnection() { + return null; + } + + @Override + public java.sql.Connection getConnection(String username, String password) { + return null; + } + + } + + private DummyResource resource; + + @BeforeEach + void setUp() { + resource = new DummyResource(); + } + + @Test + void testHoldReleaseSuccess() { + DummyHoldable h = new DummyHoldable(); + assertNull(resource.hold("k1", h)); + assertEquals(h, resource.lookup("k1")); + assertTrue(h.isHeld()); + + DummyHoldable removed = resource.release("k1", h); + assertEquals(h, removed); + assertFalse(h.isHeld()); + } + + @Test + void testHoldTwiceShouldFail() { + DummyHoldable h1 = new DummyHoldable(); + DummyHoldable h2 = new DummyHoldable(); + resource.hold("k", h1); + h2.setHeld(true); + assertThrows(ShouldNeverHappenException.class, () -> resource.hold("k", h2)); + } + + @Test + void testReleaseWrongObject() { + DummyHoldable h1 = new DummyHoldable(); + DummyHoldable h2 = new DummyHoldable(); + resource.hold("k", h1); + assertThrows(ShouldNeverHappenException.class, () -> resource.release("k", h2)); + } + + @Test + void testBranchStatusCacheOps() { + BaseDataSourceResource.setBranchStatus("x1", BranchStatus.PhaseOne_Done); + assertEquals(BranchStatus.PhaseOne_Done, BaseDataSourceResource.getBranchStatus("x1")); + BaseDataSourceResource.remove("x1"); + assertNull(BaseDataSourceResource.getBranchStatus("x1")); + } + + @Test + void testResourceIdGroupSetters() { + resource.setResourceId("resId"); + resource.setResourceGroupId("groupId"); + resource.setDbType("mysql"); + + assertEquals("resId", resource.getResourceId()); + assertEquals("groupId", resource.getResourceGroupId()); + assertEquals("mysql", resource.getDbType()); + } + + @Test + void testShouldBeHeldFlag() { + resource.setShouldBeHeld(true); + assertTrue(resource.isShouldBeHeld()); + } + + @Test + void testDriverSetter() { + assertNull(resource.getDriver()); + assertDoesNotThrow(() -> resource.setDriver(null)); + } + + @Test + void testWrapperMethods() throws SQLException { + assertTrue(resource.isWrapperFor(DummyResource.class)); + assertSame(resource, resource.unwrap(DummyResource.class)); + } + + @Test + void testKeeperAccess() { + DummyHoldable h = new DummyHoldable(); + resource.hold("a", h); + Map<String, DummyHoldable> keeper = resource.getKeeper(); + assertTrue(keeper.containsKey("a")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org