Просмотр исходного кода

Tidy up unit tests

Change-Id: I83b28c60e64fc0f367279d70dff3daf0714e0087
Reviewed-on: http://gerrit.dmdirc.com/2441
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 12 лет назад
Родитель
Сommit
70766a12a7

+ 11
- 22
src/com/dmdirc/util/validators/ServerNameValidator.java Просмотреть файл

@@ -19,37 +19,26 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
-
23 22
 package com.dmdirc.util.validators;
24 23
 
25
-import java.net.URI;
26
-import java.net.URISyntaxException;
27
-
28 24
 /**
29 25
  * Validates a server name, prepending an irc protocol if no protocol is
30 26
  * specified.
31 27
  */
32
-public class ServerNameValidator implements Validator<String> {
28
+public class ServerNameValidator extends URIValidator {
33 29
 
34
-    /** {@inheritDoc} */
30
+    /**
31
+     * {@inheritDoc}
32
+     */
35 33
     @Override
36 34
     public ValidationResponse validate(final String object) {
37
-        try {
38
-            if (object == null || object.isEmpty()) {
39
-                return new ValidationResponse("Server name is required.");
40
-            }
41
-            if (!object.matches(".*://")) {
42
-                new URI("irc://" + object);
43
-            } else {
44
-                new URI(object);
45
-            }
46
-            return new ValidationResponse();
47
-        } catch (URISyntaxException ex) {
48
-            if ("Expected authority".equals(ex.getReason())) {
49
-                return new ValidationResponse("Address requires a hostname.");
50
-            } else {
51
-                return new ValidationResponse(ex.getReason());
52
-            }
35
+        if (object == null || object.isEmpty()) {
36
+            return new ValidationResponse("Server name is required.");
37
+        }
38
+        if (!object.matches(".*://")) {
39
+            return super.validate("irc://" + object);
40
+        } else {
41
+            return super.validate(object);
53 42
         }
54 43
     }
55 44
 }

+ 6
- 8
src/com/dmdirc/util/validators/URIValidator.java Просмотреть файл

@@ -33,17 +33,15 @@ public class URIValidator implements Validator<String> {
33 33
     /** {@inheritDoc} */
34 34
     @Override
35 35
     public ValidationResponse validate(final String object) {
36
-        ValidationResponse result;
37 36
         try {
38
-            final URI uri = new URI(object);
39
-            if (uri.getHost() == null || uri.getHost().isEmpty()) {
40
-                result = new ValidationResponse("Address requires a hostname.");
37
+            new URI(object);
38
+        } catch (URISyntaxException ex) {
39
+            if ("Expected authority".equals(ex.getReason())) {
40
+                return new ValidationResponse("Address requires a hostname.");
41 41
             } else {
42
-                result = new ValidationResponse();
42
+                return new ValidationResponse(ex.getReason());
43 43
             }
44
-        } catch (URISyntaxException ex) {
45
-            result = new ValidationResponse(ex.getReason());
46 44
         }
47
-        return result;
45
+        return new ValidationResponse();
48 46
     }
49 47
 }

+ 3
- 2
test/com/dmdirc/util/validators/URIValidatorTest.java Просмотреть файл

@@ -30,8 +30,9 @@ public class URIValidatorTest {
30 30
 
31 31
     @Test
32 32
     public void testValidateNoHostname() {
33
-        URIValidator instance = new URIValidator();
34
-        assertTrue(instance.validate("http://").isFailure());
33
+        ServerNameValidator instance = new ServerNameValidator();
34
+        assertEquals("Address requires a hostname.",
35
+                instance.validate("irc://").getFailureReason());
35 36
     }
36 37
 
37 38
     @Test

Загрузка…
Отмена
Сохранить