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:
Tastybento 2018-02-19 09:57:19 -08:00
parent 7f0d71e25c
commit 382f195015
5 changed files with 212 additions and 18 deletions

View File

@ -61,6 +61,7 @@
<target>1.8</target>
</configuration>
</plugin>
<!-- I'm going to comment out these sections for now to speed up compilation
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
@ -90,6 +91,7 @@
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
<dependencies>

View File

@ -69,7 +69,7 @@ public class PanelItem {
}
public Optional<ClickHandler> getClickHandler() {
return Optional.of(clickHandler);
return Optional.ofNullable(clickHandler);
}
public boolean isGlow() {

View File

@ -1,7 +1,7 @@
package us.tastybento.bskyblock.api.panels.builders;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@ -36,7 +36,7 @@ public class PanelItemBuilder {
* @return PanelItemBuilder
*/
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;
}
/**
* Adds a list of strings to the descriptions
* @param description - List of strings
* @return PanelItemBuilder
*/
public PanelItemBuilder description(List<String> description) {
this.description = description;
this.description.addAll(description);
return this;
}
/**
* Add any number of lines to the description
* @param description strings of lines
* @return PanelItemBuilder
*/
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;
}
/**
* Adds a line to the description
* @param description - string
* @return PanelItemBuilder
*/
public PanelItemBuilder description(String description) {
this.description.add(description);
return this;

View File

@ -6,7 +6,9 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
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.ArrayList;
import java.util.Arrays;
@ -24,6 +26,7 @@ import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -33,6 +36,7 @@ import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.Assert;
import org.junit.BeforeClass;
@ -80,26 +84,36 @@ public class TestBSkyBlock {
private static World world;
private static Player ownerOfIsland;
private static Player visitorToIsland;
@BeforeClass
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);
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");
Bukkit.setServer(server);
Mockito.when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
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);
player = mock(Player.class);
ownerOfIsland = mock(Player.class);
@ -124,11 +138,12 @@ public class TestBSkyBlock {
Mockito.when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID);
// Mock itemFactory for ItemStack
/*
ItemFactory itemFactory = PowerMockito.mock(ItemFactory.class);
PowerMockito.when(Bukkit.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = PowerMockito.mock(ItemMeta.class);
PowerMockito.when(itemFactory.getItemMeta(Matchers.any())).thenReturn(itemMeta);
*/
PowerMockito.mockStatic(Flags.class);
plugin = Mockito.mock(BSkyBlock.class);

View File

@ -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;
}
}
}