All Downloads are FREE. Search and download functionalities are using the official Maven repository.

holmos.webtest.junitextentions.parameters.database.ConnectionInfoGetter Maven / Gradle / Ivy

There is a newer version: 1.0.2u10
Show newest version
package holmos.webtest.junitextentions.parameters.database;

import holmos.webtest.basetools.HolmosAnnotationTool;
import holmos.webtest.exceptions.HolmosFailedError;
import holmos.webtest.junitextentions.annotations.DataBase;
import holmos.webtest.log.MyLogger;

import java.util.HashMap;

/**
 * 获取一个测试类里面所有的带有{@link DataBase}注解的数据库连接,这些数据库连接如果给定了连接的名字,那么名字将被作为寻
 * 找这个连接的索引,如果没有给定数据库连接的名字,那么数据库连接的url将被作为索引,如果这个没有设置,将会抛出异常
 * 
 * @author 吴银龙([email protected])
 * */
public class ConnectionInfoGetter {
	MyLogger logger=MyLogger.getLogger(ConnectionInfoGetter.class);
	@SuppressWarnings("rawtypes")
	private Class testClass;
	private HashMapconnections;
	@SuppressWarnings("rawtypes")
	public ConnectionInfoGetter(Class testClass){
		this.testClass=testClass;
		this.connections=new HashMap();
	}
	public ConnectionInfo getConnection(String connectionName){
		if(connections.get(connectionName)!=null)
			return connections.get(connectionName);
		getClassLevelDataBaseInfo();
		getAllMethodLevelDataBaseInfo();
		if(connections.get(connectionName)==null)
			throw new HolmosFailedError("在"+testClass+"测试类中没有"+connectionName+"数据库连接的配置!");
		return connections.get(connectionName);
	}
	private void getAllMethodLevelDataBaseInfo() {
		for(DataBase dataBase:HolmosAnnotationTool.getMethodLevelAnnotations(testClass, DataBase.class)){
			createConnectionInfo(dataBase);
		}
	}
	private void getClassLevelDataBaseInfo() {
		DataBase dataBase=HolmosAnnotationTool.getClassLevelAnnotation(testClass, DataBase.class);
		createConnectionInfo(dataBase);
	}
	private void createConnectionInfo(DataBase dataBase){
		if(dataBase.url()==null||"".equalsIgnoreCase(dataBase.url()))
			throw new HolmosFailedError(testClass.toString()+"Database注解中的url字段为null或者空窜!");
		String key=(dataBase.connectionName()==null)?dataBase.url():dataBase.connectionName();
		if(connections.get(key)!=null){
			logger.warn("在同一个类里面的数据库连接配置重复!");
		}connections.put(key, new ConnectionInfo(dataBase));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy