]> source.dussan.org Git - vaadin-framework.git/blob
feaa1371ecb5ddffcfcd70873159cd5abf608b07
[vaadin-framework.git] /
1 package com.vaadin.tests.components.abstractfield;
2
3 import com.vaadin.tests.components.TestBase;
4 import com.vaadin.ui.Button;
5 import com.vaadin.ui.Button.ClickEvent;
6 import com.vaadin.ui.Button.ClickListener;
7 import com.vaadin.ui.Notification;
8 import com.vaadin.v7.data.util.ObjectProperty;
9 import com.vaadin.v7.data.validator.StringLengthValidator;
10 import com.vaadin.v7.ui.TextField;
11
12 public class AbstractFieldCommitWithInvalidValues extends TestBase {
13
14     private TextField tf;
15
16     @Override
17     protected String getDescription() {
18         return "Commiting a field with invalid values should throw an exception";
19     }
20
21     @Override
22     protected Integer getTicketNumber() {
23         return 2532;
24     }
25
26     @Override
27     protected void setup() {
28         tf = new TextField("A field, must contain 1-2 chars",
29                 new ObjectProperty<String>("a"));
30         tf.addValidator(
31                 new StringLengthValidator("Invalid length", 1, 2, false));
32         tf.setBuffered(true);
33         tf.setRequired(true);
34
35         Button b = new Button("Commit", new ClickListener() {
36
37             @Override
38             public void buttonClick(ClickEvent event) {
39                 try {
40                     tf.commit();
41                     if (tf.isValid()) {
42                         getMainWindow().showNotification(
43                                 "OK! Form validated and no error was thrown",
44                                 Notification.TYPE_HUMANIZED_MESSAGE);
45                     } else {
46                         getMainWindow().showNotification(
47                                 "Form is invalid but no exception was thrown",
48                                 Notification.TYPE_ERROR_MESSAGE);
49                     }
50                 } catch (Exception e) {
51                     if (tf.isValid()) {
52                         getMainWindow().showNotification(
53                                 "Form is valid but an exception was thrown",
54                                 Notification.TYPE_ERROR_MESSAGE);
55                     } else {
56                         getMainWindow().showNotification(
57                                 "OK! Error was thrown for an invalid input",
58                                 Notification.TYPE_HUMANIZED_MESSAGE);
59
60                     }
61                 }
62             }
63
64         });
65
66         addComponent(tf);
67         addComponent(b);
68     }
69 }