/***/packagecom.chenlb.study.ehcache;importjava.util.List;importjava.util.Random;importjunit.framework.TestCase;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;/*** 2007-8-2
*@authorchenlb
**/publicclassCityCacheTestextendsTestCase {protectedfinalLog logger=LogFactory.getLog(getClass());privateApplicationContext context;privateRandom random;/***@throwsjava.lang.Exception*/protectedvoidsetUp()throwsException {
context=newClassPathXmlApplicationContext(newString[] {"application*.xml"}
);
random=newRandom();
}/***@throwsjava.lang.Exception*/protectedvoidtearDown()throwsException {
}/*** Test method for {@linkcom.chenlb.study.ehcache.CityCache#getCitys(java.lang.Integer)}.*/publicvoidtestGetCitys() {
CityCache cityCache=(CityCache) context.getBean("cityCache");
Listcitys=cityCache.getCitys(1);
assertNotNull(citys);
assertTrue(citys.size()>0);for(IdAndName inn : citys) {
logger.info("省1 :"+inn.getName());
}
citys=cityCache.getCitys(2);
assertNotNull(citys);
assertTrue(citys.size()>0);for(IdAndName inn : citys) {
logger.info("省2 :"+inn.getName());
}
}publicvoidtestGetCitysWithLoop() {
CityCache cityCache=(CityCache) context.getBean("cityCache");
Listcitys, citys_2;intloopNum=5;inttotelTimes=0;do{
logger.info("===============第"+(6-loopNum)+"次循环===============");
citys=cityCache.getCitys(1);
assertNotNull(citys);
assertTrue(citys.size()>0);for(IdAndName inn : citys) {
logger.info("省1 :"+inn.getName());
}
citys_2=cityCache.getCitys(2);
assertNotNull(citys_2);
assertTrue(citys_2.size()>0);for(IdAndName inn : citys_2) {
logger.info("省2 :"+inn.getName());
}
loopNum--;try{inttimes=random.nextInt(800)+400;//400 - 1200 msThread.sleep(times);
totelTimes+=times;
logger.info(totelTimes+"ms 后, 间隔:"+times+"ms");
}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();
}
}while(loopNum>0);
loopNum=5;do{//省2logger.info("===============第"+(6-loopNum)+"次循环===============");
citys=cityCache.getCitys(2);
assertNotNull(citys);
assertTrue(citys.size()>0);for(IdAndName inn : citys) {
logger.info("省2 :"+inn.getName());
}
loopNum--;try{inttimes=random.nextInt(400)+400;//400 - 800 msThread.sleep(times);
totelTimes+=times;
logger.info(totelTimes+"ms 后, 间隔:"+times+"ms");
}catch(InterruptedException e) {//TODO Auto-generated catch blocke.printStackTrace();
}
}while(loopNum>0);
}
}