import java.sql.ResultSet;
import java.util.List;
import mockit.Mock;
import mockit.MockUp;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.geezn.exception.GeeznException;
import com.runqian.mis2.util.DBAccess;
import com.runqianapp.ngr.data.model.MyLink;
import com.runqianapp.schedule.utils.PathUtils;
/**
* @author RAQ
*
*/
public class LinkDaoTest {
private LinkDao linkDao = new LinkDao();
/**
* 配置文件项目路径
*/
public static final String setProjectPath = "E:/workplace_link/LinkManagerJava/WebRoot";
/**
* 初始化环境
*/
@BeforeClass
public static void init() throws Exception {
mockPathUtils();
}
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* 测试删除批量超链接方法
* Test method for {@link com.runqianapp.ngr.link.dao.LinkDao#removeLinks(java.lang.String)}.
* @throws GeeznException
*/
@Test
public void testRemoveLinks() throws GeeznException {
StringBuffer links = new StringBuffer();
for(int i=21;i<1000;i++){
links.append("/link").append(i).append(",");
}
links.deleteCharAt(links.length()-1);
linkDao.removeLinks(links.toString());
for(int j=20;j<1000;j++){
MyLink linkCheck = linkDao.getMyLinkByShortLink("/link"+j);
assertEquals("判断删除超链接是否成功","",linkCheck.getShortLink());  //逐一验证超链接是否被成功删除
}
}
/**
* 测试删除单个超链接方法
* Test method for {@link com.runqianapp.ngr.link.dao.LinkDao#removeLink(java.lang.String)}.
* @throws Exception
*/
@Test
public void testRemoveLink() throws Exception {
JunitParamUtil junitUtil = new JunitParamUtil(System.getProperty("user.dir") + "/test/com/runqianapp/ngr/link/dao/removelinkparams",1);
// 从参数文件中获取到的参数List
List paramList = junitUtil.getParamList();
for (int i = 0; i < paramList.size(); i++) {
JunitParam param = (JunitParam) paramList.get(i);
linkDao.removeLinks(param.getString(0));
MyLink linkCheck = linkDao.getMyLinkByShortLink(param.getString(0));
assertEquals("判断删除超链接是否成功","",linkCheck.getShortLink());
}
}
/**
* 测试获取超链接方法
* Test method for {@link com.runqianapp.ngr.link.dao.LinkDao#getMyLinkByShortLink(java.lang.String)}.
* @throws Exception
*/
@Test
public void testGetMyLinkByShortLink() throws Exception {
JunitParamUtil junitUtil = new JunitParamUtil(System.getProperty("user.dir") + "/test/com/runqianapp/ngr/link/dao/getmylinkbyshortlinkparams",1);
// 从参数文件中获取到的参数List
List paramList = junitUtil.getParamList();
for (int i = 0; i < paramList.size(); i++) {
JunitParam param = (JunitParam) paramList.get(i);
MyLink linkCheck = linkDao.getMyLinkByShortLink(param.getString(0));
assertEquals("判断获取到的超链接是否为空",false,linkCheck.getLinkName().equals(""));
}
}
/**
* 测试修改超链接方法
* Test method for {@link com.runqianapp.ngr.link.dao.LinkDao#modifyMyLink(com.runqianapp.ngr.data.model.MyLink)}.
* @throws Exception
*/
@Test
public void testModifyMyLink() throws Exception {
JunitParamUtil junitUtil = new JunitParamUtil(System.getProperty("user.dir") + "/test/com/runqianapp/ngr/link/dao/modifylinkparams",6);
// 从参数文件中获取到的参数List
List paramList = junitUtil.getParamList();
for (int i = 0; i < paramList.size(); i++) {
MyLink myLink = new MyLink();
JunitParam param = (JunitParam) paramList.get(i);
// 获取并设置参数
myLink.setShortLink(param.getString(0));
myLink.setLinkName(param.getString(1));
myLink.setLinkUrl(param.getString(2));
myLink.setLinkDes(param.getString(3));
myLink.setIsRes(param.getInt(4));
myLink.setParams(param.getString(5));
linkDao.modifyMyLink(myLink);
String linkName = linkDao.getMyLinkByShortLink(param.getString(0)).getLinkName();
String linkUrl = linkDao.getMyLinkByShortLink(param.getString(0)).getLinkUrl();
String linkDes = linkDao.getMyLinkByShortLink(param.getString(0)).getLinkDes();
int linkIsRes = linkDao.getMyLinkByShortLink(param.getString(0)).getIsRes();
String linkParams = linkDao.getMyLinkByShortLink(param.getString(0)).getParams();
assertEquals("判断超链接名称是否已被修改",param.getString(1),linkName);
assertEquals("判断超链接Url是否已被修改",param.getString(2),linkUrl);
assertEquals("判断超链接描述是否已被修改",param.getString(3),linkDes);
assertEquals("判断超链接引用是否已被修改",param.getInt(4),linkIsRes);
assertEquals("判断超链接参数是否已被修改",param.getString(5),linkParams);
}
}
/**
* 测试新增超链接方法
* Test method for {@link com.runqianapp.ngr.link.dao.LinkDao#saveLink(com.runqianapp.ngr.data.model.MyLink)}.
* @throws Exception
*/
@Test
public void testSaveLink() throws Exception {
// Junit参数工具类
JunitParamUtil junitUtil = new JunitParamUtil(System.getProperty("user.dir") + "/test/com/runqianapp/ngr/link/dao/savelinkparams",6);
// 从参数文件中获取到的参数List
List paramList = junitUtil.getParamList();
for (int i = 0; i < paramList.size(); i++) {
MyLink myLink = new MyLink();
// Junit参数类
JunitParam param = (JunitParam) paramList.get(i);
// 获取并设置参数
myLink.setLinkName(param.getString(1));
myLink.setLinkUrl(param.getString(2));
myLink.setLinkDes(param.getString(3));
myLink.setIsRes(param.getInt(4));
myLink.setParams(param.getString(5));
//根据数据总条数来获得后一个超链接ID,用于断言
DBAccess dba = new DBAccess();
String shortLink = "";
StringBuffer sql = new StringBuffer();
sql.append("select count(*) total from t_hyperlink");
ResultSet rs  = dba.executeQuery(sql.toString());
int total = 0;
while(rs.next()){
total = rs.getInt("total");
}
linkDao.saveLink(myLink);
MyLink checkLink = linkDao.getMyLinkByShortLink("/link"+total);
assertEquals("判断超链接名称",param.getString(1),checkLink.getLinkName());
assertEquals("判断超链接URL",param.getString(2),checkLink.getLinkUrl());
assertEquals("判断超链接描述",param.getString(3),checkLink.getLinkDes());
assertEquals("判断超链接是否引用",param.getInt(4),checkLink.getIsRes());
assertEquals("判断超链接参数",param.getString(5),checkLink.getParams());
}
//压力测试
//      for(int i = 20;i<1000;i++){
//          MyLink myLink = new MyLink();
//          myLink.setShortLink("/link100");
//          myLink.setLinkName("测试用例01");
//          myLink.setLinkUrl("http://www.runqian.com.cn");
//          myLink.setLinkDes("");
//          myLink.setIsRes(1);
//          try {
//              linkDao.saveLink(myLink);
//              MyLink checkLink = linkDao.getMyLinkByShortLink("/link20");
//              assertEquals("判断超链接名称","测试用例01",checkLink.getLinkName());
//          } catch (GeeznException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
//      }
}
/**
* 模拟PathUtils
*/
public  static void mockPathUtils() {
new MockUp<PathUtils>() {
@Mock
public String getAppPhyPath() {
return setProjectPath;
}
};
}
}