????B??Junit??÷??????2
????1??????????????
??????WordDealUtil?е????wordFormat4DB( )???????????????
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordDealUtil {
/**
* ??Java???????????????????????д??????
* ????????????????и????
* ?????????????Сд???????????????????????????
* ???磺employeeInfo ?????????????? employee_info
* @param name Java????????
*/
public static String wordFormat4DB(String name){
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(name);
StringBuffer strBuffer = new StringBuffer();
while(m.find()){
//????????????滻???????????
//??????滻??????????????????????????????????????????StringBuffer??????
m.appendReplacement(strBuffer?? "_"+m.group());
}
//?????????乤????????????????????StringBuffer??????
return m.appendTail(strBuffer).toString().toLowerCase();
}
}
????2??д??????????
import static org.junit.Assert.*;
import org.junit.Test;
public class WordDealUtilTest {
@Test
public void testWordFormat4DB() {
String target = "employeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info"?? result);
}
}
????3??????????????????
?????????????Χ???棬?????????????????????????????????????????????????????磺??????????????????????????????????н??????????????????Щ???????????????
//???? null ?????????
@Test public void wordFormat4DBNull(){
String target = null;
String result = WordDealUtil.wordFormat4DB(target);
assertNull(result);
}
//??????????????????
@Test public void wordFormat4DBEmpty(){
String target = "";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals(""?? result);
}
//????????????д??????
@Test public void wordFormat4DBegin(){
String target = "EmployeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info"?? result);
}
//?????β??????д??????
@Test public void wordFormat4DBEnd(){
String target = "employeeInfoA";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info_a"?? result);
}
//???????????????д??????
@Test public void wordFormat4DBTogether(){
String target = "employeeAInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_a_info"?? result);
}
????4???????????н?????????????
??????????в????JUnit ???н???????????????????????δ???????????6?????????????д????????????????????????????????failure????????????? null ??????????????????????——???????error???????????????????в???ж????????д?? null ????????????????д??????????£?
//????????wordFormat4DB
public static String wordFormat4DB(String name){
if(name == null){
return null;
}
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(name);
StringBuffer sb = new StringBuffer();
while(m.find()){
if(m.start() != 0)
m.appendReplacement(sb?? ("_"+m.group()).toLowerCase());
}
return m.appendTail(sb).toString().toLowerCase();
}
????2?????Junit??????Date????DateUtil???μ?????????е???????
????????????????????????в??????????
??????Date?е?
????isDayValid(int year?? int month?? int day)
????isMonthValid(int month)
????isYearValid(int year)
??????DateUtil?е?
????isLeapYear(int year)
????getDayofYear(Date date)