Browse Source

Add some validator unit tests.

pull/47/head
Greg Holmes 9 years ago
parent
commit
09c1b0a700

+ 0
- 5
src/com/dmdirc/util/validators/ListNotEmptyValidator.java View File

@@ -31,11 +31,6 @@ import java.util.List;
31 31
  */
32 32
 public class ListNotEmptyValidator<T> implements Validator<List<T>> {
33 33
 
34
-    /**
35
-     * Serialisation version.
36
-     */
37
-    private static final long serialVersionUID = 1;
38
-
39 34
     @Override
40 35
     public ValidationResponse validate(final List<T> object) {
41 36
         if (object == null || object.isEmpty()) {

+ 81
- 0
test/com/dmdirc/util/validators/ColourValidatorTest.java View File

@@ -0,0 +1,81 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import java.util.Arrays;
26
+import java.util.Collection;
27
+
28
+import org.junit.Test;
29
+import org.junit.runner.RunWith;
30
+import org.junit.runners.Parameterized;
31
+
32
+import static org.junit.Assert.assertEquals;
33
+
34
+@RunWith(Parameterized.class)
35
+public class ColourValidatorTest {
36
+
37
+    @Parameterized.Parameters
38
+    public static Collection<Object[]> data() {
39
+        return Arrays.asList(new Object[][]{
40
+                        {"-1", false},
41
+                        {"0", true},
42
+                        {"1", true},
43
+                        {"2", true},
44
+                        {"2", true},
45
+                        {"3", true},
46
+                        {"4", true},
47
+                        {"5", true},
48
+                        {"6", true},
49
+                        {"7", true},
50
+                        {"8", true},
51
+                        {"9", true},
52
+                        {"10", true},
53
+                        {"11", true},
54
+                        {"12", true},
55
+                        {"13", true},
56
+                        {"14", true},
57
+                        {"15", true},
58
+                        {"16", false},
59
+                        {"000000", true},
60
+                        {"ffffff", true},
61
+                        {"0000000", false},
62
+                        {"gggggg", false},
63
+                }
64
+        );
65
+    }
66
+
67
+    private final String input;
68
+
69
+    private final boolean expected;
70
+
71
+    public ColourValidatorTest(final String input, final boolean expected) {
72
+        this.input = input;
73
+        this.expected = expected;
74
+    }
75
+
76
+    @Test
77
+    public void test() {
78
+        final Validator<String> instance = new ColourValidator();
79
+        assertEquals(expected, !instance.validate(input).isFailure());
80
+    }
81
+}

+ 60
- 0
test/com/dmdirc/util/validators/IntegerPortValidatorTest.java View File

@@ -0,0 +1,60 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+import org.junit.runner.RunWith;
27
+import org.mockito.runners.MockitoJUnitRunner;
28
+
29
+import static org.junit.Assert.assertFalse;
30
+import static org.junit.Assert.assertTrue;
31
+
32
+@RunWith(MockitoJUnitRunner.class)
33
+public class IntegerPortValidatorTest {
34
+
35
+    @Test
36
+    public void testValidate_TooLow() throws Exception {
37
+        assertTrue(new IntegerPortValidator().validate(0).isFailure());
38
+    }
39
+
40
+    @Test
41
+    public void testValidate_Min() throws Exception {
42
+        assertFalse(new IntegerPortValidator().validate(1).isFailure());
43
+    }
44
+
45
+    @Test
46
+    public void testValidate_Normal() throws Exception {
47
+        assertFalse(new IntegerPortValidator().validate(5).isFailure());
48
+    }
49
+
50
+    @Test
51
+    public void testValidate_Max() throws Exception {
52
+        assertFalse(new IntegerPortValidator().validate(65535).isFailure());
53
+    }
54
+
55
+    @Test
56
+    public void testValidate_TooHigh() throws Exception {
57
+        assertTrue(new IntegerPortValidator().validate(65536).isFailure());
58
+    }
59
+
60
+}

+ 106
- 0
test/com/dmdirc/util/validators/IntegerValidatorTest.java View File

@@ -0,0 +1,106 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Before;
26
+import org.junit.Test;
27
+import org.junit.runner.RunWith;
28
+import org.mockito.runners.MockitoJUnitRunner;
29
+
30
+import static org.junit.Assert.assertEquals;
31
+import static org.junit.Assert.assertFalse;
32
+import static org.junit.Assert.assertTrue;
33
+
34
+@RunWith(MockitoJUnitRunner.class)
35
+public class IntegerValidatorTest {
36
+
37
+    private IntegerValidator instance;
38
+
39
+    @Before
40
+    public void setUp() throws Exception {
41
+        instance = new IntegerValidator(5, 10);
42
+    }
43
+
44
+    @Test(expected = IllegalArgumentException.class)
45
+    public void testInvalidConstruction() throws Exception {
46
+        instance = new IntegerValidator(10, 5);
47
+    }
48
+
49
+    @Test
50
+    public void testPlaceholderMinimum() throws Exception {
51
+        instance = new IntegerValidator(-1, 10);
52
+        assertEquals(Integer.MIN_VALUE, instance.getMin());
53
+    }
54
+
55
+    @Test
56
+    public void testPlaceholderMaximum() throws Exception {
57
+        instance = new IntegerValidator(5, -1);
58
+        assertEquals(Integer.MAX_VALUE, instance.getMax());
59
+    }
60
+
61
+    @Test
62
+    public void testGetMax() throws Exception {
63
+        assertEquals(5, instance.getMin());
64
+    }
65
+
66
+    @Test
67
+    public void testGetMin() throws Exception {
68
+        assertEquals(10, instance.getMax());
69
+
70
+    }
71
+
72
+    @Test
73
+    public void testValidate_Less_Reason() throws Exception {
74
+        assertEquals("Must be at least 5", instance.validate(4).getFailureReason());
75
+    }
76
+
77
+    @Test
78
+    public void testValidate_LessThanMin() throws Exception {
79
+        assertTrue(instance.validate(4).isFailure());
80
+    }
81
+
82
+    @Test
83
+    public void testValidate_Min() throws Exception {
84
+        assertFalse(instance.validate(5).isFailure());
85
+    }
86
+
87
+    @Test
88
+    public void testValidate_Max() throws Exception {
89
+        assertFalse(instance.validate(10).isFailure());
90
+    }
91
+
92
+    @Test
93
+    public void testValidate_MoreThanMax() throws Exception {
94
+        assertTrue(instance.validate(11).isFailure());
95
+    }
96
+
97
+    @Test
98
+    public void testValidate_Max_Reason() throws Exception {
99
+        assertEquals("Must be at most 10", instance.validate(11).getFailureReason());
100
+    }
101
+
102
+    @Test
103
+    public void testValidate_Normal() throws Exception {
104
+        assertFalse(instance.validate(6).isFailure());
105
+    }
106
+}

+ 54
- 0
test/com/dmdirc/util/validators/ListNotEmptyValidatorTest.java View File

@@ -0,0 +1,54 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import java.util.ArrayList;
26
+import java.util.List;
27
+
28
+import org.junit.Test;
29
+import org.junit.runner.RunWith;
30
+import org.mockito.runners.MockitoJUnitRunner;
31
+
32
+import static org.junit.Assert.assertFalse;
33
+import static org.junit.Assert.assertTrue;
34
+
35
+@RunWith(MockitoJUnitRunner.class)
36
+public class ListNotEmptyValidatorTest {
37
+
38
+    @Test
39
+    public void testValidate_Null() throws Exception {
40
+        assertTrue(new ListNotEmptyValidator<String>().validate(null).isFailure());
41
+    }
42
+
43
+    @Test
44
+    public void testValidate_Empty() throws Exception {
45
+        assertTrue(new ListNotEmptyValidator<String>().validate(new ArrayList<>()).isFailure());
46
+    }
47
+
48
+    @Test
49
+     public void testValidate_NotEmpty() throws Exception {
50
+        final List<String> list = new ArrayList<>();
51
+        list.add("RAR");
52
+        assertFalse(new ListNotEmptyValidator<String>().validate(list).isFailure());
53
+    }
54
+}

+ 55
- 0
test/com/dmdirc/util/validators/OptionalEmailAddressValidatorTest.java View File

@@ -0,0 +1,55 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+import org.junit.runner.RunWith;
27
+import org.mockito.runners.MockitoJUnitRunner;
28
+
29
+import static org.junit.Assert.assertFalse;
30
+import static org.junit.Assert.assertTrue;
31
+
32
+@RunWith(MockitoJUnitRunner.class)
33
+public class OptionalEmailAddressValidatorTest {
34
+
35
+    @Test
36
+    public void testValidate_Null() throws Exception {
37
+        assertFalse(new OptionalEmailAddressValidator().validate(null).isFailure());
38
+    }
39
+
40
+    @Test
41
+    public void testValidate_Empty() throws Exception {
42
+        assertFalse(new OptionalEmailAddressValidator().validate("").isFailure());
43
+    }
44
+
45
+    @Test
46
+    public void testValidate_Valid() throws Exception {
47
+        assertFalse(new OptionalEmailAddressValidator().validate("test@test.com").isFailure());
48
+    }
49
+
50
+    @Test
51
+    public void testValidate_Invalid() throws Exception {
52
+        assertTrue(new OptionalEmailAddressValidator().validate("email@example@example.com")
53
+                .isFailure());
54
+    }
55
+}

+ 70
- 0
test/com/dmdirc/util/validators/ValidationResponseTest.java View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+import org.junit.runner.RunWith;
27
+import org.mockito.runners.MockitoJUnitRunner;
28
+
29
+import static org.junit.Assert.assertEquals;
30
+import static org.junit.Assert.assertFalse;
31
+import static org.junit.Assert.assertTrue;
32
+
33
+@RunWith(MockitoJUnitRunner.class)
34
+public class ValidationResponseTest {
35
+
36
+    @Test
37
+    public void testIsFailure_Reason() throws Exception {
38
+        assertTrue(new ValidationResponse("Reason").isFailure());
39
+    }
40
+
41
+    @Test
42
+    public void testIsFailure_EmptyReason() throws Exception {
43
+        assertTrue(new ValidationResponse("").isFailure());
44
+    }
45
+
46
+    @Test
47
+    public void testIsFailure_NoArgs() throws Exception {
48
+        assertFalse(new ValidationResponse().isFailure());
49
+    }
50
+
51
+    @Test
52
+    public void testIsFailure_Null() throws Exception {
53
+        assertFalse(new ValidationResponse(null).isFailure());
54
+    }
55
+
56
+    @Test
57
+    public void testGetFailureReason_Null() throws Exception {
58
+        assertEquals(null, new ValidationResponse().getFailureReason());
59
+    }
60
+
61
+    @Test
62
+    public void testGetFailureReason_Empty() throws Exception {
63
+        assertEquals("", new ValidationResponse("").getFailureReason());
64
+    }
65
+
66
+    @Test
67
+    public void testGetFailureReason_Args() throws Exception {
68
+        assertEquals("RAR", new ValidationResponse("RAR").getFailureReason());
69
+    }
70
+}

Loading…
Cancel
Save