Skip to content

Commit

Permalink
修正单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 23, 2024
1 parent 2b73a1e commit 71278bf
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 18 deletions.
4 changes: 4 additions & 0 deletions nop-dao/src/main/java/io/nop/dao/DaoConfigs.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public interface DaoConfigs {
@Description("启用动态querySpace支持,可以动态将default的querySpace映射为指定的querySpace")
IConfigReference<Boolean> CFG_ORM_ENABLE_DYNAMIC_QUERY_SPACE = varRef(s_loc,
"nop.orm.enable-dynamic-query-space", Boolean.class, true);

@Description("查询空间和数据库方言之间的映射关系")
IConfigReference<String> CFG_QUERY_SPACE_TO_DIALECT = varRef(s_loc,
"nop.dao.config.query-space-to-dialect", String.class, null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class DefaultTransactionManager implements ITransactionManager {
private final Map<String, ITransactionFactory> transactionFactoryMap = new ConcurrentHashMap<>();
private final Map<String, String> txnGroupMap = new ConcurrentHashMap<>();

private final Map<String, String> querySpaceToDialectMap = new ConcurrentHashMap<>();
private Map<String, String> querySpaceToDialectMap;

private ITransactionFactory defaultFactory;

private ITransactionListener defaultListener;
Expand All @@ -62,15 +63,8 @@ public void onAfterCompletion(ITransaction txn, CompleteStatus status, Throwable
}
};

public void setQuerySpaceToDialectMap(Map<String, String> map) {
if (map != null) {
querySpaceToDialectMap.putAll(map);
}
}

public void setQuerySpaceToDialectConfig(String config) {
Map<String, String> map = StringHelper.parseStringMap(config, '=', ',');
setQuerySpaceToDialectMap(map);
public void setQuerySpaceToDialectMap(Map<String, String> querySpaceToDialectMap) {
this.querySpaceToDialectMap = querySpaceToDialectMap;
}

public void setTransactionMetrics(ITransactionMetrics transactionMetrics) {
Expand All @@ -91,7 +85,7 @@ public void setDataSourceMap(Map<String, DataSource> dataSourceMap) {
String name = entry.getKey();
DataSource ds = entry.getValue();

transactionFactoryMap.put(name, new JdbcTransactionFactory(ds));
transactionFactoryMap.put(name, new JdbcTransactionFactory(ds, getDialectNameForQuerySpace(name)));
}
}
}
Expand Down Expand Up @@ -119,7 +113,7 @@ public Set<String> getNamedQuerySpaces() {
public void addQuerySpace(String querySpace, DataSource ds) {
Guard.notEmpty(querySpace, "querySpace");
Guard.notNull(ds, "ds");
transactionFactoryMap.put(querySpace, new JdbcTransactionFactory(ds));
transactionFactoryMap.put(querySpace, new JdbcTransactionFactory(ds, getDialectNameForQuerySpace(querySpace)));
}

public void removeQuerySpace(String querySpace) {
Expand Down Expand Up @@ -217,11 +211,22 @@ public boolean isQuerySpaceDefined(String querySpace) {
return transactionFactoryMap.containsKey(querySpace);
}

protected Map<String, String> getQuerySpaceToDialectMap() {
if (this.querySpaceToDialectMap == null) {
this.querySpaceToDialectMap = DaoHelper.getQuerySpaceToDialectConfig();
}
return querySpaceToDialectMap;
}

protected String getDialectNameForQuerySpace(String querySpace) {
return getQuerySpaceToDialectMap().get(querySpace);
}

@Override
public IDialect getDialectForQuerySpace(String querySpace) {
querySpace = DaoHelper.normalizeQuerySpace(querySpace);

String dialectName = querySpaceToDialectMap.get(querySpace);
String dialectName = getDialectNameForQuerySpace(querySpace);
if (dialectName != null)
return DialectManager.instance().getDialect(dialectName);

Expand Down
14 changes: 14 additions & 0 deletions nop-dao/src/main/java/io/nop/dao/utils/DaoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import io.nop.commons.util.StringHelper;
import io.nop.dao.DaoConstants;

import java.util.Collections;
import java.util.Map;

import static io.nop.dao.DaoConfigs.CFG_QUERY_SPACE_TO_DIALECT;

public class DaoHelper {
public static boolean isDefaultQuerySpace(String querySpace) {
return StringHelper.isEmpty(querySpace) || DaoConstants.DEFAULT_QUERY_SPACE.equals(querySpace);
Expand All @@ -26,4 +29,15 @@ public static String normalizeQuerySpace(String querySpace) {
public static String getChangeType(Map<String, Object> map) {
return StringHelper.toString(map.get(DaoConstants.PROP_CHANGE_TYPE), "");
}

public static Map<String, String> getQuerySpaceToDialectConfig() {
String text = CFG_QUERY_SPACE_TO_DIALECT.get();
if (StringHelper.isEmpty(text))
return Collections.emptyMap();

Map<String, String> map = StringHelper.parseStringMap(text, '=', ',');
if (map == null)
map = Collections.emptyMap();
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</property>

<property name="txnGroupMapConfig" value="@cfg:nop.dao.config.txn-group-map|"/>
<property name="querySpaceToDialectConfig" value="@cfg:nop.dao.config.query-space-to-dialect|"/>
<property name="transactionMetrics" ref="nopDaoMetrics"/>
</bean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void test_genDiffDdl_by_Entities() {
resultsJson = JsonTool.stringify(diffDdlList, null, " ");

LOG.info("nop.test.orm.db-differ.by-entities.old-is-null.ddl={}", resultsJson);
Assertions.assertEquals(attachmentJsonText("db-diff-results.old-null-entities.json"), resultsJson);
Assertions.assertEquals(normalizeCRLF(attachmentJsonText("db-diff-results.old-null-entities.json")), normalizeCRLF(resultsJson));

diffDdlList = OrmDbDiffer.forDialect("h2").genDiffDdl(oldOrmModel.getEntities(), new ArrayList<>());
resultsJson = JsonTool.stringify(diffDdlList, null, " ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "add"
},
{
"ddl": "CREATE TABLE dev_app_dict(\n ID VARCHAR(32) NOT NULL COMMENT 'ID' ,\n APP_ID VARCHAR(32) NOT NULL COMMENT '所属应用' ,\n NAME VARCHAR(100) NOT NULL COMMENT '名称' ,\n DESCRIPTION VARCHAR(500) COMMENT '说明' ,\n constraint UK_DEV_APP_DICT_APPID_NAME unique (APP_ID,NAME),\n constraint PK_dev_app_dict primary key (ID)\n)",
"ddl": "CREATE TABLE dev_app_dict(\n ID VARCHAR(32) NOT NULL COMMENT 'ID' ,\n APP_ID VARCHAR(32) NOT NULL COMMENT '所属应用' ,\n NAME VARCHAR(100) NOT NULL COMMENT '名称' ,\n DESCRIPTION VARCHAR(500) COMMENT '说明' ,\n constraint UK_DEV_APP_DICT_APPID_NAME unique (APP_ID,NAME) COMMENT '名称在应用内唯一',\n constraint PK_dev_app_dict primary key (ID)\n)",
"querySpace": "default",
"target": null,
"targetTable": "dev_app_dict",
Expand All @@ -23,4 +23,4 @@
"targetType": "table",
"type": "add"
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -33642,6 +33642,11 @@
"allowWrite": false,
"name": "PROP_ID_mimeType"
},
{
"allowUnsafeAccess": false,
"allowWrite": false,
"name": "PROP_ID_originFileId"
},
{
"allowUnsafeAccess": false,
"allowWrite": false,
Expand Down Expand Up @@ -33717,6 +33722,11 @@
"allowWrite": false,
"name": "PROP_NAME_mimeType"
},
{
"allowUnsafeAccess": false,
"allowWrite": false,
"name": "PROP_NAME_originFileId"
},
{
"allowUnsafeAccess": false,
"allowWrite": false,
Expand Down Expand Up @@ -33784,6 +33794,10 @@
"name": "getMimeType",
"parameterTypes": []
},
{
"name": "getOriginFileId",
"parameterTypes": []
},
{
"name": "getRemark",
"parameterTypes": []
Expand Down Expand Up @@ -33926,6 +33940,12 @@
"java.lang.String"
]
},
{
"name": "setOriginFileId",
"parameterTypes": [
"java.lang.String"
]
},
{
"name": "setRemark",
"parameterTypes": [
Expand Down Expand Up @@ -33971,6 +33991,15 @@
"long"
]
},
{
"name": "copyFile",
"parameterTypes": [
"java.lang.String",
"java.lang.String",
"java.lang.String",
"java.lang.String"
]
},
{
"name": "decodeFileId",
"parameterTypes": [
Expand Down Expand Up @@ -34020,6 +34049,13 @@
"name": "init",
"parameterTypes": []
},
{
"name": "isUniqueRef",
"parameterTypes": [
"io.nop.dao.api.IEntityDao",
"io.nop.file.dao.entity.NopFileRecord"
]
},
{
"name": "newFileId",
"parameterTypes": []
Expand Down Expand Up @@ -36608,6 +36644,10 @@
"name": "getValue",
"parameterTypes": []
},
{
"name": "isIocIgnoreDepends",
"parameterTypes": []
},
{
"name": "isIocSkipIfEmpty",
"parameterTypes": []
Expand All @@ -36622,6 +36662,12 @@
"io.nop.core.lang.json.IJsonHandler"
]
},
{
"name": "setIocIgnoreDepends",
"parameterTypes": [
"boolean"
]
},
{
"name": "setIocSkipIfEmpty",
"parameterTypes": [
Expand Down Expand Up @@ -39499,6 +39545,10 @@
"name": "getIdProp",
"parameterTypes": []
},
{
"name": "getLabelProp",
"parameterTypes": []
},
{
"name": "getMaxBatchLoadSize",
"parameterTypes": []
Expand Down Expand Up @@ -39587,6 +39637,10 @@
"name": "getShardPropId",
"parameterTypes": []
},
{
"name": "getStateProp",
"parameterTypes": []
},
{
"name": "getTableName",
"parameterTypes": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
xmlns:ioc="ioc"
>

<bean id="io.nop.orm.geo.dialect.h2gis.H2GisInitializer" ioc:type="@bean:id" init-method="init">
<bean id="io.nop.orm.geo.dialect.h2gis.H2GisInitializer" ioc:type="@bean:id" init-method="init"
ioc:before="nopOrmSessionFactory">
<ioc:condition>
<on-class>org.h2gis.functions.factory.H2GISFunctions</on-class>
</ioc:condition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.nop.dao.txn.ITransactionTemplate;
import io.nop.orm.IOrmEntity;
import io.nop.orm.IOrmTemplate;
import io.nop.orm.geo.dialect.h2gis.H2GisInitializer;
import io.nop.orm.sql_lib.ISqlLibManager;
import io.nop.xlang.api.XLang;
import jakarta.inject.Inject;
Expand All @@ -41,6 +42,9 @@ public class TestMultiDataSource extends JunitBaseTestCase {
@Inject
ISqlLibManager sqlLibManager;

@Inject
H2GisInitializer h2GisInitializer;

@Test
public void testSqlQuerySpace() {
IEvalScope scope = XLang.newEvalScope();
Expand Down

0 comments on commit 71278bf

Please sign in to comment.