diff options
author | Wesley Wolfe <weswolf@aol.com> | 2012-12-17 01:31:41 -0600 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2012-12-17 01:31:41 -0600 |
commit | 78f48258d8ab491065498492588acc5287a6fb6c (patch) | |
tree | 11027439669a05c335fc6cb07c12a82b9c551f31 /src/test/java/org | |
parent | d20d4dc43aaa74fba457c44994cda8180523c9db (diff) | |
download | craftbukkit-78f48258d8ab491065498492588acc5287a6fb6c.tar craftbukkit-78f48258d8ab491065498492588acc5287a6fb6c.tar.gz craftbukkit-78f48258d8ab491065498492588acc5287a6fb6c.tar.lz craftbukkit-78f48258d8ab491065498492588acc5287a6fb6c.tar.xz craftbukkit-78f48258d8ab491065498492588acc5287a6fb6c.zip |
Implement ItemFactory and ItemMeta values. Adds BUKKIT-15
Diffstat (limited to 'src/test/java/org')
20 files changed, 1784 insertions, 23 deletions
diff --git a/src/test/java/org/bukkit/DyeColorsTest.java b/src/test/java/org/bukkit/DyeColorsTest.java new file mode 100644 index 00000000..296f49a4 --- /dev/null +++ b/src/test/java/org/bukkit/DyeColorsTest.java @@ -0,0 +1,39 @@ +package org.bukkit; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.server.EntitySheep; + +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 { + + @Parameters(name= "{index}: {0}") + public static List<Object[]> data() { + List<Object[]> list = new ArrayList<Object[]>(); + for (DyeColor dye : DyeColor.values()) { + list.add(new Object[] {dye}); + } + return list; + } + + @Parameter public DyeColor dye; + + @Test + public void checkColor() { + Color color = dye.getColor(); + float[] nmsColorArray = EntitySheep.d[dye.getData()]; + Color nmsColor = Color.fromRGB((int) (nmsColorArray[0] * 255), (int) (nmsColorArray[1] * 255), (int) (nmsColorArray[2] * 255)); + assertThat(color, is(nmsColor)); + } +} diff --git a/src/test/java/org/bukkit/PerMaterialTest.java b/src/test/java/org/bukkit/PerMaterialTest.java index 766bce26..da2b46c1 100644 --- a/src/test/java/org/bukkit/PerMaterialTest.java +++ b/src/test/java/org/bukkit/PerMaterialTest.java @@ -11,6 +11,8 @@ import net.minecraft.server.Item; import net.minecraft.server.ItemFood; import net.minecraft.server.ItemRecord; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; import org.bukkit.support.AbstractTestingBase; import org.bukkit.support.Util; import org.junit.BeforeClass; @@ -74,10 +76,17 @@ public class PerMaterialTest extends AbstractTestingBase { @Test public void maxStackSize() { + final ItemStack bukkit = new ItemStack(material); + final CraftItemStack craft = CraftItemStack.asCraftCopy(bukkit); if (material == Material.AIR) { - assertThat(material.getMaxStackSize(), is(64 /* Why can't I hold all of these 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(Item.byId[material.getId()].getMaxStackSize())); + assertThat(bukkit.getMaxStackSize(), is(material.getMaxStackSize())); + assertThat(craft.getMaxStackSize(), is(material.getMaxStackSize())); } } 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 00000000..1349a7f1 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/CompositeSerialization.java @@ -0,0 +1,61 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +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<ItemStack> stacks = new ArrayList<ItemStack>(); + 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((Object) 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 00000000..885964dd --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/FactoryItemMaterialTest.java @@ -0,0 +1,142 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Material; +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 = Material.values(); + + 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<Object[]> data() { + List<Object[]> list = new ArrayList<Object[]>(); + for (Material material : materials) { + list.add(new Object[] {material}); + } + return list; + } + + @Parameter(0) public Material material; + + @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/ItemMetaImplementationOverrideTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java new file mode 100644 index 00000000..ac0702a2 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaImplementationOverrideTest.java @@ -0,0 +1,67 @@ +package org.bukkit.craftbukkit.inventory; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.craftbukkit.Overridden; +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<CraftMetaItem> parent = CraftMetaItem.class; + static final Class<Overridden> annotation = Overridden.class; + + static final List<Object[]> testData = new ArrayList<Object[]>(); + static final Method[] methods; + + static final Class<? extends CraftMetaItem>[] subclasses; + + static { + List<Class<? extends CraftMetaItem>> classes = new ArrayList<Class<? extends CraftMetaItem>>(); + + for (Material material : ItemStackTest.COMPOUND_MATERIALS) { + Class<? extends CraftMetaItem> clazz = CraftItemFactory.instance().getItemMeta(material).getClass().asSubclass(parent); + if (clazz != parent) { + classes.add(clazz); + } + } + subclasses = classes.toArray(new Class[0]); + + + List<Method> list = new ArrayList<Method>(); + + for (Method method: parent.getDeclaredMethods()) { + if (method.isAnnotationPresent(annotation)) { + list.add(method); + } + } + + for (Class<?> clazz : subclasses) { + for (Method method : list) { + testData.add(new Object[]{clazz, method, clazz.getSimpleName() + " contains " + method.getName()}); + } + } + + methods = list.toArray(new Method[list.size()]); + } + + @Parameters(name="[{index}]:{2}") + public static List<Object[]> data() { + return testData; + } + + @Parameter(0) public Class clazz; + @Parameter(1) public Method method; + @Parameter(2) public String name; + + @Test + public void testClass() throws Throwable { + clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); + } +} 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 00000000..c95f499f --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -0,0 +1,113 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.craftbukkit.inventory.ItemStackTest.StackProvider; +import org.bukkit.craftbukkit.inventory.ItemStackTest.StackWrapper; +import org.bukkit.craftbukkit.inventory.ItemStackTest.BukkitWrapper; +import org.bukkit.craftbukkit.inventory.ItemStackTest.CraftWrapper; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.MapMeta; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.support.AbstractTestingBase; +import org.junit.Test; + +public class ItemMetaTest extends AbstractTestingBase { + + @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((ItemStack) craft)); + } + + @Test + public void testEachExtraData() { + final List<StackProvider> 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.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.addCustomEffect(PotionEffectType.CONFUSION.createEffect(1, 1), false); + cleanStack.setItemMeta(meta); + return cleanStack; + } + } + ); + + assertThat("Forgotten test?", providers, hasSize(ItemStackTest.COMPOUND_MATERIALS.length - 1 /* Normal item meta */)); + + 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 00000000..8321eefe --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackBookTest.java @@ -0,0 +1,212 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.google.common.base.Joiner; + +@RunWith(Parameterized.class) +public class ItemStackBookTest extends ItemStackTest { + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.WRITTEN_BOOK, Material.BOOK_AND_QUILL); + } + + static List<Object[]> 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/ItemStackLeatherTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java new file mode 100644 index 00000000..a6f95831 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLeatherTest.java @@ -0,0 +1,88 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.LeatherArmorMeta; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.google.common.base.Joiner; + +@RunWith(Parameterized.class) +public class ItemStackLeatherTest extends ItemStackTest { + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.LEATHER_BOOTS, Material.LEATHER_CHESTPLATE, Material.LEATHER_HELMET, Material.LEATHER_LEGGINGS); + } + + static List<Object[]> 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 00000000..d3604fbc --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackLoreEnchantmentTest.java @@ -0,0 +1,240 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.google.common.base.Joiner; + +@RunWith(Parameterized.class) +public class ItemStackLoreEnchantmentTest extends ItemStackTest { + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, ItemStackTest.COMPOUND_MATERIALS); + } + + static List<Object[]> 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" + } + ) + ); + } +} 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 00000000..e6aa2c46 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackPotionsTest.java @@ -0,0 +1,145 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionEffectType; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.google.common.base.Joiner; + +@RunWith(Parameterized.class) +public class ItemStackPotionsTest extends ItemStackTest { + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.POTION); + } + + static List<Object[]> 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 00000000..ea6381fa --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackSkullTest.java @@ -0,0 +1,87 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.google.common.base.Joiner; + +@RunWith(Parameterized.class) +public class ItemStackSkullTest extends ItemStackTest { + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return StackProvider.compound(operators(), "%s %s", NAME_PARAMETER, Material.SKULL_ITEM); + } + + static List<Object[]> 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 00000000..4592e1dc --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemStackTest.java @@ -0,0 +1,428 @@ +package org.bukkit.craftbukkit.inventory; + +import static org.bukkit.support.Matchers.sameHash; +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +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.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; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +@RunWith(Parameterized.class) +public class ItemStackTest extends AbstractTestingBase { + static abstract class StackProvider { + final Material material; + + StackProvider(Material material) { + this.material = material; + } + + ItemStack bukkit() { + return operate(cleanStack(material, false)); + } + + ItemStack craft() { + return operate(cleanStack(material, true)); + } + + abstract ItemStack operate(ItemStack cleanStack); + + static ItemStack cleanStack(Material material, boolean craft) { + final ItemStack stack = new ItemStack(material); + return craft ? CraftItemStack.asCraftCopy(stack) : stack; + } + + @Override + public String toString() { + return material.toString(); + } + + /** + * 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. + * + * @param parameterList + * @param nameFormat + * @param nameIndex + * @param materials + * @return + */ + static List<Object[]> compound(final List<Object[]> parameterList, final String nameFormat, final int nameIndex, final Material...materials) { + final List<Object[]> out = new ArrayList<Object[]>(); + 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; + } + } + + interface Operator { + ItemStack operate(ItemStack cleanStack); + } + + static class CompoundOperator implements Operator { + static class RecursiveContainer { + final Joiner joiner; + final Object[] strings; + final int nameParameter; + final List<Object[]> stack; + final List<Object[]> out; + final List<Object[]>[] lists; + + RecursiveContainer(Joiner joiner, Object[] strings, int nameParameter, List<Object[]> stack, List<Object[]> out, List<Object[]>[] lists) { + this.joiner = joiner; + this.strings = strings; + this.nameParameter = nameParameter; + this.stack = stack; + this.out = out; + this.lists = lists; + } + } + final Operator[] operators; + + CompoundOperator(Operator...operators) { + this.operators = operators; + } + + public ItemStack operate(ItemStack cleanStack) { + for (Operator operator : operators) { + operator.operate(cleanStack); + } + return cleanStack; + } + + @Override + public String toString() { + return Arrays.toString(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. + * @param originalLists + * @return + */ + static List<Object[]> compound(final Joiner joiner, final int nameParameter, final long singletonBitmask, final List<Object[]>...originalLists) { + + final List<Object[]> out = new ArrayList<Object[]>(); + final List<List<Object[]>> singletons = new ArrayList<List<Object[]>>(); + final List<List<Object[]>> notSingletons = new ArrayList<List<Object[]>>(); + + { // Separate and prime the 'singletons' + int i = 0; + for (List<Object[]> list : originalLists) { + (((singletonBitmask >>> i++) & 0x1) == 0x1 ? singletons : notSingletons).add(list); + } + } + + for (final List<Object[]> 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); + } + + final List<Object[]>[] 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<Object[]>(lists.length), new ArrayList<Object[]>(), lists); + + recursivelyCompound(methodParams, 0); + methodParams.out.addAll(out); + + return methodParams.out; + } + + private static void recursivelyCompound(final RecursiveContainer methodParams, final int level) { + final List<Object[]> 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); + } + } + } + } + + interface StackWrapper { + ItemStack stack(); + } + + 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(); + } + } + + @Parameters(name="[{index}]:{" + NAME_PARAMETER + "}") + public static List<Object[]> data() { + return ImmutableList.of(); // TODO, test basic durability issues + } + + 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 { + COMPOUND_MATERIALS = new Object() { // Workaround for JDK5 + Material[] value() { + final ItemFactory factory = CraftItemFactory.instance(); + final Map<Class<? extends ItemMeta>, Material> possibleMaterials = new HashMap<Class<? extends ItemMeta>, Material>(); + for (final Material material : Material.values()) { + final ItemMeta meta = factory.getItemMeta(material); + if (meta == null || possibleMaterials.containsKey(meta.getClass())) + continue; + possibleMaterials.put(meta.getClass(), material); + + } + return possibleMaterials.values().toArray(new Material[possibleMaterials.size()]); + } + }.value(); + } + + @Parameter(0) public StackProvider provider; + @Parameter(1) public StackProvider unequalProvider; + @Parameter(NAME_PARAMETER) public String name; + + @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))); + } + + 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()))); + } + + @Test + public void testBukkitDeserialize() { + testDeserialize(new BukkitWrapper(provider), new BukkitWrapper(unequalProvider)); + } + + @Test + public void testCraftDeserialize() { + testDeserialize(new CraftWrapper(provider), new CraftWrapper(unequalProvider)); + } + + static void testDeserialize(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); + } + + assertThat(out, configIn.getItemStack("provider"), allOf(equalTo(stack), sameHash(stack))); + assertThat(out, configIn.getItemStack("unequal"), allOf(equalTo(unequalStack), sameHash(unequalStack))); + assertThat(out, configIn.getItemStack("provider"), is(not(unequalStack))); + assertThat(out, configIn.getItemStack("provider"), is(not(configIn.getItemStack("unequal")))); + } +} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/CraftItemStackTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java index fe2bc181..be1ffcf6 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/CraftItemStackTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java @@ -9,13 +9,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.support.AbstractTestingBase; import org.junit.Test; -public class CraftItemStackTest extends AbstractTestingBase { +public class NMSCraftItemStackTest extends AbstractTestingBase { @Test public void testCloneEnchantedItem() throws Exception { net.minecraft.server.ItemStack nmsItemStack = new net.minecraft.server.ItemStack(net.minecraft.server.Item.POTION); nmsItemStack.addEnchantment(Enchantment.DAMAGE_ALL, 1); - ItemStack itemStack = new CraftItemStack(nmsItemStack); + ItemStack itemStack = CraftItemStack.asCraftMirror(nmsItemStack); ItemStack clone = itemStack.clone(); assertThat(clone.getType(), is(itemStack.getType())); assertThat(clone.getAmount(), is(itemStack.getAmount())); @@ -29,7 +29,7 @@ public class CraftItemStackTest extends AbstractTestingBase { @Test public void testCloneNullItem() throws Exception { net.minecraft.server.ItemStack nmsItemStack = null; - ItemStack itemStack = new CraftItemStack(nmsItemStack); + ItemStack itemStack = CraftItemStack.asCraftMirror(nmsItemStack); ItemStack clone = itemStack.clone(); assertThat(clone, is(itemStack)); } diff --git a/src/test/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterServiceTest.java b/src/test/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterServiceTest.java index 342949b7..df7437c9 100644 --- a/src/test/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterServiceTest.java +++ b/src/test/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterServiceTest.java @@ -1,14 +1,13 @@ package org.bukkit.craftbukkit.updater; +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + import java.io.FileNotFoundException; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import static org.junit.Assert.assertNotNull; -import org.junit.Ignore; import org.junit.Test; -@Ignore ("useful tests, but not necessary to run on each compile") public class BukkitDLUpdaterServiceTest { @Test(expected=IOException.class) public void testHostNotFound() throws IOException { @@ -28,6 +27,6 @@ public class BukkitDLUpdaterServiceTest { public void testArtifactExists() throws IOException { BukkitDLUpdaterService service = new BukkitDLUpdaterService("dl.bukkit.org"); - assertNotNull(service.fetchArtifact("latest-dev")); + assertThat(service.fetchArtifact("latest-dev"), is(not(nullValue()))); } } diff --git a/src/test/java/org/bukkit/potion/PotionTest.java b/src/test/java/org/bukkit/potion/PotionTest.java index 1040b6ad..a9df7d19 100644 --- a/src/test/java/org/bukkit/potion/PotionTest.java +++ b/src/test/java/org/bukkit/potion/PotionTest.java @@ -6,21 +6,11 @@ import static org.hamcrest.Matchers.*; import java.util.EnumMap; import java.util.Map; -import org.bukkit.craftbukkit.potion.CraftPotionBrewer; +import org.bukkit.support.AbstractTestingBase; import org.bukkit.support.Util; -import org.junit.BeforeClass; import org.junit.Test; -import net.minecraft.server.MobEffectList; - -public class PotionTest { - - @BeforeClass - public static void setUp() { - Potion.setPotionBrewer(new CraftPotionBrewer()); - MobEffectList.BLINDNESS.getClass(); - PotionEffectType.stopAcceptingRegistrations(); - } +public class PotionTest extends AbstractTestingBase { @Test public void getEffects() { @@ -35,7 +25,7 @@ public class PotionTest { } @Test - public void testEffectCompleteness() throws SecurityException, IllegalAccessException, NoSuchFieldException { + public void testEffectCompleteness() throws Throwable { Map<Integer, ?> effectDurations = Util.getInternalState(net.minecraft.server.PotionBrewer.class, null, "effectDurations"); Map<PotionType, String> effects = new EnumMap(PotionType.class); @@ -46,7 +36,7 @@ public class PotionTest { PotionType enumType = PotionType.getByEffect(type); assertNotNull(type.getName(), enumType); - assertThat(enumType.name(), effects.put(enumType, enumType.name()), is((String)null)); + assertThat(enumType.name(), effects.put(enumType, enumType.name()), is(nullValue())); } assertThat(effects.entrySet(), hasSize(effectDurations.size())); diff --git a/src/test/java/org/bukkit/support/AbstractTestingBase.java b/src/test/java/org/bukkit/support/AbstractTestingBase.java index ca14db9b..e04158d6 100644 --- a/src/test/java/org/bukkit/support/AbstractTestingBase.java +++ b/src/test/java/org/bukkit/support/AbstractTestingBase.java @@ -17,5 +17,8 @@ public abstract class AbstractTestingBase { @BeforeClass public static void setup() { StatisticList.a(); + DummyServer.setup(); + DummyPotions.setup(); + DummyEnchantments.setup(); } }
\ No newline at end of file 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 00000000..5ed00a72 --- /dev/null +++ b/src/test/java/org/bukkit/support/DummyEnchantments.java @@ -0,0 +1,12 @@ +package org.bukkit.support; + +import net.minecraft.server.Enchantment; + +public class DummyEnchantments { + static { + Enchantment.byId.getClass(); + org.bukkit.enchantments.Enchantment.stopAcceptingRegistrations(); + } + + public static void setup() {} +} diff --git a/src/test/java/org/bukkit/support/DummyPotions.java b/src/test/java/org/bukkit/support/DummyPotions.java new file mode 100644 index 00000000..30666abd --- /dev/null +++ b/src/test/java/org/bukkit/support/DummyPotions.java @@ -0,0 +1,17 @@ +package org.bukkit.support; + +import net.minecraft.server.MobEffectList; + +import org.bukkit.craftbukkit.potion.CraftPotionBrewer; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionEffectType; + +public class DummyPotions { + static { + Potion.setPotionBrewer(new CraftPotionBrewer()); + MobEffectList.BLINDNESS.getClass(); + PotionEffectType.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 00000000..e5215bb4 --- /dev/null +++ b/src/test/java/org/bukkit/support/DummyServer.java @@ -0,0 +1,79 @@ +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.inventory.CraftItemFactory;
+import org.bukkit.craftbukkit.util.Versioning;
+
+public class DummyServer implements InvocationHandler {
+ private static interface MethodHandler {
+ Object handle(DummyServer server, Object[] args);
+ }
+ private static final HashMap<Method, MethodHandler> methods = new HashMap<Method, MethodHandler>();
+ 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);
+ }
+ }
+
+ public static void setup() {}
+
+ private DummyServer() {};
+
+ 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));
+ }
+}
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 00000000..b190c673 --- /dev/null +++ b/src/test/java/org/bukkit/support/Matchers.java @@ -0,0 +1,30 @@ +package org.bukkit.support; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +public final class Matchers { + + private Matchers() {} + + public static <T> Matcher<T> sameHash(T value) { + return new SameHash<T>(value); + } + + static class SameHash<T> extends BaseMatcher<T> { + 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); + } + } +} |