Skip to content

Commit

Permalink
fix: Fixed equals method of database inventory to avoid duplicate ent…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
Philip Urban committed Dec 22, 2020
1 parent cfd81c0 commit b5383a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import lombok.Setter;
import net.silthus.ebean.BaseEntity;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.*;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
Expand All @@ -25,6 +23,7 @@ public class TDatabaseInventory extends BaseEntity {

private UUID holderId;
@Lob
@Basic(fetch= FetchType.EAGER)
private String serializedInventory;
private Float saturation;
private Float exp;
Expand Down Expand Up @@ -82,7 +81,7 @@ public boolean equals(Object obj) {

TDatabaseInventory other = (TDatabaseInventory)obj;

if(holderId != other.getHolderId()) return false;
if(!holderId.equals(other.getHolderId())) return false;
if(!serializedInventory.equals(other.getSerializedInventory())) return false;
if(!saturation.equals(other.getSaturation())) return false;
if(!health.equals(other.getHealth())) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.raidcraft.rcinventory.util;

import de.raidcraft.rcinventory.RCInventory;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.io.BukkitObjectInputStream;
Expand All @@ -21,6 +23,10 @@ public static String playerInventoryToBase64(PlayerInventory playerInventory) th
}

public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
if(RCInventory.isTesting()) {
return "no-serialization-due-to-testing";
}

try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
Expand All @@ -43,6 +49,11 @@ public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalSta
}

public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
if(RCInventory.isTesting()) {
ItemStack[] dummyContent = { new ItemStack(Material.COBBLESTONE ) };
return dummyContent;
}

try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
Expand Down
58 changes: 47 additions & 11 deletions src/test/java/de/raidcraft/rcinventory/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import de.raidcraft.rcinventory.database.TDatabaseInventory;
import org.bukkit.entity.Player;
import org.junit.jupiter.api.*;
import static org.assertj.core.api.Assertions.assertThat;

public class IntegrationTest {

Expand All @@ -23,6 +25,46 @@ void tearDown() {
MockBukkit.unmock();
}

@Nested
@DisplayName("InventoryManager")
class InventoryManagerTests {

@BeforeEach
void setUp() {
TDatabaseInventory.find.all().forEach(entry -> entry.delete());
}

@Test
@DisplayName("save-inventory")
void storeInventory() {

Player player = server.addPlayer();

// Database must be empty
assertThat(TDatabaseInventory.find.query().findCount() == 0).isTrue();

// Add inventory
plugin.getInventoryManager().savePlayerInventory(player);

// Check if added
assertThat(TDatabaseInventory.find.query().findCount() == 1).isTrue();

// Add again -> Due to missing changes must not be saved
plugin.getInventoryManager().savePlayerInventory(player);

// Must still be '1'
assertThat(TDatabaseInventory.find.query().findCount() == 1).isTrue();

Player player2 = server.addPlayer();

// Add another players inventory
plugin.getInventoryManager().savePlayerInventory(player2);

// Must be '2'
assertThat(TDatabaseInventory.find.query().findCount() == 2).isTrue();
}
}

@Nested
@DisplayName("Commands")
class Commands {
Expand All @@ -35,20 +77,14 @@ void setUp() {
}

@Nested
@DisplayName("/template:admin")
@DisplayName("/rcinventories")
class AdminCommands {

@Nested
@DisplayName("foo bar")
class add {

@Test
@Disabled
@DisplayName("should work")
void shouldWork() {
@Test
@DisplayName("reload")
void reload() {

server.dispatchCommand(server.getConsoleSender(),"rc:template add foo " + player.getName() + " bar");
}
server.dispatchCommand(server.getConsoleSender(),"rcinventory reload");
}
}
}
Expand Down

0 comments on commit b5383a1

Please sign in to comment.