From be99fda3d63d3566a261490da563e27add62cffb Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 13 Apr 2022 23:48:42 +0800 Subject: [PATCH] fix code and add test --- .../cn/hutool/db/ds/AbstractDSFactory.java | 1 + .../cn/hutool/db/ds/DataSourceWrapper.java | 13 +++++++++++-- .../cn/hutool/db/ds/DataSourceWrapperTest.java | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 hutool-db/src/test/java/cn/hutool/db/ds/DataSourceWrapperTest.java diff --git a/hutool-db/src/main/java/cn/hutool/db/ds/AbstractDSFactory.java b/hutool-db/src/main/java/cn/hutool/db/ds/AbstractDSFactory.java index 30ebb0284..b52390612 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ds/AbstractDSFactory.java +++ b/hutool-db/src/main/java/cn/hutool/db/ds/AbstractDSFactory.java @@ -153,6 +153,7 @@ public abstract class AbstractDSFactory extends DSFactory { DataSourceWrapper ds = dsMap.get(group); if (ds != null) { ds.close(); + //noinspection resource dsMap.remove(group); } } diff --git a/hutool-db/src/main/java/cn/hutool/db/ds/DataSourceWrapper.java b/hutool-db/src/main/java/cn/hutool/db/ds/DataSourceWrapper.java index af5bd4483..f96e8b268 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ds/DataSourceWrapper.java +++ b/hutool-db/src/main/java/cn/hutool/db/ds/DataSourceWrapper.java @@ -1,5 +1,6 @@ package cn.hutool.db.ds; +import cn.hutool.core.clone.CloneRuntimeException; import cn.hutool.core.io.IoUtil; import javax.sql.DataSource; @@ -30,7 +31,7 @@ public class DataSourceWrapper implements DataSource, Closeable, Cloneable { * * @param ds 原始的DataSource * @param driver 数据库驱动类名 - * @return {@link DataSourceWrapper} + * @return DataSourceWrapper */ public static DataSourceWrapper wrap(DataSource ds, String driver) { return new DataSourceWrapper(ds, driver); @@ -117,4 +118,12 @@ public class DataSourceWrapper implements DataSource, Closeable, Cloneable { } } -} \ No newline at end of file + @Override + public DataSourceWrapper clone() { + try { + return (DataSourceWrapper) super.clone(); + } catch (CloneNotSupportedException e) { + throw new CloneRuntimeException(e); + } + } +} diff --git a/hutool-db/src/test/java/cn/hutool/db/ds/DataSourceWrapperTest.java b/hutool-db/src/test/java/cn/hutool/db/ds/DataSourceWrapperTest.java new file mode 100644 index 000000000..14819d78f --- /dev/null +++ b/hutool-db/src/test/java/cn/hutool/db/ds/DataSourceWrapperTest.java @@ -0,0 +1,18 @@ +package cn.hutool.db.ds; + +import cn.hutool.db.ds.simple.SimpleDataSource; +import org.junit.Assert; +import org.junit.Test; + +public class DataSourceWrapperTest { + + @Test + public void cloneTest(){ + final SimpleDataSource simpleDataSource = new SimpleDataSource("jdbc:sqlite:test.db", "", ""); + final DataSourceWrapper wrapper = new DataSourceWrapper(simpleDataSource, "test.driver"); + + final DataSourceWrapper clone = wrapper.clone(); + Assert.assertEquals("test.driver", clone.getDriver()); + Assert.assertEquals(simpleDataSource, clone.getRaw()); + } +}