From 4b53a8dfea8673cba1c8dc3eab976ba18d3b3b50 Mon Sep 17 00:00:00 2001 From: "main()" Date: Sun, 11 Dec 2011 13:32:21 +0100 Subject: [PATCH] Mocking is better than implementing Mock-classes --- .../test/utils/TestCommandSender.java | 213 ------------------ .../test/utils/TestInstanceCreator.java | 20 +- 2 files changed, 19 insertions(+), 214 deletions(-) delete mode 100644 src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java deleted file mode 100644 index c7089599..00000000 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java +++ /dev/null @@ -1,213 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * Multiverse 2 is licensed under the BSD License. * - * For more information please check the README.md file included * - * with this project. * - ******************************************************************************/ - -package com.onarandombox.MultiverseCore.test.utils; - -import org.bukkit.ChatColor; -import org.bukkit.Server; -import org.bukkit.command.CommandSender; -import org.bukkit.permissions.Permission; -import org.bukkit.permissions.PermissionAttachment; -import org.bukkit.permissions.PermissionAttachmentInfo; -import org.bukkit.plugin.Plugin; - -import java.util.Set; -import java.util.logging.Logger; - -public class TestCommandSender implements CommandSender { - - private Server server; - private boolean isOp; - - static { - Logger.getLogger("CommandSender").setParent(Util.logger); - } - - private static final Logger logger = Logger.getLogger("CommandSender"); - - public TestCommandSender(Server server) { - this.server = server; - } - - /** - * Sends this sender a message - * - * @param message Message to be displayed - */ - @Override - public void sendMessage(String message) { - logger.info(ChatColor.stripColor(message)); - } - - /** - * Returns the server instance that this command is running on - * - * @return Server instance - */ - @Override - public Server getServer() { - return this.server; - } - - /** - * Gets the name of this command sender - * - * @return Name of the sender - */ - @Override - public String getName() { - return "CONSOLE"; - } - - /** - * Checks if this object contains an override for the specified permission, by fully qualified name - * - * @param name Name of the permission - * @return true if the permission is set, otherwise false - */ - @Override - public boolean isPermissionSet(String name) { - return true; - } - - /** - * Checks if this object contains an override for the specified {@link org.bukkit.permissions.Permission} - * - * @param perm Permission to check - * @return true if the permission is set, otherwise false - */ - @Override - public boolean isPermissionSet(Permission perm) { - return true; - } - - /** - * Gets the value of the specified permission, if set. - *

- * If a permission override is not set on this object, the default value of the permission will be returned. - * - * @param name Name of the permission - * @return Value of the permission - */ - @Override - public boolean hasPermission(String name) { - return true; - } - - /** - * Gets the value of the specified permission, if set. - *

- * If a permission override is not set on this object, the default value of the permission will be returned - * - * @param perm Permission to get - * @return Value of the permission - */ - @Override - public boolean hasPermission(Permission perm) { - return true; - } - - /** - * Adds a new {@link org.bukkit.permissions.PermissionAttachment} with a single permission by name and value - * - * @param plugin Plugin responsible for this attachment, may not be null or disabled - * @param name Name of the permission to attach - * @param value Value of the permission - * @return The PermissionAttachment that was just created - */ - @Override - public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) { - return null; - } - - /** - * Adds a new empty {@link org.bukkit.permissions.PermissionAttachment} to this object - * - * @param plugin Plugin responsible for this attachment, may not be null or disabled - * @return The PermissionAttachment that was just created - */ - @Override - public PermissionAttachment addAttachment(Plugin plugin) { - return null; - } - - /** - * Temporarily adds a new {@link org.bukkit.permissions.PermissionAttachment} with a single permission by name and - * value - * - * @param plugin Plugin responsible for this attachment, may not be null or disabled - * @param name Name of the permission to attach - * @param value Value of the permission - * @param ticks Amount of ticks to automatically remove this attachment after - * @return The PermissionAttachment that was just created - */ - @Override - public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) { - return null; - } - - /** - * Temporarily adds a new empty {@link org.bukkit.permissions.PermissionAttachment} to this object - * - * @param plugin Plugin responsible for this attachment, may not be null or disabled - * @param ticks Amount of ticks to automatically remove this attachment after - * @return The PermissionAttachment that was just created - */ - @Override - public PermissionAttachment addAttachment(Plugin plugin, int ticks) { - return null; - } - - /** - * Removes the given {@link org.bukkit.permissions.PermissionAttachment} from this object - * - * @param attachment Attachment to remove - * @throws IllegalArgumentException Thrown when the specified attachment isn't part of this object - */ - @Override - public void removeAttachment(PermissionAttachment attachment) { - } - - /** - * Recalculates the permissions for this object, if the attachments have changed values. - *

- * This should very rarely need to be called from a plugin. - */ - @Override - public void recalculatePermissions() { - } - - /** - * Gets a set containing all of the permissions currently in effect by this object - * - * @return Set of currently effective permissions - */ - @Override - public Set getEffectivePermissions() { - return null; - } - - /** - * Checks if this object is a server operator - * - * @return true if this is an operator, otherwise false - */ - @Override - public boolean isOp() { - return this.isOp; - } - - /** - * Sets the operator status of this object - * - * @param value New operator value - */ - @Override - public void setOp(boolean value) { - this.isOp = value; - } -} diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java index 8d395547..ce6471ef 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -20,10 +20,12 @@ import java.util.logging.Logger; import junit.framework.Assert; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.WorldCreator; import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permission; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -138,7 +140,23 @@ public class TestInstanceCreator { worldmanagerfield.set(core, wm); // Init our command sender - commandSender = spy(new TestCommandSender(mockServer)); + final Logger commandSenderLogger = Logger.getLogger("CommandSender"); + commandSenderLogger.setParent(Util.logger); + commandSender = mock(CommandSender.class); + doAnswer(new Answer() { + public Void answer(InvocationOnMock invocation) throws Throwable { + commandSenderLogger.info(ChatColor.stripColor((String) invocation.getArguments()[0])); + return null; + }}).when(commandSender).sendMessage(anyString()); + when(commandSender.getServer()).thenReturn(mockServer); + when(commandSender.getName()).thenReturn("MockCommandSender"); + when(commandSender.isPermissionSet(anyString())).thenReturn(true); + when(commandSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true); + when(commandSender.hasPermission(anyString())).thenReturn(true); + when(commandSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); + when(commandSender.addAttachment(core)).thenReturn(null); + when(commandSender.isOp()).thenReturn(true); + Bukkit.setServer(mockServer); // Load Multiverse Core