1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package com.vaadin.tests.validation;
import com.vaadin.data.Validator;
import com.vaadin.data.validator.AbstractStringValidator;
import com.vaadin.data.validator.CompositeValidator;
import com.vaadin.data.validator.CompositeValidator.CombinationMode;
import com.vaadin.data.validator.DoubleValidator;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class TestValidators extends TestBase {
@Override
protected Integer getTicketNumber() {
return 680;
}
@Override
protected String getDescription() {
return "This test verifies that various validators work correctly";
}
@Override
public void setup() {
final Form form = new Form(new VerticalLayout());
// simple validators
TextField tf = new TextField("A field, must contain 1-2 chars");
tf.addValidator(new StringLengthValidator("Invalid length", 1, 2, false));
tf.setRequired(true);
tf.setValue("ab");
form.addField("a", tf);
tf = new TextField("A field, must contain an integer");
tf.addValidator(new IntegerValidator("Invalid integer {0}"));
tf.setRequired(true);
tf.setValue("123");
form.addField("b", tf);
tf = new TextField("A field, must contain an integer or be empty");
tf.addValidator(new IntegerValidator("Invalid integer {0}"));
tf.setValue("-321");
form.addField("c", tf);
tf = new TextField(
"A field, must contain a floating point number or be empty");
tf.addValidator(new DoubleValidator("Invalid double {0}"));
tf.setValue("-123.45e6");
form.addField("d", tf);
tf = new TextField(
"A field, must contain an e-mail address or be empty");
tf.addValidator(new EmailValidator("Invalid e-mail address {0}"));
tf.setValue("a.b@example.com");
form.addField("e", tf);
// regular expressions
tf = new TextField("A field, must match the regular expression a.*b.*c");
tf.addValidator(new RegexpValidator("a.*b.*c",
"{0} does not match the regular expression"));
tf.setValue("aagsabeqgc");
form.addField("f", tf);
tf = new TextField(
"A field, must contain the regular expression a.*b.*c");
tf.addValidator(new RegexpValidator("a.*b.*c", false,
"{0} does not contain the regular expression"));
tf.setValue("aagsabeqgc");
form.addField("g", tf);
tf = new TextField(
"A field, must match the regular expression ^a.*b.*c$");
tf.addValidator(new RegexpValidator("^a.*b.*c$", false,
"{0} does not match the regular expression with ^ and $"));
tf.setValue("aagsabeqgc");
form.addField("h", tf);
tf = new TextField(
"A field, must contain the regular expression ^a.*b.*c$");
tf.addValidator(new RegexpValidator("^a.*b.*c$", false,
"{0} does not contain the regular expression with ^ and $"));
tf.setValue("aagsabeqgc");
form.addField("i", tf);
// TODO CompositeValidator
tf = new TextField(
"A field, must be a floating point number with 4-5 chars");
CompositeValidator cv = new CompositeValidator(CombinationMode.AND,
"The field must contain a floating point number with 4-5 characters");
cv.addValidator(new StringLengthValidator(
"String length of '{0}' should be 4-5 characters", 4, 5, false));
cv.addValidator(new DoubleValidator(
"{0} must be a floating point number"));
tf.addValidator(cv);
tf.setValue("12.34");
form.addField("j", tf);
tf = new TextField(
"A field, must be a floating point number or 4-5 chars");
cv = new CompositeValidator(CombinationMode.OR,
"The field must contain a floating point or with 4-5 characters");
cv.addValidator(new StringLengthValidator(
"String length of '{0}' should be 4-5 characters", 4, 5, false));
cv.addValidator(new DoubleValidator(
"{0} must be a floating point number"));
tf.addValidator(cv);
tf.setValue("12.34g");
form.addField("jb", tf);
// Postal code that must be 5 digits (10000-99999).
tf = new TextField("Postal Code");
tf.setColumns(5);
// Create the validator - this would be even easier with RegexpValidator
Validator postalCodeValidator = new AbstractStringValidator(
"Postal code must be a number 10000-99999.") {
@Override
protected boolean isValidValue(String value) {
return value.matches("[1-9][0-9]{4}");
}
};
tf.addValidator(postalCodeValidator);
tf.setValue("12345");
form.addField("k", tf);
Button b = new Button("Commit", new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
form.commit();
if (form.isValid()) {
getMainWindow().showNotification(
"OK! Form validated and no error was thrown",
Notification.TYPE_HUMANIZED_MESSAGE);
} else {
getMainWindow().showNotification(
"Form is invalid but no exception was thrown",
Notification.TYPE_ERROR_MESSAGE);
}
} catch (Exception e) {
if (form.isValid()) {
getMainWindow().showNotification(
"Form is valid but an exception was thrown",
Notification.TYPE_ERROR_MESSAGE);
} else {
getMainWindow().showNotification(
"OK! Error was thrown for an invalid input",
Notification.TYPE_HUMANIZED_MESSAGE);
}
}
}
});
addComponent(form);
addComponent(b);
}
}
|