[英]DBUnit PostgresqlDataTypeFactory does not recognizes enum list
我正在使用DBUnit進行集成測試,在執行測試代碼之前,我遇到了這個錯誤:
badges.track_types data type (2003, '_text') not recognized and will be ignored. See FAQ for more information.
org.dbunit.dataset.NoSuchColumnException: badges.TRACK_TYPES - (Non-uppercase input column: track_types) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.
被忽略的列是枚舉列表。 在數據集中,它是這樣編寫的:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
// More info ...
<badges name="30'000" description="30k a day" image_name="30000.png" threshold_val="30000.00000000" has_many="true" id="45" track_types="{TRACK_GENERIC}" "/>
</dataset>
我查看了DBUnit FAQ並看到了這個問題 ,說我必須覆蓋isEnumType()方法來支持我的枚舉是Postgresql,所以我這樣做了:
/**
* Override method to set custom properties/features
*/
protected void setUpDatabaseConfig(DatabaseConfig config) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory(){
public boolean isEnumType(String sqlTypeName) {
if(sqlTypeName.equalsIgnoreCase("track_types")){
return true;
}
return false;
}
});
config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new DefaultMetadataHandler());
}
但我仍然得到同樣的錯誤,我不知道為什么。 也許我沒有超越這個方法? 也許它甚至不是我的問題的原因? 如果您需要任何其他代碼,請詢問,謝謝!
嘗試用它的值來保持枚舉
enum.values();
它返回一個數組而不是保存這個元素
嗯...我無法完全解決這個問題,但設法通過解決方法解決了這個問題。
由於@DatabaseSetup注釋而出現此錯誤。 如果我在不使用它的情況下執行此過程,它仍會拋出“未識別列”錯誤,因為它無法識別postgres數組(這是我的根本原因)但我可以通過創建一個新的DataTypeFactory來解決它從默認的:
public class PsqlArrayDataTypeFactory extends DefaultDataTypeFactory {
public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {
if (sqlType == Types.ARRAY)
{
return DataType.VARCHAR;
}
return super.createDataType(sqlType, sqlTypeName);
}
}
我有下一個錯誤:
Caused by: org.postgresql.util.PSQLException: ERROR: column "status" is of type topic_status but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
DbUnit
為表請求meta並從PostgreSQL接收枚舉類型VARCHAR
。 但是PostgreSQL不會接受這種類型。
我以下一種方式從PostgresqlDataTypeFactory
中覆蓋了方法:
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new PostgresqlDataTypeFactory() {
@Override
public boolean isEnumType(String sqlTypeName) {
return "topic_status".equalsIgnoreCase(sqlTypeName);
}
@Override
public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {
if (isEnumType(sqlTypeName)) {
sqlType = Types.OTHER;
}
return super.createDataType(sqlType, sqlTypeName);
}
});
);
這將枚舉的類型設置為OTHER
,父方法super.createDataType(sqlType, sqlTypeName)
以適當的方式創建DataType
。
PostgreSQL版本:9.6.5
DbUnit版本:2.5.4
可以在討論中找到更多信息。
對postgresql枚舉的支持有限,因為自dbunit 2.4.6起只支持讀寫字符串。 為此,您必須覆蓋PostgresqlDataTypeFactory中的方法“isEnumType”,如下所示:
PostgresqlDataTypeFactory factory = new PostgresqlDataTypeFactory(){
public boolean isEnumType(String sqlTypeName) {
if(sqlTypeName.equalsIgnoreCase("abc_enum")){
return true;
}
return false;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.