2009年11月24日星期二

旅途中仓促引入 的bug

需求:当 endTime 比当前时间超过 20 年时,将 expiration date 置为空。

错误地将 date 要么减 20 年,要么为null:

Calendar endTime = response.getConsumerService().getEndTime();
if (endTime != null) {
// roll endTime back 20 years
CalendarUtil.truncateDay(endTime).roll(Calendar.YEAR, -20);

if (endTime.after(CalendarUtil.truncateDay(Calendar.getInstance()))) {
// If the expiration date of the current subscription received from ECWSis greater than 20 years, make this null
endTime = null;
}
}
currentSubscriptionDto.setExpirationDate(endTime);

正确的逻辑应该为:
Calendar endTime = response.getConsumerService().getEndTime();
if (endTime != null) {
Calendar clonedEndTime = (Calendar)endTime.clone();

// roll clonedEndTime back 20 years
CalendarUtil.truncateDay(clonedEndTime ).roll(Calendar.YEAR, -20);

if (clonedEndTime .after(CalendarUtil.truncateDay(Calendar.getInstance()))) {
// If the expiration date of the current subscription received from ECWSis greater than 20 years, make this null
endTime = null;
}
}
currentSubscriptionDto.setExpirationDate(endTime);


原本高兴地在机场第一次 bug fixing, 可是时间仓促,也是因为 Date 和 BigInteger, BigDecimal 一样,思考起来一点也不直观。

慎加注意!

没有评论: