📄 role.java
字号:
package com.testDataBuilder.core.role;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.testDataBuilder.core.XmlUtil;
import com.testDataBuilder.core.baseType.IType;
import com.testDataBuilder.core.baseType.JavaTypes;
import com.testDataBuilder.exception.BaseException;
import com.testDataBuilder.util.Global;
import com.testDataBuilder.util.JDBCUtil;
import com.testDataBuilder.util.RandomUtil;
public class Role implements Comparable,Cloneable{
static Logger logger = Logger.getLogger(Role.class);
//public static final String TAG_DB_REF = "dbRef";
public static final String TAG_JAVA_SOURCE = "javaCode";
public static final String TAG_SQL = "sql";
public static final String TAG_SOURCE_NAME = "sourceName";
public static final String TAG_ROLE = "role";
public static final String TAG_NAME = "name";
public static final String TAG_TYPE = "type";
public static final String TAG_MIN = "min";
public static final String TAG_MAX = "max";
public static final String TAG_MIN_DATE = "minDate";
public static final String TAG_MAX_DATE = "maxDate";
public static final String TAG_METHOD = "method";
public static final String TAG_DISTINCT_COUNT = "distinctCount";
public static final String TAG_ENUM = "enum";
public static final String TAG_PERCENT = "percent";
public static final String TAG_VALUE = "value";
public static final String TAG_STEP = "step";
public static final String TAG_PREFIX = "prefix";
public static final String TAG_SUFFIX = "suffix";
public static final String TAG_SIZE = "size";
public static final String TAG_DECIMAL_DIGITS = "decimalDigits";
public static final String TAG_NULL_PERCENT = "nullPercent";
/**
* 数据产生方法,随机方式
*/
public static final String METHOD_RANDOM = "random";
/**
* 数据产生方式,自动增长方式.
*/
public static final String METHOD_INCREMENT = "increment";
/**
* 使用枚举值.
*/
public static final String METHOD_ENUM = "enum";
/**
* 使用SQL查询语句。
*/
public static final String METHOD_SQL_QUERY = "sqlquery";
public static final String METHOD_SQL_FUNC = "sqlfunc";
/**
* 提供给高级用户的,使用Java语言生成测试数据.
*/
public static final String METHOD_JAVA = "java";
/**
* 从配置文件中读取规则信息.
* <p><code>configure</code></p>
* @param roleEle
* @return 数据类.
* @throws BaseException
* @author LiuXiaojie 2007-6-16
*/
public Role(Element roleEle) throws BaseException{
String name = XmlUtil.getStringAttributeValue(roleEle,TAG_NAME,true);
this.setName(name);
String strType = XmlUtil.getStringAttributeValue(roleEle,TAG_TYPE,true);
Class javaType = null;
try {
javaType = Global.getInstance().loadTypeClass(strType);
this.setType(javaType);
} catch (ClassNotFoundException e) {
logger.error("Role", e);
} catch (MalformedURLException e) {
logger.error("Role", e);
}
String prefix = roleEle.attributeValue(TAG_PREFIX);
this.setPrefix(prefix);
String suffix = roleEle.attributeValue(TAG_SUFFIX);
this.setSuffix(suffix);
Integer step = XmlUtil.getIntAttributeValue(roleEle,TAG_STEP,false);
if(step != null){
this.setStep(step);
}
Integer distinctCount = XmlUtil.getIntAttributeValue(roleEle,TAG_DISTINCT_COUNT,false);
if(distinctCount != null){
this.setDistinctCount(distinctCount);
}
Float nullPercent = XmlUtil.getFloatAttributeValue(roleEle,TAG_NULL_PERCENT,false);
if(nullPercent != null){
this.setNullPercent(nullPercent);
}
String strMethod = roleEle.attributeValue(TAG_METHOD);
if(StringUtils.isNotEmpty(strMethod)){
if(strMethod.equalsIgnoreCase(METHOD_INCREMENT)
|| strMethod.equalsIgnoreCase(METHOD_RANDOM)){
String strMin = roleEle.attributeValue(TAG_MIN);
double min = 0;
if(StringUtils.isNotEmpty(strMin)){
try{
min = JavaTypes.getInstance().getDbValue(javaType, strMin);
this.setMin(min);
}catch(Exception ex){
throw new BaseException(name, "invalid min value [" + strMin + "]");
}
}
String strMax = roleEle.attributeValue(TAG_MAX);
double max = 0;
if(StringUtils.isNotEmpty(strMax)){
try{
max = JavaTypes.getInstance().getDbValue(javaType, strMax);
this.setMax(max);
}catch(Exception ex){
ex.printStackTrace();
throw new BaseException(name, "invalid max value [" + strMax + "]");
}
}
}else if(strMethod.equalsIgnoreCase(METHOD_ENUM)){
List enums = roleEle.elements(TAG_ENUM);
if(enums != null){
for(int i=0;i < enums.size(); i++){
Element enumEle = (Element) enums.get(i);
String value = enumEle.attributeValue(TAG_VALUE);
Object eValue = JavaTypes.getInstance().valueOf(javaType,value);
String strPercent = enumEle.attributeValue(TAG_PERCENT);
Integer percent = 1;
if(StringUtils.isNotEmpty(strPercent)){
try{
percent = Integer.valueOf(strPercent);
}catch(Exception ex){
logger.error("Role", ex);
}
}
this.addEnum(eValue, percent);
}
}
}else if(strMethod.equalsIgnoreCase(METHOD_SQL_QUERY)
||strMethod.equalsIgnoreCase(METHOD_SQL_FUNC)){
Element sqlEle = roleEle.element(TAG_SQL);
if(sqlEle != null){
String sql = sqlEle.getTextTrim();
this.setSQL(sql);
String sourceName = sqlEle.attributeValue(TAG_SOURCE_NAME);
if(StringUtils.isNotEmpty(sourceName)){
this.setSourceName(sourceName);
}
}
}else if(strMethod.equalsIgnoreCase(METHOD_JAVA)){
Element javaSourceEle =XmlUtil.getChildElement(roleEle, TAG_JAVA_SOURCE, true);
JavaSource javaSource = JavaSource.configure(javaSourceEle);
this.setJavaSource(javaSource);
}else{
throw new BaseException(name, "invalid method value[" + strMethod + "]");
}
this.setMethod(strMethod.toLowerCase());
}
}
public Element toElement(Element roleEle){
Class type = this.getType();
roleEle.clearContent();
roleEle.addAttribute(TAG_NAME, this.getName());
roleEle.addAttribute(TAG_TYPE, type.getName());
roleEle.addAttribute(TAG_METHOD, this.getMethod());
roleEle.addAttribute(TAG_DISTINCT_COUNT, this.getDistinctCount() + "");
roleEle.addAttribute(TAG_PREFIX, this.getPrefix());
roleEle.addAttribute(TAG_SUFFIX, this.getSuffix());
roleEle.addAttribute(TAG_MIN, this.getStringMin());
roleEle.addAttribute(TAG_MAX, this.getStringMax());
roleEle.addAttribute(TAG_NULL_PERCENT, this.getNullPercent() + "");
for(EnumObj e: this.getEnumerate()){
Element enuEle = roleEle.addElement(TAG_ENUM);
enuEle.addAttribute(TAG_VALUE, e.getValue() + "");
enuEle.addAttribute(TAG_PERCENT, e.getPercent() + "");
}
if(StringUtils.isNotEmpty(this.getSQL())){
Element sqlEle = roleEle.addElement(TAG_SQL);
sqlEle.setText(this.getSQL());
sqlEle.addAttribute(TAG_SOURCE_NAME, this.getSourceName());
}
if(this.getJavaSource() != null){
Element javaSourceEle = this.getJavaSource().toElement();
roleEle.add(javaSourceEle);
}
return roleEle;
}
public Element toElement(){
Element roleEle = DocumentHelper.createElement(TAG_ROLE);
return toElement(roleEle);
}
public Role() {
}
public Role(String name) {
this.setName(name);
}
public Role(Role role){
this.current = role.current;
this.distinctCount = role.distinctCount;
this.max = role.max;
this.method = role.method;
this.min = role.min;
this.name = role.name;
this.prefix = role.getPrefix();
this.step = role.step;
this.suffix = role.suffix;
this.type = role.type;
this.nullPercent = role.nullPercent;
this.roleFactory = role.roleFactory;
Map map = role.atts;
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
Object value = map.get(key);
this.atts.put(key, value);
}
this.getEnumerate().clear();
Collections.copy(this.enumerate, role.enumerate);
}
private RoleFactory roleFactory = null;
/**
* 数据类型的名称(以便于引用.)
*/
private String name;
/**
* 数据的类型
*/
private Class type = String.class;
/**
* 如果是数值型,则是最小值,如果是字段串,则是最小长度.
*/
private double min = 0;
/**
* 如果是数值型,则是最大值,如果是字段串,则是最大长度.
*/
private double max = 10;
/**
* 数据产生方式.
*/
private String method = METHOD_INCREMENT;
private int step = 1;
/**
* 同一列中数据计数总数.
*/
private int distinctCount = 0;
private EnumList<EnumObj> enumerate = new EnumList<EnumObj>();
public List<ComplexObj> tempDBObj = null;
public List<ComplexObj> getTempDBObj(){
if(tempDBObj == null){
tempDBObj = new LinkedList<ComplexObj>();
}
return tempDBObj;
}
public void setTempDBObj(List<ComplexObj> tempDBObj){
this.tempDBObj = tempDBObj;
}
private String prefix;
private String suffix;
/**
* 插入Null值的百分比,0表示不插入
*/
private float nullPercent = 0;
private boolean nullable = true;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public Object random(){
if(this.getDecimalDigits() == null){
return JavaTypes.getInstance().random(type, min, max, 0);
}else{
return JavaTypes.getInstance().random(type, min, max,this.getDecimalDigits());
}
}
public Object getIncValue(double step){
Object value = JavaTypes.getInstance().getStringValue(type, step);
if(value == null){
value = random();
}
return value;
}
private Map<String, Object> atts = new HashMap<String,Object>();
public void init(Connection conn, int maxRows) throws BaseException, SQLException{
getTempDBObj().clear();
//从数据库中读取数据.
if(this.isSQLQueryMethod()){
PreparedStatement statement = null;
ResultSet result = null;
try {
statement = conn.prepareStatement(this.getSQL());
statement.setMaxRows(maxRows);
result = statement.executeQuery();
List fields = JDBCUtil.getAllFields(this.getSQL());
//System.out.println("=========================================");
while(result.next()){
ComplexObj complex = new ComplexObj();
for(int i=0; i < fields.size(); i++){
complex.addValue((String)fields.get(i),
result.getObject((String) fields.get(i)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -