From 3e0a49659d6c5e803ea8389861d76d6cd95fb814 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 23 Jun 2017 16:49:57 +0200 Subject: [PATCH] Separated tests using a custom test runner Runs: 6/6 Errors: 0 Faliures: 4 --- .../buttondevteam/chat/ChatFormatTest.java | 45 +++++-- .../buttondevteam/chat/ObjectTestRunner.java | 112 ++++++++++++++++++ 2 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 src/test/java/buttondevteam/chat/ObjectTestRunner.java diff --git a/src/test/java/buttondevteam/chat/ChatFormatTest.java b/src/test/java/buttondevteam/chat/ChatFormatTest.java index 2d8a38e..69444cb 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatTest.java +++ b/src/test/java/buttondevteam/chat/ChatFormatTest.java @@ -1,10 +1,14 @@ package buttondevteam.chat; import java.util.ArrayList; +import java.util.List; + import org.bukkit.command.CommandSender; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mockito; +import buttondevteam.chat.ObjectTestRunner.Objects; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.chat.formatting.ChatFormatter; import buttondevteam.chat.formatting.TellrawPart; @@ -13,22 +17,43 @@ import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Color; import junit.framework.TestCase; +@RunWith(ObjectTestRunner.class) public class ChatFormatTest extends TestCase { - @Test - public void test() { + @Objects + public static List data() { TestPrepare.PrepareServer(); final CommandSender sender = Mockito.mock(CommandSender.class); DebugCommand.DebugMode = true; - testMessage(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White)); - testMessage(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White)); - testMessage(sender, "***test***", new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White)); - testMessage(sender, "***_test_***", - new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White)); - testMessage(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true) - .setUnderlined(true).setStrikethrough(true).setColor(Color.White)); + + List list = new ArrayList(); + + list.add(new ChatFormatTest(sender, "*test*", new TellrawPart("test").setItalic(true).setColor(Color.White))); + list.add(new ChatFormatTest(sender, "**test**", new TellrawPart("test").setBold(true).setColor(Color.White))); + list.add(new ChatFormatTest(sender, "***test***", + new TellrawPart("test").setBold(true).setItalic(true).setColor(Color.White))); + list.add(new ChatFormatTest(sender, "***_test_***", + new TellrawPart("test").setBold(true).setItalic(true).setUnderlined(true).setColor(Color.White))); + list.add(new ChatFormatTest(sender, "***_~~test~~_***", new TellrawPart("test").setBold(true).setItalic(true) + .setUnderlined(true).setStrikethrough(true).setColor(Color.White))); + list.add(new ChatFormatTest(sender, "¯\\\\\\_(ツ)\\_/¯", new TellrawPart("¯").setColor(Color.White), + new TellrawPart("\\").setColor(Color.White), new TellrawPart("_(ツ)").setColor(Color.White), + new TellrawPart("_/¯").setColor(Color.White))); + + return list; } - void testMessage(final CommandSender sender, final String message, TellrawPart... extras) { + private final CommandSender sender; + private final String message; + private final TellrawPart[] extras; + + public ChatFormatTest(CommandSender sender, String message, TellrawPart... extras) { + this.sender = sender; + this.message = message; + this.extras = extras; + } + + @Test + public void testMessage() { ArrayList cfs = ChatProcessing.addFormatters(Color.White); final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender, null); final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, chid); diff --git a/src/test/java/buttondevteam/chat/ObjectTestRunner.java b/src/test/java/buttondevteam/chat/ObjectTestRunner.java new file mode 100644 index 0000000..25785ee --- /dev/null +++ b/src/test/java/buttondevteam/chat/ObjectTestRunner.java @@ -0,0 +1,112 @@ +package buttondevteam.chat; + +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.runner.Runner; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.Parameterized; +import org.junit.runners.Suite; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; +import org.junit.runners.model.TestClass; + +/** + * Based on {@link Parameterized} + * + * @author NorbiPeti + * + */ +public class ObjectTestRunner extends Suite { + /** + * Annotation for a method which provides parameters to be injected into the test class constructor by Parameterized + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public static @interface Objects { + } + + private class TestClassRunnerForObjects extends BlockJUnit4ClassRunner { + + private List objectList; + private int fParameterSetNumber; + + TestClassRunnerForObjects(Class type, List objectList, int i) throws InitializationError { + super(type); + this.objectList = objectList; + fParameterSetNumber = i; + } + + @Override + public Object createTest() throws Exception { + return objectList.get(fParameterSetNumber); + } + + @Override + protected String getName() { + return String.format("[%s]", fParameterSetNumber); + } + + @Override + protected String testName(final FrameworkMethod method) { + return String.format("%s[%s]", method.getName(), fParameterSetNumber); + } + + @Override + protected void validateConstructor(List errors) { + validateOnlyOneConstructor(errors); + } + + @Override + protected Statement classBlock(RunNotifier notifier) { + return childrenInvoker(notifier); + } + + @Override + protected Annotation[] getRunnerAnnotations() { + return new Annotation[0]; + } + } + + private final ArrayList runners = new ArrayList(); + + /** + * Only called reflectively. Do not use programmatically. + */ + public ObjectTestRunner(Class klass) throws Throwable { + super(klass, Collections.emptyList()); + List objectsList = getObjectsList(getTestClass()); + for (int i = 0; i < objectsList.size(); i++) + runners.add(new TestClassRunnerForObjects(getTestClass().getJavaClass(), objectsList, i)); + } + + @Override + protected List getChildren() { + return runners; + } + + @SuppressWarnings("unchecked") + private List getObjectsList(TestClass klass) throws Throwable { + return (List) getObjectsMethod(klass).invokeExplosively(null); + } + + public static FrameworkMethod getObjectsMethod(TestClass testClass) throws Exception { + List methods = testClass.getAnnotatedMethods(Objects.class); + for (FrameworkMethod each : methods) { + int modifiers = each.getMethod().getModifiers(); + if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) + return each; + } + + throw new Exception("No public static parameters method on class " + testClass.getName()); + } +}