123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package com.vaadin.tests.validation;
-
- import com.vaadin.legacy.data.Validator;
- import com.vaadin.legacy.data.validator.LegacyAbstractStringValidator;
- import com.vaadin.legacy.data.validator.LegacyCompositeValidator;
- import com.vaadin.legacy.data.validator.LegacyCompositeValidator.CombinationMode;
- import com.vaadin.legacy.data.validator.LegacyDoubleValidator;
- import com.vaadin.legacy.data.validator.LegacyEmailValidator;
- import com.vaadin.legacy.data.validator.LegacyIntegerValidator;
- import com.vaadin.legacy.data.validator.LegacyRegexpValidator;
- import com.vaadin.legacy.data.validator.LegacyStringLengthValidator;
- 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 LegacyStringLengthValidator("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 LegacyIntegerValidator("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 LegacyIntegerValidator("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 LegacyDoubleValidator("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 LegacyEmailValidator("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 LegacyRegexpValidator("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 LegacyRegexpValidator("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 LegacyRegexpValidator("^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 LegacyRegexpValidator("^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");
- LegacyCompositeValidator cv = new LegacyCompositeValidator(
- CombinationMode.AND,
- "The field must contain a floating point number with 4-5 characters");
- cv.addValidator(new LegacyStringLengthValidator(
- "String length of '{0}' should be 4-5 characters", 4, 5,
- false));
- cv.addValidator(new LegacyDoubleValidator(
- "{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 LegacyCompositeValidator(CombinationMode.OR,
- "The field must contain a floating point or with 4-5 characters");
- cv.addValidator(new LegacyStringLengthValidator(
- "String length of '{0}' should be 4-5 characters", 4, 5,
- false));
- cv.addValidator(new LegacyDoubleValidator(
- "{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 LegacyAbstractStringValidator(
- "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);
- }
- }
|