上一篇介绍了测试用例的概念和一些例子,这次让我们应用一下这些理论吧。
  对于问题:
  允许1到6个英文字符或数字,按OK结束
  有效等价类: 长度:1到6;字符:a-z,A-Z,0-9
  无效等价类:长度:0,7;字符:英文、数字以外字符,控制字符,标点符号等
  要求用三个文本框输入进行测试,代码如下:
1 import java.awt.event.MouseAdapter;
2
3 import javafx.application.Application;
4 import javafx.event.ActionEvent;
5 import javafx.event.EventHandler;
6 import javafx.scene.Scene;
7 import javafx.scene.control.Button;
8 import javafx.scene.control.TextArea;
9 import javafx.scene.input.MouseEvent;
10 import javafx.scene.layout.AnchorPane;
11 import javafx.stage.Stage;
12
13
14 public class testview extends Application{
15     public TextArea text[]=new TextArea[3];
16     /**
17      * @param args
18      */
19     public static void main(String[] args) {
20         // TODO Auto-generated method stub
21         Application.launch(args);
22     }
23     public void start(Stage stage) throws Exception{
24         stage.setTitle("Testing");
25         AnchorPane root=new AnchorPane();
26         Scene scene=new Scene(root,300,300);
27
28         for(int i=0;i<3;i++){
29             text[i]=new TextArea();
30             text[i].setLayoutX(20);
31             text[i].setLayoutY(i*30+100);
32             text[i].setMaxHeight(30);
33             text[i].setPrefColumnCount(10);
34             root.getChildren().add(text[i]);
35         }
36         Button btn=new Button("OK");
37         btn.setLayoutX(100);
38         btn.setLayoutY(200);
39         root.getChildren().add(btn);
40         btn.setOnAction(new EventHandler<ActionEvent>(){
41             public void handle(ActionEvent e){
42                 String str = new String();
43                 boolean result=true;
44                 for(int i=0;i<3;i++){
45                     result=true;
46                     str= text[i].getText();
47                     if(str.length()>0 && str.length()<7){
48                         for(int j=0;j<str.length();j++){
49                             if(!Character.isDigit((str.charAt(j)))&&!Character.isLetter(str.charAt(j))) {
50                                 result=false;
51                                 break;
52                             }
53                         }
54                     }
55                     else result=false;
56                 System.out.println(result);
57                 }
58
59             }
60         });
61         stage.setScene(scene);
62         stage.show();
63     }
64 }