From 988854f98e727b98e1aeb67e7fa30d91f9dc9534 Mon Sep 17 00:00:00 2001 From: Hexeption Date: Sun, 29 Nov 2020 14:47:57 +0000 Subject: [PATCH] Added Tests --- build.gradle | 11 + .../v1_12_R1/generator/CraftChunkData.java | 2 +- .../v1_12_R1/inventory/CraftItemFactory.java | 2 +- .../v1_12_R1/inventory/CraftMetaBanner.java | 2 +- .../inventory/CraftMetaBlockState.java | 2 +- .../v1_12_R1/inventory/CraftMetaBook.java | 2 +- .../inventory/CraftMetaBookSigned.java | 2 +- .../v1_12_R1/inventory/CraftMetaCharge.java | 2 +- .../inventory/CraftMetaEnchantedBook.java | 2 +- .../v1_12_R1/inventory/CraftMetaFirework.java | 2 +- .../v1_12_R1/inventory/CraftMetaItem.java | 4 +- .../inventory/CraftMetaKnowledgeBook.java | 2 +- .../inventory/CraftMetaLeatherArmor.java | 2 +- .../v1_12_R1/inventory/CraftMetaMap.java | 2 +- .../v1_12_R1/inventory/CraftMetaPotion.java | 2 +- .../v1_12_R1/inventory/CraftMetaSkull.java | 2 +- .../v1_12_R1/inventory/CraftMetaSpawnEgg.java | 2 +- src/test/java/org/bukkit/ArtTest.java | 65 +++ src/test/java/org/bukkit/BiomeTest.java | 24 + src/test/java/org/bukkit/ChatTest.java | 22 + src/test/java/org/bukkit/DyeColorsTest.java | 46 ++ src/test/java/org/bukkit/MaterialTest.java | 48 ++ src/test/java/org/bukkit/NibbleArrayTest.java | 29 + src/test/java/org/bukkit/ParticleTest.java | 19 + src/test/java/org/bukkit/PerMaterialTest.java | 189 +++++++ src/test/java/org/bukkit/SoundTest.java | 43 ++ .../bukkit/StatisticsAndAchievementsTest.java | 38 ++ src/test/java/org/bukkit/WorldTypeTest.java | 26 + .../craftbukkit/generator/ChunkDataTest.java | 84 +++ .../inventory/CompositeSerialization.java | 61 +++ .../inventory/FactoryItemMaterialTest.java | 163 ++++++ .../inventory/ItemFactoryTest.java | 74 +++ .../inventory/ItemMetaCloneTest.java | 25 + .../ItemMetaImplementationOverrideTest.java | 85 +++ .../craftbukkit/inventory/ItemMetaTest.java | 326 ++++++++++++ .../inventory/ItemStackBookTest.java | 201 +++++++ .../ItemStackEnchantStorageTest.java | 95 ++++ .../ItemStackFireworkChargeTest.java | 113 ++++ .../inventory/ItemStackFireworkTest.java | 169 ++++++ .../inventory/ItemStackLeatherTest.java | 75 +++ .../ItemStackLoreEnchantmentTest.java | 284 ++++++++++ .../inventory/ItemStackMapTest.java | 109 ++++ .../inventory/ItemStackPotionsTest.java | 133 +++++ .../inventory/ItemStackSkullTest.java | 76 +++ .../craftbukkit/inventory/ItemStackTest.java | 501 ++++++++++++++++++ .../inventory/NMSCraftItemStackTest.java | 37 ++ .../bukkit/entity/EnderDragonPhaseTest.java | 51 ++ src/test/java/org/bukkit/map/MapTest.java | 61 +++ .../java/org/bukkit/potion/PotionTest.java | 36 ++ .../bukkit/support/AbstractTestingBase.java | 80 +++ .../org/bukkit/support/DummyEnchantments.java | 14 + .../java/org/bukkit/support/DummyServer.java | 85 +++ .../java/org/bukkit/support/Matchers.java | 32 ++ src/test/java/org/bukkit/support/Util.java | 31 ++ 54 files changed, 3578 insertions(+), 17 deletions(-) create mode 100644 src/test/java/org/bukkit/ArtTest.java create mode 100644 src/test/java/org/bukkit/BiomeTest.java create mode 100644 src/test/java/org/bukkit/ChatTest.java create mode 100644 src/test/java/org/bukkit/DyeColorsTest.java create mode 100644 src/test/java/org/bukkit/MaterialTest.java create mode 100644 src/test/java/org/bukkit/NibbleArrayTest.java create mode 100644 src/test/java/org/bukkit/ParticleTest.java create mode 100644 src/test/java/org/bukkit/PerMaterialTest.java create mode 100644 src/test/java/org/bukkit/SoundTest.java create mode 100644 src/test/java/org/bukkit/StatisticsAndAchievementsTest.java create mode 100644 src/test/java/org/bukkit/WorldTypeTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/generator/ChunkDataTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/CompositeSerialization.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/FactoryItemMaterialTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemFactoryTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackBookTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackEnchantStorageTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkChargeTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLoreEnchantmentTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackMapTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackPotionsTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackSkullTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemStackTest.java create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java create mode 100644 src/test/java/org/bukkit/entity/EnderDragonPhaseTest.java create mode 100644 src/test/java/org/bukkit/map/MapTest.java create mode 100644 src/test/java/org/bukkit/potion/PotionTest.java create mode 100644 src/test/java/org/bukkit/support/AbstractTestingBase.java create mode 100644 src/test/java/org/bukkit/support/DummyEnchantments.java create mode 100644 src/test/java/org/bukkit/support/DummyServer.java create mode 100644 src/test/java/org/bukkit/support/Matchers.java create mode 100644 src/test/java/org/bukkit/support/Util.java diff --git a/build.gradle b/build.gradle index 2d042ef..2480001 100644 --- a/build.gradle +++ b/build.gradle @@ -73,6 +73,8 @@ configurations{ } dependencies { + testCompile 'org.hamcrest:hamcrest-all:1.3' + testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile group: 'junit', name: 'junit', version: '4.12' compile(group: 'bukkit', name: 'bukkit', version: '1.12.2-R0.1') compile 'com.conversantmedia:disruptor:1.2.14' @@ -164,6 +166,9 @@ minecraft { } } +extractForgeSources { exclude "**/SideOnly.java", "**/Side.java" } +extractForgeResources { exclude "**/log4j2.xml" } + sourceCompatibility = 1.8 tasks.generateXmlConfig.dependsOn installer tasks.compileJava.enabled = false @@ -244,11 +249,17 @@ extractForgeSources { } genGradleProjects { + addTestCompileDep "junit:junit:4.12" // TODO update unit tests to junit 5 and remove this + addTestCompileDep "org.junit.jupiter:junit-jupiter-api:5.0.0" + addTestCompileDep "org.opentest4j:opentest4j:1.0.0" // needed for junit 5 + addTestCompileDep "org.hamcrest:hamcrest-all:1.3" + addTestCompileDep "org.hamcrest:hamcrest-library:1.3" addCompileDep "junit:junit:4.12" addCompileDep "bukkit:bukkit:1.12.2-R0.1" addCompileDep "org.reflections:reflections:0.9.10" addCompileDep "javassist:javassist:3.12.1.GA" addRepo("MagmaFoundation", "https://repo.hexeption.dev/repository/magma/") + filter { dep -> !dep.contains("scala") } } diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/generator/CraftChunkData.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/generator/CraftChunkData.java index ffb9c35..ea08d83 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/generator/CraftChunkData.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/generator/CraftChunkData.java @@ -21,7 +21,7 @@ public CraftChunkData(World world) { this(world.getMaxHeight()); } - /* pp for tests */ CraftChunkData(int maxHeight) { + public CraftChunkData(int maxHeight) { if (maxHeight > 256) { throw new IllegalArgumentException("World height exceeded max chunk height"); } diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftItemFactory.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftItemFactory.java index 5f9da20..8a84328 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftItemFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftItemFactory.java @@ -14,7 +14,7 @@ public final class CraftItemFactory implements ItemFactory { static final Color DEFAULT_LEATHER_COLOR = Color.fromRGB(0xA06540); - static final Collection KNOWN_NBT_ATTRIBUTE_NAMES; + public static final Collection KNOWN_NBT_ATTRIBUTE_NAMES; private static final CraftItemFactory instance; static { diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBanner.java index 9328b78..fe7ce02 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBanner.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBanner.java @@ -197,7 +197,7 @@ boolean isEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { return type == Material.BANNER; } diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBlockState.java index e4933c6..9a50591 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBlockState.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBlockState.java @@ -168,7 +168,7 @@ boolean isEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch(type){ case FURNACE: case CHEST: diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBook.java index f262787..4634e92 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBook.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBook.java @@ -157,7 +157,7 @@ boolean isBookEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case WRITTEN_BOOK: case BOOK_AND_QUILL: diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBookSigned.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBookSigned.java index b4fefb8..af61043 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBookSigned.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaBookSigned.java @@ -91,7 +91,7 @@ boolean isEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case WRITTEN_BOOK: case BOOK_AND_QUILL: diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaCharge.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaCharge.java index 0639596..f6b3390 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaCharge.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaCharge.java @@ -64,7 +64,7 @@ void applyToItem(NBTTagCompound itemTag) { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case FIREWORK_CHARGE: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaEnchantedBook.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaEnchantedBook.java index 90ec6b6..c6d5976 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaEnchantedBook.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaEnchantedBook.java @@ -56,7 +56,7 @@ void applyToItem(NBTTagCompound itemTag) { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case ENCHANTED_BOOK: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaFirework.java index d2e95f2..c9ee7d5 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaFirework.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaFirework.java @@ -247,7 +247,7 @@ static void addColors(NBTTagCompound compound, ItemMetaKey key, List colo } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch(type) { case FIREWORK: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaItem.java index ea6999b..1a836d6 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaItem.java @@ -66,7 +66,7 @@ *
  • SerializableMeta.Deserializers deserializer() */ @DelegateDeserialization(CraftMetaItem.SerializableMeta.class) -class CraftMetaItem implements ItemMeta, Repairable { +public class CraftMetaItem implements ItemMeta, Repairable { static class ItemMetaKey { @@ -615,7 +615,7 @@ void setDisplayTag(NBTTagCompound tag, String key, NBTBase value) { } @Overridden - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { return type != Material.AIR; } diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaKnowledgeBook.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaKnowledgeBook.java index c4c7140..49f1db1 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaKnowledgeBook.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaKnowledgeBook.java @@ -80,7 +80,7 @@ boolean isBookEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case KNOWLEDGE_BOOK: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaLeatherArmor.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaLeatherArmor.java index 5b771e0..7a7da23 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaLeatherArmor.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaLeatherArmor.java @@ -61,7 +61,7 @@ boolean isLeatherArmorEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch(type) { case LEATHER_HELMET: case LEATHER_CHESTPLATE: diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaMap.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaMap.java index 10840a4..c12475d 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaMap.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaMap.java @@ -95,7 +95,7 @@ void applyToItem(NBTTagCompound tag) { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case MAP: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaPotion.java index a84f0a6..11c748c 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaPotion.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaPotion.java @@ -148,7 +148,7 @@ boolean isPotionEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch(type) { case POTION: case SPLASH_POTION: diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSkull.java index d51a972..284dfbd 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSkull.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSkull.java @@ -95,7 +95,7 @@ boolean isSkullEmpty() { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch(type) { case SKULL_ITEM: return true; diff --git a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSpawnEgg.java b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSpawnEgg.java index aea9221..80c4ae5 100644 --- a/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSpawnEgg.java +++ b/src/main/java/org/bukkit/craftbukkit/v1_12_R1/inventory/CraftMetaSpawnEgg.java @@ -93,7 +93,7 @@ void applyToItem(NBTTagCompound tag) { } @Override - boolean applicableTo(Material type) { + public boolean applicableTo(Material type) { switch (type) { case MONSTER_EGG: return true; diff --git a/src/test/java/org/bukkit/ArtTest.java b/src/test/java/org/bukkit/ArtTest.java new file mode 100644 index 0000000..ff1d651 --- /dev/null +++ b/src/test/java/org/bukkit/ArtTest.java @@ -0,0 +1,65 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import com.google.common.collect.Lists; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import net.minecraft.entity.item.EntityPainting.EnumArt; +import org.bukkit.craftbukkit.v1_12_R1.CraftArt; +import org.junit.Test; + +public class ArtTest { + + private static final int UNIT_MULTIPLIER = 16; + + @Test + public void verifyMapping() { + List arts = Lists.newArrayList(Art.values()); + + for (EnumArt enumArt : EnumArt.values()) { + int id = enumArt.ordinal(); + String name = enumArt.title; + int width = enumArt.sizeX / UNIT_MULTIPLIER; + int height = enumArt.sizeY / UNIT_MULTIPLIER; + + Art subject = Art.getById(id); + + String message = String.format("org.bukkit.Art is missing id: %d named: '%s'", id, name); + assertNotNull(message, subject); + + assertThat(Art.getByName(name), is(subject)); + assertThat("Art." + subject + "'s width", subject.getBlockWidth(), is(width)); + assertThat("Art." + subject + "'s height", subject.getBlockHeight(), is(height)); + + arts.remove(subject); + } + + assertThat("org.bukkit.Art has too many arts", arts, is(Collections.EMPTY_LIST)); + } + + @Test + public void testCraftArtToNotch() { + Map cache = new EnumMap(EnumArt.class); + for (Art art : Art.values()) { + EnumArt enumArt = CraftArt.BukkitToNotch(art); + assertNotNull(art.name(), enumArt); + assertThat(art.name(), cache.put(enumArt, art), is(nullValue())); + } + } + + @Test + public void testCraftArtToBukkit() { + Map cache = new EnumMap(Art.class); + for (EnumArt enumArt : EnumArt.values()) { + Art art = CraftArt.NotchToBukkit(enumArt); + assertNotNull(enumArt.name(), art); + assertThat(enumArt.name(), cache.put(art, enumArt), is(nullValue())); + } + } +} diff --git a/src/test/java/org/bukkit/BiomeTest.java b/src/test/java/org/bukkit/BiomeTest.java new file mode 100644 index 0000000..81fa426 --- /dev/null +++ b/src/test/java/org/bukkit/BiomeTest.java @@ -0,0 +1,24 @@ +package org.bukkit; + +import org.bukkit.block.Biome; +import org.bukkit.craftbukkit.v1_12_R1.block.CraftBlock; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Assert; +import org.junit.Test; + +public class BiomeTest extends AbstractTestingBase { + + @Test + public void testBukkitToMinecraft() { + for (Biome biome : Biome.values()) { + Assert.assertNotNull("No NMS mapping for " + biome, CraftBlock.biomeToBiomeBase(biome)); + } + } + + @Test + public void testMinecraftToBukkit() { + for (Object biome : net.minecraft.world.biome.Biome.REGISTRY) { + Assert.assertNotNull("No Bukkit mapping for " + biome, CraftBlock.biomeBaseToBiome((net.minecraft.world.biome.Biome) biome)); + } + } +} diff --git a/src/test/java/org/bukkit/ChatTest.java b/src/test/java/org/bukkit/ChatTest.java new file mode 100644 index 0000000..b89d0d8 --- /dev/null +++ b/src/test/java/org/bukkit/ChatTest.java @@ -0,0 +1,22 @@ +package org.bukkit; + +import net.minecraft.util.text.TextFormatting; +import org.bukkit.craftbukkit.v1_12_R1.util.CraftChatMessage; +import org.junit.Assert; +import org.junit.Test; + +public class ChatTest { + + @Test + public void testColors() { + for (ChatColor color : ChatColor.values()) { + Assert.assertNotNull(CraftChatMessage.getColor(color)); + Assert.assertEquals(color, CraftChatMessage.getColor(CraftChatMessage.getColor(color))); + } + + for (TextFormatting format : TextFormatting.values()) { + Assert.assertNotNull(CraftChatMessage.getColor(format)); + Assert.assertEquals(format, CraftChatMessage.getColor(CraftChatMessage.getColor(format))); + } + } +} diff --git a/src/test/java/org/bukkit/DyeColorsTest.java b/src/test/java/org/bukkit/DyeColorsTest.java new file mode 100644 index 0000000..268656e --- /dev/null +++ b/src/test/java/org/bukkit/DyeColorsTest.java @@ -0,0 +1,46 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.item.EnumDyeColor; +import net.minecraft.item.ItemDye; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class DyeColorsTest extends AbstractTestingBase { + + @Parameter + public DyeColor dye; + + @Parameters(name = "{index}: {0}") + public static List data() { + List list = new ArrayList(); + for (DyeColor dye : DyeColor.values()) { + list.add(new Object[]{dye}); + } + return list; + } + + @Test + public void checkColor() { + Color color = dye.getColor(); + float[] nmsColorArray = EnumDyeColor.byMetadata(dye.getWoolData()).getColorComponentValues(); + Color nmsColor = Color.fromRGB((int) (nmsColorArray[0] * 255), (int) (nmsColorArray[1] * 255), (int) (nmsColorArray[2] * 255)); + assertThat(color, is(nmsColor)); + } + + @Test + public void checkFireworkColor() { + Color color = dye.getFireworkColor(); + int nmsColor = ItemDye.DYE_COLORS[dye.getDyeData()]; + assertThat(color, is(Color.fromRGB(nmsColor))); + } +} diff --git a/src/test/java/org/bukkit/MaterialTest.java b/src/test/java/org/bukkit/MaterialTest.java new file mode 100644 index 0000000..383c3bf --- /dev/null +++ b/src/test/java/org/bukkit/MaterialTest.java @@ -0,0 +1,48 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import com.google.common.collect.Maps; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import net.minecraft.item.Item; +import org.bukkit.craftbukkit.v1_12_R1.util.CraftMagicNumbers; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class MaterialTest extends AbstractTestingBase { + + @Test + public void verifyMapping() { + Map materials = Maps.newHashMap(); + for (Material material : Material.values()) { + if (INVALIDATED_MATERIALS.contains(material)) { + continue; + } + + materials.put(material.getId(), material); + } + + Iterator items = Item.REGISTRY.iterator(); + + while (items.hasNext()) { + Item item = items.next(); + if (item == null) { + continue; + } + + int id = CraftMagicNumbers.getId(item); + String name = item.getUnlocalizedName(); + + Material material = materials.remove(id); + + assertThat("Missing " + name + "(" + id + ")", material, is(not(nullValue()))); + } + + assertThat(materials, is(Collections.EMPTY_MAP)); + } +} diff --git a/src/test/java/org/bukkit/NibbleArrayTest.java b/src/test/java/org/bukkit/NibbleArrayTest.java new file mode 100644 index 0000000..ccf23ed --- /dev/null +++ b/src/test/java/org/bukkit/NibbleArrayTest.java @@ -0,0 +1,29 @@ +package org.bukkit; + +import java.util.Random; +import net.minecraft.world.chunk.NibbleArray; +import org.junit.Assert; +import org.junit.Test; + +public class NibbleArrayTest { + + private static final int NIBBLE_SIZE = 4096; + + @Test + public void testNibble() { + Random random = new Random(); + byte[] classic = new byte[NIBBLE_SIZE]; + NibbleArray nibble = new NibbleArray(); + + for (int i = 0; i < classic.length; i++) { + byte number = (byte) (random.nextInt() & 0xF); + + classic[i] = number; + nibble.setIndex(i, number); + } + + for (int i = 0; i < classic.length; i++) { + Assert.assertEquals("Nibble array mismatch", classic[i], nibble.getFromIndex(i)); + } + } +} diff --git a/src/test/java/org/bukkit/ParticleTest.java b/src/test/java/org/bukkit/ParticleTest.java new file mode 100644 index 0000000..6491586 --- /dev/null +++ b/src/test/java/org/bukkit/ParticleTest.java @@ -0,0 +1,19 @@ +package org.bukkit; + +import net.minecraft.util.EnumParticleTypes; +import org.bukkit.craftbukkit.v1_12_R1.CraftParticle; +import org.junit.Assert; +import org.junit.Test; + +public class ParticleTest { + + @Test + public void verifyMapping() { + for (Particle bukkit : Particle.values()) { + Assert.assertNotNull("Missing Bukkit->NMS particle mapping", CraftParticle.toNMS(bukkit)); + } + for (EnumParticleTypes nms : EnumParticleTypes.values()) { + Assert.assertNotNull("Missing NMS->Bukkit particle mapping", CraftParticle.toBukkit(nms)); + } + } +} diff --git a/src/test/java/org/bukkit/PerMaterialTest.java b/src/test/java/org/bukkit/PerMaterialTest.java new file mode 100644 index 0000000..21db3e6 --- /dev/null +++ b/src/test/java/org/bukkit/PerMaterialTest.java @@ -0,0 +1,189 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.Lists; +import java.util.List; +import java.util.Map; +import net.minecraft.block.Block; +import net.minecraft.block.BlockFalling; +import net.minecraft.block.BlockFire; +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemRecord; +import net.minecraft.tileentity.TileEntityFurnace; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_12_R1.util.CraftMagicNumbers; +import org.bukkit.enchantments.EnchantmentTarget; +import org.bukkit.inventory.ItemStack; +import org.bukkit.support.AbstractTestingBase; +import org.bukkit.support.Util; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class PerMaterialTest extends AbstractTestingBase { + + private static Map fireValues; + @Parameter + public Material material; + + @BeforeClass + public static void getFireValues() { + fireValues = Util.getInternalState(BlockFire.class, Blocks.FIRE, "encouragements"); + } + + @Parameters(name = "{index}: {0}") + public static List data() { + List list = Lists.newArrayList(); + for (Material material : Material.values()) { + list.add(new Object[]{material}); + } + return list; + } + + @Test + public void isSolid() { + if (material == Material.AIR) { + assertFalse(material.isSolid()); + } else if (material.isBlock()) { + assertThat(material.isSolid(), is(CraftMagicNumbers.getBlock(material).getDefaultState().getMaterial().blocksMovement())); + } else { + assertFalse(material.isSolid()); + } + } + + @Test + public void isEdible() { + assertThat(material.isEdible(), is(CraftMagicNumbers.getItem(material) instanceof ItemFood)); + } + + @Test + public void isRecord() { + assertThat(material.isRecord(), is(CraftMagicNumbers.getItem(material) instanceof ItemRecord)); + } + + @Test + public void maxDurability() { + if (INVALIDATED_MATERIALS.contains(material)) { + return; + } + + if (material == Material.AIR) { + assertThat((int) material.getMaxDurability(), is(0)); + } else if (material.isBlock()) { + Item item = CraftMagicNumbers.getItem(material); + assertThat((int) material.getMaxDurability(), is(item.getMaxDamage())); + } + } + + @Test + public void maxStackSize() { + if (INVALIDATED_MATERIALS.contains(material)) { + return; + } + + final ItemStack bukkit = new ItemStack(material); + final CraftItemStack craft = CraftItemStack.asCraftCopy(bukkit); + if (material == Material.AIR) { + final int MAX_AIR_STACK = 0 /* Why can't I hold all of these AIR? */; + assertThat(material.getMaxStackSize(), is(MAX_AIR_STACK)); + assertThat(bukkit.getMaxStackSize(), is(MAX_AIR_STACK)); + assertThat(craft.getMaxStackSize(), is(MAX_AIR_STACK)); + } else { + assertThat(material.getMaxStackSize(), is(CraftMagicNumbers.getItem(material).getItemStackLimit())); + assertThat(bukkit.getMaxStackSize(), is(material.getMaxStackSize())); + assertThat(craft.getMaxStackSize(), is(material.getMaxStackSize())); + } + } + + @Test + public void isTransparent() { + if (material == Material.AIR) { + assertTrue(material.isTransparent()); + } else if (material.isBlock()) { + assertThat(material.isTransparent(), is(not(CraftMagicNumbers.getBlock(material).getDefaultState().getMaterial().blocksLight()))); + } else { + assertFalse(material.isTransparent()); + } + } + + @Test + public void isFlammable() { + if (material != Material.AIR && material.isBlock()) { + assertThat(material.isFlammable(), is(CraftMagicNumbers.getBlock(material).getDefaultState().getMaterial().getCanBurn())); + } else { + assertFalse(material.isFlammable()); + } + } + + @Test + public void isBurnable() { + if (material.isBlock()) { + Block block = CraftMagicNumbers.getBlock(material); + assertThat(material.isBurnable(), is(fireValues.containsKey(block) && fireValues.get(block) > 0)); + } else { + assertFalse(material.isBurnable()); + } + } + + @Test + public void isFuel() { + assertThat(material.isFuel(), is(TileEntityFurnace.isItemFuel(new net.minecraft.item.ItemStack(CraftMagicNumbers.getItem(material))))); + } + + @Test + public void isOccluding() { + if (material.isBlock()) { + assertThat(material.isOccluding(), is(CraftMagicNumbers.getBlock(material).isNormalCube(CraftMagicNumbers.getBlock(material).getDefaultState()))); + } else { + assertFalse(material.isOccluding()); + } + } + + @Test + public void hasGravity() { + if (material.isBlock()) { + assertThat(material.hasGravity(), is(CraftMagicNumbers.getBlock(material) instanceof BlockFalling)); + } else { + assertFalse(material.hasGravity()); + } + } + + @Test + public void usesDurability() { + if (!material.isBlock()) { + assertThat(EnchantmentTarget.BREAKABLE.includes(material), is(CraftMagicNumbers.getItem(material).isDamageable())); + } else { + assertFalse(EnchantmentTarget.BREAKABLE.includes(material)); + } + } + + @Test + public void testBlock() { + if (material == Material.AIR) { + assertTrue(material.isBlock()); + } else { + assertThat(material.isBlock(), is(equalTo(CraftMagicNumbers.getBlock(material) != Blocks.AIR))); + } + } + + @Test + public void testItem() { + if (material == Material.AIR) { + assertTrue(material.isItem()); + } else { + assertThat(material.isItem(), is(equalTo(CraftMagicNumbers.getItem(material) != null))); + } + } +} diff --git a/src/test/java/org/bukkit/SoundTest.java b/src/test/java/org/bukkit/SoundTest.java new file mode 100644 index 0000000..ec2c5a2 --- /dev/null +++ b/src/test/java/org/bukkit/SoundTest.java @@ -0,0 +1,43 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.SoundEvent; +import org.bukkit.craftbukkit.v1_12_R1.CraftSound; +import org.junit.Test; + +public class SoundTest { + + @Test + public void testGetSound() { + for (Sound sound : Sound.values()) { + assertThat(sound.name(), CraftSound.getSound(sound), is(not(nullValue()))); + } + } + + @Test + public void testReverse() { + for (ResourceLocation effect : SoundEvent.REGISTRY.getKeys()) { + assertNotNull(effect + "", Sound.valueOf(effect.getResourcePath().replace('.', '_').toUpperCase(java.util.Locale.ENGLISH))); + } + } + + @Test + public void testCategory() { + for (SoundCategory category : SoundCategory.values()) { + assertNotNull(category + "", SoundCategory.valueOf(category.name())); + } + } + + @Test + public void testCategoryReverse() { + for (SoundCategory category : SoundCategory.values()) { + assertNotNull(category + "", SoundCategory.valueOf(category.name())); + } + } +} diff --git a/src/test/java/org/bukkit/StatisticsAndAchievementsTest.java b/src/test/java/org/bukkit/StatisticsAndAchievementsTest.java new file mode 100644 index 0000000..1aa707b --- /dev/null +++ b/src/test/java/org/bukkit/StatisticsAndAchievementsTest.java @@ -0,0 +1,38 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import com.google.common.collect.HashMultiset; +import net.minecraft.stats.StatBase; +import net.minecraft.stats.StatList; +import org.bukkit.craftbukkit.v1_12_R1.CraftStatistic; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class StatisticsAndAchievementsTest extends AbstractTestingBase { + + @Test + @SuppressWarnings("unchecked") + public void verifyStatisticMapping() throws Throwable { + HashMultiset statistics = HashMultiset.create(); + for (StatBase statistic : StatList.ALL_STATS) { + String name = statistic.statId; + + String message = String.format("org.bukkit.Statistic is missing: '%s'", name); + + Statistic subject = CraftStatistic.getBukkitStatistic(statistic); + assertThat(message, subject, is(not(nullValue()))); + + statistics.add(subject); + } + + for (Statistic statistic : Statistic.values()) { + String message = String.format("org.bukkit.Statistic.%s does not have a corresponding minecraft statistic", statistic.name()); + assertThat(message, statistics.remove(statistic, statistics.count(statistic)), is(greaterThan(0))); + } + } +} diff --git a/src/test/java/org/bukkit/WorldTypeTest.java b/src/test/java/org/bukkit/WorldTypeTest.java new file mode 100644 index 0000000..fd8552d --- /dev/null +++ b/src/test/java/org/bukkit/WorldTypeTest.java @@ -0,0 +1,26 @@ +package org.bukkit; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import net.minecraft.world.WorldType; +import org.junit.Test; + +public class WorldTypeTest { + + @Test + public void testTypes() { + for (WorldType type : WorldType.WORLD_TYPES) { + if (type == null) { + continue; + } + if (type == WorldType.DEBUG_ALL_BLOCK_STATES) { + continue; // Doesn't work anyway + } + + assertThat(type.getName() + " has no Bukkit world", org.bukkit.WorldType.getByName(type.getName()), is(not(nullValue()))); + } + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/generator/ChunkDataTest.java b/src/test/java/org/bukkit/craftbukkit/generator/ChunkDataTest.java new file mode 100644 index 0000000..468ac89 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/generator/ChunkDataTest.java @@ -0,0 +1,84 @@ +package org.bukkit.craftbukkit.generator; + +import static org.junit.Assert.assertTrue; + +import org.bukkit.DyeColor; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_12_R1.generator.CraftChunkData; +import org.bukkit.material.MaterialData; +import org.bukkit.material.Wool; +import org.junit.Test; + +public class ChunkDataTest { + + private static final MaterialData RED_WOOL = new Wool(DyeColor.RED); + private static final MaterialData AIR = new MaterialData(Material.AIR); + + private boolean testSetBlock(CraftChunkData data, int x, int y, int z, MaterialData type, MaterialData expected) { + data.setBlock(x, y, z, type); + return expected.equals(data.getTypeAndData(x, y, z)); + } + + private void testSetRegion(CraftChunkData data, int minx, int miny, int minz, int maxx, int maxy, int maxz, MaterialData type) { + data.setRegion(minx, miny, minz, maxx, maxy, maxz, type); + for (int y = 0; y < data.getMaxHeight(); y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + boolean inRegion = miny <= y && y < maxy && minx <= x && x < maxx && minz <= z && z < maxz; + if (inRegion != type.equals(data.getTypeAndData(x, y, z))) { + throw new IllegalStateException( + "setRegion(" + minx + ", " + miny + ", " + minz + ", " + maxx + ", " + maxy + ", " + maxz + ", " + type + ")" + + "-> block at " + x + ", " + y + ", " + z + " is " + data.getTypeAndData(x, y, z)); + } + } + } + } + } + + @Test + public void testMaxHeight() { + CraftChunkData data = new CraftChunkData(128); + assertTrue("Could not set block above max height", testSetBlock(data, 0, 128, 0, RED_WOOL, AIR)); + assertTrue("Could set block below max height", testSetBlock(data, 0, 127, 0, RED_WOOL, RED_WOOL)); + } + + @Test + public void testBoundsCheckingSingle() { + CraftChunkData data = new CraftChunkData(256); + assertTrue("Can set block inside chunk bounds", testSetBlock(data, 0, 0, 0, RED_WOOL, RED_WOOL)); + assertTrue("Can set block inside chunk bounds", testSetBlock(data, 15, 255, 15, RED_WOOL, RED_WOOL)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, -1, 0, 0, RED_WOOL, AIR)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, 0, -1, 0, RED_WOOL, AIR)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, 0, 0, -1, RED_WOOL, AIR)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, 16, 0, 0, RED_WOOL, AIR)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, 0, 256, 0, RED_WOOL, AIR)); + assertTrue("Can no set block outside chunk bounds", testSetBlock(data, 0, 0, 16, RED_WOOL, AIR)); + } + + @Test + public void testSetRegion() { + CraftChunkData data = new CraftChunkData(256); + testSetRegion(data, -100, 0, -100, 0, 256, 0, RED_WOOL); // exclusively outside + testSetRegion(data, 16, 256, 16, 0, 0, 0, RED_WOOL); // minimum >= maximum + testSetRegion(data, 0, 0, 0, 0, 0, 0, RED_WOOL); // minimum == maximum + testSetRegion(data, 0, 0, 0, 16, 16, 16, RED_WOOL); // Whole Chunk Section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 8, 0, 16, 24, 16, RED_WOOL); // Start middle of this section, end middle of next + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 4, 0, 16, 12, 16, RED_WOOL); // Start in this section, end in this section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 0, 0, 16, 16, 1, RED_WOOL); // Whole Chunk Section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 8, 0, 16, 24, 1, RED_WOOL); // Start middle of this section, end middle of next + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 4, 0, 16, 12, 1, RED_WOOL); // Start in this section, end in this section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 0, 0, 1, 16, 1, RED_WOOL); // Whole Chunk Section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 8, 0, 1, 24, 1, RED_WOOL); // Start middle of this section, end middle of next + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 4, 0, 1, 12, 1, RED_WOOL); // Start in this section, end in this section + data.setRegion(0, 0, 0, 16, 256, 16, AIR); + testSetRegion(data, 0, 0, 0, 1, 1, 1, RED_WOOL); // Set single block. + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/CompositeSerialization.java b/src/test/java/org/bukkit/craftbukkit/inventory/CompositeSerialization.java new file mode 100644 index 0000000..1dc6966 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/CompositeSerialization.java @@ -0,0 +1,61 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + + +public class CompositeSerialization extends AbstractTestingBase { + + public YamlConfiguration getConfig() { + return new YamlConfiguration(); + } + + @Test + public void testSaveRestoreCompositeList() throws InvalidConfigurationException { + YamlConfiguration out = getConfig(); + + List stacks = new ArrayList(); + stacks.add(new ItemStack(1)); + stacks.add(new ItemStack(2)); + stacks.add(new ItemStack(3)); + stacks.add(new ItemStack(4, 17)); + stacks.add(new ItemStack(5, 63)); + stacks.add(new ItemStack(6, 1, (short) 1)); + stacks.add(new ItemStack(18, 32, (short) 2)); + + ItemStack item7 = new ItemStack(256); + item7.addUnsafeEnchantment(Enchantment.getById(1), 1); + stacks.add(item7); + + ItemStack item8 = new ItemStack(257); + item8.addUnsafeEnchantment(Enchantment.getById(2), 2); + item8.addUnsafeEnchantment(Enchantment.getById(3), 1); + item8.addUnsafeEnchantment(Enchantment.getById(4), 5); + item8.addUnsafeEnchantment(Enchantment.getById(5), 4); + stacks.add(item8); + + out.set("composite-list.abc.def", stacks); + String yaml = out.saveToString(); + + YamlConfiguration in = new YamlConfiguration(); + in.loadFromString(yaml); + List raw = in.getList("composite-list.abc.def"); + + assertThat(stacks, hasSize(raw.size())); + + for (int i = 0; i < 9; i++) { + assertThat(String.valueOf(i), (Object) stacks.get(i), is(raw.get(i))); + } + } +} + diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/FactoryItemMaterialTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/FactoryItemMaterialTest.java new file mode 100644 index 0000000..1baa928 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/FactoryItemMaterialTest.java @@ -0,0 +1,163 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class FactoryItemMaterialTest extends AbstractTestingBase { + + static final ItemFactory factory = CraftItemFactory.instance(); + static final StringBuilder buffer = new StringBuilder(); + static final Material[] materials; + + static { + Material[] local_materials = Material.values(); + List list = new ArrayList(local_materials.length); + for (Material material : local_materials) { + if (INVALIDATED_MATERIALS.contains(material)) { + continue; + } + + list.add(material); + } + materials = list.toArray(new Material[list.size()]); + } + + @Parameter(0) + public Material material; + + static String name(Enum from, Enum to) { + if (from.getClass() == to.getClass()) { + return buffer.delete(0, Integer.MAX_VALUE).append(from.getClass().getName()).append(' ').append(from.name()).append(" to ").append(to.name()).toString(); + } + return buffer.delete(0, Integer.MAX_VALUE).append(from.getClass().getName()).append('(').append(from.name()).append(") to ").append(to.getClass().getName()).append('(').append(to.name()) + .append(')').toString(); + } + + @Parameters(name = "Material[{index}]:{0}") + public static List data() { + List list = new ArrayList(); + for (Material material : materials) { + list.add(new Object[]{material}); + } + return list; + } + + @Test + public void itemStack() { + ItemStack bukkitStack = new ItemStack(material); + CraftItemStack craftStack = CraftItemStack.asCraftCopy(bukkitStack); + ItemMeta meta = factory.getItemMeta(material); + if (meta == null) { + assertThat(material, is(Material.AIR)); + } else { + assertTrue(factory.isApplicable(meta, bukkitStack)); + assertTrue(factory.isApplicable(meta, craftStack)); + } + } + + @Test + public void generalCase() { + CraftMetaItem meta = (CraftMetaItem) factory.getItemMeta(material); + if (meta == null) { + assertThat(material, is(Material.AIR)); + } else { + assertTrue(factory.isApplicable(meta, material)); + assertTrue(meta.applicableTo(material)); + + meta = meta.clone(); + assertTrue(factory.isApplicable(meta, material)); + assertTrue(meta.applicableTo(material)); + } + } + + @Test + public void asMetaFor() { + final CraftMetaItem baseMeta = (CraftMetaItem) factory.getItemMeta(material); + if (baseMeta == null) { + assertThat(material, is(Material.AIR)); + return; + } + + for (Material other : materials) { + final ItemStack bukkitStack = new ItemStack(other); + final CraftItemStack craftStack = CraftItemStack.asCraftCopy(bukkitStack); + final CraftMetaItem otherMeta = (CraftMetaItem) factory.asMetaFor(baseMeta, other); + + final String testName = name(material, other); + + if (otherMeta == null) { + assertThat(testName, other, is(Material.AIR)); + continue; + } + + assertTrue(testName, factory.isApplicable(otherMeta, craftStack)); + assertTrue(testName, factory.isApplicable(otherMeta, bukkitStack)); + assertTrue(testName, factory.isApplicable(otherMeta, other)); + assertTrue(testName, otherMeta.applicableTo(other)); + } + } + + @Test + public void blankEqualities() { + if (material == Material.AIR) { + return; + } + final CraftMetaItem baseMeta = (CraftMetaItem) factory.getItemMeta(material); + final CraftMetaItem baseMetaClone = baseMeta.clone(); + + final ItemStack baseMetaStack = new ItemStack(material); + baseMetaStack.setItemMeta(baseMeta); + + assertThat(baseMeta, is(not(sameInstance(baseMetaStack.getItemMeta())))); + + assertTrue(factory.equals(baseMeta, null)); + assertTrue(factory.equals(null, baseMeta)); + + assertTrue(factory.equals(baseMeta, baseMetaClone)); + assertTrue(factory.equals(baseMetaClone, baseMeta)); + + assertThat(baseMeta, is(not(sameInstance(baseMetaClone)))); + + assertThat(baseMeta, is(baseMetaClone)); + assertThat(baseMetaClone, is(baseMeta)); + + for (Material other : materials) { + final String testName = name(material, other); + + final CraftMetaItem otherMeta = (CraftMetaItem) factory.asMetaFor(baseMetaClone, other); + + if (otherMeta == null) { + assertThat(testName, other, is(Material.AIR)); + continue; + } + + assertTrue(testName, factory.equals(baseMeta, otherMeta)); + assertTrue(testName, factory.equals(otherMeta, baseMeta)); + + assertThat(testName, baseMeta, is(otherMeta)); + assertThat(testName, otherMeta, is(baseMeta)); + + assertThat(testName, baseMeta.hashCode(), is(otherMeta.hashCode())); + } + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemFactoryTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemFactoryTest.java new file mode 100644 index 0000000..c15d0ca --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemFactoryTest.java @@ -0,0 +1,74 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import net.minecraft.command.CommandBase; +import net.minecraft.entity.ai.attributes.IAttribute; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class ItemFactoryTest extends AbstractTestingBase { + + @Test + public void testKnownAttributes() throws Throwable { + final Collection names = new HashSet<>(); + File classesDirectory = new File(CommandBase.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); + Collection allClassFiles = getMinecraftServerClasses(getAllClassesFilesFromDir(classesDirectory)); + for (Class clazz : allClassFiles) { + assertThat(clazz.getName(), clazz, is(not(nullValue()))); + for (final Field field : clazz.getDeclaredFields()) { + if (IAttribute.class.isAssignableFrom(field.getType()) && Modifier.isStatic(field.getModifiers())) { + field.setAccessible(true); + final String attributeName = ((IAttribute) field.get(null)).getName(); + assertThat("Logical error: duplicate name `" + attributeName + "' in " + clazz.getName(), names.add(attributeName), is(true)); + assertThat(clazz.getName(), CraftItemFactory.KNOWN_NBT_ATTRIBUTE_NAMES, hasItem(attributeName)); + } + } + } + assertThat("Extra values detected", CraftItemFactory.KNOWN_NBT_ATTRIBUTE_NAMES, is(names)); + } + + private Collection getAllClassesFilesFromDir(File directory) { + if (!directory.exists() || !directory.isDirectory()) { + throw new IllegalStateException("File " + directory.getName() + " does not exist or is not a directory!"); + } + Collection allClasses = new HashSet<>(); + for (File fileInDir : directory.listFiles()) { + if (fileInDir.isDirectory()) { + allClasses.addAll(this.getAllClassesFilesFromDir(fileInDir)); + } else if (fileInDir.getName().endsWith(".class")) { + allClasses.add(fileInDir); + } + } + return allClasses; + } + + private Collection getMinecraftServerClasses(Collection files) { + Collection minecraftClasses = new ArrayList<>(); + String essentialClassPathElement = File.separator + "net" + File.separator + "minecraft" + File.separator; + files.stream().filter(file -> file.getPath().contains(essentialClassPathElement)).forEach(minecraftClassFile -> { + String absoluteClassPath = minecraftClassFile.getPath().replace(File.separator, "."); + int from = absoluteClassPath.indexOf("net.minecraft."); + int to = absoluteClassPath.length() - ".class".length(); + Class serverClass; + try { + serverClass = Class.forName(absoluteClassPath.substring(from, to)); + } catch (Throwable ignoreClientClass) { + return; + } + minecraftClasses.add(serverClass); + }); + return minecraftClasses; + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java new file mode 100644 index 0000000..b8b0109 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java @@ -0,0 +1,25 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import java.lang.reflect.Method; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.junit.Test; + +public class ItemMetaCloneTest { + + @Test + public void testClone() throws Throwable { + for (Material material : ItemStackTest.COMPOUND_MATERIALS) { + Class clazz = CraftItemFactory.instance().getItemMeta(material).getClass(); + + Method clone = clazz.getDeclaredMethod("clone"); + assertNotNull("Class " + clazz + " does not override clone()", clone); + assertThat("Class " + clazz + " clone return type does not match", clone.getReturnType(), is(equalTo(clazz))); + } + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java new file mode 100644 index 0000000..3fba70e --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java @@ -0,0 +1,85 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import org.bukkit.Material; +import org.bukkit.configuration.serialization.DelegateDeserialization; +import org.bukkit.craftbukkit.v1_12_R1.Overridden; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class ItemMetaImplementationOverrideTest { + + static final Class parent = CraftMetaItem.class; + @Parameter(0) + public Callable test; + @Parameter(1) + public String name; + + @Parameters(name = "[{index}]:{1}") + public static List data() { + final List testData = new ArrayList(); + List> classes = new ArrayList>(); + + for (Material material : ItemStackTest.COMPOUND_MATERIALS) { + Class clazz = CraftItemFactory.instance().getItemMeta(material).getClass().asSubclass(parent); + if (clazz != parent) { + classes.add(clazz); + } + } + + List list = new ArrayList(); + + for (Method method : parent.getDeclaredMethods()) { + if (method.isAnnotationPresent(Overridden.class)) { + list.add(method); + } + } + + for (final Class clazz : classes) { + for (final Method method : list) { + testData.add( + new Object[]{ + new Callable() { + public Method call() throws Exception { + return clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); + } + }, + clazz.getSimpleName() + " contains " + method.getName() + } + ); + } + + testData.add( + new Object[]{ + new Callable() { + public DelegateDeserialization call() throws Exception { + return clazz.getAnnotation(DelegateDeserialization.class); + } + }, + clazz.getSimpleName() + " contains annotation " + DelegateDeserialization.class + } + ); + } + + return testData; + } + + @Test + public void testClass() throws Throwable { + assertThat(name, test.call(), is(not(nullValue()))); + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java new file mode 100644 index 0000000..61b9572 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -0,0 +1,326 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; +import net.minecraft.block.Block; +import net.minecraft.block.ITileEntityProvider; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemBlockSpecial; +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.DyeColor; +import org.bukkit.FireworkEffect; +import org.bukkit.FireworkEffect.Type; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.block.banner.Pattern; +import org.bukkit.block.banner.PatternType; +import org.bukkit.craftbukkit.inventory.ItemStackTest.BukkitWrapper; +import org.bukkit.craftbukkit.inventory.ItemStackTest.CraftWrapper; +import org.bukkit.craftbukkit.inventory.ItemStackTest.StackProvider; +import org.bukkit.craftbukkit.inventory.ItemStackTest.StackWrapper; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.EntityType; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BannerMeta; +import org.bukkit.inventory.meta.BlockStateMeta; +import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; +import org.bukkit.inventory.meta.FireworkEffectMeta; +import org.bukkit.inventory.meta.FireworkMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.KnowledgeBookMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.MapMeta; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.SpawnEggMeta; +import org.bukkit.potion.PotionData; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class ItemMetaTest extends AbstractTestingBase { + + static final int MAX_FIREWORK_POWER = 127; // Please update ItemStackFireworkTest if/when this gets changed. + + private static FireworkMeta newFireworkMeta() { + return ((FireworkMeta) Bukkit.getItemFactory().getItemMeta(Material.FIREWORK)); + } + + @Test(expected = IllegalArgumentException.class) + public void testPowerLimitExact() { + newFireworkMeta().setPower(MAX_FIREWORK_POWER + 1); + } + + @Test(expected = IllegalArgumentException.class) + public void testPowerLimitMax() { + newFireworkMeta().setPower(Integer.MAX_VALUE); + } + + @Test(expected = IllegalArgumentException.class) + public void testPowerLimitMin() { + newFireworkMeta().setPower(Integer.MIN_VALUE); + } + + @Test(expected = IllegalArgumentException.class) + public void testPowerLimitNegative() { + newFireworkMeta().setPower(-1); + } + + @Test + public void testPowers() { + for (int i = 0; i <= MAX_FIREWORK_POWER; i++) { + FireworkMeta firework = newFireworkMeta(); + firework.setPower(i); + assertThat(String.valueOf(i), firework.getPower(), is(i)); + } + } + + @Test + public void testConflictingEnchantment() { + ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(Material.DIAMOND_PICKAXE); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.DURABILITY), is(false)); + + itemMeta.addEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.DURABILITY), is(false)); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingEnchant(null), is(false)); + } + + @Test + public void testConflictingStoredEnchantment() { + EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); + + itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); + } + + @Test + public void testConflictingEnchantments() { + ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(Material.DIAMOND_PICKAXE); + itemMeta.addEnchant(Enchantment.DURABILITY, 6, true); + itemMeta.addEnchant(Enchantment.DIG_SPEED, 6, true); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(false)); + + itemMeta.addEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingEnchant(null), is(false)); + } + + @Test + public void testConflictingStoredEnchantments() { + EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); + itemMeta.addStoredEnchant(Enchantment.DURABILITY, 6, true); + itemMeta.addStoredEnchant(Enchantment.DIG_SPEED, 6, true); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(false)); + + itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); + } + + @Test + public void testCrazyEquality() { + CraftItemStack craft = CraftItemStack.asCraftCopy(new ItemStack(1)); + craft.setItemMeta(craft.getItemMeta()); + ItemStack bukkit = new ItemStack(craft); + assertThat(craft, is(bukkit)); + assertThat(bukkit, is(craft)); + } + + @Test + public void testBlockStateMeta() { + for (Item item : Item.REGISTRY) { + Block block = null; + + if (item instanceof ItemBlock) { + block = ((ItemBlock) item).getBlock(); + } else if (item instanceof ItemBlockSpecial) { + block = ((ItemBlockSpecial) item).getBlock(); + } + + if (block != null) { + if (block instanceof ITileEntityProvider) { + ItemStack stack = CraftItemStack.asNewCraftStack(Item.getItemFromBlock(block)); + + // Command blocks aren't unit testable atm + if (stack.getType() == Material.AIR || stack.getType() == Material.COMMAND || stack.getType() == Material.COMMAND_CHAIN || stack.getType() == Material.COMMAND_REPEATING) { + return; + } + + ItemMeta meta = stack.getItemMeta(); + assertTrue(stack + " has meta of type " + meta + " expected BlockStateMeta", meta instanceof BlockStateMeta); + + BlockStateMeta blockState = (BlockStateMeta) meta; + assertNotNull(stack + " has null block state", blockState.getBlockState()); + + blockState.setBlockState(blockState.getBlockState()); + } + } + } + } + + @Test + public void testEachExtraData() { + final List providers = Arrays.asList( + new StackProvider(Material.BOOK_AND_QUILL) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final BookMeta meta = (BookMeta) cleanStack.getItemMeta(); + meta.setAuthor("Some author"); + meta.setPages("Page 1", "Page 2"); + meta.setTitle("A title"); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.WRITTEN_BOOK) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final BookMeta meta = (BookMeta) cleanStack.getItemMeta(); + meta.setAuthor("Some author"); + meta.setPages("Page 1", "Page 2"); + meta.setTitle("A title"); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + /* Skulls rely on a running server instance + new StackProvider(Material.SKULL_ITEM) { + @Override ItemStack operate(final ItemStack cleanStack) { + final SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); + meta.setOwner("Notch"); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + */ + new StackProvider(Material.MAP) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final MapMeta meta = (MapMeta) cleanStack.getItemMeta(); + meta.setScaling(true); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.LEATHER_BOOTS) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); + meta.setColor(Color.FUCHSIA); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.POTION) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); + meta.setBasePotionData(new PotionData(PotionType.UNCRAFTABLE, false, false)); + meta.addCustomEffect(PotionEffectType.CONFUSION.createEffect(1, 1), false); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.FIREWORK) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); + meta.addEffect(FireworkEffect.builder().withColor(Color.GREEN).withFade(Color.OLIVE).with(Type.BALL_LARGE).build()); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.ENCHANTED_BOOK) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); + meta.addStoredEnchant(Enchantment.ARROW_FIRE, 1, true); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.FIREWORK_CHARGE) { + @Override + ItemStack operate(final ItemStack cleanStack) { + final FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); + meta.setEffect(FireworkEffect.builder().withColor(Color.MAROON, Color.BLACK).with(Type.CREEPER).withFlicker().build()); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.BANNER) { + @Override + ItemStack operate(ItemStack cleanStack) { + final BannerMeta meta = (BannerMeta) cleanStack.getItemMeta(); + meta.setBaseColor(DyeColor.CYAN); + meta.addPattern(new Pattern(DyeColor.WHITE, PatternType.BRICKS)); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.MONSTER_EGG) { + @Override + ItemStack operate(ItemStack cleanStack) { + final SpawnEggMeta meta = (SpawnEggMeta) cleanStack.getItemMeta(); + meta.setSpawnedType(EntityType.ZOMBIE); + cleanStack.setItemMeta(meta); + return cleanStack; + } + }, + new StackProvider(Material.KNOWLEDGE_BOOK) { + @Override + ItemStack operate(ItemStack cleanStack) { + final KnowledgeBookMeta meta = (KnowledgeBookMeta) cleanStack.getItemMeta(); + meta.addRecipe(new NamespacedKey("minecraft", "test"), new NamespacedKey("plugin", "test")); + cleanStack.setItemMeta(meta); + return cleanStack; + } + } + ); + + assertThat("Forgotten test?", providers, hasSize(ItemStackTest.COMPOUND_MATERIALS.length - 3/* Normal item meta, skulls and tile entities */)); + + for (final StackProvider provider : providers) { + downCastTest(new BukkitWrapper(provider)); + downCastTest(new CraftWrapper(provider)); + } + } + + private void downCastTest(final StackWrapper provider) { + final String name = provider.toString(); + final ItemStack blank = new ItemStack(1); + final ItemStack craftBlank = CraftItemStack.asCraftCopy(blank); + + downCastTest(name, provider.stack(), blank); + blank.setItemMeta(blank.getItemMeta()); + downCastTest(name, provider.stack(), blank); + + downCastTest(name, provider.stack(), craftBlank); + craftBlank.setItemMeta(craftBlank.getItemMeta()); + downCastTest(name, provider.stack(), craftBlank); + } + + private void downCastTest(final String name, final ItemStack stack, final ItemStack blank) { + assertThat(name, stack, is(not(blank))); + assertThat(name, stack.getItemMeta(), is(not(blank.getItemMeta()))); + + stack.setTypeId(1); + + assertThat(name, stack, is(blank)); + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackBookTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackBookTest.java new file mode 100644 index 0000000..9cde985 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackBookTest.java @@ -0,0 +1,201 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackBookTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.WRITTEN_BOOK, Material.BOOK_AND_QUILL); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("1110", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 1", "Page 2"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Pages vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 1", "Page 2"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.setItemMeta(cleanStack.getItemMeta()); +// return cleanStack; +// } +// }, +// "Pages vs. blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 1", "Page 2"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 2", "Page 1"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Pages switched" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 1", "Page 2"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.addPage("Page 1"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Pages short" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setAuthor("AnAuthor"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Author vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setAuthor("AnAuthor"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.setItemMeta(cleanStack.getItemMeta()); +// return cleanStack; +// } +// }, +// "Author vs. blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setAuthor("AnAuthor"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setAuthor("AnotherAuthor"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Authors" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setTitle("Some title"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Title vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setTitle("Some title"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.setItemMeta(cleanStack.getItemMeta()); +// return cleanStack; +// } +// }, +// "title vs. blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setTitle("Some title"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// BookMeta meta = (BookMeta) cleanStack.getItemMeta(); +// meta.setTitle("Different title"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Titles" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackEnchantStorageTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackEnchantStorageTest.java new file mode 100644 index 0000000..7330fdd --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackEnchantStorageTest.java @@ -0,0 +1,95 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackEnchantStorageTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.ENCHANTED_BOOK); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addStoredEnchant(Enchantment.DURABILITY, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Enchantable vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addStoredEnchant(Enchantment.KNOCKBACK, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Enchantable vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addStoredEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addStoredEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); +// meta.addStoredEnchant(Enchantment.FIRE_ASPECT, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Enchantable vs More" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addStoredEnchant(Enchantment.PROTECTION_FIRE, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// EnchantmentStorageMeta meta = (EnchantmentStorageMeta) cleanStack.getItemMeta(); +// meta.addEnchant(Enchantment.PROTECTION_FIRE, 2, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Enchantable vs Other" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkChargeTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkChargeTest.java new file mode 100644 index 0000000..4c6d42e --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkChargeTest.java @@ -0,0 +1,113 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackFireworkChargeTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.FIREWORK_CHARGE); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.BLACK).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect Color 1 vs. Effect Color 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).with(Type.CREEPER).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).with(Type.BURST).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect type 1 vs. Effect type 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).withFade(Color.BLUE).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).withFade(Color.RED).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect fade 1 vs. Effect fade 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).withFlicker().build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkEffectMeta meta = (FireworkEffectMeta) cleanStack.getItemMeta(); +// meta.setEffect(FireworkEffect.builder().withColor(Color.WHITE).withTrail().build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Effect vs. None" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkTest.java new file mode 100644 index 0000000..cdc697b --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackFireworkTest.java @@ -0,0 +1,169 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackFireworkTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.FIREWORK); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("110", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.BLACK).build()); +// meta.addEffect(FireworkEffect.builder().withColor(Color.GREEN).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect Color 1 vs. Effect Color 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).with(Type.CREEPER).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).with(Type.BURST).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect type 1 vs. Effect type 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).withFade(Color.BLUE).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).withFade(Color.RED).build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect fade 1 vs. Effect fade 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).withFlicker().build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Effect vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.addEffect(FireworkEffect.builder().withColor(Color.WHITE).withTrail().build()); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Effect vs. None" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.setPower(127); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.setPower(100); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Height vs. Other" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.setPower(42); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Height vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// FireworkMeta meta = (FireworkMeta) cleanStack.getItemMeta(); +// meta.setPower(10); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Height vs. None" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java new file mode 100644 index 0000000..6732378 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java @@ -0,0 +1,75 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackLeatherTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.LEATHER_BOOTS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET, Material.LEATHER_LEGGINGS); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); +// meta.setColor(Color.FUCHSIA); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Color vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); +// meta.setColor(Color.GRAY); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Color vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); +// meta.setColor(Color.MAROON); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// LeatherArmorMeta meta = (LeatherArmorMeta) cleanStack.getItemMeta(); +// meta.setColor(Color.ORANGE); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Color vs Other" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLoreEnchantmentTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLoreEnchantmentTest.java new file mode 100644 index 0000000..735ac7f --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLoreEnchantmentTest.java @@ -0,0 +1,284 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackLoreEnchantmentTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, ItemStackTest.COMPOUND_MATERIALS); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// ~0l, +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setLore(Arrays.asList("First Lore", "Second Lore")); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Lore vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setLore(Arrays.asList("Some lore")); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Lore vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setLore(Arrays.asList("Some more lore", "Another lore")); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setLore(Arrays.asList("Some more lore")); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Lore vs Other" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setDisplayName("TestItemName"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Name vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setDisplayName("AnotherItemName"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Name vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setDisplayName("The original ItemName"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.setDisplayName("The other name"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Name vs Other" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.addUnsafeEnchantment(Enchantment.DIG_SPEED, 2); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "EnchantStack vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.addUnsafeEnchantment(Enchantment.OXYGEN, 1); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "EnchantStack vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// cleanStack.addUnsafeEnchantment(Enchantment.ARROW_FIRE, 1); +// return cleanStack; +// } +// }, +// "EnchantStack vs OtherEnchantStack" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.addEnchant(Enchantment.DURABILITY, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Enchant vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.addEnchant(Enchantment.KNOCKBACK, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Enchant vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.addEnchant(Enchantment.PROTECTION_FIRE, 1, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// meta.addEnchant(Enchantment.PROTECTION_FIRE, 2, true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Enchant vs Other" +// } +// ), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// ((Repairable) meta).setRepairCost(42); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Repair vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// ((Repairable) meta).setRepairCost(36); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Repair vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// ((Repairable) meta).setRepairCost(89); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// ItemMeta meta = cleanStack.getItemMeta(); +// ((Repairable) meta).setRepairCost(88); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Repair vs Other" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackMapTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackMapTest.java new file mode 100644 index 0000000..706b03d --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackMapTest.java @@ -0,0 +1,109 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackMapTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.MAP); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Scale vs. Unscale" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Scale vs. Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Unscale vs. Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(true); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Scale vs. None" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// MapMeta meta = (MapMeta) cleanStack.getItemMeta(); +// meta.setScaling(false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Unscale vs. None" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackPotionsTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackPotionsTest.java new file mode 100644 index 0000000..d540e70 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackPotionsTest.java @@ -0,0 +1,133 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackPotionsTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.POTION); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.CONFUSION.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Potion vs Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.HARM.createEffect(2, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Potion vs Blank" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.SLOW_DIGGING.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.FAST_DIGGING.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Potion vs Harder" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.JUMP.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.JUMP.createEffect(1, 1), false); +// meta.addCustomEffect(PotionEffectType.REGENERATION.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Potion vs Better" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.SPEED.createEffect(10, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.SPEED.createEffect(5, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Potion vs Faster" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.INCREASE_DAMAGE.createEffect(1, 1), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// final PotionMeta meta = (PotionMeta) cleanStack.getItemMeta(); +// meta.addCustomEffect(PotionEffectType.INCREASE_DAMAGE.createEffect(1, 2), false); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Potion vs Stronger" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackSkullTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackSkullTest.java new file mode 100644 index 0000000..fc6de1b --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackSkullTest.java @@ -0,0 +1,76 @@ +package org.bukkit.craftbukkit.inventory; + +// @RunWith(Parameterized.class) +//public class ItemStackSkullTest extends ItemStackTest { +// +// @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") +// public static List data() { +// return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.SKULL_ITEM); +// } +// +// @SuppressWarnings("unchecked") +// static List operators() { +// return CompoundOperator.compound( +// Joiner.on('+'), +// NAME_PARAMETER, +// Long.parseLong("10", 2), +// ItemStackLoreEnchantmentTest.operators(), +// Arrays.asList( +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); +// meta.setOwner("Notch"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); +// meta.setOwner("Dinnerbone"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Name 1 vs. Name 2" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); +// meta.setOwner("Notch"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); +// meta.setOwner(null); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// "Name vs. Null" +// }, +// new Object[] { +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// SkullMeta meta = (SkullMeta) cleanStack.getItemMeta(); +// meta.setOwner("Notch"); +// cleanStack.setItemMeta(meta); +// return cleanStack; +// } +// }, +// new Operator() { +// public ItemStack operate(ItemStack cleanStack) { +// return cleanStack; +// } +// }, +// "Name vs. None" +// } +// ) +// ); +// } +//} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackTest.java new file mode 100644 index 0000000..be12ecc --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackTest.java @@ -0,0 +1,501 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.bukkit.support.Matchers.sameHash; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.bukkit.Material; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.support.AbstractTestingBase; +import org.bukkit.util.io.BukkitObjectInputStream; +import org.bukkit.util.io.BukkitObjectOutputStream; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; + +@RunWith(Parameterized.class) +public class ItemStackTest extends AbstractTestingBase { + + static final Object[][] EMPTY_ARRAY = new Object[0][]; + /** + * Materials that generate unique item meta types. + */ + static final Material[] COMPOUND_MATERIALS; + static final int NAME_PARAMETER = 2; + + static { + final ItemFactory factory = CraftItemFactory.instance(); + final Map, Material> possibleMaterials = new HashMap, Material>(); + ItemMeta meta; + for (final Material material : Material.values()) { + meta = factory.getItemMeta(material); + if (meta == null || possibleMaterials.containsKey(meta.getClass())) { + continue; + } + possibleMaterials.put(meta.getClass(), material); + + } + COMPOUND_MATERIALS = possibleMaterials.values().toArray(new Material[possibleMaterials.size()]); + } + + @Parameter(0) + public StackProvider provider; + @Parameter(1) + public StackProvider unequalProvider; + @Parameter(NAME_PARAMETER) + public String name; + + @Parameters(name = "[{index}]:{" + NAME_PARAMETER + "}") + public static List data() { + return ImmutableList.of(); // TODO, test basic durability issues + } + + static void testInequality(StackWrapper provider, StackWrapper unequalProvider) { + final ItemStack stack = provider.stack(); + final ItemStack stack2 = provider.stack(); + assertThat(stack, allOf(equalTo(stack), sameHash(stack))); + assertThat(stack, is(not(sameInstance(stack2)))); + assertThat(stack, allOf(equalTo(stack2), sameHash(stack2))); + + final ItemStack unequalStack = unequalProvider.stack(); + final ItemStack unequalStack2 = unequalProvider.stack(); + assertThat(unequalStack, allOf(equalTo(unequalStack), sameHash(unequalStack))); + assertThat(unequalStack, is(not(sameInstance(unequalStack2)))); + assertThat(unequalStack, allOf(equalTo(unequalStack2), sameHash(unequalStack2))); + + assertThat(stack, is(not(unequalStack))); + assertThat(unequalStack, is(not(stack))); + + final ItemStack newStack = new ItemStack(stack2); + assertThat(newStack, allOf(equalTo(stack), sameHash(stack))); + assertThat(newStack, is(not(unequalStack))); + assertThat(newStack.getItemMeta(), allOf(equalTo(stack.getItemMeta()), sameHash(stack.getItemMeta()))); + assertThat(newStack.getItemMeta(), is(not(unequalStack.getItemMeta()))); + + final ItemStack craftStack = CraftItemStack.asCraftCopy(stack2); + assertThat(craftStack, allOf(equalTo(stack), sameHash(stack))); + assertThat(craftStack, is(not(unequalStack))); + assertThat(craftStack.getItemMeta(), allOf(equalTo(stack.getItemMeta()), sameHash(stack.getItemMeta()))); + assertThat(craftStack.getItemMeta(), is(not(unequalStack.getItemMeta()))); + + final ItemStack newUnequalStack = new ItemStack(unequalStack2); + assertThat(newUnequalStack, allOf(equalTo(unequalStack), sameHash(unequalStack))); + assertThat(newUnequalStack, is(not(stack))); + assertThat(newUnequalStack.getItemMeta(), allOf(equalTo(unequalStack.getItemMeta()), sameHash(unequalStack.getItemMeta()))); + assertThat(newUnequalStack.getItemMeta(), is(not(stack.getItemMeta()))); + + final ItemStack newUnequalCraftStack = CraftItemStack.asCraftCopy(unequalStack2); + assertThat(newUnequalCraftStack, allOf(equalTo(unequalStack), sameHash(unequalStack))); + assertThat(newUnequalCraftStack, is(not(stack))); + assertThat(newUnequalCraftStack.getItemMeta(), allOf(equalTo(unequalStack.getItemMeta()), sameHash(unequalStack.getItemMeta()))); + assertThat(newUnequalCraftStack.getItemMeta(), is(not(stack.getItemMeta()))); + } + + static void testStreamDeserialize(StackWrapper provider, StackWrapper unequalProvider) throws Throwable { + final ItemStack stack = provider.stack(); + final ItemStack unequalStack = unequalProvider.stack(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream oos = null; + try { + oos = new BukkitObjectOutputStream(out); + + oos.writeObject(stack); + oos.writeObject(unequalStack); + } finally { + if (oos != null) { + try { + oos.close(); + } catch (IOException ex) { + } + } + } + + final String data = new String(Base64Coder.encode(out.toByteArray())); + + ObjectInputStream ois = null; + + final ItemStack readFirst; + final ItemStack readSecond; + + try { + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + ois = new BukkitObjectInputStream(in); + + readFirst = (ItemStack) ois.readObject(); + readSecond = (ItemStack) ois.readObject(); + } finally { + if (ois != null) { + try { + ois.close(); + } catch (IOException ex) { + } + } + } + + testEqualities(data, readFirst, readSecond, stack, unequalStack); + } + + static void testYamlDeserialize(StackWrapper provider, StackWrapper unequalProvider) { + final ItemStack stack = provider.stack(); + final ItemStack unequalStack = unequalProvider.stack(); + final YamlConfiguration configOut = new YamlConfiguration(); + + configOut.set("provider", stack); + configOut.set("unequal", unequalStack); + + final String out = '\n' + configOut.saveToString(); + final YamlConfiguration configIn = new YamlConfiguration(); + + try { + configIn.loadFromString(out); + } catch (InvalidConfigurationException ex) { + throw new RuntimeException(out, ex); + } + + testEqualities(out, configIn.getItemStack("provider"), configIn.getItemStack("unequal"), stack, unequalStack); + } + + static void testEqualities(String information, ItemStack primaryRead, ItemStack unequalRead, ItemStack primaryOriginal, ItemStack unequalOriginal) { + assertThat(information, primaryRead, allOf(equalTo(primaryOriginal), sameHash(primaryOriginal))); + assertThat(information, unequalRead, allOf(equalTo(unequalOriginal), sameHash(unequalOriginal))); + assertThat(information, primaryRead, is(not(unequalOriginal))); + assertThat(information, primaryRead, is(not(unequalRead))); + } + + // TODO: Fix this tests + // @Test + public void testBukkitInequality() { + final StackWrapper bukkitWrapper = new CraftWrapper(provider); + testInequality(bukkitWrapper, new BukkitWrapper(unequalProvider)); + testInequality(bukkitWrapper, new BukkitWrapper(new NoOpProvider(provider.material))); + } + + // @Test + public void testCraftInequality() { + final StackWrapper craftWrapper = new CraftWrapper(provider); + testInequality(craftWrapper, new CraftWrapper(unequalProvider)); + testInequality(craftWrapper, new CraftWrapper(new NoOpProvider(provider.material))); + } + + // @Test + public void testMixedInequality() { + final StackWrapper craftWrapper = new CraftWrapper(provider); + testInequality(craftWrapper, new BukkitWrapper(unequalProvider)); + testInequality(craftWrapper, new BukkitWrapper(new NoOpProvider(provider.material))); + + final StackWrapper bukkitWrapper = new CraftWrapper(provider); + testInequality(bukkitWrapper, new CraftWrapper(unequalProvider)); + testInequality(bukkitWrapper, new CraftWrapper(new NoOpProvider(provider.material))); + } + + // @Test + public void testBukkitYamlDeserialize() throws Throwable { + testYamlDeserialize(new BukkitWrapper(provider), new BukkitWrapper(unequalProvider)); + } + + // @Test + public void testCraftYamlDeserialize() throws Throwable { + testYamlDeserialize(new CraftWrapper(provider), new CraftWrapper(unequalProvider)); + } + + // @Test + public void testBukkitStreamDeserialize() throws Throwable { + testStreamDeserialize(new BukkitWrapper(provider), new BukkitWrapper(unequalProvider)); + } + + // @Test + public void testCraftStreamDeserialize() throws Throwable { + testStreamDeserialize(new CraftWrapper(provider), new CraftWrapper(unequalProvider)); + } + + interface Operator { + + ItemStack operate(ItemStack cleanStack); + } + + interface StackWrapper { + + ItemStack stack(); + } + + static abstract class StackProvider { + + final Material material; + + StackProvider(Material material) { + this.material = material; + } + + static ItemStack cleanStack(Material material, boolean craft) { + final ItemStack stack = new ItemStack(material); + return craft ? CraftItemStack.asCraftCopy(stack) : stack; + } + + /** + * For each item in parameterList, it will apply nameFormat at nameIndex. For each item in parameterList for each item in materials, it will create a stack provider at each array index that + * contains an Operator. + */ + static List compound(final List parameterList, final String nameFormat, final int nameIndex, final Material... materials) { + final List out = new ArrayList(); + for (Object[] params : parameterList) { + final int len = params.length; + for (final Material material : materials) { + final Object[] paramsOut = params.clone(); + for (int i = 0; i < len; i++) { + final Object param = paramsOut[i]; + if (param instanceof Operator) { + final Operator operator = (Operator) param; + paramsOut[i] = new StackProvider(material) { + @Override + ItemStack operate(ItemStack cleanStack) { + return operator.operate(cleanStack); + } + }; + } + } + paramsOut[nameIndex] = String.format(nameFormat, paramsOut[nameIndex], material); + out.add(paramsOut); + } + } + return out; + } + + ItemStack bukkit() { + return operate(cleanStack(material, false)); + } + + ItemStack craft() { + return operate(cleanStack(material, true)); + } + + abstract ItemStack operate(ItemStack cleanStack); + + @Override + public String toString() { + return material.toString(); + } + } + + static class CompoundOperator implements Operator { + + final Operator[] operators; + + CompoundOperator(Operator... operators) { + this.operators = operators; + } + + /** + * This combines different tests into one large collection, combining no two tests from the same list. + * + * @param joiner used to join names + * @param nameParameter index of the name parameter + * @param singletonBitmask a list of bits representing the 'singletons' located in your originalLists. Lowest order bits represent the first items in originalLists. Singletons are + * exponentially linked with each other, such that, the output will contain every unique subset of only items from the singletons, as well as every unique subset that contains at least one + * item from each non-singleton. + */ + static List compound(final Joiner joiner, final int nameParameter, final long singletonBitmask, final List... originalLists) { + + final List out = new ArrayList(); + final List> singletons = new ArrayList>(); + final List> notSingletons = new ArrayList>(); + + { // Separate and prime the 'singletons' + int i = 0; + for (List list : originalLists) { + (((singletonBitmask >>> i++) & 0x1) == 0x1 ? singletons : notSingletons).add(list); + } + } + + for (final List primarySingleton : singletons) { + // Iterate over our singletons, to multiply the 'out' each time + for (final Object[] entry : out.toArray(EMPTY_ARRAY)) { + // Iterate over a snapshot of 'out' to prevent CMEs / infinite iteration + final int len = entry.length; + for (final Object[] singleton : primarySingleton) { + // Iterate over each item in our singleton for the current 'out' entry + final Object[] toOut = entry.clone(); + for (int i = 0; i < len; i++) { + // Iterate over each parameter + if (i == nameParameter) { + toOut[i] = joiner.join(toOut[i], singleton[i]); + } else if (toOut[i] instanceof Operator) { + final Operator op1 = (Operator) toOut[i]; + final Operator op2 = (Operator) singleton[i]; + toOut[i] = new Operator() { + public ItemStack operate(final ItemStack cleanStack) { + return op2.operate(op1.operate(cleanStack)); + } + }; + } + } + out.add(toOut); + } + } + out.addAll(primarySingleton); + } + + @SuppressWarnings("unchecked") final List[] lists = new List[notSingletons.size() + 1]; + notSingletons.toArray(lists); + lists[lists.length - 1] = out; + + final RecursiveContainer methodParams = new RecursiveContainer(joiner, new Object[lists.length], nameParameter, new ArrayList(lists.length), new ArrayList(), lists); + + recursivelyCompound(methodParams, 0); + methodParams.out.addAll(out); + + return methodParams.out; + } + + private static void recursivelyCompound(final RecursiveContainer methodParams, final int level) { + final List stack = methodParams.stack; + + if (level == methodParams.lists.length) { + final Object[] firstParams = stack.get(0); + final int len = firstParams.length; + final int stackSize = stack.size(); + final Object[] params = new Object[len]; + + for (int i = 0; i < len; i++) { + final Object firstParam = firstParams[i]; + + if (firstParam instanceof Operator) { + final Operator[] operators = new Operator[stackSize]; + for (int j = 0; j < stackSize; j++) { + operators[j] = (Operator) stack.get(j)[i]; + } + + params[i] = new CompoundOperator(operators); + } else if (i == methodParams.nameParameter) { + final Object[] strings = methodParams.strings; + for (int j = 0; j < stackSize; j++) { + strings[j] = stack.get(j)[i]; + } + + params[i] = methodParams.joiner.join(strings); + } else { + params[i] = firstParam; + } + } + + methodParams.out.add(params); + } else { + final int marker = stack.size(); + + for (final Object[] params : methodParams.lists[level]) { + stack.add(params); + recursivelyCompound(methodParams, level + 1); + stack.remove(marker); + } + } + } + + public ItemStack operate(ItemStack cleanStack) { + for (Operator operator : operators) { + operator.operate(cleanStack); + } + return cleanStack; + } + + @Override + public String toString() { + return Arrays.toString(operators); + } + + static class RecursiveContainer { + + final Joiner joiner; + final Object[] strings; + final int nameParameter; + final List stack; + final List out; + final List[] lists; + + RecursiveContainer(Joiner joiner, Object[] strings, int nameParameter, List stack, List out, List[] lists) { + this.joiner = joiner; + this.strings = strings; + this.nameParameter = nameParameter; + this.stack = stack; + this.out = out; + this.lists = lists; + } + } + } + + static class CraftWrapper implements StackWrapper { + + final StackProvider provider; + + CraftWrapper(StackProvider provider) { + this.provider = provider; + } + + public ItemStack stack() { + return provider.craft(); + } + + @Override + public String toString() { + return "Craft " + provider; + } + } + + static class BukkitWrapper implements StackWrapper { + + final StackProvider provider; + + BukkitWrapper(StackProvider provider) { + this.provider = provider; + } + + public ItemStack stack() { + return provider.bukkit(); + } + + @Override + public String toString() { + return "Bukkit " + provider; + } + } + + static class NoOpProvider extends StackProvider { + + NoOpProvider(Material material) { + super(material); + } + + @Override + ItemStack operate(ItemStack cleanStack) { + return cleanStack; + } + + @Override + public String toString() { + return "NoOp " + super.toString(); + } + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java new file mode 100644 index 0000000..d6e7dbb --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java @@ -0,0 +1,37 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import net.minecraft.init.Enchantments; +import net.minecraft.init.Items; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class NMSCraftItemStackTest extends AbstractTestingBase { + + @Test + public void testCloneEnchantedItem() throws Exception { + net.minecraft.item.ItemStack nmsItemStack = new net.minecraft.item.ItemStack(Items.POTIONITEM); + nmsItemStack.addEnchantment(Enchantments.SHARPNESS, 1); + ItemStack itemStack = CraftItemStack.asCraftMirror(nmsItemStack); + ItemStack clone = itemStack.clone(); + assertThat(clone.getType(), is(itemStack.getType())); + assertThat(clone.getAmount(), is(itemStack.getAmount())); + assertThat(clone.getDurability(), is(itemStack.getDurability())); + assertThat(clone.getEnchantments(), is(itemStack.getEnchantments())); + assertThat(clone.getTypeId(), is(itemStack.getTypeId())); + assertThat(clone.getData(), is(itemStack.getData())); + assertThat(clone, is(itemStack)); + } + + @Test + public void testCloneNullItem() throws Exception { + net.minecraft.item.ItemStack nmsItemStack = null; + ItemStack itemStack = CraftItemStack.asCraftMirror(nmsItemStack); + ItemStack clone = itemStack.clone(); + assertThat(clone, is(itemStack)); + } +} diff --git a/src/test/java/org/bukkit/entity/EnderDragonPhaseTest.java b/src/test/java/org/bukkit/entity/EnderDragonPhaseTest.java new file mode 100644 index 0000000..689caed --- /dev/null +++ b/src/test/java/org/bukkit/entity/EnderDragonPhaseTest.java @@ -0,0 +1,51 @@ +package org.bukkit.entity; + +import static junit.framework.TestCase.assertNotNull; + +import net.minecraft.entity.boss.dragon.phase.PhaseList; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEnderDragon; +import org.junit.Assert; +import org.junit.Test; + +public class EnderDragonPhaseTest { + + @Test + public void testNotNull() { + for (EnderDragon.Phase phase : EnderDragon.Phase.values()) { + PhaseList dragonControllerPhase = CraftEnderDragon.getMinecraftPhase(phase); + assertNotNull(phase.name(), dragonControllerPhase); + assertNotNull(phase.name(), CraftEnderDragon.getBukkitPhase(dragonControllerPhase)); + } + } + + @Test + public void testBukkitToMinecraft() { + Assert.assertEquals("CIRCLING", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.CIRCLING), PhaseList.HOLDING_PATTERN); // PAIL: Rename HOLDING_PATTERN + Assert.assertEquals("STRAFING", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.STRAFING), PhaseList.STRAFE_PLAYER); // PAIL: Rename STRAFE_PLAYER + Assert.assertEquals("FLY_TO_PORTAL", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.FLY_TO_PORTAL), PhaseList.LANDING_APPROACH); // PAIL: Rename LANDING_APPROACH + Assert.assertEquals("LAND_ON_PORTAL", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.LAND_ON_PORTAL), PhaseList.LANDING); // PAIL: Rename LANDING + Assert.assertEquals("LEAVE_PORTAL", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.LEAVE_PORTAL), PhaseList.TAKEOFF); // PAIL: Rename TAKEOFF + Assert.assertEquals("BREATH_ATTACK", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.BREATH_ATTACK), PhaseList.SITTING_FLAMING); // PAIL: Rename SITTING_FLAMING + Assert.assertEquals("SEARCH_FOR_BREATH_ATTACK_TARGET", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.SEARCH_FOR_BREATH_ATTACK_TARGET), + PhaseList.SITTING_SCANNING); // PAIL: Rename SITTING_SCANNING + Assert.assertEquals("ROAR_BEFORE_ATTACK", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.ROAR_BEFORE_ATTACK), PhaseList.SITTING_ATTACKING); // PAIL: Rename SITTING_ATTACKING + Assert.assertEquals("CHARGE_PLAYER", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.CHARGE_PLAYER), PhaseList.CHARGING_PLAYER); // PAIL: Rename CHARGING_PLAYER + Assert.assertEquals("DYING", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.DYING), PhaseList.DYING); // PAIL: Rename DYING + Assert.assertEquals("HOVER", CraftEnderDragon.getMinecraftPhase(EnderDragon.Phase.HOVER), PhaseList.HOVER); // PAIL: Rename HOVER + } + + @Test + public void testMinecraftToBukkit() { + Assert.assertEquals("CIRCLING", CraftEnderDragon.getBukkitPhase(PhaseList.HOLDING_PATTERN), EnderDragon.Phase.CIRCLING); + Assert.assertEquals("STRAFING", CraftEnderDragon.getBukkitPhase(PhaseList.STRAFE_PLAYER), EnderDragon.Phase.STRAFING); + Assert.assertEquals("FLY_TO_PORTAL", CraftEnderDragon.getBukkitPhase(PhaseList.LANDING_APPROACH), EnderDragon.Phase.FLY_TO_PORTAL); + Assert.assertEquals("LAND_ON_PORTAL", CraftEnderDragon.getBukkitPhase(PhaseList.LANDING), EnderDragon.Phase.LAND_ON_PORTAL); + Assert.assertEquals("LEAVE_PORTAL", CraftEnderDragon.getBukkitPhase(PhaseList.TAKEOFF), EnderDragon.Phase.LEAVE_PORTAL); + Assert.assertEquals("BREATH_ATTACK", CraftEnderDragon.getBukkitPhase(PhaseList.SITTING_FLAMING), EnderDragon.Phase.BREATH_ATTACK); + Assert.assertEquals("SEARCH_FOR_BREATH_ATTACK_TARGET", CraftEnderDragon.getBukkitPhase(PhaseList.SITTING_SCANNING), EnderDragon.Phase.SEARCH_FOR_BREATH_ATTACK_TARGET); + Assert.assertEquals("ROAR_BEFORE_ATTACK", CraftEnderDragon.getBukkitPhase(PhaseList.SITTING_ATTACKING), EnderDragon.Phase.ROAR_BEFORE_ATTACK); + Assert.assertEquals("CHARGE_PLAYER", CraftEnderDragon.getBukkitPhase(PhaseList.CHARGING_PLAYER), EnderDragon.Phase.CHARGE_PLAYER); + Assert.assertEquals("DYING", CraftEnderDragon.getBukkitPhase(PhaseList.DYING), EnderDragon.Phase.DYING); + Assert.assertEquals("HOVER", CraftEnderDragon.getBukkitPhase(PhaseList.HOVER), EnderDragon.Phase.HOVER); + } +} diff --git a/src/test/java/org/bukkit/map/MapTest.java b/src/test/java/org/bukkit/map/MapTest.java new file mode 100644 index 0000000..96eb34d --- /dev/null +++ b/src/test/java/org/bukkit/map/MapTest.java @@ -0,0 +1,61 @@ +package org.bukkit.map; + +import java.awt.Color; +import java.util.logging.Level; +import java.util.logging.Logger; +import net.minecraft.block.material.MapColor; +import org.junit.Assert; +import org.junit.Test; + +public class MapTest { + + private static final Logger logger = Logger.getLogger("MapTest"); + + private static final int[] modifiers = {180, 220, 255, 135}; + + @Test + public void testColors() { + MapColor[] nmsColors = MapColor.COLORS; + Color[] bukkitColors = MapPalette.colors; + + boolean fail = false; + for (int i = 0; i < nmsColors.length; i++) { + if (nmsColors[i] == null) { + break; + } + int rgb = nmsColors[i].colorValue; + + int r = (rgb >> 16) & 0xFF; + int g = (rgb >> 8) & 0xFF; + int b = rgb & 0xFF; + + if (i + 1 > bukkitColors.length / 4) { + for (int modi : modifiers) { + int mr = (r * modi) / 255; + int mg = (g * modi) / 255; + int mb = (b * modi) / 255; + logger.log(Level.WARNING, "Missing color: c({0}, {1}, {2})", new Object[]{mr, mg, mb}); + } + fail = true; + } else { + for (int j = 0; j < modifiers.length; j++) { + int modi = modifiers[j]; + Color bukkit = bukkitColors[i * 4 + j]; + int mr = (r * modi) / 255; + int mg = (g * modi) / 255; + int mb = (b * modi) / 255; + + if (bukkit.getRed() != mr || bukkit.getGreen() != mg || bukkit.getBlue() != mb) { + logger.log(Level.WARNING, "Incorrect color: {6} {7} c({0}, {1}, {2}) != c({3}, {4}, {5})", new Object[]{ + bukkit.getRed(), bukkit.getGreen(), bukkit.getBlue(), + mr, mg, mb, + i, j + }); + fail = true; + } + } + } + } + Assert.assertFalse(fail); + } +} diff --git a/src/test/java/org/bukkit/potion/PotionTest.java b/src/test/java/org/bukkit/potion/PotionTest.java new file mode 100644 index 0000000..5887f5c --- /dev/null +++ b/src/test/java/org/bukkit/potion/PotionTest.java @@ -0,0 +1,36 @@ +package org.bukkit.potion; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class PotionTest extends AbstractTestingBase { + + @Test + public void testEffectCompleteness() throws Throwable { + Map effects = new EnumMap(PotionType.class); + for (Object reg : net.minecraft.potion.PotionType.REGISTRY) { + List eff = ((net.minecraft.potion.PotionType) reg).getEffects(); + if (eff.size() != 1) { + continue; + } + int id = Potion.getIdFromPotion(eff.get(0).getPotion()); + PotionEffectType type = PotionEffectType.getById(id); + assertNotNull(String.valueOf(id), PotionEffectType.getById(id)); + + PotionType enumType = PotionType.getByEffect(type); + assertNotNull(type.getName(), enumType); + + effects.put(enumType, enumType.name()); + } + + assertEquals(effects.entrySet().size(), PotionType.values().length - /* PotionTypes with no Effects */ 5); + } +} diff --git a/src/test/java/org/bukkit/support/AbstractTestingBase.java b/src/test/java/org/bukkit/support/AbstractTestingBase.java new file mode 100644 index 0000000..83e9c28 --- /dev/null +++ b/src/test/java/org/bukkit/support/AbstractTestingBase.java @@ -0,0 +1,80 @@ +package org.bukkit.support; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import net.minecraft.init.Bootstrap; +import org.bukkit.Material; +import org.junit.BeforeClass; + +/** + * If you are getting: java.lang.ExceptionInInitializerError at net.minecraft.server.StatisticList.(SourceFile:58) at net.minecraft.server.Item.(SourceFile:252) at + * net.minecraft.server.Block.(Block.java:577) + *

    + * extend this class to solve it. + */ +public abstract class AbstractTestingBase { + + public static final List INVALIDATED_MATERIALS = ImmutableList.builder() + .add( + Material.BREWING_STAND, + Material.BED_BLOCK, + Material.NETHER_WARTS, + Material.CAULDRON, + Material.FLOWER_POT, + Material.CROPS, + Material.SUGAR_CANE_BLOCK, + Material.CAKE_BLOCK, + Material.SKULL, + Material.PISTON_EXTENSION, + Material.PISTON_MOVING_PIECE, + Material.GLOWING_REDSTONE_ORE, + Material.DIODE_BLOCK_ON, + Material.PUMPKIN_STEM, + Material.SIGN_POST, + Material.REDSTONE_COMPARATOR_ON, + Material.TRIPWIRE, + Material.REDSTONE_LAMP_ON, + Material.MELON_STEM, + Material.REDSTONE_TORCH_OFF, + Material.REDSTONE_COMPARATOR_OFF, + Material.REDSTONE_WIRE, + Material.WALL_SIGN, + Material.DIODE_BLOCK_OFF, + Material.IRON_DOOR_BLOCK, + Material.WOODEN_DOOR, + Material.WATER, + Material.STATIONARY_WATER, + Material.LAVA, + Material.STATIONARY_LAVA, + Material.DOUBLE_STEP, + Material.DOUBLE_STEP, + Material.FIRE, + Material.PORTAL, + Material.ENDER_PORTAL, + Material.WOOD_DOUBLE_STEP, + Material.COCOA, + Material.CARROT, + Material.POTATO, + Material.STANDING_BANNER, + Material.WALL_BANNER, + Material.DAYLIGHT_DETECTOR_INVERTED, + Material.DOUBLE_STONE_SLAB2, + Material.SPRUCE_DOOR, + Material.BIRCH_DOOR, + Material.JUNGLE_DOOR, + Material.ACACIA_DOOR, + Material.DARK_OAK_DOOR, + Material.PURPUR_DOUBLE_SLAB, + Material.BEETROOT_BLOCK, + Material.END_GATEWAY, + Material.BURNING_FURNACE, + Material.FROSTED_ICE + ).build(); + + @BeforeClass + public static void setup() { + Bootstrap.register(); + DummyServer.setup(); + DummyEnchantments.setup(); + } +} diff --git a/src/test/java/org/bukkit/support/DummyEnchantments.java b/src/test/java/org/bukkit/support/DummyEnchantments.java new file mode 100644 index 0000000..a23d715 --- /dev/null +++ b/src/test/java/org/bukkit/support/DummyEnchantments.java @@ -0,0 +1,14 @@ +package org.bukkit.support; + +import net.minecraft.init.Enchantments; + +public class DummyEnchantments { + + static { + Enchantments.SHARPNESS.getClass(); + org.bukkit.enchantments.Enchantment.stopAcceptingRegistrations(); + } + + public static void setup() { + } +} diff --git a/src/test/java/org/bukkit/support/DummyServer.java b/src/test/java/org/bukkit/support/DummyServer.java new file mode 100644 index 0000000..0ab02f5 --- /dev/null +++ b/src/test/java/org/bukkit/support/DummyServer.java @@ -0,0 +1,85 @@ +package org.bukkit.support; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.HashMap; +import java.util.logging.Logger; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_12_R1.util.Versioning; + +public class DummyServer implements InvocationHandler { + + private static final HashMap methods = new HashMap(); + + static { + try { + methods.put( + Server.class.getMethod("getItemFactory"), + new MethodHandler() { + public Object handle(DummyServer server, Object[] args) { + return CraftItemFactory.instance(); + } + } + ); + methods.put( + Server.class.getMethod("getName"), + new MethodHandler() { + public Object handle(DummyServer server, Object[] args) { + return DummyServer.class.getName(); + } + } + ); + methods.put( + Server.class.getMethod("getVersion"), + new MethodHandler() { + public Object handle(DummyServer server, Object[] args) { + return DummyServer.class.getPackage().getImplementationVersion(); + } + } + ); + methods.put( + Server.class.getMethod("getBukkitVersion"), + new MethodHandler() { + public Object handle(DummyServer server, Object[] args) { + return Versioning.getBukkitVersion(); + } + } + ); + methods.put( + Server.class.getMethod("getLogger"), + new MethodHandler() { + final Logger logger = Logger.getLogger(DummyServer.class.getCanonicalName()); + + public Object handle(DummyServer server, Object[] args) { + return logger; + } + } + ); + Bukkit.setServer(Proxy.getProxyClass(Server.class.getClassLoader(), Server.class).asSubclass(Server.class).getConstructor(InvocationHandler.class).newInstance(new DummyServer())); + } catch (Throwable t) { + throw new Error(t); + } + } + + private DummyServer() { + } + + public static void setup() { + } + + public Object invoke(Object proxy, Method method, Object[] args) { + MethodHandler handler = methods.get(method); + if (handler != null) { + return handler.handle(this, args); + } + throw new UnsupportedOperationException(String.valueOf(method)); + } + + private interface MethodHandler { + + Object handle(DummyServer server, Object[] args); + } +} diff --git a/src/test/java/org/bukkit/support/Matchers.java b/src/test/java/org/bukkit/support/Matchers.java new file mode 100644 index 0000000..7dd30a5 --- /dev/null +++ b/src/test/java/org/bukkit/support/Matchers.java @@ -0,0 +1,32 @@ +package org.bukkit.support; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +public final class Matchers { + + private Matchers() { + } + + public static Matcher sameHash(T value) { + return new SameHash(value); + } + + static class SameHash extends BaseMatcher { + + private final int expected; + + SameHash(T object) { + expected = object.hashCode(); + } + + public boolean matches(Object item) { + return item.hashCode() == expected; + } + + public void describeTo(Description description) { + description.appendValue(expected); + } + } +} diff --git a/src/test/java/org/bukkit/support/Util.java b/src/test/java/org/bukkit/support/Util.java new file mode 100644 index 0000000..2f24d9a --- /dev/null +++ b/src/test/java/org/bukkit/support/Util.java @@ -0,0 +1,31 @@ +package org.bukkit.support; + +import java.lang.reflect.Field; + +public class Util { + /* + public static T getInternalState(Object object, String fieldName) { + return getInternalState(object.getClass(), object, fieldName); + } + */ + + @SuppressWarnings("unchecked") + public static T getInternalState(Class clazz, Object object, String fieldName) { + Field field; + try { + field = clazz.getDeclaredField(fieldName); + } catch (SecurityException e) { + throw new RuntimeException("Not allowed to access " + clazz, e); + } catch (NoSuchFieldException e) { + throw new RuntimeException("Unable to find field " + fieldName, e); + } + + field.setAccessible(true); + try { + return (T) field.get(object); + } catch (IllegalArgumentException e) { + } catch (IllegalAccessException e) { + } + throw new RuntimeException("Unable to get internal value"); + } +}