diff --git a/src/test/java/fr/xephi/authme/util/PlayerUtilsTest.java b/src/test/java/fr/xephi/authme/util/PlayerUtilsTest.java index be5425c2..d16a8547 100644 --- a/src/test/java/fr/xephi/authme/util/PlayerUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/PlayerUtilsTest.java @@ -64,4 +64,10 @@ public class PlayerUtilsTest { // then assertThat(result, equalTo(name)); } + + @Test + public void shouldHaveHiddenConstructor() { + // given / when / then + TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerUtils.class); + } } diff --git a/src/test/java/fr/xephi/authme/util/RandomStringUtilsTest.java b/src/test/java/fr/xephi/authme/util/RandomStringUtilsTest.java index c0f9cd0b..b5200b01 100644 --- a/src/test/java/fr/xephi/authme/util/RandomStringUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/RandomStringUtilsTest.java @@ -1,5 +1,6 @@ package fr.xephi.authme.util; +import fr.xephi.authme.TestHelper; import org.junit.Test; import java.util.regex.Pattern; @@ -68,4 +69,10 @@ public class RandomStringUtilsTest { // then - throw exception } + @Test + public void shouldHaveHiddenConstructor() { + // given / when / then + TestHelper.validateHasOnlyPrivateEmptyConstructor(RandomStringUtils.class); + } + } diff --git a/src/test/java/fr/xephi/authme/util/lazytags/TagBuilderTest.java b/src/test/java/fr/xephi/authme/util/lazytags/TagBuilderTest.java new file mode 100644 index 00000000..60b5b68e --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/lazytags/TagBuilderTest.java @@ -0,0 +1,50 @@ +package fr.xephi.authme.util.lazytags; + +import fr.xephi.authme.TestHelper; +import org.junit.Test; + +import java.util.function.Function; +import java.util.function.Supplier; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertThat; + +/** + * Test for {@link TagBuilder}. + */ +public class TagBuilderTest { + + @Test + public void shouldCreateNoArgsTag() { + // given + Supplier supplier = () -> "hello"; + + // when + Tag tag = TagBuilder.createTag("hey", supplier); + + // then + assertThat(tag, instanceOf(SimpleTag.class)); + assertThat(tag.getValue(null), equalTo("hello")); + } + + @Test + public void shouldCreateDependentTag() { + // given + Function function = d -> Double.toString(d + d/10); + + // when + Tag tag = TagBuilder.createTag("%test", function); + + // then + assertThat(tag, instanceOf(DependentTag.class)); + assertThat(tag.getValue(24d), equalTo("26.4")); + } + + @Test + public void shouldHaveHiddenConstructor() { + // given / when / then + TestHelper.validateHasOnlyPrivateEmptyConstructor(TagBuilder.class); + } + +} diff --git a/src/test/java/fr/xephi/authme/util/lazytags/TagReplacerTest.java b/src/test/java/fr/xephi/authme/util/lazytags/TagReplacerTest.java new file mode 100644 index 00000000..8c9a8187 --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/lazytags/TagReplacerTest.java @@ -0,0 +1,90 @@ +package fr.xephi.authme.util.lazytags; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static fr.xephi.authme.util.lazytags.TagBuilder.createTag; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Test for {@link TagReplacer}. + */ +public class TagReplacerTest { + + @Test + public void shouldReplaceTags() { + // given + TestTagService tagService = new TestTagService(); + List> tags = tagService.getAvailableTags(); + List messages = Arrays.asList("pi = %PI", "for i = %self, i^2 = %square", "%self %self %PI"); + + // when + TagReplacer tagReplacer = TagReplacer.newReplacer(tags, messages); + List result = tagReplacer.getAdaptedMessages(3); + + // then + assertThat(tagService.piCount, equalTo(1)); + assertThat(tagService.selfCount, equalTo(1)); + assertThat(tagService.doubleCount, equalTo(0)); + assertThat(tagService.squareCount, equalTo(1)); + assertThat(result, contains("pi = 3.14159", "for i = 3, i^2 = 9", "3 3 3.14159")); + } + + @Test + public void shouldNotCallUnusedTags() { + // given + TestTagService tagService = new TestTagService(); + List> tags = tagService.getAvailableTags(); + List messages = Arrays.asList("pi = %PI", "double i = %double"); + + // when + TagReplacer tagReplacer = TagReplacer.newReplacer(tags, messages); + List result1 = tagReplacer.getAdaptedMessages(-4); + List result2 = tagReplacer.getAdaptedMessages(0); + + // then + assertThat(tagService.piCount, equalTo(2)); + assertThat(tagService.selfCount, equalTo(0)); + assertThat(tagService.doubleCount, equalTo(2)); + assertThat(tagService.squareCount, equalTo(0)); + assertThat(result1, contains("pi = 3.14159", "double i = -8")); + assertThat(result2, contains("pi = 3.14159", "double i = 0")); + } + + static final class TestTagService { + int piCount, selfCount, doubleCount, squareCount; + + String pi() { + ++piCount; + return "3.14159"; + } + + String self(int i) { + ++selfCount; + return Integer.toString(i); + } + + String calcDouble(int i) { + ++doubleCount; + return Integer.toString(2 * i); + } + + String calcSquare(int i) { + ++squareCount; + return Integer.toString(i * i); + } + + List> getAvailableTags() { + return Arrays.asList( + createTag("%PI", this::pi), + createTag("%self", this::self), + createTag("%double", this::calcDouble), + createTag("%square", this::calcSquare)); + } + } + +} diff --git a/src/test/java/fr/xephi/authme/util/lazytags/WrappedTagReplacerTest.java b/src/test/java/fr/xephi/authme/util/lazytags/WrappedTagReplacerTest.java new file mode 100644 index 00000000..19915054 --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/lazytags/WrappedTagReplacerTest.java @@ -0,0 +1,76 @@ +package fr.xephi.authme.util.lazytags; + +import fr.xephi.authme.util.lazytags.TagReplacerTest.TestTagService; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Test for {@link WrappedTagReplacer}. + */ +public class WrappedTagReplacerTest { + + @Test + public void shouldApplyTags() { + // given + TestTagService tagService = new TestTagService(); + List> tags = tagService.getAvailableTags(); + List objects = Arrays.asList( + new SampleClass(3, "pi is %PI"), + new SampleClass(5, "no tags here"), + new SampleClass(7, "i+i = %double")); + + // when + WrappedTagReplacer replacer = new WrappedTagReplacer<>( + tags, objects, SampleClass::getDescription, (o, s) -> new SampleClass(o.number, s)); + List result1 = replacer.getAdaptedItems(8); + List result2 = replacer.getAdaptedItems(1); + + // then + assertThat(tagService.piCount, equalTo(2)); + assertThat(tagService.selfCount, equalTo(0)); + assertThat(tagService.doubleCount, equalTo(2)); + assertThat(tagService.squareCount, equalTo(0)); + assertThat(result1, contains( + sampleClass(3, "pi is 3.14159"), sampleClass(5, "no tags here"), sampleClass(7, "i+i = 16"))); + assertThat(result2, contains( + sampleClass(3, "pi is 3.14159"), sampleClass(5, "no tags here"), sampleClass(7, "i+i = 2"))); + } + + + private static Matcher sampleClass(int number, String description) { + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(SampleClass item) { + return number == item.number && description.equals(item.description); + } + + @Override + public void describeTo(Description description) { + description.appendText("SampleClass[number=" + number + ";description=" + description + "]"); + } + }; + } + + private static final class SampleClass { + private final int number; + private final String description; + + SampleClass(int number, String description) { + this.number = number; + this.description = description; + } + + String getDescription() { + return description; + } + } +}