-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api,test-shared-classes): move mock creation to test-shared-classes
- Loading branch information
1 parent
826f78c
commit b534bde
Showing
5 changed files
with
145 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ain/java/net/okocraft/box/test/shared/mock/bukkit/inventory/ContentsHoldingInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.okocraft.box.test.shared.mock.bukkit.inventory; | ||
|
||
import net.okocraft.box.test.shared.mock.bukkit.item.ItemStackMock; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Arrays; | ||
|
||
public abstract class ContentsHoldingInventory implements Inventory { | ||
|
||
public static @NotNull ContentsHoldingInventory create(int size) { | ||
return createMock(new ItemStack[size]); | ||
} | ||
|
||
public static @NotNull ContentsHoldingInventory create(@Nullable ItemStack @NotNull [] contents) { | ||
return createMock(Arrays.copyOf(contents, contents.length)); | ||
} | ||
|
||
private static @NotNull ContentsHoldingInventory createMock(@Nullable ItemStack @NotNull [] contents) { | ||
var mock = Mockito.mock(ContentsHoldingInventory.class); | ||
|
||
Mockito.doCallRealMethod().when(mock).getContents(); | ||
Mockito.doCallRealMethod().when(mock).getStorageContents(); | ||
Mockito.doCallRealMethod().when(mock).setContents(Mockito.any()); | ||
Mockito.doCallRealMethod().when(mock).setStorageContents(Mockito.any()); | ||
Mockito.doCallRealMethod().when(mock).checkContents(Mockito.any()); | ||
|
||
mock.contents = contents; | ||
return mock; | ||
} | ||
|
||
ItemStack[] contents; | ||
|
||
@Override | ||
public @Nullable ItemStack @NotNull [] getContents() { | ||
if (this.contents == null) { | ||
throw new IllegalStateException("Not initialized."); | ||
} | ||
return Arrays.copyOf(this.contents, this.contents.length); | ||
} | ||
|
||
@Override | ||
public @Nullable ItemStack @NotNull [] getStorageContents() { | ||
return this.getContents(); | ||
} | ||
|
||
@Override | ||
public void setContents(@Nullable ItemStack @NotNull [] newContents) throws IllegalArgumentException { | ||
if (this.contents == null) { | ||
throw new IllegalStateException("Not initialized."); | ||
} | ||
|
||
if (this.contents.length != newContents.length) { | ||
throw new IllegalArgumentException("The number of new items does not match the size of this inventory."); | ||
} | ||
|
||
this.contents = Arrays.copyOf(newContents, newContents.length); | ||
} | ||
|
||
@Override | ||
public void setStorageContents(@Nullable ItemStack @NotNull [] newContents) throws IllegalArgumentException { | ||
this.setContents(newContents); | ||
} | ||
|
||
public void checkContents(@Nullable ItemStack @NotNull [] expectedContents) { | ||
var actualContents = this.contents; | ||
|
||
if (actualContents == null) { | ||
throw new IllegalStateException("Not initialized."); | ||
} | ||
|
||
Assertions.assertEquals(expectedContents.length, actualContents.length); | ||
|
||
for (int i = 0; i < expectedContents.length; i++) { | ||
var expectedItem = expectedContents[i]; | ||
var actualItem = actualContents[i]; | ||
|
||
if (expectedItem == null) { | ||
Assertions.assertNull(actualItem); | ||
} else { | ||
Assertions.assertNotNull(actualItem); | ||
ItemStackMock.checkSameTypeAndSameAmount(expectedItem, actualItem); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ed-classes/src/main/java/net/okocraft/box/test/shared/mock/bukkit/item/ItemStackMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.okocraft.box.test.shared.mock.bukkit.item; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.mockito.Mockito; | ||
|
||
public final class ItemStackMock { | ||
|
||
public static @NotNull ItemStack createItemStack(@NotNull Material item, int amount) { | ||
if (amount < 1 || item.getMaxStackSize() < amount) { | ||
throw new IllegalArgumentException("Invalid amount: " + amount); | ||
} | ||
|
||
var mock = Mockito.mock(ItemStack.class); | ||
|
||
Mockito.when(mock.getType()).thenReturn(item); | ||
Mockito.when(mock.getAmount()).thenReturn(amount); | ||
Mockito.when(mock.getMaxStackSize()).thenReturn(item.getMaxStackSize()); | ||
Mockito.when(mock.asQuantity(Mockito.anyInt())).thenAnswer(invocation -> createItemStack(item, invocation.getArgument(0))); | ||
Mockito.when(mock.isSimilar(Mockito.any())).thenAnswer(invocation -> mock.getType() == invocation.<ItemStack>getArgument(0).getType()); | ||
|
||
return mock; | ||
} | ||
|
||
public static void checkSameTypeAndSameAmount(@NotNull ItemStack item1, @NotNull ItemStack item2) { | ||
checkSameType(item1, item2); | ||
Assertions.assertEquals(item1.getAmount(), item2.getAmount()); | ||
} | ||
|
||
public static void checkSameType(@NotNull ItemStack item1, @NotNull ItemStack item2) { | ||
Assertions.assertEquals(item1.getType(), item2.getType()); | ||
} | ||
|
||
private ItemStackMock() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |