Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PotionTest.java
1 package org.bukkit.potion;
2 
3 import static org.junit.Assert.*;
4 import static org.hamcrest.Matchers.is;
5 
6 import org.bukkit.Material;
9 import org.junit.Test;
10 
11 public class PotionTest {
12  @Test
13  public void applyToItemStack() {
14  Potion potion = new Potion(PotionType.POISON);
15  ItemStack stack = new ItemStack(Material.POTION, 1);
16  potion.apply(stack);
17  assertTrue(stack.getDurability() == potion.toDamageValue());
18  }
19 
20  @Test
21  public void fromDamage() {
22  Potion potion = Potion.fromDamage(PotionType.POISON.getDamageValue());
23  assertTrue(potion.getType() == PotionType.POISON);
24  potion = Potion.fromDamage(PotionType.POISON.getDamageValue() | SPLASH_BIT);
25  assertTrue(potion.getType() == PotionType.POISON && potion.isSplash());
26  potion = Potion.fromDamage(0x25 /* Potion of Healing II */);
27  assertTrue(potion.getType() == PotionType.INSTANT_HEAL && potion.getLevel() == 2);
28  }
29 
30  @Test(expected = IllegalArgumentException.class)
31  public void illegalApplyToItemStack() {
32  Potion potion = new Potion(PotionType.POISON);
33  potion.apply(new ItemStack(Material.AIR, 1));
34  }
35 
36  @Test
37  public void ItemStackConversion() {
38  Potion potion = new Potion(PotionType.POISON);
39  ItemStack itemstack = potion.toItemStack(1);
40  assertThat(itemstack.getType(), is(Material.POTION));
41  assertTrue(itemstack.getAmount() == 1);
42  assertTrue(itemstack.getDurability() == potion.toDamageValue());
43  }
44 
45  @Test
46  public void setExtended() {
48  @Override
49  public double getDurationModifier() {
50  return 1;
51  }
52 
53  @Override
54  public String getName() {
55  return "Poison";
56  }
57 
58  @Override
59  public boolean isInstant() {
60  return false;
61  }
62  });
63  Potion potion = new Potion(PotionType.POISON);
64  assertFalse(potion.hasExtendedDuration());
65  potion.setHasExtendedDuration(true);
66  assertTrue(potion.hasExtendedDuration());
67  assertTrue((potion.toDamageValue() & EXTENDED_BIT) != 0);
68  }
69 
70  @Test
71  public void setSplash() {
72  Potion potion = new Potion(PotionType.POISON);
73  assertFalse(potion.isSplash());
74  potion.setSplash(true);
75  assertTrue(potion.isSplash());
76  assertTrue((potion.toDamageValue() & SPLASH_BIT) != 0);
77  }
78 
79  @Test
80  public void setLevel() {
81  Potion potion = new Potion(PotionType.POISON);
82  assertEquals(1, potion.getLevel());
83  potion.setLevel(2);
84  assertEquals(2, potion.getLevel());
85  assertTrue((potion.toDamageValue() & 0x3F) == (PotionType.POISON.getDamageValue() | 0x20));
86  }
87 
88  @Test(expected=IllegalArgumentException.class)
89  public void nullType() {
90  new Potion(null, 2);
91  }
92 
93  @Test(expected=IllegalArgumentException.class)
94  public void maxLevelConstruct() {
95  new Potion(PotionType.POISON, 3);
96  }
97 
98  @Test(expected=IllegalArgumentException.class)
99  public void maxLevelSet() {
100  Potion potion = new Potion(PotionType.POISON);
101  potion.setLevel(3);
102  }
103 
104  @Test(expected=IllegalArgumentException.class)
105  public void nullStack() {
106  Potion potion = new Potion(PotionType.POISON);
107  potion.apply((ItemStack) null);
108  }
109 
110  @Test(expected=IllegalArgumentException.class)
111  public void nullEntity() {
112  Potion potion = new Potion(PotionType.POISON);
113  potion.apply((LivingEntity) null);
114  }
115 
116  @Test
117  public void water() {
118  Potion potion = new Potion(PotionType.WATER);
119  assertEquals(0, potion.getLevel());
120  assertFalse(potion.isSplash());
121  assertFalse(potion.hasExtendedDuration());
122  assertEquals(0, potion.toDamageValue());
123  }
124 
125  @Test
126  public void mundane() {
127  Potion potion = new Potion(0);
128  assertFalse(potion.getType() == PotionType.WATER);
129  assertFalse(potion.toDamageValue() == 0);
130  assertEquals(8192, potion.toDamageValue());
131  Potion potion2 = Potion.fromDamage(8192);
132  assertEquals(potion, potion2);
133  assertEquals(0, potion.getLevel());
134  }
135 
136  @Test
137  public void awkward() {
138  Potion potion = new Potion(16);
139  assertEquals(16, potion.getNameId());
140  assertFalse(potion.isSplash());
141  assertFalse(potion.hasExtendedDuration());
142  assertNull(potion.getType());
143  assertEquals(16, potion.toDamageValue());
144  }
145 
146  private static final int EXTENDED_BIT = 0x40;
147  private static final int SPLASH_BIT = 0x4000;
148 }
PotionType getType()
Definition: Potion.java:223
void apply(ItemStack to)
Definition: Potion.java:156
void setLevel(int level)
Definition: Potion.java:302
static void registerPotionEffectType(PotionEffectType type)
boolean hasExtendedDuration()
Definition: Potion.java:232
static Potion fromDamage(int damage)
Definition: Potion.java:387
void setHasExtendedDuration(boolean isExtended)
Definition: Potion.java:261
void setSplash(boolean isSplash)
Definition: Potion.java:272
ItemStack toItemStack(int amount)
Definition: Potion.java:345