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;
}
};
}
}