1.首先下载springmock的jar包
2.把springmock.jar加入项目中
3.导入jutil lib包代码如下
1.)首先定义一个测试类的父类AbstractMvcTest
package com.guagua.v.action;
import java.util.Hashtable;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import com.guagua.core.action.CoreActionSupport;
import com.guagua.v.util.Constants;
import com.guagua.v.util.CookieUtil;
/**
* Action 单元测试基类<br/>
* 使用 spring 3.1 的 mock 来模拟 http 对象,需引用 testlib 下的 lib 包
*
* @author 吴振中
* @version 2011/10/13 15:06
*/
@Ignore
public abstract class AbstractMvcTest {
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
protected MockHttpSession httpSession;
protected Map<Object, Object> session;
/**
* 测试前初始化相关对象
*
* @throws Throwable
*/
@Before
public void init() throws Throwable {
session = new Hashtable<Object, Object>();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
httpSession = new MockHttpSession();
request.setSession(httpSession);
initServletConfig();
}
protected abstract void initServletConfig();
/**
* 测试结束后释放对象
*/
@After
public void destroy() {
session.clear();
}
/**
* 模拟用户登录
*/
public void mockLoginUser() {
CookieUtil.saveCookie(Constants.COOKIE_ID_NICKNAME,
"35444653|天涯 2016|", response, 1000);
}
/**
* 公用方法,方便初始化 action
*
* @param action
*/
public void initAction(CoreActionSupport action) {
action.setServletRequest(request);
action.setServletResponse(response);
action.setSession(session);
}
}
CoreActionSupport 为你项目中所有的action父类如:
public class RoomAction extends CoreActionSupport
2)所有action测试类继承AbstractMvcTest 例如:
package com.guagua.v.action;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* 房间页测试
*
* @author 吴振中
* @version 2011/10/13 15:06
*/
public class RoomActionTest extends AbstractMvcTest {
@Override
protected void initServletConfig() {
}
//测试未登录访问已经存在的房间ID
@Test
public void testRoomNoLogin() throws Throwable {
init();
RoomAction roomAction = new RoomAction();
initAction(roomAction);
roomAction.setRid(19L);
assertEquals("返回非成功页面", "success", roomAction.execute());
assertEquals("房间号与原先不等", 19L, request.getAttribute("rid"));
}
//测试登录访问已经存在的房间ID
@Test
public void testRoomLogin() throws Throwable {
init();
RoomAction roomAction = new RoomAction();
initAction(roomAction);
mockLoginUser();
roomAction.setRid(10l);
assertEquals("返回非成功页面", "success", roomAction.execute());
}
}
3)测试一些非action类如util包下的
package com.guagua.v.util;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public final class StrUtils {
private static final Logger logger = Logger.getLogger(StrUtils.class);
public static boolean isEmptyOrNull(String str){
return str==null || str.trim().equals("");
}
public static boolean isNullOrEmpty(String value) {
if (value == null)
return true;
value = value.trim();
if (value.equals(""))
return true;
return false;
}
public static String notNull(String value) {
if (value == null)
return Constants.ANY_STRING;
return value;
}
public static String trim(String value) {
if (value == null)
return Constants.ANY_STRING;
return value.trim();
}
public static String shortString(String value, int maxWidth) {
if (value == null)
return Constants.ANY_STRING;
int width = 0, index = 0;
while (index < value.length()) {
int ch = value.charAt(index);
width += ((ch & 0xFF00) == 0) ? 1 : 2;
if (width + 3 > maxWidth)
return value.substring(0, index) + "...";
index++;
}
return value;
}
public static String replace(String value, String find, String replace) {
if (value == null)
return Constants.ANY_STRING;
StringBuffer buffer = new StringBuffer(value.length());
int findLength = find.length();
int fromIndex = 0;
int toIndex = value.indexOf(find);
while (toIndex >= 0) {
buffer.append(value.substring(fromIndex, toIndex));
fromIndex = toIndex + findLength;
toIndex = value.indexOf(find, fromIndex);
}
buffer.append(value.substring(fromIndex, value.length()));
return buffer.toString();
}
public static String replaceIgnoreCase(String value, String find,
String replace) {
if (value == null)
return Constants.ANY_STRING;
StringBuffer buffer = new StringBuffer(value.length());
String valueLowerCase = value.toLowerCase();
String findLowerCase = find.toLowerCase();
int findLength = find.length();
int fromIndex = 0;
int toIndex = valueLowerCase.indexOf(findLowerCase);
while (toIndex >= 0) {
buffer.append(value.substring(fromIndex, toIndex));
fromIndex = toIndex + findLength;
toIndex = valueLowerCase.indexOf(findLowerCase, fromIndex);
}
buffer.append(value.substring(fromIndex, value.length()));
return buffer.toString();
}
private static final DateFormat tidyDateFormat = new SimpleDateFormat(
"yyyyMMdd");
private static final DateFormat shortDateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
private static final DateFormat yearMonthFormat = new SimpleDateFormat(
"yyyy-MM");
private static final DateFormat briefDateFormat = new SimpleDateFormat(
"MM-dd HH:mm");
private static final DateFormat middleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
private static final DateFormat longDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static final DateFormat compactDateFormat = new SimpleDateFormat(
"yyyyMMddHHmmss");
public static Date regularizeDate(Date date, int type) {
if (date == null)
return null;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new Date().getTime());
if (type == 1) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
} else if (type == 2) {
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
}
return date;
}
public static String formatTidyDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return tidyDateFormat.format(date);
}
public static Date parseTidyDate(String value) throws ParseException {
if (value == null)
return null;
return tidyDateFormat.parse(value);
}
public static Date parseTidyDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return tidyDateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrect tidy date, default used.", e);
return defaultValue;
}
}
public static Date parseTidyDateLast(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return new Date(tidyDateFormat.parse(value).getTime()
+ (86400000 - 1000));
} catch (ParseException e) {
logger.debug("incorrect tidy date, default used.", e);
return defaultValue;
}
}
public static String formatShortDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return shortDateFormat.format(date);
}
/**
* @author tangfeng
* @param Date
* @return String
* @throws ParseException
*/
public static String formatYearMonthDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return yearMonthFormat.format(date);
}
public static Date parseShortDate(String value) throws ParseException {
if (value == null)
return null;
return shortDateFormat.parse(value);
}
public static Date parseShortDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return shortDateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrect short date, default used.", e);
return defaultValue;
}
}
public static Date parseShort59Date(Date tempDate) {
return new Date(StrUtils.parseShortDate(StrUtils.formatShortDate(tempDate), tempDate).getTime() + 86399999);
}
public static String formatBriefDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return briefDateFormat.format(date);
}
public static String formatMiddleDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return middleDateFormat.format(date);
}
public static Date parseMiddleDate(String value) throws ParseException {
if (value == null)
return null;
return middleDateFormat.parse(value);
}
public static Date parseMiddleDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return middleDateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrect middle date, default used.", e);
return defaultValue;
}
}
public static String formatLongDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return longDateFormat.format(date);
}
public static Date parseLongDate(String value) throws ParseException {
if (value == null)
return null;
return longDateFormat.parse(value);
}
public static Date parseLongDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return longDateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrect long date, default used.", e);
return defaultValue;
}
}
public static String formatCompactDate(Date date) {
if (date == null)
return Constants.ANY_STRING;
return compactDateFormat.format(date);
}
public static Date parseCompactDate(String value) throws ParseException {
if (value == null)
return null;
return compactDateFormat.parse(value);
}
public static Date parseCompactDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
return compactDateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrect compact date, default used.", e);
return defaultValue;
}
}
public static Date parseDate(String value, Date defaultValue) {
try {
if (value == null)
return defaultValue;
String format = "yyyyMMdd";
if (value.indexOf("-") > 0) {
format = "yyyy-MM-dd";
} else if (value.indexOf("/") > 0) {
if (value.length() == 8)
value = "20" + value;
format = "yyyy/MM/dd";
}
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(value);
} catch (ParseException e) {
logger.debug("incorrecttidy date, default used.", e);
return defaultValue;
}
}
public static boolean parseBoolean(String value) {
if (value == null)
return false;
return value.equalsIgnoreCase("true");
}
public static boolean parseBoolean(String value, boolean defaultValue) {
if (value == null)
return defaultValue;
if (value.equalsIgnoreCase("true"))
return true;
if (value.equalsIgnoreCase("false"))
return false;
return defaultValue;
}
public static int parseInt(String value, int defaultValue) {
if (value == null)
return defaultValue;
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
logger.debug("Bad integer, default used.", e);
return defaultValue;
}
}
public static long parseLong(String value, long defaultValue) {
if (value == null)
return defaultValue;
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
logger.debug("Bad long integer, default used.", e);
e.printStackTrace();
return defaultValue;
}
}
public static boolean checkEmail(String email) {
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
return m.find();
}
public static String getSeparator() {
String separator = "";
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows"))
separator = "/";
else
separator = "/";
return separator;
}
/**
* @author tangfeng
* @param sourceStr
* @param ch
* @return
*/
public static int getCharNum(String sourceStr, char ch) {
if (sourceStr.indexOf(ch) < 0)
return -1;
else {
char[] source = sourceStr.toCharArray();
int j = 0;
for (int i = 0; i < source.length; i++) {
if (source[i] == ch)
++j;
}
return j;
}
}
public static String getRemainderStr(String existStr, String separator, String allStr){
if(existStr == null || "".equals(existStr) || allStr == null || "".equals(allStr))
return allStr;
String splitAllStr[] = allStr.split(",");
String splitAllStr_ = "";
String remainderStr = "";
for(int i = 0; i < splitAllStr.length; i++){
splitAllStr_ = splitAllStr[i];
if(!"".equals(splitAllStr_) && existStr.indexOf(splitAllStr_) == -1){
if("".equals(remainderStr)){
remainderStr = splitAllStr_;
}else
remainderStr += "," + splitAllStr_;
}
}
return remainderStr;
}
public static String getShortDateStrFormList(List<Date> dates){
StringBuffer sb = new StringBuffer();
if(dates != null && dates.size() != 0){
Iterator it = dates.iterator();
int i = 0;
while(it.hasNext()){
if(i != 0)
sb.append(",");
sb.append(formatShortDate((Date)it.next()));
i ++;
}
}
return sb.toString();
}
public static String getNOZero(float num){
if(num * 10 == 0){
return 0 + "";
}
return num + "";
}
public static String get3Str(String prefixStr, Integer last3Int){
String _3Str = "";
if(last3Int < 10)
_3Str = "00" + last3Int;
else if(10 <= last3Int && last3Int < 100){
_3Str = "0" + last3Int;
}else{
_3Str = "" + last3Int;
}
return prefixStr + _3Str;
}
public static String getSeq(String prefixStr, Integer index){
String _3Str = "" + index;
if(!"".equals(prefixStr))
_3Str = prefixStr + "." + index;
return _3Str;
}
public static String replaceEnter(String oldStr) {
//Pattern p = Pattern.compile("|\t|\r|\n");
Pattern p = Pattern.compile("\r|\n");
Matcher m = p.matcher(oldStr);
return m.replaceAll("\t");
}
public static String getSeason(int month){
if(month > 0 && month <=3){
return "Q1";
}else if(month >= 4 && month <=6){
return "Q2";
}else if(month >= 7 && month <=9){
return "Q3";
}else if(month >= 10 && month <= 12){
return "Q4";
}
return "";
}
public static List<String> convertList(String idStr, String separator){
List<String> ids = new ArrayList();
if(!StrUtils.isNullOrEmpty(idStr)){
String[] idArray = idStr.split(separator);
for(int i = 0; i < idArray.length; i++){
if(!"".equals(idArray[i])){
ids.add(idArray[i]);
}
}
}
return ids;
}
/**
* 个人中心翻页
* @param start 当前页码 从0开始,0表示第一页 1表示第二页 依此类推
* @param action 请求的action,对应x-work.xml中配置的
* @param num 多少页
* @param startName 页码参数 比如个人中心我的礼物中的页码属性为 m_start
* @return
*/
public static String getFanye(int start,String action,int num,String startName,
String curBeginCss,String curEndCss,Map<String,String> paramsMap){
StringBuilder sBuilder = new StringBuilder("");
Set<String> set = paramsMap.keySet();
StringBuilder paramsBuilder = new StringBuilder("");
for (Iterator<String> ite = set.iterator();ite.hasNext();){
String key = ite.next();
paramsBuilder.append("&"+key+"="+paramsMap.get(key));
}
if (start>0){
sBuilder.append("<a href=\""+action+".jspa?"+startName+"="+(start-1)+paramsBuilder.toString()+"\">[上一页]</a>");
}
for (int i=0;i<num;i++){
if (i==start){
sBuilder.append(curBeginCss+(start+1)+curEndCss);
}else{
sBuilder.append("<a href=\""+action+".jspa?"+startName+"="+i+paramsBuilder.toString()+"\">["+(i+1)+"]</a>");
}
}
if (start>=0&&start<(num-1)){
sBuilder.append("<a href=\""+action+".jspa?"+startName+"="+(start+1)+paramsBuilder.toString()+"\">[下一页]</a>");
}
return sBuilder.toString();
}
/**
* 判断是否是数字
* @param str
* @return
*/
public static boolean isNumber(String str) {
if(str==null){
return false;
}
String regex = "^\\d+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 判断是否是纯由字母组成的字符串
* @param str
* @return
*/
public static boolean isStr(String str) {
if(str==null){
return false;
}
String regex = "^[a-zA-Z]+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 检查用户注册时的用户名是否合法
* @param str
* @return
*/
public static boolean isUsername(String str){
if(str==null || str.length()<4){
return false;
}
String regex = "^([a-zA-Z_]+(\\d+)?)+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 转换错误栈为为字符串。
* @param e
* @return
*/
public static String getExceptionStack(Throwable e){
if(e==null) return "";
OutputStream ou = new ByteArrayOutputStream();
PrintStream o = new PrintStream(ou);
e.printStackTrace(o);
return ou.toString();
}
public static String long2DateString(long time){
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd HH:mm");
if (time > 0) {
return format.format(time);
}
return null;
}
/**
* 把date 转换成 日/月 格式
* @param date
* @return
*/
public static String formateDayMonth(long date){
if(date<=0){
return "";
}
Date d = new Date(date);
SimpleDateFormat t = new SimpleDateFormat("dd/MM");
String s = t.format(date);
return s;
}
public static String long2DateString2(long time){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if (time > 0) {
return format.format(time);
}
return null;
}
public static void main(String[] args) {
String s = "";
s = "123q";
System.out.println(isUsername(s));
s = "asdf";
System.out.println(isUsername(s));
s = "_123_as_df";
System.out.println(isUsername(s));
s = "a123asdf";
System.out.println(isUsername(s));
s = "a123asdf_";
System.out.println(isUsername(s));
}
}
jutil测试类为:
package com.guagua.v.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class StrUtilsTest {
@Test
public void testIsEmptyOrNull() {
assertEquals(true, StrUtils.isEmptyOrNull(null));
assertEquals(true, StrUtils.isEmptyOrNull(""));
assertEquals(true, StrUtils.isEmptyOrNull(" "));
assertEquals(false, StrUtils.isEmptyOrNull("!"));
}
@Test
public void testIsNullOrEmpty() {
assertEquals(true, StrUtils.isNullOrEmpty(null));
assertEquals(true, StrUtils.isNullOrEmpty(""));
assertEquals(true, StrUtils.isNullOrEmpty(" "));
assertEquals(false, StrUtils.isNullOrEmpty("!"));
}
@Test
public void testNotNull() {
assertNotNull(StrUtils.notNull(null));
assertNotNull(StrUtils.notNull("str"));
}
@Test
public void testTrim() {
assertNotNull(StrUtils.trim(null));
assertNotNull(StrUtils.trim("str"));
}
@Test
public void testShortString() {
assertNotNull(StrUtils.shortString(null, 0));
assertNotNull(StrUtils.shortString("wubo", 10));
}
@Test
public void testReplace() {
assertNotNull(StrUtils.replace(null, "w", "o"));
assertNotNull(StrUtils.replace("wubo", "w", "o"));
assertNotNull(StrUtils.replace("wubo", "t", "o"));
}
@Test
public void testReplaceIgnoreCase() {
assertNotNull(StrUtils.replaceIgnoreCase(null, "w", "o"));
assertNotNull(StrUtils.replaceIgnoreCase("wubo", "w", "o"));
assertNotNull(StrUtils.replaceIgnoreCase("wubo", "t", "o"));
}
@Test
public void testRegularizeDate() {
assertEquals(null,StrUtils.regularizeDate(null, 0));
assertNotNull(StrUtils.regularizeDate(new Date(), 0));
assertNotNull(StrUtils.regularizeDate(new Date(), 1));
}
@Test
public void testFormatTidyDate() {
assertNotNull(StrUtils.formatTidyDate(null));
assertNotNull(StrUtils.formatTidyDate(new Date()));
}
@Test
public void testParseTidyDateLast() {
assertNotNull(StrUtils.parseTidyDateLast(null, new Date()));
assertNotNull(StrUtils.parseTidyDateLast("2011-09-21", new Date()));
assertNotNull(StrUtils.parseTidyDateLast("sfsfsf", new Date()));
}
@Test
public void testFormatShortDate() {
assertNotNull(StrUtils.formatShortDate(null));
assertNotNull(StrUtils.formatShortDate(new Date()));
}
@Test
public void testFormatYearMonthDate() {
assertNotNull(StrUtils.formatYearMonthDate(null));
assertNotNull(StrUtils.formatYearMonthDate(new Date()));
}
@Test
public void testParseShortDateString() {
try{
assertEquals(null,StrUtils.parseShortDate(null));
assertNotNull(StrUtils.parseShortDate("2011-10-11"));
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testParseShortDateStringDate() {
try{
assertNotNull(StrUtils.parseShortDate(null,new Date()));
assertNotNull(StrUtils.parseShortDate("2011-10-11",new Date()));
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testParseShort59Date() {
assertNotNull(StrUtils.parseShort59Date(null));
assertNotNull(StrUtils.parseShort59Date(new Date()));
}
@Test
public void testFormatBriefDate() {
assertNotNull(StrUtils.formatBriefDate(null));
assertNotNull(StrUtils.formatBriefDate(new Date()));
}
@Test
public void testFormatMiddleDate() {
assertNotNull(StrUtils.formatMiddleDate(null));
assertNotNull(StrUtils.formatMiddleDate(new Date()));
}
@Test
public void testParseMiddleDateString() {
try{
assertEquals(null,StrUtils.parseMiddleDate(null));
assertNotNull(StrUtils.parseMiddleDate("sfsfs"));
assertNotNull(StrUtils.parseMiddleDate("2011-10-09"));
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testParseMiddleDateStringDate() {
assertNotNull(StrUtils.parseMiddleDate(null, new Date()));
assertNotNull(StrUtils.parseMiddleDate("str", new Date()));
assertNotNull(StrUtils.parseMiddleDate("2011-10-09", new Date()));
}
@Test
public void testFormatLongDate() {
assertNotNull(StrUtils.formatLongDate(null));
assertNotNull(StrUtils.formatLongDate(new Date()));
}
@Test
public void testParseLongDateString() {
try{
assertEquals(null,StrUtils.parseLongDate(null));
assertNotNull(StrUtils.parseLongDate("sfsf"));
assertNotNull(StrUtils.parseLongDate("2011-01-10"));
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testParseLongDateStringDate() {
assertNotNull(StrUtils.parseLongDate(null, new Date()));
assertNotNull(StrUtils.parseLongDate("tt", new Date()));
assertNotNull(StrUtils.parseLongDate("2011-1-12", new Date()));
}
@Test
public void testFormatCompactDate() {
assertNotNull(StrUtils.formatCompactDate(null));
assertNotNull(StrUtils.formatCompactDate(new Date()));
}
@Test
public void testParseCompactDateString() {
try{
assertEquals(null,StrUtils.parseCompactDate(null));
assertNotNull(StrUtils.parseCompactDate("sfsf"));
assertNotNull(StrUtils.parseCompactDate("2011-01-15"));
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testParseCompactDateStringDate() {
assertNotNull(StrUtils.parseCompactDate(null, new Date()));
assertNotNull(StrUtils.parseCompactDate("sf", new Date()));
assertNotNull(StrUtils.parseCompactDate("2011-05-07", new Date()));
}
@Test
public void testParseDate() {
assertNotNull(StrUtils.parseDate(null, new Date()));
assertNotNull(StrUtils.parseDate("sf", new Date()));
assertNotNull(StrUtils.parseDate("2011-05-07", new Date()));
}
@Test
public void testParseBooleanString() {
assertEquals(false,StrUtils.parseBoolean(null));
assertEquals(false,StrUtils.parseBoolean("str"));
assertEquals(true,StrUtils.parseBoolean("true"));
}
@Test
public void testParseBooleanStringBoolean() {
assertEquals(false,StrUtils.parseBoolean("false", false));
assertEquals(true,StrUtils.parseBoolean("true", false));
assertEquals(false,StrUtils.parseBoolean("sfsf", false));
}
@Test
public void testParseInt() {
assertNotNull(StrUtils.parseInt(null, 0));
assertNotNull(StrUtils.parseInt("45", 0));
assertNotNull(StrUtils.parseInt("sfs", 0));
}
@Test
public void testParseLong() {
assertNotNull(StrUtils.parseLong(null, 0));
assertNotNull(StrUtils.parseLong("45", 0));
assertNotNull(StrUtils.parseLong("sfs", 0));
}
@Test
public void testCheckEmail() {
assertEquals(false,StrUtils.checkEmail(null));
assertEquals(false,StrUtils.checkEmail("sfsfsf"));
assertEquals(true,StrUtils.checkEmail("wwfj@163.com"));
}
@Test
public void testGetSeparator() {
assertNotNull(StrUtils.getSeparator());
}
@Test
public void testGetCharNum() {
assertNotNull(StrUtils.getCharNum(null, 'a'));
assertNotNull(StrUtils.getCharNum("sddf", 'e'));
assertNotNull(StrUtils.getCharNum("sddf", 'd'));
}
@Test
public void testGetRemainderStr() {
assertEquals(null,StrUtils.getRemainderStr(null, "", null));
assertEquals(null,StrUtils.getRemainderStr("", "", null));
assertEquals("",StrUtils.getRemainderStr("sfsf", "", ""));
assertNotNull(StrUtils.getRemainderStr(null, "", "agaga"));
assertNotNull(StrUtils.getRemainderStr("hga", "", "agaga"));
assertNotNull(StrUtils.getRemainderStr("hjk", "", "agaga"));
}
@SuppressWarnings("unchecked")
@Test
public void testGetShortDateStrFormList() {
assertNotNull(StrUtils.getShortDateStrFormList(null));
assertNotNull(StrUtils.getShortDateStrFormList(new ArrayList<Date>()));
List datelist = new ArrayList<Date>();
datelist.add(new Date());
assertNotNull(StrUtils.getShortDateStrFormList(datelist));
}
@Test
public void testGetNOZero() {
assertNotNull(StrUtils.getNOZero(0.0f));
assertNotNull(StrUtils.getNOZero(0.5f));
assertNotNull(StrUtils.getNOZero(1.2f));
}
@Test
public void testGet3Str() {
assertNotNull(StrUtils.get3Str("", 9));
assertNotNull(StrUtils.get3Str("", 55));
assertNotNull(StrUtils.get3Str("", 155));
}
@Test
public void testGetSeq() {
assertNotNull(StrUtils.getSeq("", 1));
assertNotNull(StrUtils.getSeq("af", 1));
assertNotNull(StrUtils.getSeq(null, 1));
}
@Test
public void testReplaceEnter() {
assertNotNull(StrUtils.replaceEnter(null));
assertNotNull(StrUtils.replaceEnter(""));
assertNotNull(StrUtils.replaceEnter("sf\r\nsf"));
}
@Test
public void testGetSeason() {
assertNotNull(StrUtils.getSeason(8));
assertNotNull(StrUtils.getSeason(0));
assertNotNull(StrUtils.getSeason(100));
}
@Test
public void testConvertList() {
assertNotNull(StrUtils.convertList(null, "s"));
assertNotNull(StrUtils.convertList("tsesegs", "s"));
assertNotNull(StrUtils.convertList("weweewwe", "s"));
}
@SuppressWarnings("unchecked")
@Test
public void testGetFanye() {
assertNotNull(StrUtils.getFanye(1, "gsgs.action", 10, "sff", "sf", "ee", new HashMap<String,String>()));
Map map = new HashMap<String,String>();
map.put("t", "sf");
assertNotNull(StrUtils.getFanye(1, "gsgs.action", 10, "sff", "sf", "ee", map));
}
@Test
public void testIsNumber() {
assertEquals(false,StrUtils.isNumber(null));
assertEquals(false,StrUtils.isNumber("sfsf"));
assertEquals(false,StrUtils.isNumber(""));
assertEquals(true,StrUtils.isNumber("2"));
}
@Test
public void testIsStr() {
assertEquals(false,StrUtils.isStr(null));
assertEquals(false,StrUtils.isStr(""));
assertEquals(true,StrUtils.isStr("sfsf"));
}
@Test
public void testIsUsername() {
String s = "";
s = "123q";
assertEquals(false,StrUtils.isUsername(s));
s = "asdf";
assertEquals(true,StrUtils.isUsername(s));
s = "_123_as_df";
assertEquals(true,StrUtils.isUsername(s));
s = "a123asdf";
assertEquals(true,StrUtils.isUsername(s));
s = "a123asdf_";
assertEquals(true,StrUtils.isUsername(s));
}
@Test
public void testGetExceptionStack() {
assertNotNull(StrUtils.getExceptionStack(new Throwable("fsfsfsf")));
}
@Test
public void testLong2DateString() {
assertNotNull(StrUtils.long2DateString(14526545654555l));
assertEquals(null,StrUtils.long2DateString(0));
}
@Test
public void testFormateDayMonth() {
assertNotNull(StrUtils.formateDayMonth(14526545654555l));
assertNotNull(StrUtils.formateDayMonth(0));
}
@Test
public void testLong2DateString2() {
assertNotNull(StrUtils.long2DateString2(14526545654555l));
assertEquals(null,StrUtils.long2DateString2(0));
}
}