summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/bukkit/potion/PotionTest.java
blob: fe641b865a4619db389bd9618a17d3386c472d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.bukkit.potion;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;

import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.Potion.Tier;
import org.junit.Test;

public class PotionTest {
    @Test
    public void applyToItemStack() {
        Potion potion = new Potion(PotionType.POISON);
        ItemStack stack = new ItemStack(Material.POTION, 1);
        potion.apply(stack);
        assertTrue(stack.getDurability() == potion.toDamageValue());
    }

    @Test
    public void fromDamage() {
        Potion potion = Potion.fromDamage(PotionType.POISON.getDamageValue());
        assertTrue(potion.getType() == PotionType.POISON);
        potion = Potion.fromDamage(PotionType.POISON.getDamageValue() | SPLASH_BIT);
        assertTrue(potion.getType() == PotionType.POISON && potion.isSplash());
        potion = Potion.fromDamage(0x25 /* Potion of Healing II */);
        assertTrue(potion.getType() == PotionType.INSTANT_HEAL && potion.getTier() == Tier.TWO);
    }

    @Test(expected = IllegalArgumentException.class)
    public void illegalApplyToItemStack() {
        Potion potion = new Potion(PotionType.POISON);
        potion.apply(new ItemStack(Material.AIR, 1));
    }

    @Test
    public void ItemStackConversion() {
        Potion potion = new Potion(PotionType.POISON);
        ItemStack itemstack = potion.toItemStack(1);
        assertThat(itemstack.getType(), is(Material.POTION));
        assertTrue(itemstack.getAmount() == 1);
        assertTrue(itemstack.getDurability() == potion.toDamageValue());
    }

    @Test
    public void setExtended() {
        Potion potion = new Potion(PotionType.POISON);
        assertFalse(potion.hasExtendedDuration());
        potion.setHasExtendedDuration(true);
        assertTrue(potion.hasExtendedDuration());
        assertTrue((potion.toDamageValue() & EXTENDED_BIT) != 0);
    }

    @Test
    public void setSplash() {
        Potion potion = new Potion(PotionType.POISON);
        assertFalse(potion.isSplash());
        potion.setSplash(true);
        assertTrue(potion.isSplash());
        assertTrue((potion.toDamageValue() & SPLASH_BIT) != 0);
    }

    @Test
    public void setTier() {
        Potion potion = new Potion(PotionType.POISON);
        assertThat(potion.getTier(), is(Tier.ONE));
        potion.setTier(Tier.TWO);
        assertThat(potion.getTier(), is(Tier.TWO));
        assertTrue(potion.toDamageValue() == (PotionType.POISON.getDamageValue() | potion.getTier().getDamageBit()));
    }

    @Test
    public void useNulls() {
        try {
            new Potion(null);
            fail("cannot use null type in constructor");
        } catch (IllegalArgumentException ex) {
        }

        try {
            new Potion(PotionType.POISON, null);
            fail("cannot use null tier in constructor");
        } catch (IllegalArgumentException ex) {
        }

        Potion potion = new Potion(PotionType.POISON);
        try {
            potion.setTier(null);
            fail("cannot set a null tier");
        } catch (IllegalArgumentException ex) {
        }

        try {
            potion.apply((ItemStack) null);
            fail("cannot apply to a null itemstack");
        } catch (IllegalArgumentException ex) {
        }

        try {
            potion.apply((LivingEntity) null);
            fail("cannot apply to a null entity");
        } catch (IllegalArgumentException ex) {
        }
    }

    private static final int EXTENDED_BIT = 0x40;
    private static final int SPLASH_BIT = 0x4000;
}