标签:
在上周的博客中测试了文本框中输入数据的合法性。本次我在上次程序的基础上增加了两个输入框,用于测试多组数据检测。
测试用例如下:
编号 | 输入1 | 输入2 | 输入3 | 预期 | 结果 |
Test1 | 1234567 | 无输入,字符不合法,长度大于6 | 无输入,字符不合法,长度大于6 | ||
Test2 | asdfghj | ASDFGHJ | 1,.?;:‘8 | 长度大于6,长度大于6,长度大于6 | 长度大于6,长度大于6,长度大于6 |
Test3 | 1@#$%^7 | 1(){}[]8 | ,.?;:‘ | 长度大于6,长度大于6,字符不合法 | 长度大于6,长度大于6,字符不合法 |
Test4 | @#$%^ | (){}[] | 程序 | 字符不合法,字符不合法,字符不合法 | 字符不合法,字符不合法,字符不合法 |
Test5 | asd | ASD | 09azAZ | 字符不合法,字符不合法,OK! | 字符不合法,字符不合法,OK! |
Test6 | 09azAZ | 09azAZ | OK!,OK!,无输入 | OK!,OK!,无输入 |
注:Test1的输入1为无输入; 输入2为空格。
Test5的输入1和输入2均为全角字符。
Test6的输入3为空。
测试结果:
通过检测,发现当输入中含有非法字符且长度大于6时,只指出了长度大于6这一个错误。
代码如下:
import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.scene.text.Text; import javafx.stage.Stage; public class SoftwareTesting extends Application{ public static void main(String[ ] args) { SoftwareTesting.launch(args); } int count=0; public void start(Stage primaryStage) { primaryStage.setTitle("SoftwareTesting"); //程序UI设计 AnchorPane root = new AnchorPane(); HBox hbox1 = new HBox(8); HBox hbox2 = new HBox(8); HBox hbox3 = new HBox(8); final TextField textfield1 = new TextField(); final TextField textfield2 = new TextField(); final TextField textfield3 = new TextField(); final Text text1 = new Text(); final Text text2 = new Text(); final Text text3 = new Text(); hbox1.setAlignment(Pos.CENTER_LEFT); hbox1.getChildren().addAll(new Label("Name1"),textfield1,text1); hbox2.setAlignment(Pos.CENTER_LEFT); hbox2.getChildren().addAll(new Label("Name2"),textfield2,text2); hbox3.setAlignment(Pos.CENTER_LEFT); hbox3.getChildren().addAll(new Label("Name3"),textfield3,text3); Button btn1 = new Button(" OK "); AnchorPane.setTopAnchor(hbox1, 40.0); AnchorPane.setLeftAnchor(hbox1, 60.0); AnchorPane.setTopAnchor(hbox2, 80.0); AnchorPane.setLeftAnchor(hbox2, 60.0); AnchorPane.setTopAnchor(hbox3, 120.0); AnchorPane.setLeftAnchor(hbox3, 60.0); AnchorPane.setTopAnchor(btn1, 180.0); AnchorPane.setRightAnchor(btn1, 150.0); root.getChildren().addAll(hbox1,hbox2,hbox3,btn1); //鼠标事件 btn1.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){ public void handle(MouseEvent event){ String textString1 = textfield1.getText().toString(); wordTesting(textString1,text1); String textString2 = textfield2.getText().toString(); wordTesting(textString2,text2); String textString3 = textfield3.getText().toString(); wordTesting(textString3,text3); } }); primaryStage.setScene(new Scene(root, 380, 250)); primaryStage.show(); } private void wordTesting(String textString,Text textTesting) { if(textString==null||textString.length()<=0) textTesting.setText("无输入"); else if(textString.length()>6) textTesting.setText("长度大于6"); else if(textString.matches("[a-zA-Z0-9]{1,6}$")) textTesting.setText("OK!"); else textTesting.setText("字符不合法"); } } // end of program
标签:
原文地址:http://www.cnblogs.com/tjuyyb/p/4376057.html