X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneParserTest.java;h=ee6c82fbe7cccf2771a95bc2d33be31fab770df0;hb=a4d663646b4510117b35529399cb380a50413b27;hp=06637c334c547ea82f1c72509146b238721cf55f;hpb=21cc44abe8ffbd1e6d129c2b6141085f0694c5eb;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java index 06637c3..ee6c82f 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -4,9 +4,9 @@ import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Optional.of; 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.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; @@ -15,6 +15,7 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.util.logging.Logger; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; @@ -34,6 +35,10 @@ import org.junit.Test; */ public class SoneParserTest { + static { + Logger.getLogger("").setLevel(OFF); + } + private final Core core = mock(Core.class); private final Database database = new MemoryDatabase(null); private final Sone originalSone = database.newSoneBuilder().by("test").using(new Client("TestClient", "1.0")).build(Optional.absent()); @@ -46,50 +51,69 @@ public class SoneParserTest { } @Test - public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException, SoneException { - assertThat(soneParser.parseSone(database, originalSone, getInputStream("This is not valid XML.")), nullValue()); + public void verifyThatAnInvalidXmlDocumentIsNotParsed() throws UnsupportedEncodingException { + Optional sone = soneParser.parseSone(database, originalSone, getXml("invalid-xml")); + assertThat(sone, notNullValue()); + assertThat(sone.isPresent(), is(false)); } @Test - public void verifyThatANegativeProtocolVersionCausesAnError() throws SoneException { - assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()), nullValue()); + public void verifyThatANegativeProtocolVersionCausesAnError() { + Optional sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("-1").get()); + assertThat(sone, notNullValue()); + assertThat(sone.isPresent(), is(false)); } @Test - public void verifyThatATooLargeProtocolVersionCausesAnError() throws SoneException { - assertThat(soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get()), nullValue()); + public void verifyThatATooLargeProtocolVersionCausesAnError() { + Optional sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.setProtocolVersion("1").get()); + assertThat(sone, notNullValue()); + assertThat(sone.isPresent(), is(false)); } @Test - public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() throws SoneException { - Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get()); + public void verifyThatAMissingTimeCausesAnError() { + Optional sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeTime().get()); assertThat(sone, notNullValue()); - assertThat(sone.getClient(), notNullValue()); - assertThat(sone.getClient(), is(originalSone.getClient())); + assertThat(sone.isPresent(), is(false)); } @Test - public void verifyThatTheCreatedSoneMeetsAllExpectations() throws SoneException { - Sone sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.get()); + public void verifyThatAMissingClientCausesTheOriginalClientToBeUsed() { + Optional sone = soneParser.parseSone(database, originalSone, soneXmlBuilder.removeClientInformation().get()); assertThat(sone, notNullValue()); - assertThat(sone.getTime(), is(1000L)); - assertThat(sone.getClient(), notNullValue()); - assertThat(sone.getClient().getName(), is("Test-Client")); - assertThat(sone.getClient().getVersion(), is("1.0")); - assertThat(sone.getProfile(), notNullValue()); - assertThat(sone.getProfile().getFirstName(), is("First")); - assertThat(sone.getProfile().getMiddleName(), is("M.")); - assertThat(sone.getProfile().getLastName(), is("Last")); - assertThat(sone.getProfile().getBirthYear(), is(2000)); - assertThat(sone.getProfile().getBirthMonth(), is(9)); - assertThat(sone.getProfile().getBirthDay(), is(13)); - assertThat(sone.getProfile().getAvatar(), is("avatar-id")); + assertThat(sone.isPresent(), is(true)); + assertThat(sone.get().getClient(), notNullValue()); + assertThat(sone.get().getClient(), is(originalSone.getClient())); + } + + @Test + public void verifyThatTheCreatedSoneMeetsAllExpectations() { + Optional sone = soneParser.parseSone(database, originalSone, getXml("complete")); + assertThat(sone, notNullValue()); + assertThat(sone.isPresent(), is(true)); + assertThat(sone.get().getTime(), is(1382419919000L)); + assertThat(sone.get().getClient(), notNullValue()); + assertThat(sone.get().getClient().getName(), is("Sone")); + assertThat(sone.get().getClient().getVersion(), is("0.8.7")); + assertThat(sone.get().getProfile(), notNullValue()); + assertThat(sone.get().getProfile().getFirstName(), is("First")); + assertThat(sone.get().getProfile().getMiddleName(), is("M.")); + assertThat(sone.get().getProfile().getLastName(), is("Last")); + assertThat(sone.get().getProfile().getBirthYear(), is(2013)); + assertThat(sone.get().getProfile().getBirthMonth(), is(10)); + assertThat(sone.get().getProfile().getBirthDay(), is(22)); + assertThat(sone.get().getProfile().getAvatar(), is("96431abe-3add-11e3-8a46-67047503bf6d")); } public InputStream getInputStream(String content) throws UnsupportedEncodingException { return new ByteArrayInputStream(content.getBytes("UTF-8")); } + private InputStream getXml(String name) { + return getClass().getResourceAsStream(format("/sone-parser/%s.xml", name)); + } + private static class SoneXmlBuilder { private Optional time = of(1000L); @@ -171,7 +195,6 @@ public class SoneParserTest { content.append(""); try { String xmlString = content.toString(); - System.out.println(xmlString); return new ByteArrayInputStream(xmlString.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { /* ignore. */