More mock & test fixes

This commit is contained in:
Rsl1122 2017-04-04 21:31:13 +03:00
parent 39ed2de9dd
commit af8616a208
7 changed files with 126 additions and 35 deletions

3
.gitignore vendored
View File

@ -4,7 +4,8 @@
/Plan Lite/build/
/Plan Lite/dist/
/Plan Lite/nbproject/private/
/Plan/target/
/Plan/nbproject/
/Plan/target/
/Plan/temporaryTestFolder/
/Debugger/nbproject/private/
/PlanDebugger/nbproject/private/

View File

@ -9,6 +9,7 @@ import main.java.com.djrapitops.plan.Phrase;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock;
import org.junit.Test;

View File

@ -5,6 +5,8 @@
*/
package test.java.main.java.com.djrapitops.plan.data;
import org.bukkit.OfflinePlayer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
@ -24,7 +26,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -41,6 +42,7 @@ import test.java.utils.TestInit;
public class UserDataTest {
private UserData test;
private Plan plan;
public UserDataTest() {
}
@ -49,11 +51,12 @@ public class UserDataTest {
public void setUp() {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock();
plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
DemographicsData demData = new DemographicsData();
@ -304,6 +307,38 @@ public class UserDataTest {
assertTrue("Not copied properly", test.equals(copy));
}
@Test
public void testPlayerConstructor() {
test = new UserData(MockUtils.mockPlayer(), new DemographicsData());
UserData expected = new UserData(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"), 1234567L, new Location(MockUtils.mockWorld(), 0, 0, 0), true, GameMode.SURVIVAL, new DemographicsData(), "TestName", true);
expected.updateBanned(true);
assertTrue("Not equal!", test.equals(expected));
}
@Test
public void testPlayerConstructorBrokenBanned() throws IOException {
test = new UserData(MockUtils.mockBrokenPlayer(), new DemographicsData());
UserData expected = new UserData(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"), 1234567L, new Location(MockUtils.mockWorld(), 0, 0, 0), true, GameMode.SURVIVAL, new DemographicsData(), "TestName", true);
expected.updateBanned(false);
assertTrue("Not equal!", test.equals(expected));
}
@Test
public void testOfflinePlayerConstructor() {
test = new UserData((OfflinePlayer) MockUtils.mockPlayer(), new DemographicsData());
UserData expected = new UserData(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"), 1234567L, new Location(MockUtils.mockWorld(), 0, 0, 0), true, GameMode.SURVIVAL, new DemographicsData(), "TestName", true);
expected.updateBanned(true);
assertTrue("Not equal!", test.equals(expected));
}
@Test
public void testOfflinePlayerConstructorBrokenBanned() throws IOException {
test = new UserData((OfflinePlayer) MockUtils.mockBrokenPlayer(), new DemographicsData());
UserData expected = new UserData(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"), 1234567L, new Location(MockUtils.mockWorld(), 0, 0, 0), true, GameMode.SURVIVAL, new DemographicsData(), "TestName", true);
expected.updateBanned(false);
assertTrue("Not equal!", test.equals(expected));
}
@Test
public void testGetUUID() {
assertEquals(test.getUuid(), UUID.fromString("7f8149a0-b5a5-4fcd-80b5-6cff083a99f1"));

View File

@ -20,12 +20,25 @@ import org.junit.Test;
import org.powermock.api.easymock.PowerMock;
import test.java.utils.TestInit;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.*;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.bukkit.scheduler.BukkitScheduler;
import org.easymock.EasyMock;
import static org.easymock.EasyMock.anyLong;
import org.junit.Ignore;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;
/**
*
* @author Risto
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class, Bukkit.class, BukkitScheduler.class, BukkitRunnable.class})
public class DatabaseTest {
private Plan plan;
@ -35,21 +48,26 @@ public class DatabaseTest {
}
@Before
public void setUp() throws IOException {
public void setUp() throws IOException, Exception {
TestInit t = new TestInit();
assertTrue("Not set up", t.setUp());
plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
PowerMock.mockStatic(Bukkit.class);
PowerMock.replay(Bukkit.class);
// EasyMock.expect(Bukkit.getScheduler()).andReturn();
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = 0;
if (f.exists()) {
rows = Files.readLines(f, Charset.defaultCharset()).size();
}
BukkitRunnable mockRunnable = PowerMockito.mock(BukkitRunnable.class);
when(mockRunnable.runTaskTimerAsynchronously(plan, anyLong(), anyLong())).thenReturn(null);
whenNew(BukkitRunnable.class).withNoArguments().thenReturn(mockRunnable);
PowerMock.mockStatic(Bukkit.class);
// PowerMock.replay(Bukkit.class);
BukkitScheduler mockScheduler = Mockito.mock(BukkitScheduler.class);
EasyMock.expect(Bukkit.getScheduler()).andReturn(mockScheduler);
}
@After
@ -62,7 +80,7 @@ public class DatabaseTest {
assertTrue("Errors were caught.", rows == rowsAgain);
}
@Ignore("Mock scheduler") @Test
@Ignore @Test
public void testInit() {
Database db = new SQLiteDB(plan, "debug.db");
assertTrue("Database failed to init.", db.init());

View File

@ -40,6 +40,8 @@ public class HtmlUtilsTest {
PowerMock.mockStatic(JavaPlugin.class);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
EasyMock.expect(JavaPlugin.getPlugin(Plan.class)).andReturn(plan);
PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class);
}
@ -72,21 +74,19 @@ public class HtmlUtilsTest {
assertEquals(result, exp);
}
@Ignore("Mock Server.getIp") @Test
@Test
public void testGetServerAnalysisUrl() throws FileNotFoundException {
String result = HtmlUtils.getServerAnalysisUrl();
String exp = "";
assertEquals(result, exp);
String exp = "http://0.0.0.0:8804/bAkEd/server";
assertEquals(exp, result);
}
@Ignore("Mock Server.getIp") @Test
@Test
public void testGetInspectUrl() {
String playerName = "";
String expResult = "";
String playerName = "Test";
String expResult = "http://0.0.0.0:8804/bAkEd/player/Test";
String result = HtmlUtils.getInspectUrl(playerName);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
@Test

View File

@ -1,17 +1,51 @@
package test.java.utils;
import java.util.UUID;
import org.bukkit.World;
import org.mockito.Mockito;
import org.bukkit.entity.Player;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.when;
/**
*
* @author Rsl1122
*/
public class MockUtils {
public static World mockWorld() {
World mockWorld = Mockito.mock(World.class);
Mockito.doReturn("World").when(mockWorld).toString();
return mockWorld;
}
public static Player mockPlayer() {
Player p = PowerMockito.mock(Player.class);
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL);
when(p.getUniqueId()).thenReturn(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"));
when(p.getFirstPlayed()).thenReturn(1234567L);
World mockWorld = mockWorld();
when(p.getLocation()).thenReturn(new Location(mockWorld, 0, 0, 0));
when(p.isOp()).thenReturn(true);
when(p.isBanned()).thenReturn(true);
when(p.isOnline()).thenReturn(true);
when(p.getName()).thenReturn("TestName");
return p;
}
public static Player mockBrokenPlayer() {
Player p = PowerMockito.mock(Player.class);
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL);
when(p.getUniqueId()).thenReturn(UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db"));
when(p.getFirstPlayed()).thenReturn(1234567L);
World mockWorld = mockWorld();
when(p.getLocation()).thenReturn(new Location(mockWorld, 0, 0, 0));
when(p.isOp()).thenReturn(true);
when(p.isBanned()).thenThrow(Exception.class);
when(p.isOnline()).thenReturn(true);
when(p.getName()).thenReturn("TestName");
return p;
}
}

View File

@ -7,9 +7,11 @@ package test.java.utils;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import main.java.com.djrapitops.plan.Plan;
import org.mockito.Mockito;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.Server;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.when;
/**
@ -25,25 +27,25 @@ public class TestInit {
public boolean setUp() {
try {
planMock = Mockito.mock(Plan.class);
planMock = PowerMockito.mock(Plan.class);
File configfile = new File(getClass().getResource("/config.yml").getPath());
YamlConfiguration configuration = new YamlConfiguration();
configuration.load(configfile.getAbsolutePath());
when(planMock.getConfig()).thenReturn(configuration);
// if (testFolder.exists()) {
// Files.deleteIfExists(testFolder.toPath());
// }
// testFolder.mkdir();
// when(planMock.getDataFolder()).thenReturn(new File("temporaryTestFolder"));
Files.deleteIfExists(new File("temporaryTestFolder").toPath());
File testFolder = new File("temporaryTestFolder");
testFolder.mkdir();
//
when(planMock.getDataFolder()).thenReturn(testFolder);
File analysis = new File(getClass().getResource("/analysis.html").getPath());
when(planMock.getResource("analysis.html")).thenReturn(new FileInputStream(analysis));
File player = new File(getClass().getResource("/player.html").getPath());
when(planMock.getResource("player.html")).thenReturn(new FileInputStream(player));
// Server mockServer = Mockito.mock(Server.class);
// when(mockServer.getIp()).thenReturn("0.0.0.0");
Server mockServer = PowerMockito.mock(Server.class);
when(mockServer.getIp()).thenReturn("0.0.0.0");
// Mockito.doReturn("0.0.0.0").when(mockServer).getIp();
// when(planMock.getServer()).thenReturn(mockServer);
when(planMock.getServer()).thenReturn(mockServer);
// Mockito.doReturn("0.0.0.0").when(planMock).getServer().getIp();
return true;
} catch (Exception ex) {