Use exceptions to signal an error when parsing a Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Oct 2013 20:14:18 +0000 (22:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:35 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/test/java/net/pterodactylus/sone/core/SoneParserTest.java

index 7d9053d..ab05b97 100644 (file)
@@ -82,13 +82,13 @@ public class SoneParser {
                if (document == null) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone.getId()));
-                       return null;
+                       throw new InvalidXml();
                }
 
                Optional<SimpleXML> soneXml = parseXml(originalSone, document);
                if (!soneXml.isPresent()) {
                        logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone.getId()));
-                       return null;
+                       throw new InvalidXml();
                }
 
                Optional<Client> parsedClient = parseClient(originalSone, soneXml.get());
@@ -98,11 +98,11 @@ public class SoneParser {
                if (protocolVersion.isPresent()) {
                        if (protocolVersion.get() < 0) {
                                logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
-                               return null;
+                               throw new InvalidProtocolVersion();
                        }
                        if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
                                logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
-                               return null;
+                               throw new InvalidProtocolVersion();
                        }
                }
 
@@ -110,21 +110,21 @@ public class SoneParser {
                if (soneTime == null) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
-                       return null;
+                       throw new MalformedXml();
                }
                try {
                        sone.setTime(Long.parseLong(soneTime));
                } catch (NumberFormatException nfe1) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
-                       return null;
+                       throw new MalformedXml();
                }
 
                SimpleXML profileXml = soneXml.get().getNode("profile");
                if (profileXml == null) {
                        /* TODO - mark Sone as bad. */
                        logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
-                       return null;
+                       throw new MalformedXml();
                }
 
                /* parse profile. */
@@ -344,4 +344,15 @@ public class SoneParser {
                return of(new Client(clientName, clientVersion));
        }
 
+       public static class InvalidXml extends RuntimeException {
+
+       }
+
+       public static class InvalidProtocolVersion extends RuntimeException {
+
+       }
+
+       public static class MalformedXml extends RuntimeException {
+
+       }
 }
index 726354b..b0d553d 100644 (file)
@@ -5,7 +5,6 @@ import static java.lang.String.format;
 import static java.util.logging.Level.OFF;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
@@ -14,9 +13,11 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.SoneParser.InvalidProtocolVersion;
+import net.pterodactylus.sone.core.SoneParser.InvalidXml;
+import net.pterodactylus.sone.core.SoneParser.MalformedXml;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
@@ -54,22 +55,22 @@ public class SoneParserTest {
                when(core.getImage(anyString())).thenReturn(image);
        }
 
-       @Test
+       @Test(expected = InvalidXml.class)
        public void verifyThatAnInvalidXmlDocumentIsNotParsed() {
                soneParser.parseSone(database, originalSone, getXml("invalid-xml"));
        }
 
-       @Test
+       @Test(expected = InvalidProtocolVersion.class)
        public void verifyThatANegativeProtocolVersionCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("negative-protocol-version"));
        }
 
-       @Test
+       @Test(expected = InvalidProtocolVersion.class)
        public void verifyThatATooLargeProtocolVersionCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("too-large-protocol-version"));
        }
 
-       @Test
+       @Test(expected = MalformedXml.class)
        public void verifyThatAMissingTimeCausesAnError() {
                soneParser.parseSone(database, originalSone, getXml("missing-time"));
        }