mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-03 05:51:43 +01:00
Added tests to PanelItemBuilder and fixed bugs.
Cleared up the description settings methods. Using these will add to the description of the item. Temporarily removed JavaDoc and Source jar creation from the POM to save time when compiling. Will put it back when we need it. Also, I worked out how to run the Bukkit server setup for tests. See the setUp method in the tests. This works (at last).
This commit is contained in:
parent
7f0d71e25c
commit
382f195015
2
pom.xml
2
pom.xml
@ -61,6 +61,7 @@
|
|||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<!-- I'm going to comment out these sections for now to speed up compilation
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
@ -90,6 +91,7 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
-->
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -69,7 +69,7 @@ public class PanelItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ClickHandler> getClickHandler() {
|
public Optional<ClickHandler> getClickHandler() {
|
||||||
return Optional.of(clickHandler);
|
return Optional.ofNullable(clickHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGlow() {
|
public boolean isGlow() {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package us.tastybento.bskyblock.api.panels.builders;
|
package us.tastybento.bskyblock.api.panels.builders;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ public class PanelItemBuilder {
|
|||||||
* @return PanelItemBuilder
|
* @return PanelItemBuilder
|
||||||
*/
|
*/
|
||||||
public PanelItemBuilder icon(UUID playerUUID) {
|
public PanelItemBuilder icon(UUID playerUUID) {
|
||||||
return icon(Bukkit.getOfflinePlayer(playerUUID).getName());
|
return icon(Bukkit.getServer().getOfflinePlayer(playerUUID).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,16 +60,35 @@ public class PanelItemBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a list of strings to the descriptions
|
||||||
|
* @param description - List of strings
|
||||||
|
* @return PanelItemBuilder
|
||||||
|
*/
|
||||||
public PanelItemBuilder description(List<String> description) {
|
public PanelItemBuilder description(List<String> description) {
|
||||||
this.description = description;
|
this.description.addAll(description);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add any number of lines to the description
|
||||||
|
* @param description strings of lines
|
||||||
|
* @return PanelItemBuilder
|
||||||
|
*/
|
||||||
public PanelItemBuilder description(String... description) {
|
public PanelItemBuilder description(String... description) {
|
||||||
Collections.addAll(this.description, description);
|
List<String> additions = Arrays.asList(description);
|
||||||
|
ArrayList<String> updatableList = new ArrayList<String>();
|
||||||
|
updatableList.addAll(this.description);
|
||||||
|
updatableList.addAll(additions);
|
||||||
|
this.description = updatableList;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a line to the description
|
||||||
|
* @param description - string
|
||||||
|
* @return PanelItemBuilder
|
||||||
|
*/
|
||||||
public PanelItemBuilder description(String description) {
|
public PanelItemBuilder description(String description) {
|
||||||
this.description.add(description);
|
this.description.add(description);
|
||||||
return this;
|
return this;
|
||||||
|
@ -6,7 +6,9 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
import static org.junit.Assert.assertNotSame;
|
import static org.junit.Assert.assertNotSame;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -24,6 +26,7 @@ import java.util.logging.Logger;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
@ -33,6 +36,7 @@ import org.bukkit.event.Event;
|
|||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
import org.bukkit.inventory.ItemFactory;
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.inventory.meta.SkullMeta;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
@ -83,22 +87,32 @@ public class TestBSkyBlock {
|
|||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
//PowerMockito.mockStatic(Bukkit.class);
|
|
||||||
//Mockito.doReturn(plugin).when(BSkyBlock.getPlugin());
|
|
||||||
//Mockito.when().thenReturn(plugin);
|
|
||||||
world = mock(World.class);
|
|
||||||
|
|
||||||
|
|
||||||
//Mockito.when(world.getWorldFolder()).thenReturn(worldFile);
|
|
||||||
|
|
||||||
Server server = mock(Server.class);
|
Server server = mock(Server.class);
|
||||||
|
World world = mock(World.class);
|
||||||
|
world = mock(World.class);
|
||||||
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
Mockito.when(server.getWorld("world")).thenReturn(world);
|
Mockito.when(server.getWorld("world")).thenReturn(world);
|
||||||
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
|
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||||
Bukkit.setServer(server);
|
|
||||||
Mockito.when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
|
||||||
PluginManager pluginManager = mock(PluginManager.class);
|
PluginManager pluginManager = mock(PluginManager.class);
|
||||||
Mockito.when(server.getPluginManager()).thenReturn(pluginManager);
|
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||||
|
|
||||||
|
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||||
|
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||||
|
|
||||||
|
Bukkit.setServer(server);
|
||||||
|
|
||||||
|
SkullMeta skullMeta = mock(SkullMeta.class);
|
||||||
|
ItemMeta itemMeta = mock(ItemMeta.class);
|
||||||
|
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
|
||||||
|
|
||||||
|
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||||
|
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||||
|
when(offlinePlayer.getName()).thenReturn("tastybento");
|
||||||
|
|
||||||
|
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||||
|
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
|
//when(Bukkit.getServer()).thenReturn(server);
|
||||||
|
|
||||||
sender = mock(CommandSender.class);
|
sender = mock(CommandSender.class);
|
||||||
player = mock(Player.class);
|
player = mock(Player.class);
|
||||||
@ -124,11 +138,12 @@ public class TestBSkyBlock {
|
|||||||
Mockito.when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID);
|
Mockito.when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID);
|
||||||
|
|
||||||
// Mock itemFactory for ItemStack
|
// Mock itemFactory for ItemStack
|
||||||
|
/*
|
||||||
ItemFactory itemFactory = PowerMockito.mock(ItemFactory.class);
|
ItemFactory itemFactory = PowerMockito.mock(ItemFactory.class);
|
||||||
PowerMockito.when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
PowerMockito.when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||||
ItemMeta itemMeta = PowerMockito.mock(ItemMeta.class);
|
ItemMeta itemMeta = PowerMockito.mock(ItemMeta.class);
|
||||||
PowerMockito.when(itemFactory.getItemMeta(Matchers.any())).thenReturn(itemMeta);
|
PowerMockito.when(itemFactory.getItemMeta(Matchers.any())).thenReturn(itemMeta);
|
||||||
|
*/
|
||||||
PowerMockito.mockStatic(Flags.class);
|
PowerMockito.mockStatic(Flags.class);
|
||||||
|
|
||||||
plugin = Mockito.mock(BSkyBlock.class);
|
plugin = Mockito.mock(BSkyBlock.class);
|
||||||
|
@ -0,0 +1,158 @@
|
|||||||
|
package us.tastybento.bskyblock.api.panels.builders;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.inventory.ItemFactory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.SkullMeta;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
|
||||||
|
import us.tastybento.bskyblock.api.commands.User;
|
||||||
|
import us.tastybento.bskyblock.api.panels.ClickType;
|
||||||
|
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||||
|
|
||||||
|
@RunWith(PowerMockRunner.class)
|
||||||
|
public class PanelItemBuilderTest {
|
||||||
|
|
||||||
|
private static PanelItemBuilder builder;
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() throws Exception {
|
||||||
|
builder = new PanelItemBuilder();
|
||||||
|
Server server = mock(Server.class);
|
||||||
|
World world = mock(World.class);
|
||||||
|
world = mock(World.class);
|
||||||
|
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
|
Mockito.when(server.getWorld("world")).thenReturn(world);
|
||||||
|
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||||
|
|
||||||
|
PluginManager pluginManager = mock(PluginManager.class);
|
||||||
|
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||||
|
|
||||||
|
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||||
|
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||||
|
|
||||||
|
Bukkit.setServer(server);
|
||||||
|
|
||||||
|
SkullMeta skullMeta = mock(SkullMeta.class);
|
||||||
|
when(skullMeta.getOwner()).thenReturn("tastybento");
|
||||||
|
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
|
||||||
|
|
||||||
|
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||||
|
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||||
|
when(offlinePlayer.getName()).thenReturn("tastybento");
|
||||||
|
|
||||||
|
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||||
|
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
|
//when(Bukkit.getServer()).thenReturn(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIconMaterial() {
|
||||||
|
builder.icon(Material.STONE);
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertNotNull(item.getItem().getType());
|
||||||
|
assertEquals(Material.STONE, item.getItem().getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIconItemStack() {
|
||||||
|
builder.icon(new ItemStack(Material.IRON_ORE));
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertNotNull(item.getItem().getType());
|
||||||
|
assertEquals(Material.IRON_ORE, item.getItem().getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIconUUID() {
|
||||||
|
builder.icon(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419"));
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertNotNull(item.getItem().getType());
|
||||||
|
assertEquals(Material.SKULL_ITEM, item.getItem().getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Test
|
||||||
|
public void testIconString() {
|
||||||
|
builder.icon("tastybento");
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertNotNull(item.getItem().getType());
|
||||||
|
SkullMeta skullMeta = (SkullMeta)item.getItem().getItemMeta();
|
||||||
|
assertEquals("tastybento",skullMeta.getOwner());
|
||||||
|
assertEquals(Material.SKULL_ITEM, item.getItem().getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testName() {
|
||||||
|
builder.name("test");
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertEquals("test",item.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDescriptionListOfString() {
|
||||||
|
List<String> test = Arrays.asList("test line 1", "test line 2");
|
||||||
|
builder.description(test);
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertEquals(test, item.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDescriptionStringArray() {
|
||||||
|
List<String> test = Arrays.asList("test line 1", "test line 2", "test line 3", "test line 4");
|
||||||
|
builder.description("test line 3", "test line 4");
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertEquals(test, item.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDescriptionString() {
|
||||||
|
List<String> test = Arrays.asList("test line 1", "test line 2", "test line 3", "test line 4", "test line 5");
|
||||||
|
builder.description("test line 5");
|
||||||
|
PanelItem item = builder.build();
|
||||||
|
assertEquals(test, item.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClickHandler() {
|
||||||
|
// Test without click handler
|
||||||
|
PanelItem item = builder.clickHandler(null).build();
|
||||||
|
assertFalse(item.getClickHandler().isPresent());
|
||||||
|
|
||||||
|
item = builder.clickHandler(new Clicker()).build();
|
||||||
|
assertTrue(item.getClickHandler().isPresent());
|
||||||
|
assertTrue(item.getClickHandler().map(x -> x.onClick(null, ClickType.LEFT)).orElse(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Clicker implements PanelItem.ClickHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onClick(User user, ClickType click) {
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user