瀏覽代碼

Add basic email address validator.

Change-Id: Ia38d3a8f19f12e21bc2d933c4411cc5e886e74ab
Reviewed-on: http://gerrit.dmdirc.com/3599
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/99/3599/2
Greg Holmes 10 年之前
父節點
當前提交
5afbb601a6

+ 30
- 0
src/com/dmdirc/util/validators/EmailAddressValidator.java 查看文件

@@ -0,0 +1,30 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.util.validators;
23
+
24
+public class EmailAddressValidator extends RegexStringValidator {
25
+
26
+    public EmailAddressValidator() {
27
+        super("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}",
28
+                "Must be a valid email address");
29
+    }
30
+}

+ 76
- 0
test/com/dmdirc/util/validators/EmailAddressValidatorTest.java 查看文件

@@ -0,0 +1,76 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.util.validators;
23
+
24
+import java.util.Arrays;
25
+import java.util.Collection;
26
+
27
+import org.junit.Test;
28
+import org.junit.runner.RunWith;
29
+import org.junit.runners.Parameterized;
30
+import org.junit.runners.Parameterized.Parameters;
31
+
32
+import static org.junit.Assert.assertEquals;
33
+
34
+@RunWith(Parameterized.class)
35
+public class EmailAddressValidatorTest {
36
+
37
+    @Parameters
38
+    public static Collection<Object[]> data() {
39
+        return Arrays.asList(new Object[][]{
40
+            {"email@example.com", true},
41
+            {"firstname.lastname@example.com", true},
42
+            {"email@subdomain.example.com", true},
43
+            {"firstname+lastname@example.com", true},
44
+            {"1234567890@example.com", true},
45
+            {"email@example-one.com", true},
46
+            {"_______@example.com", true},
47
+            {"email@example.name", true},
48
+            {"email@example.museum", true},
49
+            {"email@example.co.jp", true},
50
+            {"firstname-lastname@example.com", true},
51
+            {"plainaddress", false},
52
+            {"#@%^%#$@#$@#.com", false},
53
+            {"@example.com", false},
54
+            {"Foo Bar <email@example.com>", false},
55
+            {"email.example.com", false},
56
+            {"email@example@example.com", false},
57
+            {"?????@example.com", false},
58
+            {"email@example.com (Foo Bar)", false},
59
+            {"email@example", false},});
60
+    }
61
+
62
+    private String input;
63
+
64
+    private boolean expected;
65
+
66
+    public EmailAddressValidatorTest(final String input, boolean expected) {
67
+        this.input = input;
68
+        this.expected = expected;
69
+    }
70
+
71
+    @Test
72
+    public void test() {
73
+        final EmailAddressValidator instance = new EmailAddressValidator();
74
+        assertEquals(expected, !instance.validate(input).isFailure());
75
+    }
76
+}

Loading…
取消
儲存