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());
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();
}
}
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. */
return of(new Client(clientName, clientVersion));
}
+ public static class InvalidXml extends RuntimeException {
+
+ }
+
+ public static class InvalidProtocolVersion extends RuntimeException {
+
+ }
+
+ public static class MalformedXml extends RuntimeException {
+
+ }
}
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;
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;
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"));
}