1.实验要求
日期问题
用决策表测试法测试以下程序:该程序有三个输入变量month、day、year(month 、 day 和year均为整数值,并且满足:1≤month≤12 和1≤day≤31),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如,输入为 2004 年11 月29 日,则该程序的输出为2004 年12 月1 日。
假如输入日期符合规定,则如上图所示直接显示正确的结果,否则系统自动弹出提示,如下图所示。
根据该程序的规格说明,利用边界值法设计测试方案,并完成其实验报告。
(1)编写如图所示小程序
(2)设计对应的测试方案
2.编写代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DateValidator extends JFrame {
private JTextField yearField, monthField, dayField;
private JTextArea resultArea;
public DateValidator() {
setTitle("黑盒测试_因果图(到期日期)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
inputPanel.add(new JLabel("年:"));
yearField = new JTextField(5);
inputPanel.add(yearField);
inputPanel.add(new JLabel("月:"));
monthField = new JTextField(5);
inputPanel.add(monthField);
inputPanel.add(new JLabel("日:"));
dayField = new JTextField(5);
inputPanel.add(dayField);
JButton validateButton = new JButton("提交");
validateButton.addActionListener(new ValidateButtonListener());
inputPanel.add(validateButton);
add(inputPanel, BorderLayout.NORTH);
resultArea = new JTextArea(5, 40);
resultArea.setEditable(false);
add(new JScrollPane(resultArea), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
private class ValidateButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String yearText = yearField.getText();
String monthText = monthField.getText();
String dayText = dayField.getText();
try {
int year = Integer.parseInt(yearText);
int month = Integer.parseInt(monthText);
int day = Integer.parseInt(dayText);
if (isValidDate(year, month, day)) {
// 计算下一天的日期
int nextMonth, nextDay, nextYear;
// 判断是否为闰年
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
// 根据月份确定每个月的天数
int[] daysInMonth = {31, isLeapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (day == daysInMonth[month - 1]) {
// 如果当前日期是该月的最后一天,则下一天的日期为下一个月的第一天
nextMonth = month + 1;
nextDay = 2;
nextYear = year;
if (nextMonth > 12) {
// 如果下一个月超过12月,则下一年为当前年份加1,下一个月为1月
nextMonth = 1;
nextYear++;
}
}
else if (day == daysInMonth[month - 1]-1) {
// 如果当前日期是该月的最后一天,则下一天的日期为下一个月的第一天
nextMonth = month + 1;
nextDay = 1;
nextYear = year;
if (nextMonth > 12) {
// 如果下一个月超过12月,则下一年为当前年份加1,下一个月为1月
nextMonth = 1;
nextYear++;
}
}else {
// 如果当前日期不是该月的最后一天,则下一天的日期为当前日期加1
nextMonth = month;
nextDay = day + 2;
nextYear = year;
}
//resultArea.setLineWrap(true);//启用文本域换行
resultArea.setText("尊敬的顾客,本店承诺售出商品两天内无理由退换,请保存好包装和发票。\n"+
"如确需退换,请于" + nextYear + "-" + nextMonth + "-" + nextDay + "日前进行");
} else {
resultArea.setText("顾客到商场购物,商场承法售出商品两天内可以无理由退换。现某一\n" +
"顾客于YYYY年MM月DD日购物,请确定无理由退换截止日期。\n"+
"1.输入日期必须符合规则\n" +
"2.输出日期为输入日期天数加2,必须转为正常的日期规则");
}
} catch (NumberFormatException ex) {
resultArea.setText("顾客到商场购物,商场承法售出商品两天内可以无理由退换。现某一\n" +
"顾客于YYYY年MM月DD日购物,请确定无理由退换截止日期。\n"+
"1.输入日期必须符合规则\n" +
"2.输出日期为输入日期天数加2,必须转为正常的日期规则");
}
}
private boolean isValidDate(int year, int month, int day) {
if (year < 1 || month < 1 || month > 12 || day < 1)
return false;
int maxDay = 31; // Default to maximum days in a month
switch (month) {
case 4:
case 6:
case 9:
case 11:
maxDay = 30;
break;
case 2:
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
maxDay = 29; // Leap year
else
maxDay = 28;
break;
}
return day <= maxDay;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new DateValidator().setVisible(true);
});
}
}
3.实验过程
编写测试方案如下:
| 输入数据(年,月,日) | 预期结果 | 测试结果 |
case1 | 1912,6,15 | 测试通过 | |
case2 | 1913,6,15 | 测试通过 | |
Case3 | 2000,6,15 | 测试通过 | |
Case4 | 2049,6,15 | 测试通过 | |
Case5 | 2050,6,15 | 测试通过 | |
Case6 | 2000,1,15 | 测试通过 | |
Case7 | 2000,2,15 | 测试通过 | |
Case8 | 2000,11,15 | 测试通过 | |
Case9 | 2000,12,15 | 测试通过 | |
Case10 | 2000,6,1 | 测试通过 | |
Case11 | 2000,6,2 | 测试通过 | |
Case12 | 2000,6,30 | 测试通过 | |
Case13 | 2000,6,31 | 测试不通过 | |
Case14 | 2000,7,31 | 测试通过 |