aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/validator/RegexpValidatorTest.java
blob: 077d8f3a6aed883691fac363ae264efc25fb1fc8 (plain)
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
package com.vaadin.data.validator;

import org.junit.Test;

public class RegexpValidatorTest extends ValidatorTestBase {

    @Test
    public void testNullStringFails() {
        assertPasses(null, new RegexpValidator("Should be 'abc'", "abc"));
    }

    @Test
    public void testEmptyPatternMatchesEmptyString() {
        assertPasses("", new RegexpValidator("Should be empty", "", true));
    }

    @Test
    public void testEmptyPatternDoesNotMatchNonEmptyString() {
        assertFails("x", new RegexpValidator("Should be empty", "", true));
    }

    @Test
    public void testPatternMatchesString() {
        RegexpValidator v = new RegexpValidator(
                "Should be foo and bar repeating", "(foo|bar)+", true);

        assertPasses("foo", v);
        assertPasses("barfoo", v);
        assertPasses("foobarbarbarfoobarfoofoobarbarfoofoofoobar", v);
    }

    @Test
    public void testPatternDoesNotMatchString() {
        RegexpValidator v = new RegexpValidator(
                "Should be foo and bar repeating", "(foo|bar)+", true);

        assertFails("", v);
        assertFails("barf", v);
        assertFails(" bar", v);
        assertFails("foobarbarbarfoobar.foofoobarbarfoofoofoobar", v);
    }

    @Test
    public void testEmptyPatternFoundInAnyString() {
        RegexpValidator v = new RegexpValidator("Should always pass", "",
                false);

        assertPasses("", v);
        assertPasses("      ", v);
        assertPasses("qwertyuiopasdfghjklzxcvbnm", v);
    }

    @Test
    public void testPatternFoundInString() {
        RegexpValidator v = new RegexpValidator("Should contain a number",
                "\\d+", false);

        assertPasses("0", v);
        assertPasses("     123     ", v);
        assertPasses("qwerty9iop", v);
    }

    @Test
    public void testPatternNotFoundInString() {
        RegexpValidator v = new RegexpValidator("Should contain a number",
                "\\d+", false);

        assertFails("", v);
        assertFails("qwertyiop", v);
    }
}