Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved the potion test to detect missing PotionType values.

  • Loading branch information...
commit 4a656a3f5c8a5f70580331f8baba9393c3b65b80 1 parent 8032c36
Wesley Wolfe authored November 01, 2012
32  src/test/java/org/bukkit/potion/PotionTest.java
... ...
@@ -1,6 +1,11 @@
1 1
 package org.bukkit.potion;
2 2
 
3 3
 import static org.junit.Assert.*;
  4
+import static org.hamcrest.Matchers.*;
  5
+
  6
+import java.lang.reflect.Field;
  7
+import java.util.EnumMap;
  8
+import java.util.Map;
4 9
 
5 10
 import org.bukkit.craftbukkit.potion.CraftPotionBrewer;
6 11
 import org.junit.BeforeClass;
@@ -9,6 +14,7 @@
9 14
 import net.minecraft.server.MobEffectList;
10 15
 
11 16
 public class PotionTest {
  17
+
12 18
     @BeforeClass
13 19
     public static void setUp() {
14 20
         Potion.setPotionBrewer(new CraftPotionBrewer());
@@ -20,8 +26,32 @@ public static void setUp() {
20 26
     public void getEffects() {
21 27
         for (PotionType type : PotionType.values()) {
22 28
             for (PotionEffect effect : new Potion(type).getEffects()) {
23  
-                assertTrue(effect.getType() == PotionEffectType.getById(effect.getType().getId()));
  29
+                PotionEffectType potionType = effect.getType();
  30
+                assertThat(effect.getType(), is(sameInstance(PotionEffectType.getById(potionType.getId()))));
  31
+
  32
+                assertNotNull(potionType.getName(), PotionType.getByEffect(potionType));
24 33
             }
25 34
         }
26 35
     }
  36
+
  37
+    @Test
  38
+    public void testEffectCompleteness() throws SecurityException, IllegalAccessException, NoSuchFieldException {
  39
+        Field durationsField = net.minecraft.server.PotionBrewer.class.getDeclaredField("effectDurations");
  40
+        durationsField.setAccessible(true);
  41
+        Map<Integer, ?> effectDurations = (Map<Integer, ?>) durationsField.get(null);
  42
+
  43
+        Map<PotionType, String> effects = new EnumMap(PotionType.class);
  44
+        for (int id : effectDurations.keySet()) {
  45
+            PotionEffectType type = PotionEffectType.getById(id);
  46
+            assertNotNull(String.valueOf(id), PotionEffectType.getById(id));
  47
+
  48
+            PotionType enumType = PotionType.getByEffect(type);
  49
+            assertNotNull(type.getName(), enumType);
  50
+
  51
+            assertThat(enumType.name(), effects.put(enumType, enumType.name()), is((String)null));
  52
+        }
  53
+
  54
+        assertThat(effects.entrySet(), hasSize(effectDurations.size()));
  55
+        assertThat(effectDurations.entrySet(), hasSize(PotionType.values().length - /* WATER */ 1));
  56
+    }
27 57
 }

0 notes on commit 4a656a3

Please sign in to comment.
Something went wrong with that request. Please try again.