三角形黑盒测试-Java Swing
因为《软件测试》课程需要,写一个程序对三角形进行测试,本程序对整型和浮点型数据都进行了覆盖,测试用例都能通过,但是输入其他字符串或者其他的字符并没有进行判断(毕竟三角形边长是数字),所以程序还是有点瑕疵,有兴趣就自行修改完善。
实验内容
三角形问题
根据下面给出的规格说明,利用等价类划分方法,给出足够的测试用例并根据用例执行测试。
输入三个整数a、b、c,分别作为三角形的三条边,通过程序判断这三条边是否能构成三角形?
如果能构成三角形,则判断三角形的类型(等边三角形、等腰三角形、一般三角形)。
要求输入三个整数a、b、c,必须满足以下条件:1≤a≤200;1≤b≤200;1≤c≤200。
请用黑盒测试方法设计测试用例。
主函数:
public class test {public static void main(String[] args) {Triangle win = new Triangle();}
}
功能函数:
//功能函数
//Triangle.javaimport java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class Triangle extends JFrame implements ActionListener {int a, b, c;//三角形的三条边double d, f, g;JTextField text1, text2, text3, text4;JButton button1, button2, button3;JLabel label1, label2, label3, label4;Box basebox, box1, box2, box3;public Triangle() {super("三角形黑盒测试程序");setLayout(new FlowLayout());text1 = new JTextField(22);text2 = new JTextField(22);text3 = new JTextField(22);text4 = new JTextField(22);label1 = new JLabel("请输入第一条边长:");label2 = new JLabel("请输入第二条边长:");label3 = new JLabel("请输入第三条边长:");label4 = new JLabel("判断结果:");button1 = new JButton("确定");button2 = new JButton("清除");button3 = new JButton("结束程序");box1 = Box.createVerticalBox();box1.add(label1);box1.add(Box.createVerticalStrut(10));box1.add(label2);box1.add(Box.createVerticalStrut(10));box1.add(label3);box1.add(Box.createVerticalStrut(20));box1.add(label4);box2 = Box.createVerticalBox();box2.add(text1);box2.add(Box.createVerticalStrut(4));box2.add(text2);box2.add(Box.createVerticalStrut(4));box2.add(text3);box2.add(Box.createVerticalStrut(20));box2.add(text4);box3 = Box.createVerticalBox();box3.add(button1);box3.add(Box.createVerticalStrut(8));box3.add(button2);box3.add(Box.createVerticalStrut(8));box3.add(button3);basebox = Box.createHorizontalBox();basebox.add(box1);basebox.add(Box.createHorizontalStrut(10));basebox.add(box2);basebox.add(Box.createHorizontalStrut(15));basebox.add(box3);add(basebox);button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);setBounds(0, 50, 500, 180);//设置背景图片setSize(500, 180); //设置大小//设置位置setLocation(0, 50);//背景图片的路径。(相对路径或者绝对路径。本例图片放于"java项目名"的文件下)String path = "C://Users//五十少年//Desktop//p8g3le.jpg";// 背景图片ImageIcon background = new ImageIcon(path);// 把背景图片显示在一个标签里面JLabel label = new JLabel(background);// 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, this.getWidth(), this.getHeight());// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明JPanel imagePanel = (JPanel) this.getContentPane();imagePanel.setOpaque(false);// 把背景图片添加到分层窗格的最底层作为背景this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));//点关闭按钮时退出setVisible(true);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@Overridepublic void actionPerformed(ActionEvent e) {if (text1.getText().isEmpty() || text2.getText().isEmpty() || text3.getText().isEmpty()) { //判断输入是否为空text4.setText("请输入有效值");} else if (text1.getText().matches("^[0-9]+([.]{0,1}[0-9]+){0,1}$") || text2.getText().matches("^[0-9]+([.]{0,1}[0-9]+){0,1}$") ||text3.getText().matches("^[0-9]+([.]{0,1}[0-9]+){0,1}$")) {//判断是否是浮点型d = Double.parseDouble(text1.getText());f = Double.parseDouble(text2.getText());g = Double.parseDouble(text3.getText());if (e.getSource() == button1) {if ((d < 1 || d > 200) || (f < 1 || f > 200) || (g < 1 || g > 200)) {text4.setText("取值在不允许的范围内,请重新输入");} else {if ((d + f > g && d + g > f && f + g > d)) { //判断是否能构成三角形if (d == f && d == g && f == g) {text4.setText("该三角形是等边三角形");} else if (d == f || d == g || f == g) {text4.setText("该三角形是等腰三角形");} else {text4.setText("该三角形是一般三角形");}} else {text4.setText("不能构成三角形");}}}} else { //输入为整数的情况a = Integer.parseInt(text1.getText());b = Integer.parseInt(text2.getText());c = Integer.parseInt(text3.getText());if (e.getSource() == button1) {if ((a < 1 || a > 200) || (b < 1 || b > 200) || (c < 1 || c > 200)) {text4.setText("取值在不允许的范围内,请重新输入");} else {if ((a + b > c && a + c > b && b + c > a)) { //判断是否能构成三角形if (a == b && a == c && b == c) {text4.setText("该三角形是等边三角形");} else if (a == b || a == c || b == c) {text4.setText("该三角形是等腰三角形");} else {text4.setText("该三角形是一般三角形");}} else {text4.setText("不能构成三角形");}}}}if (e.getSource() == button2) { //清除数据text1.setText("");text2.setText("");text3.setText("");text4.setText("");}if (e.getSource() == button3) {System.exit(0); //关闭程序}}
}
运行效果: