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/build/
/Plan Lite/dist/ /Plan Lite/dist/
/Plan Lite/nbproject/private/ /Plan Lite/nbproject/private/
/Plan/target/
/Plan/nbproject/ /Plan/nbproject/
/Plan/target/
/Plan/temporaryTestFolder/
/Debugger/nbproject/private/ /Debugger/nbproject/private/
/PlanDebugger/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.Plan;
import main.java.com.djrapitops.plan.api.Gender; import main.java.com.djrapitops.plan.api.Gender;
import main.java.com.djrapitops.plan.data.DemographicsData; import main.java.com.djrapitops.plan.data.DemographicsData;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.easymock.EasyMock; import org.easymock.EasyMock;
import org.junit.Test; import org.junit.Test;

View File

@ -5,6 +5,8 @@
*/ */
package test.java.main.java.com.djrapitops.plan.data; package test.java.main.java.com.djrapitops.plan.data;
import org.bukkit.OfflinePlayer;
import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
@ -24,7 +26,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock; import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
@ -41,6 +42,7 @@ import test.java.utils.TestInit;
public class UserDataTest { public class UserDataTest {
private UserData test; private UserData test;
private Plan plan;
public UserDataTest() { public UserDataTest() {
} }
@ -49,11 +51,12 @@ public class UserDataTest {
public void setUp() { public void setUp() {
TestInit t = new TestInit(); TestInit t = new TestInit();
assertTrue("Not set up", t.setUp()); assertTrue("Not set up", t.setUp());
Plan plan = t.getPlanMock(); plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class); 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);
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.replay(JavaPlugin.class);
// PowerMock.verify(JavaPlugin.class); // PowerMock.verify(JavaPlugin.class);
DemographicsData demData = new DemographicsData(); DemographicsData demData = new DemographicsData();
@ -304,6 +307,38 @@ public class UserDataTest {
assertTrue("Not copied properly", test.equals(copy)); 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 @Test
public void testGetUUID() { public void testGetUUID() {
assertEquals(test.getUuid(), UUID.fromString("7f8149a0-b5a5-4fcd-80b5-6cff083a99f1")); 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 org.powermock.api.easymock.PowerMock;
import test.java.utils.TestInit; import test.java.utils.TestInit;
import org.bukkit.Bukkit; 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.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 * @author Risto
*/ */
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class, Bukkit.class, BukkitScheduler.class, BukkitRunnable.class})
public class DatabaseTest { public class DatabaseTest {
private Plan plan; private Plan plan;
@ -35,21 +48,26 @@ public class DatabaseTest {
} }
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException, Exception {
TestInit t = new TestInit(); TestInit t = new TestInit();
assertTrue("Not set up", t.setUp()); assertTrue("Not set up", t.setUp());
plan = t.getPlanMock(); plan = t.getPlanMock();
PowerMock.mockStatic(JavaPlugin.class); PowerMock.mockStatic(JavaPlugin.class);
PowerMock.replay(JavaPlugin.class); PowerMock.replay(JavaPlugin.class);
// PowerMock.verify(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"); File f = new File(plan.getDataFolder(), "Errors.txt");
rows = 0; rows = 0;
if (f.exists()) { if (f.exists()) {
rows = Files.readLines(f, Charset.defaultCharset()).size(); 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 @After
@ -62,7 +80,7 @@ public class DatabaseTest {
assertTrue("Errors were caught.", rows == rowsAgain); assertTrue("Errors were caught.", rows == rowsAgain);
} }
@Ignore("Mock scheduler") @Test @Ignore @Test
public void testInit() { public void testInit() {
Database db = new SQLiteDB(plan, "debug.db"); Database db = new SQLiteDB(plan, "debug.db");
assertTrue("Database failed to init.", db.init()); assertTrue("Database failed to init.", db.init());

View File

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

View File

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