X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParserTest.java;h=4edc1b77089b7266db9b2cb7a352f470919b9d98;hp=03b0672fc96cbbb05b8dfb8d857ca3a04add5238;hb=e482c6e00025f94082362b3805ca8d38773f66e4;hpb=f90d92147c4cff4abe4ae904c6e89d7382b5921e diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java index 03b0672..4edc1b7 100644 --- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java @@ -1,13 +1,10 @@ package net.pterodactylus.sone.core; -import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Optional.of; -import static java.lang.System.currentTimeMillis; -import static java.util.UUID.randomUUID; -import static net.pterodactylus.sone.Matchers.isAlbum; -import static net.pterodactylus.sone.Matchers.isImage; -import static net.pterodactylus.sone.Matchers.isPost; -import static net.pterodactylus.sone.Matchers.isPostReply; +import static net.pterodactylus.sone.test.Matchers.isAlbum; +import static net.pterodactylus.sone.test.Matchers.isImage; +import static net.pterodactylus.sone.test.Matchers.isPost; +import static net.pterodactylus.sone.test.Matchers.isPostReply; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -16,19 +13,15 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound; @@ -36,8 +29,6 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFou import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound; import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.Album.Modifier; -import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; @@ -51,9 +42,12 @@ import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilderFactory; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.PostReplyBuilderFactory; +import net.pterodactylus.sone.test.TestAlbumBuilder; +import net.pterodactylus.sone.test.TestImageBuilder; +import net.pterodactylus.sone.test.TestPostBuilder; +import net.pterodactylus.sone.test.TestPostReplyBuilder; +import net.pterodactylus.sone.test.TestValue; import net.pterodactylus.util.config.Configuration; -import net.pterodactylus.util.config.ConfigurationException; -import net.pterodactylus.util.config.Value; import com.google.common.base.Optional; import org.hamcrest.Matchers; @@ -63,8 +57,6 @@ import org.mockito.stubbing.Answer; /** * Unit test for {@link ConfigurationSoneParser}. - * - * @author David ‘Bombe’ Roden */ public class ConfigurationSoneParserTest { @@ -94,9 +86,9 @@ public class ConfigurationSoneParserTest { private void setupEmptyProfile() { when(configuration.getStringValue(anyString())).thenReturn( - new TestValue(null)); + TestValue.from(null)); when(configuration.getIntValue(anyString())).thenReturn( - new TestValue(null)); + TestValue.from(null)); } @Test @@ -134,12 +126,12 @@ public class ConfigurationSoneParserTest { private void setupString(String nodeName, String value) { when(configuration.getStringValue(eq(nodeName))).thenReturn( - new TestValue(value)); + TestValue.from(value)); } private void setupInteger(String nodeName, Integer value) { when(configuration.getIntValue(eq(nodeName))).thenReturn( - new TestValue(value)); + TestValue.from(value)); } @Test @@ -186,7 +178,7 @@ public class ConfigurationSoneParserTest { private void setupLong(String nodeName, Long value) { when(configuration.getLongValue(eq(nodeName))).thenReturn( - new TestValue(value)); + TestValue.from(value)); } @Test(expected = InvalidPostFound.class) @@ -339,15 +331,15 @@ public class ConfigurationSoneParserTest { albumBuilderFactory); assertThat(topLevelAlbums, hasSize(2)); Album firstAlbum = topLevelAlbums.get(0); - assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1", "I1")); + assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1")); assertThat(firstAlbum.getAlbums(), emptyIterable()); assertThat(firstAlbum.getImages(), emptyIterable()); Album secondAlbum = topLevelAlbums.get(1); - assertThat(secondAlbum, isAlbum("A2", null, "T2", "D2", null)); + assertThat(secondAlbum, isAlbum("A2", null, "T2", "D2")); assertThat(secondAlbum.getAlbums(), hasSize(1)); assertThat(secondAlbum.getImages(), emptyIterable()); Album thirdAlbum = secondAlbum.getAlbums().get(0); - assertThat(thirdAlbum, isAlbum("A3", "A2", "T3", "D3", "I3")); + assertThat(thirdAlbum, isAlbum("A3", "A2", "T3", "D3")); assertThat(thirdAlbum.getAlbums(), emptyIterable()); assertThat(thirdAlbum.getImages(), emptyIterable()); } @@ -527,353 +519,4 @@ public class ConfigurationSoneParserTest { configurationSoneParser.parseImages(createImageBuilderFactory()); } - private static class TestValue implements Value { - - private final AtomicReference value = new AtomicReference(); - - public TestValue(T originalValue) { - value.set(originalValue); - } - - @Override - public T getValue() throws ConfigurationException { - return value.get(); - } - - @Override - public T getValue(T defaultValue) { - final T realValue = value.get(); - return (realValue != null) ? realValue : defaultValue; - } - - @Override - public void setValue(T newValue) throws ConfigurationException { - value.set(newValue); - } - - } - - private static class TestPostBuilder implements PostBuilder { - - private final Post post = mock(Post.class); - private String recipientId = null; - - @Override - public PostBuilder copyPost(Post post) throws NullPointerException { - return this; - } - - @Override - public PostBuilder from(String senderId) { - final Sone sone = mock(Sone.class); - when(sone.getId()).thenReturn(senderId); - when(post.getSone()).thenReturn(sone); - return this; - } - - @Override - public PostBuilder randomId() { - when(post.getId()).thenReturn(randomUUID().toString()); - return this; - } - - @Override - public PostBuilder withId(String id) { - when(post.getId()).thenReturn(id); - return this; - } - - @Override - public PostBuilder currentTime() { - when(post.getTime()).thenReturn(currentTimeMillis()); - return this; - } - - @Override - public PostBuilder withTime(long time) { - when(post.getTime()).thenReturn(time); - return this; - } - - @Override - public PostBuilder withText(String text) { - when(post.getText()).thenReturn(text); - return this; - } - - @Override - public PostBuilder to(String recipientId) { - this.recipientId = recipientId; - return this; - } - - @Override - public Post build() throws IllegalStateException { - when(post.getRecipientId()).thenReturn(fromNullable(recipientId)); - return post; - } - - } - - private static class TestPostReplyBuilder implements PostReplyBuilder { - - private final PostReply postReply = mock(PostReply.class); - - @Override - public PostReplyBuilder to(String postId) { - when(postReply.getPostId()).thenReturn(postId); - return this; - } - - @Override - public PostReply build() throws IllegalStateException { - return postReply; - } - - @Override - public PostReplyBuilder randomId() { - when(postReply.getId()).thenReturn(randomUUID().toString()); - return this; - } - - @Override - public PostReplyBuilder withId(String id) { - when(postReply.getId()).thenReturn(id); - return this; - } - - @Override - public PostReplyBuilder from(String senderId) { - Sone sone = mock(Sone.class); - when(sone.getId()).thenReturn(senderId); - when(postReply.getSone()).thenReturn(sone); - return this; - } - - @Override - public PostReplyBuilder currentTime() { - when(postReply.getTime()).thenReturn(currentTimeMillis()); - return this; - } - - @Override - public PostReplyBuilder withTime(long time) { - when(postReply.getTime()).thenReturn(time); - return this; - } - - @Override - public PostReplyBuilder withText(String text) { - when(postReply.getText()).thenReturn(text); - return this; - } - - } - - private static class TestAlbumBuilder implements AlbumBuilder { - - private final Album album = mock(Album.class); - private final List albums = new ArrayList(); - private final List images = new ArrayList(); - private Album parentAlbum; - private String title; - private String description; - private String imageId; - - public TestAlbumBuilder() { - when(album.getTitle()).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) { - return title; - } - }); - when(album.getDescription()).thenAnswer(new Answer() { - @Override - public String answer(InvocationOnMock invocation) { - return description; - } - }); - when(album.getAlbumImage()).thenAnswer(new Answer() { - @Override - public Image answer(InvocationOnMock invocation) { - if (imageId == null) { - return null; - } - Image image = mock(Image.class); - when(image.getId()).thenReturn(imageId); - return image; - } - }); - when(album.getAlbums()).thenReturn(albums); - when(album.getImages()).thenReturn(images); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) { - albums.add((Album) invocation.getArguments()[0]); - ((Album) invocation.getArguments()[0]).setParent(album); - return null; - } - }).when(album).addAlbum(any(Album.class)); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) { - images.add((Image) invocation.getArguments()[0]); - return null; - } - }).when(album).addImage(any(Image.class)); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) { - parentAlbum = (Album) invocation.getArguments()[0]; - return null; - } - }).when(album).setParent(any(Album.class)); - when(album.getParent()).thenAnswer(new Answer() { - @Override - public Album answer(InvocationOnMock invocation) { - return parentAlbum; - } - }); - when(album.modify()).thenReturn(new Modifier() { - @Override - public Modifier setTitle(String title) { - TestAlbumBuilder.this.title = title; - return this; - } - - @Override - public Modifier setDescription(String description) { - TestAlbumBuilder.this.description = description; - return this; - } - - @Override - public Modifier setAlbumImage(String imageId) { - TestAlbumBuilder.this.imageId = imageId; - return this; - } - - @Override - public Album update() throws IllegalStateException { - return album; - } - }); - } - - @Override - public AlbumBuilder randomId() { - when(album.getId()).thenReturn(randomUUID().toString()); - return this; - } - - @Override - public AlbumBuilder withId(String id) { - when(album.getId()).thenReturn(id); - return this; - } - - @Override - public AlbumBuilder by(Sone sone) { - when(album.getSone()).thenReturn(sone); - return this; - } - - @Override - public Album build() throws IllegalStateException { - return album; - } - - } - - private static class TestImageBuilder implements ImageBuilder { - - private final Image image; - - private TestImageBuilder() { - image = mock(Image.class); - Image.Modifier imageModifier = new Image.Modifier() { - private Sone sone = image.getSone(); - private long creationTime = image.getCreationTime(); - private String key = image.getKey(); - private String title = image.getTitle(); - private String description = image.getDescription(); - private int width = image.getWidth(); - private int height = image.getHeight(); - - @Override - public Image.Modifier setSone(Sone sone) { - this.sone = sone; - return this; - } - - @Override - public Image.Modifier setCreationTime(long creationTime) { - this.creationTime = creationTime; - return this; - } - - @Override - public Image.Modifier setKey(String key) { - this.key = key; - return this; - } - - @Override - public Image.Modifier setTitle(String title) { - this.title = title; - return this; - } - - @Override - public Image.Modifier setDescription(String description) { - this.description = description; - return this; - } - - @Override - public Image.Modifier setWidth(int width) { - this.width = width; - return this; - } - - @Override - public Image.Modifier setHeight(int height) { - this.height = height; - return this; - } - - @Override - public Image update() throws IllegalStateException { - when(image.getSone()).thenReturn(sone); - when(image.getCreationTime()).thenReturn(creationTime); - when(image.getKey()).thenReturn(key); - when(image.getTitle()).thenReturn(title); - when(image.getDescription()).thenReturn(description); - when(image.getWidth()).thenReturn(width); - when(image.getHeight()).thenReturn(height); - return image; - } - }; - when(image.modify()).thenReturn(imageModifier); - } - - @Override - public ImageBuilder randomId() { - when(image.getId()).thenReturn(randomUUID().toString()); - return this; - } - - @Override - public ImageBuilder withId(String id) { - when(image.getId()).thenReturn(id); - return this; - } - - @Override - public Image build() throws IllegalStateException { - return image; - } - - } - }