fix code and add test

This commit is contained in:
Looly 2022-04-13 23:48:42 +08:00
parent 11d7d8c092
commit be99fda3d6
3 changed files with 30 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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 {
}
}
}
@Override
public DataSourceWrapper clone() {
try {
return (DataSourceWrapper) super.clone();
} catch (CloneNotSupportedException e) {
throw new CloneRuntimeException(e);
}
}
}

View File

@ -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());
}
}