From: David ‘Bombe’ Roden Date: Sun, 9 Apr 2017 09:36:24 +0000 (+0200) Subject: Move classes for tests to test package X-Git-Tag: 0.9.7^2~248 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=eb32457356fac09e16ec98394c1b5d48f9dfba84 Move classes for tests to test package --- diff --git a/src/test/java/net/pterodactylus/sone/Matchers.java b/src/test/java/net/pterodactylus/sone/Matchers.java deleted file mode 100644 index 823cdf9..0000000 --- a/src/test/java/net/pterodactylus/sone/Matchers.java +++ /dev/null @@ -1,373 +0,0 @@ -/* - * Sone - Matchers.java - Copyright © 2013–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone; - -import static java.util.regex.Pattern.compile; - -import java.io.IOException; -import java.io.InputStream; - -import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.Image; -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; - -import com.google.common.base.Optional; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; -import org.hamcrest.TypeSafeMatcher; - -/** - * Matchers used throughout the tests. - * - * @author David ‘Bombe’ Roden - */ -public class Matchers { - - public static Matcher matchesRegex(final String regex) { - return new TypeSafeMatcher() { - @Override - protected boolean matchesSafely(String item) { - return compile(regex).matcher(item).matches(); - } - - @Override - public void describeTo(Description description) { - description.appendText("matches: ").appendValue(regex); - } - }; - } - - public static Matcher delivers(final byte[] data) { - return new TypeSafeMatcher() { - byte[] readData = new byte[data.length]; - - @Override - protected boolean matchesSafely(InputStream inputStream) { - int offset = 0; - try { - while (true) { - int r = inputStream.read(); - if (r == -1) { - return offset == data.length; - } - if (offset == data.length) { - return false; - } - if (data[offset] != (readData[offset] = (byte) r)) { - return false; - } - offset++; - } - } catch (IOException ioe1) { - return false; - } - } - - @Override - public void describeTo(Description description) { - description.appendValue(data); - } - - @Override - protected void describeMismatchSafely(InputStream item, - Description mismatchDescription) { - mismatchDescription.appendValue(readData); - } - }; - } - - public static Matcher isPost(String postId, long time, - String text, Optional recipient) { - return new PostMatcher(postId, time, text, recipient); - } - - public static Matcher isPostWithId(String postId) { - return new PostIdMatcher(postId); - } - - public static Matcher isPostReply(String postReplyId, - String postId, long time, String text) { - return new PostReplyMatcher(postReplyId, postId, time, text); - } - - public static Matcher isAlbum(final String albumId, - final String parentAlbumId, - final String title, final String albumDescription) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(Album album, - Description mismatchDescription) { - if (!album.getId().equals(albumId)) { - mismatchDescription.appendText("ID is ") - .appendValue(album.getId()); - return false; - } - if (parentAlbumId == null) { - if (album.getParent() != null) { - mismatchDescription.appendText("has parent album"); - return false; - } - } else { - if (album.getParent() == null) { - mismatchDescription.appendText("has no parent album"); - return false; - } - if (!album.getParent().getId().equals(parentAlbumId)) { - mismatchDescription.appendText("parent album is ") - .appendValue(album.getParent().getId()); - return false; - } - } - if (!title.equals(album.getTitle())) { - mismatchDescription.appendText("has title ") - .appendValue(album.getTitle()); - return false; - } - if (!albumDescription.equals(album.getDescription())) { - mismatchDescription.appendText("has description ") - .appendValue(album.getDescription()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is album ").appendValue(albumId); - if (parentAlbumId == null) { - description.appendText(", has no parent"); - } else { - description.appendText(", has parent ") - .appendValue(parentAlbumId); - } - description.appendText(", has title ").appendValue(title); - description.appendText(", has description ") - .appendValue(albumDescription); - } - }; - } - - public static Matcher isImage(final String id, - final long creationTime, - final String key, final String title, - final String imageDescription, - final int width, final int height) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(Image image, - Description mismatchDescription) { - if (!image.getId().equals(id)) { - mismatchDescription.appendText("ID is ") - .appendValue(image.getId()); - return false; - } - if (image.getCreationTime() != creationTime) { - mismatchDescription.appendText("created at @") - .appendValue(image.getCreationTime()); - return false; - } - if (!image.getKey().equals(key)) { - mismatchDescription.appendText("key is ") - .appendValue(image.getKey()); - return false; - } - if (!image.getTitle().equals(title)) { - mismatchDescription.appendText("title is ") - .appendValue(image.getTitle()); - return false; - } - if (!image.getDescription().equals(imageDescription)) { - mismatchDescription.appendText("description is ") - .appendValue(image.getDescription()); - return false; - } - if (image.getWidth() != width) { - mismatchDescription.appendText("width is ") - .appendValue(image.getWidth()); - return false; - } - if (image.getHeight() != height) { - mismatchDescription.appendText("height is ") - .appendValue(image.getHeight()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("image with ID ").appendValue(id); - description.appendText(", created at @") - .appendValue(creationTime); - description.appendText(", has key ").appendValue(key); - description.appendText(", has title ").appendValue(title); - description.appendText(", has description ") - .appendValue(imageDescription); - description.appendText(", has width ").appendValue(width); - description.appendText(", has height ").appendValue(height); - } - }; - } - - private static class PostMatcher extends TypeSafeDiagnosingMatcher { - - private final String postId; - private final long time; - private final String text; - private final Optional recipient; - - private PostMatcher(String postId, long time, String text, - Optional recipient) { - this.postId = postId; - this.time = time; - this.text = text; - this.recipient = recipient; - } - - @Override - protected boolean matchesSafely(Post post, - Description mismatchDescription) { - if (!post.getId().equals(postId)) { - mismatchDescription.appendText("ID is not ") - .appendValue(postId); - return false; - } - if (post.getTime() != time) { - mismatchDescription.appendText("Time is not @") - .appendValue(time); - return false; - } - if (!post.getText().equals(text)) { - mismatchDescription.appendText("Text is not ") - .appendValue(text); - return false; - } - if (recipient.isPresent()) { - if (!post.getRecipientId().isPresent()) { - mismatchDescription.appendText( - "Recipient not present"); - return false; - } - if (!post.getRecipientId().get().equals(recipient.get())) { - mismatchDescription.appendText("Recipient is not ") - .appendValue(recipient.get()); - return false; - } - } else { - if (post.getRecipientId().isPresent()) { - mismatchDescription.appendText("Recipient is present"); - return false; - } - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is post with ID ") - .appendValue(postId); - description.appendText(", created at @").appendValue(time); - description.appendText(", text ").appendValue(text); - if (recipient.isPresent()) { - description.appendText(", directed at ") - .appendValue(recipient.get()); - } - } - - } - - private static class PostIdMatcher extends TypeSafeDiagnosingMatcher { - - private final String id; - - private PostIdMatcher(String id) { - this.id = id; - } - - @Override - protected boolean matchesSafely(Post item, - Description mismatchDescription) { - if (!item.getId().equals(id)) { - mismatchDescription.appendText("post has ID ").appendValue(item.getId()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("post with ID ").appendValue(id); - } - - } - - private static class PostReplyMatcher - extends TypeSafeDiagnosingMatcher { - - private final String postReplyId; - private final String postId; - private final long time; - private final String text; - - private PostReplyMatcher(String postReplyId, String postId, long time, - String text) { - this.postReplyId = postReplyId; - this.postId = postId; - this.time = time; - this.text = text; - } - - @Override - protected boolean matchesSafely(PostReply postReply, - Description mismatchDescription) { - if (!postReply.getId().equals(postReplyId)) { - mismatchDescription.appendText("is post reply ") - .appendValue(postReply.getId()); - return false; - } - if (!postReply.getPostId().equals(postId)) { - mismatchDescription.appendText("is reply to ") - .appendValue(postReply.getPostId()); - return false; - } - if (postReply.getTime() != time) { - mismatchDescription.appendText("is created at @").appendValue( - postReply.getTime()); - return false; - } - if (!postReply.getText().equals(text)) { - mismatchDescription.appendText("says ") - .appendValue(postReply.getText()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is post reply ").appendValue(postReplyId); - description.appendText(", replies to post ").appendValue(postId); - description.appendText(", is created at @").appendValue(time); - description.appendText(", says ").appendValue(text); - } - - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestAlbumBuilder.java b/src/test/java/net/pterodactylus/sone/TestAlbumBuilder.java deleted file mode 100644 index ea21118..0000000 --- a/src/test/java/net/pterodactylus/sone/TestAlbumBuilder.java +++ /dev/null @@ -1,122 +0,0 @@ -package net.pterodactylus.sone; - -import static java.util.UUID.randomUUID; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.List; - -import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.Album.Modifier; -import net.pterodactylus.sone.data.Image; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.database.AlbumBuilder; - -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -/** - * {@link AlbumBuilder} that returns a mocked {@link Album}. - * - * @author David ‘Bombe’ Roden - */ -public 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.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 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; - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestImageBuilder.java b/src/test/java/net/pterodactylus/sone/TestImageBuilder.java deleted file mode 100644 index 48a8fad..0000000 --- a/src/test/java/net/pterodactylus/sone/TestImageBuilder.java +++ /dev/null @@ -1,105 +0,0 @@ -package net.pterodactylus.sone; - -import static java.util.UUID.randomUUID; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.data.Image; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.database.ImageBuilder; - -/** - * {@link ImageBuilder} implementation that returns a mocked {@link Image}. - * - * @author David ‘Bombe’ Roden - */ -public class TestImageBuilder implements ImageBuilder { - - private final Image image; - - public 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; - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestPostBuilder.java b/src/test/java/net/pterodactylus/sone/TestPostBuilder.java deleted file mode 100644 index 29c4c40..0000000 --- a/src/test/java/net/pterodactylus/sone/TestPostBuilder.java +++ /dev/null @@ -1,78 +0,0 @@ -package net.pterodactylus.sone; - -import static com.google.common.base.Optional.fromNullable; -import static java.lang.System.currentTimeMillis; -import static java.util.UUID.randomUUID; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.database.PostBuilder; - -/** - * {@link PostBuilder} implementation that returns a mocked {@link Post}. - * - * @author David ‘Bombe’ Roden - */ -public 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; - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestPostReplyBuilder.java b/src/test/java/net/pterodactylus/sone/TestPostReplyBuilder.java deleted file mode 100644 index 6b929c7..0000000 --- a/src/test/java/net/pterodactylus/sone/TestPostReplyBuilder.java +++ /dev/null @@ -1,70 +0,0 @@ -package net.pterodactylus.sone; - -import static java.lang.System.currentTimeMillis; -import static java.util.UUID.randomUUID; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.database.PostReplyBuilder; - -/** - * {@link PostReplyBuilder} that returns a mocked {@link PostReply}. - * - * @author David ‘Bombe’ Roden - */ -public 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; - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestUtil.java b/src/test/java/net/pterodactylus/sone/TestUtil.java deleted file mode 100644 index fa7879d..0000000 --- a/src/test/java/net/pterodactylus/sone/TestUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -package net.pterodactylus.sone; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; - -/** - * Utilities for testing. - * - * @author David ‘Bombe’ Roden - */ -public class TestUtil { - - public static void setFinalField(Object object, String fieldName, Object value) { - try { - Field clientCoreField = object.getClass().getField(fieldName); - clientCoreField.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL); - clientCoreField.set(object, value); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - public static T getPrivateField(Object object, String fieldName) { - try { - Field field = object.getClass().getDeclaredField(fieldName); - field.setAccessible(true); - return (T) field.get(object); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - public static T callPrivateMethod(Object object, String methodName) { - try { - Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]); - method.setAccessible(true); - return (T) method.invoke(object); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/src/test/java/net/pterodactylus/sone/TestValue.java b/src/test/java/net/pterodactylus/sone/TestValue.java deleted file mode 100644 index 43cf0a6..0000000 --- a/src/test/java/net/pterodactylus/sone/TestValue.java +++ /dev/null @@ -1,59 +0,0 @@ -package net.pterodactylus.sone; - -import java.util.concurrent.atomic.AtomicReference; - -import net.pterodactylus.util.config.ConfigurationException; -import net.pterodactylus.util.config.Value; - -import com.google.common.base.Objects; - -/** - * Simple {@link Value} implementation. - * - * @author David ‘Bombe’ Roden - */ -public 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); - } - - @Override - public int hashCode() { - return value.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return (obj instanceof TestValue) && Objects.equal(value.get(), - ((TestValue) obj).value.get()); - } - - @Override - public String toString() { - return String.valueOf(value.get()); - } - - public static Value from(T value) { - return new TestValue(value); - } - -} diff --git a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java index 7deb805..4a26203 100644 --- a/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java @@ -1,10 +1,10 @@ package net.pterodactylus.sone.core; import static com.google.common.base.Optional.of; -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; @@ -23,11 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import net.pterodactylus.sone.TestAlbumBuilder; -import net.pterodactylus.sone.TestImageBuilder; -import net.pterodactylus.sone.TestPostBuilder; -import net.pterodactylus.sone.TestPostReplyBuilder; -import net.pterodactylus.sone.TestValue; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound; @@ -47,6 +42,11 @@ 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 com.google.common.base.Optional; diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index 59ee67d..2ebc428 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -4,8 +4,8 @@ import static freenet.client.FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUN import static freenet.keys.InsertableClientSSK.createRandom; import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; -import static net.pterodactylus.sone.Matchers.delivers; -import static net.pterodactylus.sone.TestUtil.setFinalField; +import static net.pterodactylus.sone.test.Matchers.delivers; +import static net.pterodactylus.sone.test.TestUtil.setFinalField; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -29,7 +29,6 @@ import java.io.IOException; import java.net.MalformedURLException; import java.util.HashMap; -import net.pterodactylus.sone.TestUtil; import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback; import net.pterodactylus.sone.core.FreenetInterface.Callback; import net.pterodactylus.sone.core.FreenetInterface.Fetched; @@ -43,6 +42,7 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.data.impl.ImageImpl; +import net.pterodactylus.sone.test.TestUtil; import freenet.client.ClientMetadata; import freenet.client.FetchContext; diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java index e12fe93..49777f0 100644 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java @@ -7,7 +7,7 @@ import static org.hamcrest.Matchers.not; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import net.pterodactylus.sone.TestValue; +import net.pterodactylus.sone.test.TestValue; import net.pterodactylus.util.config.Configuration; import com.google.common.eventbus.EventBus; diff --git a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java index 47e26b7..8dfce3d 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java @@ -10,7 +10,7 @@ import static org.mockito.Mockito.when; import java.util.HashSet; import java.util.Set; -import net.pterodactylus.sone.TestValue; +import net.pterodactylus.sone.test.TestValue; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.Value; diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java index c16ce9d..e6bfffe 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java @@ -1,7 +1,7 @@ package net.pterodactylus.sone.database.memory; import static com.google.common.base.Optional.fromNullable; -import static net.pterodactylus.sone.Matchers.isPostWithId; +import static net.pterodactylus.sone.test.Matchers.isPostWithId; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index d305031..352b3b2 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -20,10 +20,10 @@ package net.pterodactylus.sone.database.memory; import static com.google.common.base.Optional.of; import static java.util.Arrays.asList; 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.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; @@ -44,17 +44,17 @@ import java.util.List; import java.util.Map; import java.util.Set; -import net.pterodactylus.sone.TestAlbumBuilder; -import net.pterodactylus.sone.TestImageBuilder; -import net.pterodactylus.sone.TestPostBuilder; -import net.pterodactylus.sone.TestPostReplyBuilder; -import net.pterodactylus.sone.TestValue; import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.impl.AlbumImpl; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.impl.AlbumImpl; +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.Value; diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java index f11c7e3..8def0e0 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java @@ -19,7 +19,7 @@ package net.pterodactylus.sone.freenet.wot; import static com.google.common.collect.ImmutableMap.of; import static java.util.Arrays.asList; -import static net.pterodactylus.sone.Matchers.matchesRegex; +import static net.pterodactylus.sone.test.Matchers.matchesRegex; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; diff --git a/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java index aace929..56d2f29 100644 --- a/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java +++ b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java @@ -14,11 +14,11 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import net.pterodactylus.sone.TestUtil; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.test.TestUtil; import org.hamcrest.Description; import org.hamcrest.Matcher; diff --git a/src/test/java/net/pterodactylus/sone/test/Matchers.java b/src/test/java/net/pterodactylus/sone/test/Matchers.java new file mode 100644 index 0000000..08ad611 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/Matchers.java @@ -0,0 +1,373 @@ +/* + * Sone - Matchers.java - Copyright © 2013–2016 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.test; + +import static java.util.regex.Pattern.compile; + +import java.io.IOException; +import java.io.InputStream; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; + +import com.google.common.base.Optional; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matchers used throughout the tests. + * + * @author David ‘Bombe’ Roden + */ +public class Matchers { + + public static Matcher matchesRegex(final String regex) { + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(String item) { + return compile(regex).matcher(item).matches(); + } + + @Override + public void describeTo(Description description) { + description.appendText("matches: ").appendValue(regex); + } + }; + } + + public static Matcher delivers(final byte[] data) { + return new TypeSafeMatcher() { + byte[] readData = new byte[data.length]; + + @Override + protected boolean matchesSafely(InputStream inputStream) { + int offset = 0; + try { + while (true) { + int r = inputStream.read(); + if (r == -1) { + return offset == data.length; + } + if (offset == data.length) { + return false; + } + if (data[offset] != (readData[offset] = (byte) r)) { + return false; + } + offset++; + } + } catch (IOException ioe1) { + return false; + } + } + + @Override + public void describeTo(Description description) { + description.appendValue(data); + } + + @Override + protected void describeMismatchSafely(InputStream item, + Description mismatchDescription) { + mismatchDescription.appendValue(readData); + } + }; + } + + public static Matcher isPost(String postId, long time, + String text, Optional recipient) { + return new PostMatcher(postId, time, text, recipient); + } + + public static Matcher isPostWithId(String postId) { + return new PostIdMatcher(postId); + } + + public static Matcher isPostReply(String postReplyId, + String postId, long time, String text) { + return new PostReplyMatcher(postReplyId, postId, time, text); + } + + public static Matcher isAlbum(final String albumId, + final String parentAlbumId, + final String title, final String albumDescription) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Album album, + Description mismatchDescription) { + if (!album.getId().equals(albumId)) { + mismatchDescription.appendText("ID is ") + .appendValue(album.getId()); + return false; + } + if (parentAlbumId == null) { + if (album.getParent() != null) { + mismatchDescription.appendText("has parent album"); + return false; + } + } else { + if (album.getParent() == null) { + mismatchDescription.appendText("has no parent album"); + return false; + } + if (!album.getParent().getId().equals(parentAlbumId)) { + mismatchDescription.appendText("parent album is ") + .appendValue(album.getParent().getId()); + return false; + } + } + if (!title.equals(album.getTitle())) { + mismatchDescription.appendText("has title ") + .appendValue(album.getTitle()); + return false; + } + if (!albumDescription.equals(album.getDescription())) { + mismatchDescription.appendText("has description ") + .appendValue(album.getDescription()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is album ").appendValue(albumId); + if (parentAlbumId == null) { + description.appendText(", has no parent"); + } else { + description.appendText(", has parent ") + .appendValue(parentAlbumId); + } + description.appendText(", has title ").appendValue(title); + description.appendText(", has description ") + .appendValue(albumDescription); + } + }; + } + + public static Matcher isImage(final String id, + final long creationTime, + final String key, final String title, + final String imageDescription, + final int width, final int height) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Image image, + Description mismatchDescription) { + if (!image.getId().equals(id)) { + mismatchDescription.appendText("ID is ") + .appendValue(image.getId()); + return false; + } + if (image.getCreationTime() != creationTime) { + mismatchDescription.appendText("created at @") + .appendValue(image.getCreationTime()); + return false; + } + if (!image.getKey().equals(key)) { + mismatchDescription.appendText("key is ") + .appendValue(image.getKey()); + return false; + } + if (!image.getTitle().equals(title)) { + mismatchDescription.appendText("title is ") + .appendValue(image.getTitle()); + return false; + } + if (!image.getDescription().equals(imageDescription)) { + mismatchDescription.appendText("description is ") + .appendValue(image.getDescription()); + return false; + } + if (image.getWidth() != width) { + mismatchDescription.appendText("width is ") + .appendValue(image.getWidth()); + return false; + } + if (image.getHeight() != height) { + mismatchDescription.appendText("height is ") + .appendValue(image.getHeight()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("image with ID ").appendValue(id); + description.appendText(", created at @") + .appendValue(creationTime); + description.appendText(", has key ").appendValue(key); + description.appendText(", has title ").appendValue(title); + description.appendText(", has description ") + .appendValue(imageDescription); + description.appendText(", has width ").appendValue(width); + description.appendText(", has height ").appendValue(height); + } + }; + } + + private static class PostMatcher extends TypeSafeDiagnosingMatcher { + + private final String postId; + private final long time; + private final String text; + private final Optional recipient; + + private PostMatcher(String postId, long time, String text, + Optional recipient) { + this.postId = postId; + this.time = time; + this.text = text; + this.recipient = recipient; + } + + @Override + protected boolean matchesSafely(Post post, + Description mismatchDescription) { + if (!post.getId().equals(postId)) { + mismatchDescription.appendText("ID is not ") + .appendValue(postId); + return false; + } + if (post.getTime() != time) { + mismatchDescription.appendText("Time is not @") + .appendValue(time); + return false; + } + if (!post.getText().equals(text)) { + mismatchDescription.appendText("Text is not ") + .appendValue(text); + return false; + } + if (recipient.isPresent()) { + if (!post.getRecipientId().isPresent()) { + mismatchDescription.appendText( + "Recipient not present"); + return false; + } + if (!post.getRecipientId().get().equals(recipient.get())) { + mismatchDescription.appendText("Recipient is not ") + .appendValue(recipient.get()); + return false; + } + } else { + if (post.getRecipientId().isPresent()) { + mismatchDescription.appendText("Recipient is present"); + return false; + } + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is post with ID ") + .appendValue(postId); + description.appendText(", created at @").appendValue(time); + description.appendText(", text ").appendValue(text); + if (recipient.isPresent()) { + description.appendText(", directed at ") + .appendValue(recipient.get()); + } + } + + } + + private static class PostIdMatcher extends TypeSafeDiagnosingMatcher { + + private final String id; + + private PostIdMatcher(String id) { + this.id = id; + } + + @Override + protected boolean matchesSafely(Post item, + Description mismatchDescription) { + if (!item.getId().equals(id)) { + mismatchDescription.appendText("post has ID ").appendValue(item.getId()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("post with ID ").appendValue(id); + } + + } + + private static class PostReplyMatcher + extends TypeSafeDiagnosingMatcher { + + private final String postReplyId; + private final String postId; + private final long time; + private final String text; + + private PostReplyMatcher(String postReplyId, String postId, long time, + String text) { + this.postReplyId = postReplyId; + this.postId = postId; + this.time = time; + this.text = text; + } + + @Override + protected boolean matchesSafely(PostReply postReply, + Description mismatchDescription) { + if (!postReply.getId().equals(postReplyId)) { + mismatchDescription.appendText("is post reply ") + .appendValue(postReply.getId()); + return false; + } + if (!postReply.getPostId().equals(postId)) { + mismatchDescription.appendText("is reply to ") + .appendValue(postReply.getPostId()); + return false; + } + if (postReply.getTime() != time) { + mismatchDescription.appendText("is created at @").appendValue( + postReply.getTime()); + return false; + } + if (!postReply.getText().equals(text)) { + mismatchDescription.appendText("says ") + .appendValue(postReply.getText()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is post reply ").appendValue(postReplyId); + description.appendText(", replies to post ").appendValue(postId); + description.appendText(", is created at @").appendValue(time); + description.appendText(", says ").appendValue(text); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestAlbumBuilder.java b/src/test/java/net/pterodactylus/sone/test/TestAlbumBuilder.java new file mode 100644 index 0000000..8871ee3 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestAlbumBuilder.java @@ -0,0 +1,122 @@ +package net.pterodactylus.sone.test; + +import static java.util.UUID.randomUUID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Album.Modifier; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.AlbumBuilder; + +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +/** + * {@link AlbumBuilder} that returns a mocked {@link Album}. + * + * @author David ‘Bombe’ Roden + */ +public 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.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 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; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestImageBuilder.java b/src/test/java/net/pterodactylus/sone/test/TestImageBuilder.java new file mode 100644 index 0000000..edeeb1a --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestImageBuilder.java @@ -0,0 +1,105 @@ +package net.pterodactylus.sone.test; + +import static java.util.UUID.randomUUID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.ImageBuilder; + +/** + * {@link ImageBuilder} implementation that returns a mocked {@link Image}. + * + * @author David ‘Bombe’ Roden + */ +public class TestImageBuilder implements ImageBuilder { + + private final Image image; + + public 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; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestPostBuilder.java b/src/test/java/net/pterodactylus/sone/test/TestPostBuilder.java new file mode 100644 index 0000000..1933857 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestPostBuilder.java @@ -0,0 +1,78 @@ +package net.pterodactylus.sone.test; + +import static com.google.common.base.Optional.fromNullable; +import static java.lang.System.currentTimeMillis; +import static java.util.UUID.randomUUID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.PostBuilder; + +/** + * {@link PostBuilder} implementation that returns a mocked {@link Post}. + * + * @author David ‘Bombe’ Roden + */ +public 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; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestPostReplyBuilder.java b/src/test/java/net/pterodactylus/sone/test/TestPostReplyBuilder.java new file mode 100644 index 0000000..e09e120 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestPostReplyBuilder.java @@ -0,0 +1,70 @@ +package net.pterodactylus.sone.test; + +import static java.lang.System.currentTimeMillis; +import static java.util.UUID.randomUUID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.PostReplyBuilder; + +/** + * {@link PostReplyBuilder} that returns a mocked {@link PostReply}. + * + * @author David ‘Bombe’ Roden + */ +public 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; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestUtil.java b/src/test/java/net/pterodactylus/sone/test/TestUtil.java new file mode 100644 index 0000000..8b3160f --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestUtil.java @@ -0,0 +1,56 @@ +package net.pterodactylus.sone.test; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +/** + * Utilities for testing. + * + * @author David ‘Bombe’ Roden + */ +public class TestUtil { + + public static void setFinalField(Object object, String fieldName, Object value) { + try { + Field clientCoreField = object.getClass().getField(fieldName); + clientCoreField.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL); + clientCoreField.set(object, value); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public static T getPrivateField(Object object, String fieldName) { + try { + Field field = object.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return (T) field.get(object); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public static T callPrivateMethod(Object object, String methodName) { + try { + Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]); + method.setAccessible(true); + return (T) method.invoke(object); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/test/java/net/pterodactylus/sone/test/TestValue.java b/src/test/java/net/pterodactylus/sone/test/TestValue.java new file mode 100644 index 0000000..94ff3c6 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/TestValue.java @@ -0,0 +1,59 @@ +package net.pterodactylus.sone.test; + +import java.util.concurrent.atomic.AtomicReference; + +import net.pterodactylus.util.config.ConfigurationException; +import net.pterodactylus.util.config.Value; + +import com.google.common.base.Objects; + +/** + * Simple {@link Value} implementation. + * + * @author David ‘Bombe’ Roden + */ +public 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); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof TestValue) && Objects.equal(value.get(), + ((TestValue) obj).value.get()); + } + + @Override + public String toString() { + return String.valueOf(value.get()); + } + + public static Value from(T value) { + return new TestValue(value); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/utils/IntegerRangePredicateTest.java b/src/test/java/net/pterodactylus/sone/utils/IntegerRangePredicateTest.java index b2d078e..687afa3 100644 --- a/src/test/java/net/pterodactylus/sone/utils/IntegerRangePredicateTest.java +++ b/src/test/java/net/pterodactylus/sone/utils/IntegerRangePredicateTest.java @@ -4,7 +4,7 @@ import static net.pterodactylus.sone.utils.IntegerRangePredicate.range; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import net.pterodactylus.sone.TestUtil; +import net.pterodactylus.sone.test.TestUtil; import org.junit.Test; diff --git a/src/test/kotlin/net/pterodactylus/sone/OneByOneMatcher.kt b/src/test/kotlin/net/pterodactylus/sone/OneByOneMatcher.kt deleted file mode 100644 index 109add2..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/OneByOneMatcher.kt +++ /dev/null @@ -1,33 +0,0 @@ -package net.pterodactylus.sone - -import org.hamcrest.Description -import org.hamcrest.TypeSafeDiagnosingMatcher - -class OneByOneMatcher : TypeSafeDiagnosingMatcher() { - private data class Matcher(val expected: V, val actual: (A) -> V, val description: String) - - private val matchers = mutableListOf>() - - fun expect(description: String, expected: V, actual: (A) -> V) { - matchers += Matcher(expected, actual, description) - } - - override fun describeTo(description: Description) { - matchers.forEachIndexed { index, matcher -> - if (index > 0) { - description.appendText(", ") - } - description.appendText("${matcher.description} is ").appendValue(matcher.expected) - } - } - - override fun matchesSafely(item: A, mismatchDescription: Description) = - matchers.all { - if (it.expected != it.actual(item)) { - mismatchDescription.appendText("${it.description} is ").appendValue(it.actual(item)) - false - } else { - true - } - } -} diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index e5c2066..f3994c8 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.fcp import com.google.common.base.Optional import com.google.common.base.Optional.absent import freenet.support.SimpleFieldSet -import net.pterodactylus.sone.OneByOneMatcher import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply @@ -11,6 +10,7 @@ import net.pterodactylus.sone.data.Profile import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException import net.pterodactylus.sone.template.SoneAccessor +import net.pterodactylus.sone.test.OneByOneMatcher import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever diff --git a/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt index d56bdc5..c51cf27 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt @@ -1,7 +1,7 @@ package net.pterodactylus.sone.template import freenet.support.api.HTTPRequest -import net.pterodactylus.sone.Matchers.matchesRegex +import net.pterodactylus.sone.test.Matchers.matchesRegex import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.web.page.FreenetRequest diff --git a/src/test/kotlin/net/pterodactylus/sone/test/OneByOneMatcher.kt b/src/test/kotlin/net/pterodactylus/sone/test/OneByOneMatcher.kt new file mode 100644 index 0000000..f0cceca --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/test/OneByOneMatcher.kt @@ -0,0 +1,33 @@ +package net.pterodactylus.sone.test + +import org.hamcrest.Description +import org.hamcrest.TypeSafeDiagnosingMatcher + +class OneByOneMatcher : TypeSafeDiagnosingMatcher() { + private data class Matcher(val expected: V, val actual: (A) -> V, val description: String) + + private val matchers = mutableListOf>() + + fun expect(description: String, expected: V, actual: (A) -> V) { + matchers += Matcher(expected, actual, description) + } + + override fun describeTo(description: Description) { + matchers.forEachIndexed { index, matcher -> + if (index > 0) { + description.appendText(", ") + } + description.appendText("${matcher.description} is ").appendValue(matcher.expected) + } + } + + override fun matchesSafely(item: A, mismatchDescription: Description) = + matchers.all { + if (it.expected != it.actual(item)) { + mismatchDescription.appendText("${it.description} is ").appendValue(it.actual(item)) + false + } else { + true + } + } +}