Work on the tests, they no longer fail and we mock a lot more now.

This commit is contained in:
graywolf336 2013-12-05 19:15:32 -06:00
parent 596c9de2ad
commit d66b9d3447
4 changed files with 560 additions and 546 deletions

85
.gitignore vendored
View File

@ -1,43 +1,44 @@
# Compiled source # # Compiled source #
################### ###################
*.com *.com
*.class *.class
*.dll *.dll
*.exe *.exe
*.o *.o
*.so *.so
# Packages # # Packages #
############ ############
# it's better to unpack these files and commit the raw source # it's better to unpack these files and commit the raw source
# git has its own built in compression methods # git has its own built in compression methods
*.7z *.7z
*.dmg *.dmg
*.gz *.gz
*.iso *.iso
*.jar *.jar
*.rar *.rar
*.tar *.tar
*.zip *.zip
# Logs and databases # # Logs and databases #
###################### ######################
*.log *.log
*.sql *.sql
*.sqlite *.sqlite
# OS generated files # # OS generated files #
###################### ######################
.DS_Store* .DS_Store*
ehthumbs.db ehthumbs.db
Icon? Icon?
Thumbs.db Thumbs.db
# folders # # folders #
########### ###########
bin/ bin/
target/ target/
.settings/ .settings/
.classpath .classpath
.project .project
config.yml
src/com/matejdro/ src/com/matejdro/

346
pom.xml
View File

@ -1,174 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.graywolf336</groupId> <groupId>com.graywolf336</groupId>
<artifactId>Jail</artifactId> <artifactId>Jail</artifactId>
<version>3.0.0-SNAPSHOT</version> <version>3.0.0-SNAPSHOT</version>
<name>Jail</name> <name>Jail</name>
<description>Ban too harsh? Mute too weak? Kicking not enough? Jail them! </description> <description>Ban too harsh? Mute too weak? Kicking not enough? Jail them! </description>
<url>http://dev.bukkit.org/bukkit-plugins/jail/</url> <url>http://dev.bukkit.org/bukkit-plugins/jail/</url>
<ciManagement> <ciManagement>
<url>http://ci.graywolf336.com/job/Jail/</url> <url>http://ci.graywolf336.com/job/Jail/</url>
<system>jenkins</system> <system>jenkins</system>
</ciManagement> </ciManagement>
<issueManagement> <issueManagement>
<url>http://dev.bukkit.org/bukkit-plugins/jail/tickets/</url> <url>http://dev.bukkit.org/bukkit-plugins/jail/tickets/</url>
<system>Dev Bukkit</system> <system>Dev Bukkit</system>
</issueManagement> </issueManagement>
<scm> <scm>
<url>https://github.com/multidude/Jail</url> <url>https://github.com/graywolf336/Jail</url>
<connection>scm:git:git://github.com/multidude/Jail.git</connection> <connection>scm:git:git://github.com/graywolf336/Jail.git</connection>
<developerConnection>scm:git:git:@github.com/multidude/Jail.git</developerConnection> <developerConnection>scm:git:git:@github.com/graywolf336/Jail.git</developerConnection>
</scm> </scm>
<repositories> <repositories>
<repository> <repository>
<id>vault-repo</id> <id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything</url> <url>http://ci.herocraftonline.com/plugin/repository/everything</url>
</repository> </repository>
<repository> <repository>
<id>bukkit-repo</id> <id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url> <url>http://repo.bukkit.org/content/groups/public</url>
</repository> </repository>
<repository> <repository>
<id>sk89q-repo</id> <id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url> <url>http://maven.sk89q.com/repo/</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.6.2-R0.1-SNAPSHOT</version> <version>1.6.2-R0.1-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sk89q</groupId> <groupId>com.sk89q</groupId>
<artifactId>worldedit</artifactId> <artifactId>worldedit</artifactId>
<version>5.5.7-SNAPSHOT</version> <version>5.5.7-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.milkbowl.vault</groupId> <groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId> <artifactId>Vault</artifactId>
<version>1.2.27-SNAPSHOT</version> <version>1.2.27-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<!-- Start of Test Dependencies --> <!-- Start of Test Dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.8.2</version> <version>4.8.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId> <artifactId>powermock-module-junit4</artifactId>
<version>1.4.9</version> <version>1.4.9</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId> <artifactId>powermock-api-easymock</artifactId>
<version>1.4.9</version> <version>1.4.9</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito</artifactId>
<version>1.4.9</version> <version>1.4.9</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.easymock</groupId> <groupId>org.easymock</groupId>
<artifactId>easymock</artifactId> <artifactId>easymock</artifactId>
<version>3.0</version> <version>3.0</version>
</dependency> </dependency>
<!-- End of Test Dependencies --> <!-- End of Test Dependencies -->
</dependencies> </dependencies>
<build> <build>
<directory>target</directory> <directory>target</directory>
<outputDirectory>target/classes</outputDirectory> <outputDirectory>target/classes</outputDirectory>
<!-- <sourceDirectory>${basedir}/src</sourceDirectory> --> <!-- <sourceDirectory>${basedir}/src</sourceDirectory> -->
<defaultGoal>clean install test</defaultGoal> <defaultGoal>clean install test</defaultGoal>
<resources> <resources>
<resource> <resource>
<targetPath>.</targetPath> <targetPath>.</targetPath>
<directory>${basedir}/src/main/resources/</directory> <directory>${basedir}/src/main/resources/</directory>
</resource> </resource>
</resources> </resources>
<testResources> <testResources>
<testResource> <testResource>
<directory>${basedir}/src/main/resources</directory> <directory>${basedir}/src/main/resources</directory>
</testResource> </testResource>
</testResources> </testResources>
<!-- Plugins --> <!-- Plugins -->
<plugins> <plugins>
<!-- Compile plugin --> <!-- Compile plugin -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
<configuration> <configuration>
<source>1.6</source> <source>1.6</source>
<target>1.6</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<!-- Jar Plugin --> <!-- Jar Plugin -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version> <version>2.3.1</version>
<configuration> <configuration>
<archive> <archive>
<manifestEntries /> <manifestEntries />
</archive> </archive>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version> <version>2.11</version>
<configuration> <configuration>
<parallel>methods</parallel> <parallel>methods</parallel>
<threadCount>10</threadCount> <threadCount>10</threadCount>
<excludes> <excludes>
<exclude>**/TestCommandSender.java</exclude> <exclude>**/TestCommandSender.java</exclude>
<exclude>**/TestInstanceCreator.java</exclude> <exclude>**/TestInstanceCreator.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.maven.surefire</groupId> <groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId> <artifactId>surefire-junit47</artifactId>
<version>2.11</version> <version>2.11</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>repo-release</id> <id>repo-release</id>
<name>repo.graywolf336.com Releases</name> <name>repo.graywolf336.com Releases</name>
<url>http://repo.graywolf336.com/content/repositories/releases/</url> <url>http://repo.graywolf336.com/content/repositories/releases/</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>repo-snapshot</id> <id>repo-snapshot</id>
<name>repo.graywolf336.com Snapshots</name> <name>repo.graywolf336.com Snapshots</name>
<url>http://repo.graywolf336.com/content/repositories/snapshots/</url> <url>http://repo.graywolf336.com/content/repositories/snapshots/</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
</project> </project>

View File

@ -1,280 +1,293 @@
package test.java.com.graywolf336.jail.util; package test.java.com.graywolf336.jail.util;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Assert; import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Matchers; import org.junit.Assert;
import org.mockito.invocation.InvocationOnMock; import org.mockito.Matchers;
import org.mockito.stubbing.Answer; import org.mockito.invocation.InvocationOnMock;
import org.powermock.api.mockito.PowerMockito; import org.mockito.stubbing.Answer;
import org.powermock.core.MockGateway; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.JailMain;
public class TestInstanceCreator {
private JailMain main; public class TestInstanceCreator {
private Server mockServer; private JailMain main;
private Player mockPlayer; private Server mockServer;
private CommandSender mockSender, mockPlayerSender; private Player mockPlayer;
private CommandSender mockSender, mockPlayerSender;
public static final File pluginDirectory = new File("bin/test/server/plugins/JailTest");
public static final File serverDirectory = new File("bin/test/server"); public static final File pluginDirectory = new File("bin/test/server/plugins/JailTest");
public static final File worldsDirectory = new File("bin/test/server"); public static final File serverDirectory = new File("bin/test/server");
public static final File worldsDirectory = new File("bin/test/server");
public boolean setup() {
try { public boolean setup() {
pluginDirectory.mkdirs(); try {
Assert.assertTrue(pluginDirectory.exists()); pluginDirectory.mkdirs();
Assert.assertTrue(pluginDirectory.exists());
MockGateway.MOCK_STANDARD_METHODS = false;
MockGateway.MOCK_STANDARD_METHODS = false;
main = PowerMockito.spy(new JailMain());
// Initialize the Mock server.
doReturn(pluginDirectory).when(main).getDataFolder(); mockServer = mock(Server.class);
when(mockServer.getName()).thenReturn("TestBukkit");
PluginDescriptionFile pdf = PowerMockito.spy(new PluginDescriptionFile("Jail", "3.0.0-Test", "com.graywolf336.jail.JailMain")); when(mockServer.getVersion()).thenReturn("Jail-Testing-0.0.1");
List<String> authors = new ArrayList<String>(); when(mockServer.getBukkitVersion()).thenReturn("0.0.1");
authors.add("matejdro"); Logger.getLogger("Minecraft").setParent(Util.logger);
authors.add("multidude"); when(mockServer.getLogger()).thenReturn(Util.logger);
authors.add("graywolf336"); when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
when(pdf.getAuthors()).thenReturn(authors);
doReturn(pdf).when(main).getDescription(); main = PowerMockito.spy(new JailMain());
doReturn(true).when(main).isEnabled();
doReturn(Util.logger).when(main).getLogger(); PluginDescriptionFile pdf = PowerMockito.spy(new PluginDescriptionFile("Jail", "3.0.0-Test", "com.graywolf336.jail.JailMain"));
when(pdf.getPrefix()).thenReturn("Jail");
// Add Jail to the list of loaded plugins List<String> authors = new ArrayList<String>();
JavaPlugin[] plugins = new JavaPlugin[] { main }; authors.add("matejdro");
authors.add("multidude");
// Mock the Plugin Manager authors.add("graywolf336");
PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class); doReturn(authors).when(pdf).getAuthors();
when(mockPluginManager.getPlugins()).thenReturn(plugins); doReturn(pdf).when(main).getDescription();
when(mockPluginManager.getPlugin("Jail")).thenReturn(main); doReturn(true).when(main).isEnabled();
when(mockPluginManager.getPermission(anyString())).thenReturn(null); doReturn(Util.logger).when(main).getLogger();
doReturn(mockServer).when(main).getServer();
// Initialize the Mock server. doReturn(pluginDirectory).when(main).getDataFolder();
mockServer = mock(Server.class);
when(mockServer.getName()).thenReturn("TestBukkit"); Field configFile = JavaPlugin.class.getDeclaredField("configFile");
when(mockServer.getVersion()).thenReturn("Jail-Testing-0.0.1"); configFile.setAccessible(true);
when(mockServer.getBukkitVersion()).thenReturn("0.0.1"); configFile.set(main, new File(pluginDirectory, "config.yml"));
Logger.getLogger("Minecraft").setParent(Util.logger);
when(mockServer.getLogger()).thenReturn(Util.logger); Field logger = JavaPlugin.class.getDeclaredField("logger");
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory); logger.setAccessible(true);
logger.set(main, new PluginLogger(main));
// Give the server some worlds
when(mockServer.getWorld(anyString())).thenAnswer(new Answer<World>() { doReturn(getClass().getClassLoader().getResourceAsStream("config.yml")).when(main).getResource("config.yml");
public World answer(InvocationOnMock invocation) throws Throwable {
String arg; // Add Jail to the list of loaded plugins
try { JavaPlugin[] plugins = new JavaPlugin[] { main };
arg = (String) invocation.getArguments()[0];
} catch (Exception e) { // Mock the Plugin Manager
return null; PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
} when(mockPluginManager.getPlugins()).thenReturn(plugins);
return MockWorldFactory.getWorld(arg); when(mockPluginManager.getPlugin("Jail")).thenReturn(main);
} when(mockPluginManager.getPermission(anyString())).thenReturn(null);
});
// Give the server some worlds
when(mockServer.getWorld(any(UUID.class))).thenAnswer(new Answer<World>() { when(mockServer.getWorld(anyString())).thenAnswer(new Answer<World>() {
public World answer(InvocationOnMock invocation) throws Throwable { public World answer(InvocationOnMock invocation) throws Throwable {
UUID arg; String arg;
try { try {
arg = (UUID) invocation.getArguments()[0]; arg = (String) invocation.getArguments()[0];
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
return MockWorldFactory.getWorld(arg); return MockWorldFactory.getWorld(arg);
} }
}); });
when(mockServer.getWorlds()).thenAnswer(new Answer<List<World>>() { when(mockServer.getWorld(any(UUID.class))).thenAnswer(new Answer<World>() {
public List<World> answer(InvocationOnMock invocation) throws Throwable { public World answer(InvocationOnMock invocation) throws Throwable {
return MockWorldFactory.getWorlds(); UUID arg;
} try {
}); arg = (UUID) invocation.getArguments()[0];
} catch (Exception e) {
when(mockServer.getPluginManager()).thenReturn(mockPluginManager); return null;
}
when(mockServer.createWorld(Matchers.isA(WorldCreator.class))).thenAnswer( return MockWorldFactory.getWorld(arg);
new Answer<World>() { }
public World answer(InvocationOnMock invocation) throws Throwable { });
WorldCreator arg;
try { when(mockServer.getWorlds()).thenAnswer(new Answer<List<World>>() {
arg = (WorldCreator) invocation.getArguments()[0]; public List<World> answer(InvocationOnMock invocation) throws Throwable {
} catch (Exception e) { return MockWorldFactory.getWorlds();
return null; }
} });
// Add special case for creating null worlds.
// Not sure I like doing it this way, but this is a special case when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
if (arg.name().equalsIgnoreCase("nullworld")) {
return MockWorldFactory.makeNewNullMockWorld(arg.name(), arg.environment(), arg.type()); when(mockServer.createWorld(Matchers.isA(WorldCreator.class))).thenAnswer(
} new Answer<World>() {
return MockWorldFactory.makeNewMockWorld(arg.name(), arg.environment(), arg.type()); public World answer(InvocationOnMock invocation) throws Throwable {
} WorldCreator arg;
}); try {
arg = (WorldCreator) invocation.getArguments()[0];
when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true); } catch (Exception e) {
return null;
// add mock scheduler }
BukkitScheduler mockScheduler = mock(BukkitScheduler.class); // Add special case for creating null worlds.
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())). // Not sure I like doing it this way, but this is a special case
thenAnswer(new Answer<Integer>() { if (arg.name().equalsIgnoreCase("nullworld")) {
public Integer answer(InvocationOnMock invocation) throws Throwable { return MockWorldFactory.makeNewNullMockWorld(arg.name(), arg.environment(), arg.type());
Runnable arg; }
try { return MockWorldFactory.makeNewMockWorld(arg.name(), arg.environment(), arg.type());
arg = (Runnable) invocation.getArguments()[1]; }
} catch (Exception e) { });
return null;
} when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);
arg.run();
return null; // add mock scheduler
}}); BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))). when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())).
thenAnswer(new Answer<Integer>() { thenAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable { public Integer answer(InvocationOnMock invocation) throws Throwable {
Runnable arg; Runnable arg;
try { try {
arg = (Runnable) invocation.getArguments()[1]; arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
arg.run(); arg.run();
return null; return null;
}}); }});
when(mockServer.getScheduler()).thenReturn(mockScheduler); when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))).
thenAnswer(new Answer<Integer>() {
// Set server public Integer answer(InvocationOnMock invocation) throws Throwable {
Field serverField = JavaPlugin.class.getDeclaredField("server"); Runnable arg;
serverField.setAccessible(true); try {
serverField.set(main, mockServer); arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) {
// Init our command sender return null;
final Logger commandSenderLogger = Logger.getLogger("CommandSender"); }
commandSenderLogger.setParent(Util.logger); arg.run();
mockSender = mock(CommandSender.class); return null;
doAnswer(new Answer<Void>() { }});
public Void answer(InvocationOnMock invocation) throws Throwable { when(mockServer.getScheduler()).thenReturn(mockScheduler);
commandSenderLogger.info(ChatColor.stripColor((String) invocation.getArguments()[0]));
return null; // Set server
} Field serverField = JavaPlugin.class.getDeclaredField("server");
}).when(mockSender).sendMessage(anyString()); serverField.setAccessible(true);
when(mockSender.getServer()).thenReturn(mockServer); serverField.set(main, mockServer);
when(mockSender.getName()).thenReturn("MockCommandSender");
when(mockSender.isPermissionSet(anyString())).thenReturn(true); // Init our command sender
when(mockSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true); final Logger commandSenderLogger = Logger.getLogger("CommandSender");
when(mockSender.hasPermission(anyString())).thenReturn(true); commandSenderLogger.setParent(Util.logger);
when(mockSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); mockSender = mock(CommandSender.class);
when(mockSender.addAttachment(main)).thenReturn(null); doAnswer(new Answer<Void>() {
when(mockSender.isOp()).thenReturn(true); public Void answer(InvocationOnMock invocation) throws Throwable {
commandSenderLogger.info(ChatColor.stripColor((String) invocation.getArguments()[0]));
// Init our player, who is op and who has all permissions (with name of graywolf336) return null;
mockPlayer = mock(Player.class); }
when(mockPlayer.getName()).thenReturn("graywolf336"); }).when(mockSender).sendMessage(anyString());
when(mockPlayer.getDisplayName()).thenReturn("TheGrayWolf"); when(mockSender.getServer()).thenReturn(mockServer);
when(mockPlayer.isPermissionSet(anyString())).thenReturn(true); when(mockSender.getName()).thenReturn("MockCommandSender");
when(mockPlayer.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true); when(mockSender.isPermissionSet(anyString())).thenReturn(true);
when(mockPlayer.hasPermission(anyString())).thenReturn(true); when(mockSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); when(mockSender.hasPermission(anyString())).thenReturn(true);
when(mockPlayer.isOp()).thenReturn(true); when(mockSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
when(mockSender.addAttachment(main)).thenReturn(null);
// Init our second command sender, but this time is an instance of a player when(mockSender.isOp()).thenReturn(true);
mockPlayerSender = (CommandSender) mockPlayer;
when(mockPlayerSender.getServer()).thenReturn(mockServer); // Init our player, who is op and who has all permissions (with name of graywolf336)
when(mockPlayerSender.getName()).thenReturn("graywolf336"); mockPlayer = mock(Player.class);
when(mockPlayerSender.isPermissionSet(anyString())).thenReturn(true); when(mockPlayer.getName()).thenReturn("graywolf336");
when(mockPlayerSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true); when(mockPlayer.getDisplayName()).thenReturn("TheGrayWolf");
when(mockPlayerSender.hasPermission(anyString())).thenReturn(true); when(mockPlayer.isPermissionSet(anyString())).thenReturn(true);
when(mockPlayerSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true); when(mockPlayer.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
when(mockPlayerSender.addAttachment(main)).thenReturn(null); when(mockPlayer.hasPermission(anyString())).thenReturn(true);
when(mockPlayerSender.isOp()).thenReturn(true); when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
when(mockPlayer.isOp()).thenReturn(true);
Bukkit.setServer(mockServer);
// Init our second command sender, but this time is an instance of a player
// Load Jail mockPlayerSender = (CommandSender) mockPlayer;
main.onLoad(); when(mockPlayerSender.getServer()).thenReturn(mockServer);
when(mockPlayerSender.getName()).thenReturn("graywolf336");
// Enable it when(mockPlayerSender.isPermissionSet(anyString())).thenReturn(true);
main.onEnable(); when(mockPlayerSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
when(mockPlayerSender.hasPermission(anyString())).thenReturn(true);
return true; when(mockPlayerSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
} catch (Exception e) { when(mockPlayerSender.addAttachment(main)).thenReturn(null);
e.printStackTrace(); when(mockPlayerSender.isOp()).thenReturn(true);
}
Bukkit.setServer(mockServer);
return false;
} // Load Jail
main.onLoad();
public boolean tearDown() {
try { // Enable it
Field serverField = Bukkit.class.getDeclaredField("server"); main.onEnable();
serverField.setAccessible(true);
serverField.set(Class.forName("org.bukkit.Bukkit"), null); return true;
} catch (Exception e) { } catch (Exception e) {
Util.log(Level.SEVERE, "Error while trying to unregister the server from Bukkit. Has Bukkit changed?"); e.printStackTrace();
e.printStackTrace(); }
Assert.fail(e.getMessage());
return false; return false;
} }
main.onDisable(); public boolean tearDown() {
try {
deleteFolder(serverDirectory); Field serverField = Bukkit.class.getDeclaredField("server");
serverField.setAccessible(true);
return true; serverField.set(Class.forName("org.bukkit.Bukkit"), null);
} } catch (Exception e) {
Util.log(Level.SEVERE, "Error while trying to unregister the server from Bukkit. Has Bukkit changed?");
public JailMain getMain() { e.printStackTrace();
return this.main; Assert.fail(e.getMessage());
} return false;
}
public Server getServer() {
return this.mockServer; main.onDisable();
}
public CommandSender getCommandSender() { deleteFolder(serverDirectory);
return this.mockSender;
} return true;
}
public Player getPlayer() {
return this.mockPlayer; public JailMain getMain() {
} return this.main;
}
public CommandSender getPlayerCommandSender() {
return this.mockPlayerSender; public Server getServer() {
} return this.mockServer;
}
private void deleteFolder(File folder) {
File[] files = folder.listFiles(); public CommandSender getCommandSender() {
if(files != null) { return this.mockSender;
for(File f: files) { }
if(f.isDirectory()) {
deleteFolder(f); public Player getPlayer() {
}else { return this.mockPlayer;
f.delete(); }
}
} public CommandSender getPlayerCommandSender() {
} return this.mockPlayerSender;
}
folder.delete();
} private void deleteFolder(File folder) {
} File[] files = folder.listFiles();
if(files != null) {
for(File f: files) {
if(f.isDirectory()) {
deleteFolder(f);
}else {
f.delete();
}
}
}
folder.delete();
}
}

View File

@ -1,52 +1,52 @@
package test.java.com.graywolf336.jail.util; package test.java.com.graywolf336.jail.util;
import java.util.logging.ConsoleHandler; import java.util.logging.ConsoleHandler;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
public class Util { public class Util {
private Util() {} private Util() {}
public static final Logger logger = Logger.getLogger("Jail-Test"); public static final Logger logger = Logger.getLogger("Jail-Test");
static { static {
logger.setUseParentHandlers(false); logger.setUseParentHandlers(false);
Handler handler = new ConsoleHandler(); Handler handler = new ConsoleHandler();
handler.setFormatter(new TestLogFormatter()); handler.setFormatter(new TestLogFormatter());
Handler[] handlers = logger.getHandlers(); Handler[] handlers = logger.getHandlers();
for (Handler h : handlers) for (Handler h : handlers)
logger.removeHandler(h); logger.removeHandler(h);
logger.addHandler(handler); logger.addHandler(handler);
} }
public static void log(Throwable t) { public static void log(Throwable t) {
log(Level.WARNING, t.getLocalizedMessage(), t); log(Level.WARNING, t.getLocalizedMessage(), t);
} }
public static void log(Level level, Throwable t) { public static void log(Level level, Throwable t) {
log(level, t.getLocalizedMessage(), t); log(level, t.getLocalizedMessage(), t);
} }
public static void log(String message, Throwable t) { public static void log(String message, Throwable t) {
log(Level.WARNING, message, t); log(Level.WARNING, message, t);
} }
public static void log(Level level, String message, Throwable t) { public static void log(Level level, String message, Throwable t) {
LogRecord record = new LogRecord(level, message); LogRecord record = new LogRecord(level, message);
record.setThrown(t); record.setThrown(t);
logger.log(record); logger.log(record);
} }
public static void log(String message) { public static void log(String message) {
log(Level.INFO, message); log(Level.INFO, message);
} }
public static void log(Level level, String message) { public static void log(Level level, String message) {
logger.log(level, message); logger.log(level, message);
} }
} }