Work on the tests, they no longer fail and we mock a lot more now.
This commit is contained in:
parent
596c9de2ad
commit
d66b9d3447
85
.gitignore
vendored
85
.gitignore
vendored
@ -1,43 +1,44 @@
|
||||
# Compiled source #
|
||||
###################
|
||||
*.com
|
||||
*.class
|
||||
*.dll
|
||||
*.exe
|
||||
*.o
|
||||
*.so
|
||||
|
||||
# Packages #
|
||||
############
|
||||
# it's better to unpack these files and commit the raw source
|
||||
# git has its own built in compression methods
|
||||
*.7z
|
||||
*.dmg
|
||||
*.gz
|
||||
*.iso
|
||||
*.jar
|
||||
*.rar
|
||||
*.tar
|
||||
*.zip
|
||||
|
||||
# Logs and databases #
|
||||
######################
|
||||
*.log
|
||||
*.sql
|
||||
*.sqlite
|
||||
|
||||
# OS generated files #
|
||||
######################
|
||||
.DS_Store*
|
||||
ehthumbs.db
|
||||
Icon?
|
||||
Thumbs.db
|
||||
|
||||
# folders #
|
||||
###########
|
||||
bin/
|
||||
target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
# Compiled source #
|
||||
###################
|
||||
*.com
|
||||
*.class
|
||||
*.dll
|
||||
*.exe
|
||||
*.o
|
||||
*.so
|
||||
|
||||
# Packages #
|
||||
############
|
||||
# it's better to unpack these files and commit the raw source
|
||||
# git has its own built in compression methods
|
||||
*.7z
|
||||
*.dmg
|
||||
*.gz
|
||||
*.iso
|
||||
*.jar
|
||||
*.rar
|
||||
*.tar
|
||||
*.zip
|
||||
|
||||
# Logs and databases #
|
||||
######################
|
||||
*.log
|
||||
*.sql
|
||||
*.sqlite
|
||||
|
||||
# OS generated files #
|
||||
######################
|
||||
.DS_Store*
|
||||
ehthumbs.db
|
||||
Icon?
|
||||
Thumbs.db
|
||||
|
||||
# folders #
|
||||
###########
|
||||
bin/
|
||||
target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
config.yml
|
||||
src/com/matejdro/
|
346
pom.xml
346
pom.xml
@ -1,174 +1,174 @@
|
||||
<?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"
|
||||
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>
|
||||
<groupId>com.graywolf336</groupId>
|
||||
<artifactId>Jail</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
<name>Jail</name>
|
||||
<description>Ban too harsh? Mute too weak? Kicking not enough? Jail them! </description>
|
||||
<url>http://dev.bukkit.org/bukkit-plugins/jail/</url>
|
||||
<ciManagement>
|
||||
<url>http://ci.graywolf336.com/job/Jail/</url>
|
||||
<system>jenkins</system>
|
||||
</ciManagement>
|
||||
<issueManagement>
|
||||
<url>http://dev.bukkit.org/bukkit-plugins/jail/tickets/</url>
|
||||
<system>Dev Bukkit</system>
|
||||
</issueManagement>
|
||||
<scm>
|
||||
<url>https://github.com/multidude/Jail</url>
|
||||
<connection>scm:git:git://github.com/multidude/Jail.git</connection>
|
||||
<developerConnection>scm:git:git:@github.com/multidude/Jail.git</developerConnection>
|
||||
</scm>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>vault-repo</id>
|
||||
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>bukkit-repo</id>
|
||||
<url>http://repo.bukkit.org/content/groups/public</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>sk89q-repo</id>
|
||||
<url>http://maven.sk89q.com/repo/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.6.2-R0.1-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sk89q</groupId>
|
||||
<artifactId>worldedit</artifactId>
|
||||
<version>5.5.7-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.milkbowl.vault</groupId>
|
||||
<artifactId>Vault</artifactId>
|
||||
<version>1.2.27-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
|
||||
<!-- Start of Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-easymock</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
<!-- End of Test Dependencies -->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<directory>target</directory>
|
||||
<outputDirectory>target/classes</outputDirectory>
|
||||
<!-- <sourceDirectory>${basedir}/src</sourceDirectory> -->
|
||||
<defaultGoal>clean install test</defaultGoal>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<targetPath>.</targetPath>
|
||||
<directory>${basedir}/src/main/resources/</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>${basedir}/src/main/resources</directory>
|
||||
</testResource>
|
||||
</testResources>
|
||||
|
||||
<!-- Plugins -->
|
||||
<plugins>
|
||||
<!-- Compile plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Jar Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries />
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.11</version>
|
||||
<configuration>
|
||||
<parallel>methods</parallel>
|
||||
<threadCount>10</threadCount>
|
||||
<excludes>
|
||||
<exclude>**/TestCommandSender.java</exclude>
|
||||
<exclude>**/TestInstanceCreator.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-junit47</artifactId>
|
||||
<version>2.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>repo-release</id>
|
||||
<name>repo.graywolf336.com Releases</name>
|
||||
<url>http://repo.graywolf336.com/content/repositories/releases/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>repo-snapshot</id>
|
||||
<name>repo.graywolf336.com Snapshots</name>
|
||||
<url>http://repo.graywolf336.com/content/repositories/snapshots/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<?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"
|
||||
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>
|
||||
<groupId>com.graywolf336</groupId>
|
||||
<artifactId>Jail</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
<name>Jail</name>
|
||||
<description>Ban too harsh? Mute too weak? Kicking not enough? Jail them! </description>
|
||||
<url>http://dev.bukkit.org/bukkit-plugins/jail/</url>
|
||||
<ciManagement>
|
||||
<url>http://ci.graywolf336.com/job/Jail/</url>
|
||||
<system>jenkins</system>
|
||||
</ciManagement>
|
||||
<issueManagement>
|
||||
<url>http://dev.bukkit.org/bukkit-plugins/jail/tickets/</url>
|
||||
<system>Dev Bukkit</system>
|
||||
</issueManagement>
|
||||
<scm>
|
||||
<url>https://github.com/graywolf336/Jail</url>
|
||||
<connection>scm:git:git://github.com/graywolf336/Jail.git</connection>
|
||||
<developerConnection>scm:git:git:@github.com/graywolf336/Jail.git</developerConnection>
|
||||
</scm>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>vault-repo</id>
|
||||
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>bukkit-repo</id>
|
||||
<url>http://repo.bukkit.org/content/groups/public</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<id>sk89q-repo</id>
|
||||
<url>http://maven.sk89q.com/repo/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.6.2-R0.1-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sk89q</groupId>
|
||||
<artifactId>worldedit</artifactId>
|
||||
<version>5.5.7-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.milkbowl.vault</groupId>
|
||||
<artifactId>Vault</artifactId>
|
||||
<version>1.2.27-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
|
||||
<!-- Start of Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-easymock</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>1.4.9</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
<!-- End of Test Dependencies -->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<directory>target</directory>
|
||||
<outputDirectory>target/classes</outputDirectory>
|
||||
<!-- <sourceDirectory>${basedir}/src</sourceDirectory> -->
|
||||
<defaultGoal>clean install test</defaultGoal>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<targetPath>.</targetPath>
|
||||
<directory>${basedir}/src/main/resources/</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>${basedir}/src/main/resources</directory>
|
||||
</testResource>
|
||||
</testResources>
|
||||
|
||||
<!-- Plugins -->
|
||||
<plugins>
|
||||
<!-- Compile plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Jar Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries />
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.11</version>
|
||||
<configuration>
|
||||
<parallel>methods</parallel>
|
||||
<threadCount>10</threadCount>
|
||||
<excludes>
|
||||
<exclude>**/TestCommandSender.java</exclude>
|
||||
<exclude>**/TestInstanceCreator.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-junit47</artifactId>
|
||||
<version>2.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>repo-release</id>
|
||||
<name>repo.graywolf336.com Releases</name>
|
||||
<url>http://repo.graywolf336.com/content/repositories/releases/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>repo-snapshot</id>
|
||||
<name>repo.graywolf336.com Snapshots</name>
|
||||
<url>http://repo.graywolf336.com/content/repositories/snapshots/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
</project>
|
@ -1,280 +1,293 @@
|
||||
package test.java.com.graywolf336.jail.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
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.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.junit.Assert;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.MockGateway;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.graywolf336.jail.JailMain;
|
||||
|
||||
public class TestInstanceCreator {
|
||||
private JailMain main;
|
||||
private Server mockServer;
|
||||
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 worldsDirectory = new File("bin/test/server");
|
||||
|
||||
public boolean setup() {
|
||||
try {
|
||||
pluginDirectory.mkdirs();
|
||||
Assert.assertTrue(pluginDirectory.exists());
|
||||
|
||||
MockGateway.MOCK_STANDARD_METHODS = false;
|
||||
|
||||
main = PowerMockito.spy(new JailMain());
|
||||
|
||||
doReturn(pluginDirectory).when(main).getDataFolder();
|
||||
|
||||
PluginDescriptionFile pdf = PowerMockito.spy(new PluginDescriptionFile("Jail", "3.0.0-Test", "com.graywolf336.jail.JailMain"));
|
||||
List<String> authors = new ArrayList<String>();
|
||||
authors.add("matejdro");
|
||||
authors.add("multidude");
|
||||
authors.add("graywolf336");
|
||||
when(pdf.getAuthors()).thenReturn(authors);
|
||||
doReturn(pdf).when(main).getDescription();
|
||||
doReturn(true).when(main).isEnabled();
|
||||
doReturn(Util.logger).when(main).getLogger();
|
||||
|
||||
// Add Jail to the list of loaded plugins
|
||||
JavaPlugin[] plugins = new JavaPlugin[] { main };
|
||||
|
||||
// Mock the Plugin Manager
|
||||
PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
|
||||
when(mockPluginManager.getPlugins()).thenReturn(plugins);
|
||||
when(mockPluginManager.getPlugin("Jail")).thenReturn(main);
|
||||
when(mockPluginManager.getPermission(anyString())).thenReturn(null);
|
||||
|
||||
// Initialize the Mock server.
|
||||
mockServer = mock(Server.class);
|
||||
when(mockServer.getName()).thenReturn("TestBukkit");
|
||||
when(mockServer.getVersion()).thenReturn("Jail-Testing-0.0.1");
|
||||
when(mockServer.getBukkitVersion()).thenReturn("0.0.1");
|
||||
Logger.getLogger("Minecraft").setParent(Util.logger);
|
||||
when(mockServer.getLogger()).thenReturn(Util.logger);
|
||||
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
|
||||
|
||||
// Give the server some worlds
|
||||
when(mockServer.getWorld(anyString())).thenAnswer(new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
String arg;
|
||||
try {
|
||||
arg = (String) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return MockWorldFactory.getWorld(arg);
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getWorld(any(UUID.class))).thenAnswer(new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
UUID arg;
|
||||
try {
|
||||
arg = (UUID) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return MockWorldFactory.getWorld(arg);
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getWorlds()).thenAnswer(new Answer<List<World>>() {
|
||||
public List<World> answer(InvocationOnMock invocation) throws Throwable {
|
||||
return MockWorldFactory.getWorlds();
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
|
||||
|
||||
when(mockServer.createWorld(Matchers.isA(WorldCreator.class))).thenAnswer(
|
||||
new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
WorldCreator arg;
|
||||
try {
|
||||
arg = (WorldCreator) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
// Add special case for creating null worlds.
|
||||
// Not sure I like doing it this way, but this is a special case
|
||||
if (arg.name().equalsIgnoreCase("nullworld")) {
|
||||
return MockWorldFactory.makeNewNullMockWorld(arg.name(), arg.environment(), arg.type());
|
||||
}
|
||||
return MockWorldFactory.makeNewMockWorld(arg.name(), arg.environment(), arg.type());
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);
|
||||
|
||||
// add mock scheduler
|
||||
BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
|
||||
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())).
|
||||
thenAnswer(new Answer<Integer>() {
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
Runnable arg;
|
||||
try {
|
||||
arg = (Runnable) invocation.getArguments()[1];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
arg.run();
|
||||
return null;
|
||||
}});
|
||||
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))).
|
||||
thenAnswer(new Answer<Integer>() {
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
Runnable arg;
|
||||
try {
|
||||
arg = (Runnable) invocation.getArguments()[1];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
arg.run();
|
||||
return null;
|
||||
}});
|
||||
when(mockServer.getScheduler()).thenReturn(mockScheduler);
|
||||
|
||||
// Set server
|
||||
Field serverField = JavaPlugin.class.getDeclaredField("server");
|
||||
serverField.setAccessible(true);
|
||||
serverField.set(main, mockServer);
|
||||
|
||||
// Init our command sender
|
||||
final Logger commandSenderLogger = Logger.getLogger("CommandSender");
|
||||
commandSenderLogger.setParent(Util.logger);
|
||||
mockSender = mock(CommandSender.class);
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
commandSenderLogger.info(ChatColor.stripColor((String) invocation.getArguments()[0]));
|
||||
return null;
|
||||
}
|
||||
}).when(mockSender).sendMessage(anyString());
|
||||
when(mockSender.getServer()).thenReturn(mockServer);
|
||||
when(mockSender.getName()).thenReturn("MockCommandSender");
|
||||
when(mockSender.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockSender.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockSender.addAttachment(main)).thenReturn(null);
|
||||
when(mockSender.isOp()).thenReturn(true);
|
||||
|
||||
// Init our player, who is op and who has all permissions (with name of graywolf336)
|
||||
mockPlayer = mock(Player.class);
|
||||
when(mockPlayer.getName()).thenReturn("graywolf336");
|
||||
when(mockPlayer.getDisplayName()).thenReturn("TheGrayWolf");
|
||||
when(mockPlayer.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockPlayer.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayer.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayer.isOp()).thenReturn(true);
|
||||
|
||||
// Init our second command sender, but this time is an instance of a player
|
||||
mockPlayerSender = (CommandSender) mockPlayer;
|
||||
when(mockPlayerSender.getServer()).thenReturn(mockServer);
|
||||
when(mockPlayerSender.getName()).thenReturn("graywolf336");
|
||||
when(mockPlayerSender.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockPlayerSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayerSender.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockPlayerSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayerSender.addAttachment(main)).thenReturn(null);
|
||||
when(mockPlayerSender.isOp()).thenReturn(true);
|
||||
|
||||
Bukkit.setServer(mockServer);
|
||||
|
||||
// Load Jail
|
||||
main.onLoad();
|
||||
|
||||
// Enable it
|
||||
main.onEnable();
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean tearDown() {
|
||||
try {
|
||||
Field serverField = Bukkit.class.getDeclaredField("server");
|
||||
serverField.setAccessible(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?");
|
||||
e.printStackTrace();
|
||||
Assert.fail(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
main.onDisable();
|
||||
|
||||
deleteFolder(serverDirectory);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public JailMain getMain() {
|
||||
return this.main;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return this.mockServer;
|
||||
}
|
||||
|
||||
public CommandSender getCommandSender() {
|
||||
return this.mockSender;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.mockPlayer;
|
||||
}
|
||||
|
||||
public CommandSender getPlayerCommandSender() {
|
||||
return this.mockPlayerSender;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
package test.java.com.graywolf336.jail.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
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.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginLogger;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.junit.Assert;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.MockGateway;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.graywolf336.jail.JailMain;
|
||||
|
||||
public class TestInstanceCreator {
|
||||
private JailMain main;
|
||||
private Server mockServer;
|
||||
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 worldsDirectory = new File("bin/test/server");
|
||||
|
||||
public boolean setup() {
|
||||
try {
|
||||
pluginDirectory.mkdirs();
|
||||
Assert.assertTrue(pluginDirectory.exists());
|
||||
|
||||
MockGateway.MOCK_STANDARD_METHODS = false;
|
||||
|
||||
// Initialize the Mock server.
|
||||
mockServer = mock(Server.class);
|
||||
when(mockServer.getName()).thenReturn("TestBukkit");
|
||||
when(mockServer.getVersion()).thenReturn("Jail-Testing-0.0.1");
|
||||
when(mockServer.getBukkitVersion()).thenReturn("0.0.1");
|
||||
Logger.getLogger("Minecraft").setParent(Util.logger);
|
||||
when(mockServer.getLogger()).thenReturn(Util.logger);
|
||||
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
|
||||
|
||||
main = PowerMockito.spy(new JailMain());
|
||||
|
||||
PluginDescriptionFile pdf = PowerMockito.spy(new PluginDescriptionFile("Jail", "3.0.0-Test", "com.graywolf336.jail.JailMain"));
|
||||
when(pdf.getPrefix()).thenReturn("Jail");
|
||||
List<String> authors = new ArrayList<String>();
|
||||
authors.add("matejdro");
|
||||
authors.add("multidude");
|
||||
authors.add("graywolf336");
|
||||
doReturn(authors).when(pdf).getAuthors();
|
||||
doReturn(pdf).when(main).getDescription();
|
||||
doReturn(true).when(main).isEnabled();
|
||||
doReturn(Util.logger).when(main).getLogger();
|
||||
doReturn(mockServer).when(main).getServer();
|
||||
doReturn(pluginDirectory).when(main).getDataFolder();
|
||||
|
||||
Field configFile = JavaPlugin.class.getDeclaredField("configFile");
|
||||
configFile.setAccessible(true);
|
||||
configFile.set(main, new File(pluginDirectory, "config.yml"));
|
||||
|
||||
Field logger = JavaPlugin.class.getDeclaredField("logger");
|
||||
logger.setAccessible(true);
|
||||
logger.set(main, new PluginLogger(main));
|
||||
|
||||
doReturn(getClass().getClassLoader().getResourceAsStream("config.yml")).when(main).getResource("config.yml");
|
||||
|
||||
// Add Jail to the list of loaded plugins
|
||||
JavaPlugin[] plugins = new JavaPlugin[] { main };
|
||||
|
||||
// Mock the Plugin Manager
|
||||
PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
|
||||
when(mockPluginManager.getPlugins()).thenReturn(plugins);
|
||||
when(mockPluginManager.getPlugin("Jail")).thenReturn(main);
|
||||
when(mockPluginManager.getPermission(anyString())).thenReturn(null);
|
||||
|
||||
// Give the server some worlds
|
||||
when(mockServer.getWorld(anyString())).thenAnswer(new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
String arg;
|
||||
try {
|
||||
arg = (String) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return MockWorldFactory.getWorld(arg);
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getWorld(any(UUID.class))).thenAnswer(new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
UUID arg;
|
||||
try {
|
||||
arg = (UUID) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return MockWorldFactory.getWorld(arg);
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getWorlds()).thenAnswer(new Answer<List<World>>() {
|
||||
public List<World> answer(InvocationOnMock invocation) throws Throwable {
|
||||
return MockWorldFactory.getWorlds();
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
|
||||
|
||||
when(mockServer.createWorld(Matchers.isA(WorldCreator.class))).thenAnswer(
|
||||
new Answer<World>() {
|
||||
public World answer(InvocationOnMock invocation) throws Throwable {
|
||||
WorldCreator arg;
|
||||
try {
|
||||
arg = (WorldCreator) invocation.getArguments()[0];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
// Add special case for creating null worlds.
|
||||
// Not sure I like doing it this way, but this is a special case
|
||||
if (arg.name().equalsIgnoreCase("nullworld")) {
|
||||
return MockWorldFactory.makeNewNullMockWorld(arg.name(), arg.environment(), arg.type());
|
||||
}
|
||||
return MockWorldFactory.makeNewMockWorld(arg.name(), arg.environment(), arg.type());
|
||||
}
|
||||
});
|
||||
|
||||
when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);
|
||||
|
||||
// add mock scheduler
|
||||
BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
|
||||
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())).
|
||||
thenAnswer(new Answer<Integer>() {
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
Runnable arg;
|
||||
try {
|
||||
arg = (Runnable) invocation.getArguments()[1];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
arg.run();
|
||||
return null;
|
||||
}});
|
||||
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))).
|
||||
thenAnswer(new Answer<Integer>() {
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
Runnable arg;
|
||||
try {
|
||||
arg = (Runnable) invocation.getArguments()[1];
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
arg.run();
|
||||
return null;
|
||||
}});
|
||||
when(mockServer.getScheduler()).thenReturn(mockScheduler);
|
||||
|
||||
// Set server
|
||||
Field serverField = JavaPlugin.class.getDeclaredField("server");
|
||||
serverField.setAccessible(true);
|
||||
serverField.set(main, mockServer);
|
||||
|
||||
// Init our command sender
|
||||
final Logger commandSenderLogger = Logger.getLogger("CommandSender");
|
||||
commandSenderLogger.setParent(Util.logger);
|
||||
mockSender = mock(CommandSender.class);
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
commandSenderLogger.info(ChatColor.stripColor((String) invocation.getArguments()[0]));
|
||||
return null;
|
||||
}
|
||||
}).when(mockSender).sendMessage(anyString());
|
||||
when(mockSender.getServer()).thenReturn(mockServer);
|
||||
when(mockSender.getName()).thenReturn("MockCommandSender");
|
||||
when(mockSender.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockSender.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockSender.addAttachment(main)).thenReturn(null);
|
||||
when(mockSender.isOp()).thenReturn(true);
|
||||
|
||||
// Init our player, who is op and who has all permissions (with name of graywolf336)
|
||||
mockPlayer = mock(Player.class);
|
||||
when(mockPlayer.getName()).thenReturn("graywolf336");
|
||||
when(mockPlayer.getDisplayName()).thenReturn("TheGrayWolf");
|
||||
when(mockPlayer.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockPlayer.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayer.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockPlayer.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayer.isOp()).thenReturn(true);
|
||||
|
||||
// Init our second command sender, but this time is an instance of a player
|
||||
mockPlayerSender = (CommandSender) mockPlayer;
|
||||
when(mockPlayerSender.getServer()).thenReturn(mockServer);
|
||||
when(mockPlayerSender.getName()).thenReturn("graywolf336");
|
||||
when(mockPlayerSender.isPermissionSet(anyString())).thenReturn(true);
|
||||
when(mockPlayerSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayerSender.hasPermission(anyString())).thenReturn(true);
|
||||
when(mockPlayerSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
|
||||
when(mockPlayerSender.addAttachment(main)).thenReturn(null);
|
||||
when(mockPlayerSender.isOp()).thenReturn(true);
|
||||
|
||||
Bukkit.setServer(mockServer);
|
||||
|
||||
// Load Jail
|
||||
main.onLoad();
|
||||
|
||||
// Enable it
|
||||
main.onEnable();
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean tearDown() {
|
||||
try {
|
||||
Field serverField = Bukkit.class.getDeclaredField("server");
|
||||
serverField.setAccessible(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?");
|
||||
e.printStackTrace();
|
||||
Assert.fail(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
main.onDisable();
|
||||
|
||||
|
||||
deleteFolder(serverDirectory);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public JailMain getMain() {
|
||||
return this.main;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return this.mockServer;
|
||||
}
|
||||
|
||||
public CommandSender getCommandSender() {
|
||||
return this.mockSender;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.mockPlayer;
|
||||
}
|
||||
|
||||
public CommandSender getPlayerCommandSender() {
|
||||
return this.mockPlayerSender;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +1,52 @@
|
||||
package test.java.com.graywolf336.jail.util;
|
||||
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Util {
|
||||
private Util() {}
|
||||
|
||||
public static final Logger logger = Logger.getLogger("Jail-Test");
|
||||
|
||||
static {
|
||||
logger.setUseParentHandlers(false);
|
||||
|
||||
Handler handler = new ConsoleHandler();
|
||||
handler.setFormatter(new TestLogFormatter());
|
||||
Handler[] handlers = logger.getHandlers();
|
||||
|
||||
for (Handler h : handlers)
|
||||
logger.removeHandler(h);
|
||||
|
||||
logger.addHandler(handler);
|
||||
}
|
||||
|
||||
public static void log(Throwable t) {
|
||||
log(Level.WARNING, t.getLocalizedMessage(), t);
|
||||
}
|
||||
|
||||
public static void log(Level level, Throwable t) {
|
||||
log(level, t.getLocalizedMessage(), t);
|
||||
}
|
||||
|
||||
public static void log(String message, Throwable t) {
|
||||
log(Level.WARNING, message, t);
|
||||
}
|
||||
|
||||
public static void log(Level level, String message, Throwable t) {
|
||||
LogRecord record = new LogRecord(level, message);
|
||||
record.setThrown(t);
|
||||
logger.log(record);
|
||||
}
|
||||
|
||||
public static void log(String message) {
|
||||
log(Level.INFO, message);
|
||||
}
|
||||
|
||||
public static void log(Level level, String message) {
|
||||
logger.log(level, message);
|
||||
}
|
||||
package test.java.com.graywolf336.jail.util;
|
||||
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Util {
|
||||
private Util() {}
|
||||
|
||||
public static final Logger logger = Logger.getLogger("Jail-Test");
|
||||
|
||||
static {
|
||||
logger.setUseParentHandlers(false);
|
||||
|
||||
Handler handler = new ConsoleHandler();
|
||||
handler.setFormatter(new TestLogFormatter());
|
||||
Handler[] handlers = logger.getHandlers();
|
||||
|
||||
for (Handler h : handlers)
|
||||
logger.removeHandler(h);
|
||||
|
||||
logger.addHandler(handler);
|
||||
}
|
||||
|
||||
public static void log(Throwable t) {
|
||||
log(Level.WARNING, t.getLocalizedMessage(), t);
|
||||
}
|
||||
|
||||
public static void log(Level level, Throwable t) {
|
||||
log(level, t.getLocalizedMessage(), t);
|
||||
}
|
||||
|
||||
public static void log(String message, Throwable t) {
|
||||
log(Level.WARNING, message, t);
|
||||
}
|
||||
|
||||
public static void log(Level level, String message, Throwable t) {
|
||||
LogRecord record = new LogRecord(level, message);
|
||||
record.setThrown(t);
|
||||
logger.log(record);
|
||||
}
|
||||
|
||||
public static void log(String message) {
|
||||
log(Level.INFO, message);
|
||||
}
|
||||
|
||||
public static void log(Level level, String message) {
|
||||
logger.log(level, message);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user