From 839d00f9d6ca09fc7b2b4fe86b7778e91ee40040 Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Sun, 29 Jan 2012 11:10:40 +0100 Subject: Add some testing --- src/main/java/org/bukkit/Achievement.java | 27 +++-- src/main/java/org/bukkit/Art.java | 34 +++--- src/main/java/org/bukkit/ChatColor.java | 35 +++--- src/main/java/org/bukkit/CoalType.java | 19 ++-- src/main/java/org/bukkit/CropState.java | 31 +++--- src/main/java/org/bukkit/Difficulty.java | 11 +- src/main/java/org/bukkit/DyeColor.java | 47 +++++---- src/main/java/org/bukkit/Effect.java | 30 +++++- src/main/java/org/bukkit/EntityEffect.java | 47 +++++++-- src/main/java/org/bukkit/GameMode.java | 11 +- src/main/java/org/bukkit/GrassSpecies.java | 21 ++-- src/main/java/org/bukkit/Instrument.java | 23 ++-- src/main/java/org/bukkit/Material.java | 49 ++++----- src/main/java/org/bukkit/Note.java | 141 ++++++++++++++----------- src/main/java/org/bukkit/Statistic.java | 23 +++- src/main/java/org/bukkit/TreeSpecies.java | 24 ++--- src/main/java/org/bukkit/WorldType.java | 19 ++-- src/test/java/org/bukkit/AchievementTest.java | 28 +++++ src/test/java/org/bukkit/ArtTest.java | 45 ++++++++ src/test/java/org/bukkit/ChatColorTest.java | 71 ++++++++----- src/test/java/org/bukkit/CoalTypeTest.java | 15 +++ src/test/java/org/bukkit/CropStateTest.java | 15 +++ src/test/java/org/bukkit/DifficultyTest.java | 15 +++ src/test/java/org/bukkit/DyeColorTest.java | 15 +++ src/test/java/org/bukkit/EffectTest.java | 15 +++ src/test/java/org/bukkit/EntityEffectTest.java | 15 +++ src/test/java/org/bukkit/GameModeTest.java | 15 +++ src/test/java/org/bukkit/GrassSpeciesTest.java | 15 +++ src/test/java/org/bukkit/InstrumentTest.java | 15 +++ src/test/java/org/bukkit/MaterialTest.java | 82 ++++++++++++++ src/test/java/org/bukkit/NoteTest.java | 107 +++++++++++++++++++ src/test/java/org/bukkit/StatisticTest.java | 22 ++++ src/test/java/org/bukkit/TreeSpeciesTest.java | 15 +++ src/test/java/org/bukkit/WorldTypeTest.java | 15 +++ 34 files changed, 851 insertions(+), 261 deletions(-) create mode 100644 src/test/java/org/bukkit/AchievementTest.java create mode 100644 src/test/java/org/bukkit/ArtTest.java create mode 100644 src/test/java/org/bukkit/CoalTypeTest.java create mode 100644 src/test/java/org/bukkit/CropStateTest.java create mode 100644 src/test/java/org/bukkit/DifficultyTest.java create mode 100644 src/test/java/org/bukkit/DyeColorTest.java create mode 100644 src/test/java/org/bukkit/EffectTest.java create mode 100644 src/test/java/org/bukkit/EntityEffectTest.java create mode 100644 src/test/java/org/bukkit/GameModeTest.java create mode 100644 src/test/java/org/bukkit/GrassSpeciesTest.java create mode 100644 src/test/java/org/bukkit/InstrumentTest.java create mode 100644 src/test/java/org/bukkit/MaterialTest.java create mode 100644 src/test/java/org/bukkit/NoteTest.java create mode 100644 src/test/java/org/bukkit/StatisticTest.java create mode 100644 src/test/java/org/bukkit/TreeSpeciesTest.java create mode 100644 src/test/java/org/bukkit/WorldTypeTest.java (limited to 'src') diff --git a/src/main/java/org/bukkit/Achievement.java b/src/main/java/org/bukkit/Achievement.java index 06b41a1a..4226ca8f 100644 --- a/src/main/java/org/bukkit/Achievement.java +++ b/src/main/java/org/bukkit/Achievement.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents an achievement, which may be given to players */ @@ -27,8 +28,8 @@ public enum Achievement { /** * The offset used to distinguish Achievements and Statistics */ - public final static int STATISTIC_OFFSET = 5242880; - private final static Map achievements = new HashMap(); + public final static int STATISTIC_OFFSET = 0x500000; + private final static Map BY_ID = Maps.newHashMap(); private final int id; private Achievement(int id) { @@ -53,14 +54,28 @@ public enum Achievement { * * @param id ID of the achievement to return * @return Achievement with the given ID + * @deprecated use {@link Achievement#getById(int)} */ + @Deprecated public static Achievement getAchievement(int id) { - return achievements.get(id); + return BY_ID.get(id); + } + + /** + * Gets the achievement associated with the given ID. + *

+ * Note that the ID must already be offset using {@link #STATISTIC_OFFSET} + * + * @param id ID of the achievement to return + * @return Achievement with the given ID + */ + public static Achievement getById(int id) { + return BY_ID.get(id); } static { - for (Achievement ach : values()) { - achievements.put(ach.getId(), ach); + for (Achievement achievement : values()) { + BY_ID.put(achievement.id, achievement); } } } diff --git a/src/main/java/org/bukkit/Art.java b/src/main/java/org/bukkit/Art.java index 10fad07b..02ac8983 100644 --- a/src/main/java/org/bukkit/Art.java +++ b/src/main/java/org/bukkit/Art.java @@ -2,6 +2,10 @@ package org.bukkit; import java.util.HashMap; +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; + /** * Represents the art on a painting */ @@ -20,26 +24,21 @@ public enum Art { CREEBET(11, 2, 1), WANDERER(12, 1, 2), GRAHAM(13, 1, 2), - MATCH(14, 4, 2), + MATCH(14, 2, 2), BUST(15, 2, 2), STAGE(16, 2, 2), VOID(17, 2, 2), SKULL_AND_ROSES(18, 2, 2), - FIGHTERS(19, 2, 2), + FIGHTERS(19, 4, 2), POINTER(20, 4, 4), PIGSCENE(21, 4, 4), BURNINGSKULL(22, 4, 4), SKELETON(23, 4, 3), DONKEYKONG(24, 4, 3); + private int id, width, height; - private static HashMap names = new HashMap(); - private static HashMap ids = new HashMap(); - static { - for (Art art : Art.values()) { - ids.put(art.id, art); - names.put(art.toString(), art); - } - } + private static final HashMap BY_NAME = Maps.newHashMap(); + private static final HashMap BY_ID = Maps.newHashMap(); private Art(int id, int width, int height) { this.id = id; @@ -81,16 +80,27 @@ public enum Art { * @return The painting */ public static Art getById(int id) { - return ids.get(id); + return BY_ID.get(id); } /** * Get a painting by its unique name + *

+ * This ignores underscores and capitalization * * @param name The name * @return The painting */ public static Art getByName(String name) { - return names.get(name); + Validate.notNull(name, "Name cannot be null"); + + return BY_NAME.get(name.toLowerCase().replaceAll("_", "")); + } + + static { + for (Art art : values()) { + BY_ID.put(art.id, art); + BY_NAME.put(art.toString().toLowerCase().replaceAll("_", ""), art); + } } } diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java index b0ece128..eff64afc 100644 --- a/src/main/java/org/bukkit/ChatColor.java +++ b/src/main/java/org/bukkit/ChatColor.java @@ -1,13 +1,16 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; /** * All supported color values for chat */ public enum ChatColor { - /** * Represents black */ @@ -77,14 +80,19 @@ public enum ChatColor { */ MAGIC('k', 0x10); + public static final char COLOR_CHAR = '\u00A7'; + private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK]"); + private final int intCode; private final char code; - private final static Map colors = new HashMap(); - private final static Map lookup = new HashMap(); + private final String toString; + private final static Map BY_ID = Maps.newHashMap(); + private final static Map BY_CHAR = Maps.newHashMap(); private ChatColor(char code, int intCode) { this.code = code; this.intCode = intCode; + this.toString = new String(new char[] {COLOR_CHAR, code}); } /** @@ -108,7 +116,7 @@ public enum ChatColor { @Override public String toString() { - return String.format("\u00A7%c", code); + return toString; } /** @@ -119,7 +127,7 @@ public enum ChatColor { * @deprecated Use {@link #getByChar(char)} */ public static ChatColor getByCode(final int code) { - return colors.get(code); + return BY_ID.get(code); } /** @@ -129,7 +137,7 @@ public enum ChatColor { * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist */ public static ChatColor getByChar(char code) { - return lookup.get(code); + return BY_CHAR.get(code); } /** @@ -139,7 +147,10 @@ public enum ChatColor { * @return Associative {@link org.bukkit.ChatColor} with the given code, or null if it doesn't exist */ public static ChatColor getByChar(String code) { - return lookup.get(code.charAt(0)); + Validate.notNull(code, "Code cannot be null"); + Validate.isTrue(code.length() > 0, "Code must have at least one char"); + + return BY_CHAR.get(code.charAt(0)); } /** @@ -153,13 +164,13 @@ public enum ChatColor { return null; } - return input.replaceAll("(?i)\u00A7[0-9A-FK]", ""); + return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); } static { - for (ChatColor color : ChatColor.values()) { - colors.put(color.getCode(), color); - lookup.put(color.getChar(), color); + for (ChatColor color : values()) { + BY_ID.put(color.intCode, color); + BY_CHAR.put(color.code, color); } } } diff --git a/src/main/java/org/bukkit/CoalType.java b/src/main/java/org/bukkit/CoalType.java index befb117f..70404c11 100644 --- a/src/main/java/org/bukkit/CoalType.java +++ b/src/main/java/org/bukkit/CoalType.java @@ -1,20 +1,21 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the two types of coal */ public enum CoalType { - COAL((byte) 0x0), - CHARCOAL((byte) 0x1); + COAL(0x0), + CHARCOAL(0x1); private final byte data; - private final static Map types = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private CoalType(byte data) { - this.data = data; + private CoalType(final int data) { + this.data = (byte) data; } /** @@ -35,12 +36,12 @@ public enum CoalType { * it doesn't exist */ public static CoalType getByData(final byte data) { - return types.get(data); + return BY_DATA.get(data); } static { - for (CoalType type : CoalType.values()) { - types.put(type.getData(), type); + for (CoalType type : values()) { + BY_DATA.put(type.data, type); } } } diff --git a/src/main/java/org/bukkit/CropState.java b/src/main/java/org/bukkit/CropState.java index 8167f967..96ba24d8 100644 --- a/src/main/java/org/bukkit/CropState.java +++ b/src/main/java/org/bukkit/CropState.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different growth states of crops */ @@ -11,41 +12,41 @@ public enum CropState { /** * State when first seeded */ - SEEDED((byte) 0x0), + SEEDED(0x0), /** * First growth stage */ - GERMINATED((byte) 0x1), + GERMINATED(0x1), /** * Second growth stage */ - VERY_SMALL((byte) 0x2), + VERY_SMALL(0x2), /** * Third growth stage */ - SMALL((byte) 0x3), + SMALL(0x3), /** * Fourth growth stage */ - MEDIUM((byte) 0x4), + MEDIUM(0x4), /** * Fifth growth stage */ - TALL((byte) 0x5), + TALL(0x5), /** * Almost ripe stage */ - VERY_TALL((byte) 0x6), + VERY_TALL(0x6), /** * Ripe stage */ - RIPE((byte) 0x7); + RIPE(0x7); private final byte data; - private final static Map states = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private CropState(final byte data) { - this.data = data; + private CropState(final int data) { + this.data = (byte) data; } /** @@ -66,12 +67,12 @@ public enum CropState { * it doesn't exist */ public static CropState getByData(final byte data) { - return states.get(data); + return BY_DATA.get(data); } static { - for (CropState s : CropState.values()) { - states.put(s.getData(), s); + for (CropState cropState : values()) { + BY_DATA.put(cropState.getData(), cropState); } } } diff --git a/src/main/java/org/bukkit/Difficulty.java b/src/main/java/org/bukkit/Difficulty.java index da2876b0..cc885e08 100644 --- a/src/main/java/org/bukkit/Difficulty.java +++ b/src/main/java/org/bukkit/Difficulty.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the various difficulty levels that are available. */ @@ -28,7 +29,7 @@ public enum Difficulty { HARD(3); private final int value; - private final static Map difficulties = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private Difficulty(final int value) { this.value = value; @@ -50,12 +51,12 @@ public enum Difficulty { * @return Associative {@link Difficulty} with the given value, or null if it doesn't exist */ public static Difficulty getByValue(final int value) { - return difficulties.get(value); + return BY_ID.get(value); } static { - for (Difficulty diff : Difficulty.values()) { - difficulties.put(diff.getValue(), diff); + for (Difficulty diff : values()) { + BY_ID.put(diff.value, diff); } } } diff --git a/src/main/java/org/bukkit/DyeColor.java b/src/main/java/org/bukkit/DyeColor.java index b216dd8e..531323fe 100644 --- a/src/main/java/org/bukkit/DyeColor.java +++ b/src/main/java/org/bukkit/DyeColor.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * All supported color values for dyes and cloth */ @@ -11,73 +12,73 @@ public enum DyeColor { /** * Represents white dye */ - WHITE((byte) 0x0), + WHITE(0x0), /** * Represents orange dye */ - ORANGE((byte) 0x1), + ORANGE(0x1), /** * Represents magenta dye */ - MAGENTA((byte) 0x2), + MAGENTA(0x2), /** * Represents light blue dye */ - LIGHT_BLUE((byte) 0x3), + LIGHT_BLUE(0x3), /** * Represents yellow dye */ - YELLOW((byte) 0x4), + YELLOW(0x4), /** * Represents lime dye */ - LIME((byte) 0x5), + LIME(0x5), /** * Represents pink dye */ - PINK((byte) 0x6), + PINK(0x6), /** * Represents gray dye */ - GRAY((byte) 0x7), + GRAY(0x7), /** * Represents silver dye */ - SILVER((byte) 0x8), + SILVER(0x8), /** * Represents cyan dye */ - CYAN((byte) 0x9), + CYAN(0x9), /** * Represents purple dye */ - PURPLE((byte) 0xA), + PURPLE(0xA), /** * Represents blue dye */ - BLUE((byte) 0xB), + BLUE(0xB), /** * Represents brown dye */ - BROWN((byte) 0xC), + BROWN(0xC), /** * Represents green dye */ - GREEN((byte) 0xD), + GREEN(0xD), /** * Represents red dye */ - RED((byte) 0xE), + RED(0xE), /** * Represents black dye */ - BLACK((byte) 0xF); + BLACK(0xF); private final byte data; - private final static Map colors = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private DyeColor(final byte data) { - this.data = data; + private DyeColor(final int data) { + this.data = (byte) data; } /** @@ -96,12 +97,12 @@ public enum DyeColor { * @return The {@link DyeColor} representing the given value, or null if it doesn't exist */ public static DyeColor getByData(final byte data) { - return colors.get(data); + return BY_DATA.get(data); } static { - for (DyeColor color : DyeColor.values()) { - colors.put(color.getData(), color); + for (DyeColor color : values()) { + BY_DATA.put(color.getData(), color); } } } diff --git a/src/main/java/org/bukkit/Effect.java b/src/main/java/org/bukkit/Effect.java index 028c27dd..14c197df 100644 --- a/src/main/java/org/bukkit/Effect.java +++ b/src/main/java/org/bukkit/Effect.java @@ -1,12 +1,16 @@ package org.bukkit; +import java.util.Map; + +import com.google.common.collect.Maps; + /** * A list of effects that the server is able to send to players. */ public enum Effect { - BOW_FIRE(1002), - CLICK1(1001), CLICK2(1000), + CLICK1(1001), + BOW_FIRE(1002), DOOR_TOGGLE(1003), EXTINGUISH(1004), RECORD_PLAY(1005), @@ -20,12 +24,34 @@ public enum Effect { MOBSPAWNER_FLAMES(2004); private final int id; + private static final Map BY_ID = Maps.newHashMap(); Effect(int id) { this.id = id; } + /** + * Gets the ID for this effect. + * + * @return ID of this effect + */ public int getId() { return this.id; } + + /** + * Gets the Effect associated with the given ID. + * + * @param id ID of the Effect to return + * @return Effect with the given ID + */ + public static Effect getById(int id) { + return BY_ID.get(id); + } + + static { + for (Effect effect : values()) { + BY_ID.put(effect.id, effect); + } + } } diff --git a/src/main/java/org/bukkit/EntityEffect.java b/src/main/java/org/bukkit/EntityEffect.java index 4a04e96a..fbf21fb3 100644 --- a/src/main/java/org/bukkit/EntityEffect.java +++ b/src/main/java/org/bukkit/EntityEffect.java @@ -1,5 +1,9 @@ package org.bukkit; +import java.util.Map; + +import com.google.common.collect.Maps; + /** * A list of all Effects that can happen to entities. */ @@ -8,46 +12,73 @@ public enum EntityEffect { /** * When mobs get hurt. */ - HURT((byte) 2), + HURT(2), /** * When a mob dies. *

* This will cause client-glitches! */ - DEATH((byte) 3), + DEATH(3), /** * The smoke when taming a wolf fails. *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_SMOKE((byte) 6), + WOLF_SMOKE(6), /** * The hearts when taming a wolf succeeds. *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_HEARTS((byte) 7), + WOLF_HEARTS(7), /** * When a wolf shakes (after being wet). *

* Without client-mods this will be ignored if the entity is not a wolf. */ - WOLF_SHAKE((byte) 8); + WOLF_SHAKE(8), + + /** + * When a sheep eats a LONG_GRASS block. + */ + SHEEP_EAT(10); private final byte data; + private final static Map BY_DATA = Maps.newHashMap(); - EntityEffect(byte data) { - this.data = data; + EntityEffect(final int data) { + this.data = (byte) data; } /** - * @return The data-value that is sent to the client to play this effect. + * Gets the EntityEffect with the given data value + * + * @param data Data value to fetch + * @return The {@link EntityEffect} representing the given value, or null if + * it doesn't exist */ public byte getData() { return data; } + + /** + * Gets the EntityEffect with the given data value + * + * @param data Data value to fetch + * @return The {@link EntityEffect} representing the given value, or null if it doesn't exist + */ + public static EntityEffect getByData(final byte data) { + return BY_DATA.get(data); + } + + + static { + for (EntityEffect entityEffect : values()) { + BY_DATA.put(entityEffect.data, entityEffect); + } + } } diff --git a/src/main/java/org/bukkit/GameMode.java b/src/main/java/org/bukkit/GameMode.java index 2c516750..b81b069c 100644 --- a/src/main/java/org/bukkit/GameMode.java +++ b/src/main/java/org/bukkit/GameMode.java @@ -1,10 +1,11 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; import org.bukkit.entity.HumanEntity; +import com.google.common.collect.Maps; + /** * Represents the various type of game modes that {@link HumanEntity}s may have */ @@ -20,7 +21,7 @@ public enum GameMode { SURVIVAL(0); private final int value; - private final static Map modes = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private GameMode(final int value) { this.value = value; @@ -42,12 +43,12 @@ public enum GameMode { * @return Associative {@link GameMode} with the given value, or null if it doesn't exist */ public static GameMode getByValue(final int value) { - return modes.get(value); + return BY_ID.get(value); } static { - for (GameMode mode : GameMode.values()) { - modes.put(mode.getValue(), mode); + for (GameMode mode : values()) { + BY_ID.put(mode.getValue(), mode); } } } diff --git a/src/main/java/org/bukkit/GrassSpecies.java b/src/main/java/org/bukkit/GrassSpecies.java index d7387513..3f2cce42 100644 --- a/src/main/java/org/bukkit/GrassSpecies.java +++ b/src/main/java/org/bukkit/GrassSpecies.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different types of grass. */ @@ -11,21 +12,21 @@ public enum GrassSpecies { /** * Represents the dead looking grass. */ - DEAD((byte) 0x0), + DEAD(0x0), /** * Represents the normal grass species. */ - NORMAL((byte) 0x1), + NORMAL(0x1), /** * Represents the fern-looking grass species. */ - FERN_LIKE((byte) 0x2); + FERN_LIKE(0x2); private final byte data; - private final static Map species = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private GrassSpecies(final byte data) { - this.data = data; + private GrassSpecies(final int data) { + this.data = (byte) data; } /** @@ -46,12 +47,12 @@ public enum GrassSpecies { * it doesn't exist */ public static GrassSpecies getByData(final byte data) { - return species.get(data); + return BY_DATA.get(data); } static { - for (GrassSpecies s : GrassSpecies.values()) { - species.put(s.getData(), s); + for (GrassSpecies grassSpecies : values()) { + BY_DATA.put(grassSpecies.getData(), grassSpecies); } } } diff --git a/src/main/java/org/bukkit/Instrument.java b/src/main/java/org/bukkit/Instrument.java index e54d0a2a..0d19db67 100644 --- a/src/main/java/org/bukkit/Instrument.java +++ b/src/main/java/org/bukkit/Instrument.java @@ -1,21 +1,22 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + public enum Instrument { - PIANO((byte) 0x0), // All other - BASS_DRUM((byte) 0x1), // Stone - SNARE_DRUM((byte) 0x2), // Sand - STICKS((byte) 0x3), // Glass - BASS_GUITAR((byte) 0x4); // Wood + PIANO(0x0), // All other + BASS_DRUM(0x1), // Stone + SNARE_DRUM(0x2), // Sand + STICKS(0x3), // Glass + BASS_GUITAR(0x4); // Wood private final byte type; - private final static Map types = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private Instrument(byte type) { - this.type = type; + private Instrument(final int type) { + this.type = (byte) type; } public byte getType() { @@ -23,12 +24,12 @@ public enum Instrument { } public static Instrument getByType(final byte type) { - return types.get(type); + return BY_DATA.get(type); } static { for (Instrument instrument : Instrument.values()) { - types.put(instrument.getType(), instrument); + BY_DATA.put(instrument.getType(), instrument); } } } diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java index 23747603..e9fadb05 100644 --- a/src/main/java/org/bukkit/Material.java +++ b/src/main/java/org/bukkit/Material.java @@ -2,13 +2,16 @@ package org.bukkit; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; + +import org.apache.commons.lang.Validate; import org.bukkit.material.*; import org.bukkit.util.Java15Compat; +import com.google.common.collect.Maps; + /** * An enum of all material ids accepted by the official server + client */ @@ -76,12 +79,12 @@ public enum Material { SOIL(60, MaterialData.class), FURNACE(61, Furnace.class), BURNING_FURNACE(62, Furnace.class), - SIGN_POST(63, 1, Sign.class), + SIGN_POST(63, 64, Sign.class), WOODEN_DOOR(64, Door.class), LADDER(65, Ladder.class), RAILS(66, Rails.class), COBBLESTONE_STAIRS(67, Stairs.class), - WALL_SIGN(68, 1, Sign.class), + WALL_SIGN(68, 64, Sign.class), LEVER(69, Lever.class), STONE_PLATE(70, PressurePlate.class), IRON_DOOR_BLOCK(71, Door.class), @@ -105,7 +108,7 @@ public enum Material { GLOWSTONE(89), PORTAL(90), JACK_O_LANTERN(91, Pumpkin.class), - CAKE_BLOCK(92, 1, Cake.class), + CAKE_BLOCK(92, 64, Cake.class), DIODE_BLOCK_OFF(93, Diode.class), DIODE_BLOCK_ON(94, Diode.class), LOCKED_CHEST(95), @@ -179,11 +182,11 @@ public enum Material { SEEDS(295), WHEAT(296), BREAD(297), - LEATHER_HELMET(298, 1, 50), + LEATHER_HELMET(298, 1, 55), LEATHER_CHESTPLATE(299, 1, 80), LEATHER_LEGGINGS(300, 1, 75), LEATHER_BOOTS(301, 1, 65), - CHAINMAIL_HELMET(302, 1, 166), + CHAINMAIL_HELMET(302, 1, 165), CHAINMAIL_CHESTPLATE(303, 1, 240), CHAINMAIL_LEGGINGS(304, 1, 225), CHAINMAIL_BOOTS(305, 1, 195), @@ -279,8 +282,8 @@ public enum Material { private final int id; private final Class data; - private static Material[] lookupId = new Material[3200]; - private static final Map lookupName = new HashMap(); + private static Material[] byId = new Material[383]; + private final static Map BY_NAME = Maps.newHashMap(); private final int maxStack; private final short durability; @@ -301,14 +304,14 @@ public enum Material { } private Material(final int id, final int stack, final Class data) { - this(id, stack, -1, data); + this(id, stack, 0, data); } private Material(final int id, final int stack, final int durability, final Class data) { this.id = id; this.durability = (short) durability; this.maxStack = stack; - this.data = data; + this.data = data == null ? MaterialData.class : data; } /** @@ -344,7 +347,7 @@ public enum Material { * @return MaterialData associated with this Material */ public Class getData() { - return (data == null) ? MaterialData.class : data; + return data; } /** @@ -355,10 +358,6 @@ public enum Material { * @return New MaterialData with the given data */ public MaterialData getNewData(final byte raw) { - if (data == null) { - return new MaterialData(id, raw); - } - try { Constructor ctor = data.getConstructor(int.class, byte.class); @@ -420,8 +419,8 @@ public enum Material { * @return Material if found, or null */ public static Material getMaterial(final int id) { - if (lookupId.length > id) { - return lookupId[id]; + if (byId.length > id) { + return byId[id]; } else { return null; } @@ -436,7 +435,7 @@ public enum Material { * @return Material if found, or null */ public static Material getMaterial(final String name) { - return lookupName.get(name); + return BY_NAME.get(name); } /** @@ -448,6 +447,8 @@ public enum Material { * @return Material if found, or null */ public static Material matchMaterial(final String name) { + Validate.notNull(name, "Name cannot be null"); + Material result = null; try { @@ -458,7 +459,7 @@ public enum Material { String filtered = name.toUpperCase(); filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); - result = lookupName.get(filtered); + result = BY_NAME.get(filtered); } return result; @@ -466,13 +467,13 @@ public enum Material { static { for (Material material : values()) { - if (lookupId.length > material.id) { - lookupId[material.id] = material; + if (byId.length > material.id) { + byId[material.id] = material; } else { - lookupId = Java15Compat.Arrays_copyOfRange(lookupId, 0, material.id + 2); - lookupId[material.id] = material; + byId = Java15Compat.Arrays_copyOfRange(byId, 0, material.id + 2); + byId[material.id] = material; } - lookupName.put(material.name(), material); + BY_NAME.put(material.name(), material); } } } diff --git a/src/main/java/org/bukkit/Note.java b/src/main/java/org/bukkit/Note.java index d8b88124..fd84c14c 100644 --- a/src/main/java/org/bukkit/Note.java +++ b/src/main/java/org/bukkit/Note.java @@ -1,8 +1,11 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang.Validate; + +import com.google.common.collect.Maps; + /** * A note class to store a specific note. */ @@ -12,22 +15,23 @@ public class Note { * An enum holding tones. */ public enum Tone { - F((byte) -0x1, true), - G((byte) 0x1, true), - A((byte) 0x3, true), - B((byte) 0x5, false), - C((byte) 0x6, true), - D((byte) 0x8, true), - E((byte) 0xA, false); + G(0x1, true), + A(0x3, true), + B(0x5, false), + C(0x6, true), + D(0x8, true), + E(0xA, false), + F(0xB, true); private final boolean sharpable; private final byte id; - private static final Map tones = new HashMap(); + + private static final Map BY_DATA = Maps.newHashMap(); /** The number of tones including sharped tones. */ - public static final byte TONES_COUNT; + public static final byte TONES_COUNT = 12; - private Tone(byte id, boolean sharpable) { - this.id = id; + private Tone(int id, boolean sharpable) { + this.id = (byte) (id % TONES_COUNT); this.sharpable = sharpable; } @@ -45,17 +49,13 @@ public class Note { * sharped id of the tone. If the tone couldn't be sharped it always * return the not sharped id of this tone. * - * @param sharped - * Set to true to return the sharped id. + * @param sharped Set to true to return the sharped id. * @return the id of this tone. */ public byte getId(boolean sharped) { - byte tempId = (byte) (sharped && sharpable ? id + 1 : id); + byte id = (byte) (sharped && sharpable ? this.id + 1 : this.id); - while (tempId < 0) { - tempId += TONES_COUNT; - } - return (byte) (tempId % TONES_COUNT); + return (byte) (id % TONES_COUNT); } /** @@ -70,11 +70,9 @@ public class Note { /** * Returns if this tone id is the sharped id of the tone. * - * @param id - * the id of the tone. + * @param id the id of the tone. * @return if the tone id is the sharped id of the tone. - * @throws IllegalArgumentException - * if neither the tone nor the semitone have the id. + * @throws IllegalArgumentException if neither the tone nor the semitone have the id. */ public boolean isSharped(byte id) { if (id == getId(false)) { @@ -90,34 +88,35 @@ public class Note { /** * Returns the tone to id. Also returning the semitones. * - * @param id - * the id of the tone. + * @param id the id of the tone. * @return the tone to id. + * @deprecated use {@link #getById(byte)} */ + @Deprecated public static Tone getToneById(byte id) { - return tones.get(id); + return BY_DATA.get(id); + } + + /** + * Returns the tone to id. Also returning the semitones. + * + * @param id the id of the tone. + * @return the tone to id. + */ + public static Tone getById(byte id) { + return BY_DATA.get(id); } static { - byte lowest = F.id; - byte highest = F.id; - for (Tone tone : Tone.values()) { - byte id = tone.id; - tones.put(id, tone); - if (id < lowest) { - lowest = id; - } + for (Tone tone : values()) { + int id = tone.id % TONES_COUNT; + BY_DATA.put((byte) id, tone); + if (tone.isSharpable()) { - id++; - tones.put(id, tone); - } - if (id > highest) { - highest = id; + id = (id + 1) % TONES_COUNT; + BY_DATA.put((byte) id, tone); } } - - TONES_COUNT = (byte) (highest - lowest + 1); - tones.put((byte) (TONES_COUNT - 1), F); } } @@ -126,36 +125,29 @@ public class Note { /** * Creates a new note. * - * @param note - * Internal note id. {@link #getId()} always return this value. + * @param note Internal note id. {@link #getId()} always return this value. * The value has to be in the interval [0; 24]. */ - public Note(byte note) { - if (note < 0 || note > 24) { - throw new IllegalArgumentException("The note value has to be between 0 and 24."); - } - this.note = note; + public Note(int note) { + Validate.isTrue(note >= 0 && note <= 24, "The note value has to be between 0 and 24."); + + this.note = (byte) note; } /** * Creates a new note. * - * @param octave - * The octave where the note is in. Has to be 0 - 2. - * @param note - * The tone within the octave. If the octave is 2 the note has to - * be F#. - * @param sharped - * Set it the tone is sharped (e.g. for F#). + * @param octave The octave where the note is in. Has to be 0 - 2. + * @param tone The tone within the octave. If the octave is 2 the note has to be F#. + * @param sharped Set it the tone is sharped (e.g. for F#). */ - public Note(byte octave, Tone note, boolean sharped) { - if (sharped && !note.isSharpable()) { - throw new IllegalArgumentException("This tone could not be sharped."); - } - if (octave < 0 || octave > 2 || (octave == 2 && !(note == Tone.F && sharped))) { + public Note(int octave, Tone tone, boolean sharped) { + Validate.isTrue(!(sharped && !tone.isSharpable()), "This tone could not be sharped."); + if (octave < 0 || octave > 2 || (octave == 2 && !(tone == Tone.F && sharped))) { throw new IllegalArgumentException("Tone and octave have to be between F#0 and F#2"); } - this.note = (byte) (octave * Tone.TONES_COUNT + note.getId(sharped)); + + this.note = (byte) (octave * Tone.TONES_COUNT + tone.getId(sharped)); } /** @@ -186,7 +178,7 @@ public class Note { * @return the tone of this note. */ public Tone getTone() { - return Tone.getToneById(getToneByte()); + return Tone.getById(getToneByte()); } /** @@ -196,7 +188,28 @@ public class Note { */ public boolean isSharped() { byte note = getToneByte(); - return Tone.getToneById(note).isSharped(note); + return Tone.getById(note).isSharped(note); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + note; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Note other = (Note) obj; + if (note != other.note) + return false; + return true; + } } diff --git a/src/main/java/org/bukkit/Statistic.java b/src/main/java/org/bukkit/Statistic.java index e7195bb6..41d1b838 100644 --- a/src/main/java/org/bukkit/Statistic.java +++ b/src/main/java/org/bukkit/Statistic.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents a countable statistic, which is collected by the client */ @@ -17,7 +18,7 @@ public enum Statistic { USE_ITEM(6908288, false), BREAK_ITEM(16973824, true); - private final static Map statistics = new HashMap(); + private final static Map BY_ID = Maps.newHashMap(); private final int id; private final boolean isSubstat; private final boolean isBlock; @@ -70,14 +71,26 @@ public enum Statistic { * * @param id ID of the statistic to return * @return statistic with the given ID + * @deprecated See {@link #getById(int)} */ + @Deprecated public static Statistic getStatistic(int id) { - return statistics.get(id); + return BY_ID.get(id); + } + + /** + * Gets the statistic associated with the given ID. + * + * @param id ID of the statistic to return + * @return statistic with the given ID + */ + public static Statistic getById(int id) { + return BY_ID.get(id); } static { - for (Statistic stat : values()) { - statistics.put(stat.getId(), stat); + for (Statistic statistic : values()) { + BY_ID.put(statistic.id, statistic); } } } diff --git a/src/main/java/org/bukkit/TreeSpecies.java b/src/main/java/org/bukkit/TreeSpecies.java index cc286731..d01b79f4 100644 --- a/src/main/java/org/bukkit/TreeSpecies.java +++ b/src/main/java/org/bukkit/TreeSpecies.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents the different species of trees regardless of size. */ @@ -11,21 +12,21 @@ public enum TreeSpecies { /** * Represents the common tree species. */ - GENERIC((byte) 0x0), + GENERIC(0x0), /** * Represents the darker barked/leaved tree species. */ - REDWOOD((byte) 0x1), + REDWOOD(0x1), /** * Represents birches. */ - BIRCH((byte) 0x2); + BIRCH(0x2); private final byte data; - private final static Map species = new HashMap(); + private final static Map BY_DATA = Maps.newHashMap(); - private TreeSpecies(final byte data) { - this.data = data; + private TreeSpecies(final int data) { + this.data = (byte) data; } /** @@ -40,18 +41,17 @@ public enum TreeSpecies { /** * Gets the TreeSpecies with the given data value * - * @param data - * Data value to fetch + * @param data Data value to fetch * @return The {@link TreeSpecies} representing the given value, or null if * it doesn't exist */ public static TreeSpecies getByData(final byte data) { - return species.get(data); + return BY_DATA.get(data); } static { - for (TreeSpecies s : TreeSpecies.values()) { - species.put(s.getData(), s); + for (TreeSpecies species : values()) { + BY_DATA.put(species.data, species); } } } diff --git a/src/main/java/org/bukkit/WorldType.java b/src/main/java/org/bukkit/WorldType.java index 36ad85ef..93c0957c 100644 --- a/src/main/java/org/bukkit/WorldType.java +++ b/src/main/java/org/bukkit/WorldType.java @@ -1,8 +1,9 @@ package org.bukkit; -import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + /** * Represents various types of worlds that may exist */ @@ -10,15 +11,9 @@ public enum WorldType { NORMAL("DEFAULT"), FLAT("FLAT"); - private final static Map lookup = new HashMap(); + private final static Map BY_NAME = Maps.newHashMap(); private final String name; - static { - for (WorldType type : values()) { - lookup.put(type.name, type); - } - } - private WorldType(String name) { this.name = name; } @@ -39,6 +34,12 @@ public enum WorldType { * @return Requested WorldType, or null if not found */ public static WorldType getByName(String name) { - return lookup.get(name.toUpperCase()); + return BY_NAME.get(name.toUpperCase()); + } + + static { + for (WorldType type : values()) { + BY_NAME.put(type.name, type); + } } } diff --git a/src/test/java/org/bukkit/AchievementTest.java b/src/test/java/org/bukkit/AchievementTest.java new file mode 100644 index 00000000..ddd46e78 --- /dev/null +++ b/src/test/java/org/bukkit/AchievementTest.java @@ -0,0 +1,28 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class AchievementTest { + + @Test + public void getByDeprecated() { + for (Achievement achievement : Achievement.values()) { + assertThat(Achievement.getAchievement(achievement.getId()), is(achievement)); + } + } + + @Test + public void getById() { + for (Achievement achievement : Achievement.values()) { + assertThat(Achievement.getById(achievement.getId()), is(achievement)); + } + } + + @Test + public void getByOffset() { + assertThat(Achievement.getById(Achievement.STATISTIC_OFFSET), is(Achievement.values()[0])); + } +} diff --git a/src/test/java/org/bukkit/ArtTest.java b/src/test/java/org/bukkit/ArtTest.java new file mode 100644 index 00000000..29fe34ad --- /dev/null +++ b/src/test/java/org/bukkit/ArtTest.java @@ -0,0 +1,45 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class ArtTest { + + @Test(expected = IllegalArgumentException.class) + public void getByNullName() { + Art.getByName(null); + } + + @Test + public void getById() { + for (Art art : Art.values()) { + assertThat(Art.getById(art.getId()), is(art)); + } + } + + @Test + public void getByName() { + for (Art art : Art.values()) { + assertThat(Art.getByName(art.toString()), is(art)); + } + } + + @Test + public void dimensionSanityCheck() { + for (Art art : Art.values()) { + assertThat(art.getBlockHeight(), is(greaterThan(0))); + assertThat(art.getBlockWidth(), is(greaterThan(0))); + } + } + + @Test + public void getByNameWithMixedCase() { + Art subject = Art.values()[0]; + String name = subject.toString().replace('E', 'e'); + + assertThat(Art.getByName(name), is(subject)); + } +} diff --git a/src/test/java/org/bukkit/ChatColorTest.java b/src/test/java/org/bukkit/ChatColorTest.java index acf7c5e9..7ccff623 100644 --- a/src/test/java/org/bukkit/ChatColorTest.java +++ b/src/test/java/org/bukkit/ChatColorTest.java @@ -1,58 +1,71 @@ package org.bukkit; -import org.junit.AfterClass; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.BeforeClass; -import static org.hamcrest.CoreMatchers.*; public class ChatColorTest { @Test - public void testGetCode() { - ChatColor color = ChatColor.DARK_RED; - assertThat(color.getCode(), equalTo(4)); + public void getByDeprecated() { + for (ChatColor color : ChatColor.values()) { + assertThat(ChatColor.getByCode(color.getCode()), is(color)); + } } @Test - public void testGetChar() { - ChatColor color = ChatColor.MAGIC; - assertThat(color.getChar(), equalTo('k')); + public void getByChar() { + for (ChatColor color : ChatColor.values()) { + assertThat(ChatColor.getByChar(color.getChar()), is(color)); + } } - @Test - public void testToString() { - ChatColor color = ChatColor.LIGHT_PURPLE; - assertThat(color.toString(), equalTo("\u00A7d")); + @Test(expected = IllegalArgumentException.class) + public void getByStringWithNull() { + ChatColor.getByChar((String) null); + } + + @Test(expected = IllegalArgumentException.class) + public void getByStringWithEmpty() { + ChatColor.getByChar(""); } @Test - public void testGetByCode() { - ChatColor color = ChatColor.AQUA; - assertThat(ChatColor.getByCode(color.getCode()), equalTo(color)); + public void getByNull() { + assertThat(ChatColor.stripColor(null), is(nullValue())); } @Test - public void testGetByChar_char() { - ChatColor color = ChatColor.GOLD; - assertThat(ChatColor.getByChar(color.getChar()), equalTo(color)); + public void getByString() { + for (ChatColor color : ChatColor.values()) { + assertThat(ChatColor.getByChar(String.valueOf(color.getChar())), is(color)); + } } @Test - public void testGetByChar_String() { - ChatColor color = ChatColor.BLUE; - assertThat(ChatColor.getByChar(((Character)color.getChar()).toString()), equalTo(color)); + public void stripColorOnNullString() { + assertThat(ChatColor.stripColor(null), is(nullValue())); } @Test - public void testStripColor() { - String string = ""; - String expected = ""; + public void stripColor() { + StringBuilder subject = new StringBuilder(); + StringBuilder expected = new StringBuilder(); + final String filler = "test"; for (ChatColor color : ChatColor.values()) { - string += color + "test"; - expected += "test"; + subject.append(color).append(filler); + expected.append(filler); } - assertThat(ChatColor.stripColor(string), equalTo(expected)); + assertThat(ChatColor.stripColor(subject.toString()), is(expected.toString())); + } + + @Test + public void toStringWorks() { + for (ChatColor color : ChatColor.values()) { + assertThat(String.format("%c%c", ChatColor.COLOR_CHAR, color.getChar()), is(color.toString())); + } } } diff --git a/src/test/java/org/bukkit/CoalTypeTest.java b/src/test/java/org/bukkit/CoalTypeTest.java new file mode 100644 index 00000000..7eb11cea --- /dev/null +++ b/src/test/java/org/bukkit/CoalTypeTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class CoalTypeTest { + @Test + public void getByData() { + for (CoalType coalType : CoalType.values()) { + assertThat(CoalType.getByData(coalType.getData()), is(coalType)); + } + } +} diff --git a/src/test/java/org/bukkit/CropStateTest.java b/src/test/java/org/bukkit/CropStateTest.java new file mode 100644 index 00000000..f792a1c4 --- /dev/null +++ b/src/test/java/org/bukkit/CropStateTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class CropStateTest { + @Test + public void getByData() { + for (CropState cropState : CropState.values()) { + assertThat(CropState.getByData(cropState.getData()), is(cropState)); + } + } +} diff --git a/src/test/java/org/bukkit/DifficultyTest.java b/src/test/java/org/bukkit/DifficultyTest.java new file mode 100644 index 00000000..293e283a --- /dev/null +++ b/src/test/java/org/bukkit/DifficultyTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class DifficultyTest { + @Test + public void getByValue() { + for (Difficulty difficulty : Difficulty.values()) { + assertThat(Difficulty.getByValue(difficulty.getValue()), is(difficulty)); + } + } +} diff --git a/src/test/java/org/bukkit/DyeColorTest.java b/src/test/java/org/bukkit/DyeColorTest.java new file mode 100644 index 00000000..70049d7f --- /dev/null +++ b/src/test/java/org/bukkit/DyeColorTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class DyeColorTest { + @Test + public void getByData() { + for (DyeColor dyeColor : DyeColor.values()) { + assertThat(DyeColor.getByData(dyeColor.getData()), is(dyeColor)); + } + } +} diff --git a/src/test/java/org/bukkit/EffectTest.java b/src/test/java/org/bukkit/EffectTest.java new file mode 100644 index 00000000..08aa71d3 --- /dev/null +++ b/src/test/java/org/bukkit/EffectTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class EffectTest { + @Test + public void getById() { + for (Effect effect : Effect.values()) { + assertThat(Effect.getById(effect.getId()), is(effect)); + } + } +} diff --git a/src/test/java/org/bukkit/EntityEffectTest.java b/src/test/java/org/bukkit/EntityEffectTest.java new file mode 100644 index 00000000..0c92d5cd --- /dev/null +++ b/src/test/java/org/bukkit/EntityEffectTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class EntityEffectTest { + @Test + public void getByData() { + for (EntityEffect entityEffect : EntityEffect.values()) { + assertThat(EntityEffect.getByData(entityEffect.getData()), is(entityEffect)); + } + } +} diff --git a/src/test/java/org/bukkit/GameModeTest.java b/src/test/java/org/bukkit/GameModeTest.java new file mode 100644 index 00000000..f671faef --- /dev/null +++ b/src/test/java/org/bukkit/GameModeTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class GameModeTest { + @Test + public void getByValue() { + for (GameMode gameMode : GameMode.values()) { + assertThat(GameMode.getByValue(gameMode.getValue()), is(gameMode)); + } + } +} diff --git a/src/test/java/org/bukkit/GrassSpeciesTest.java b/src/test/java/org/bukkit/GrassSpeciesTest.java new file mode 100644 index 00000000..9e332c71 --- /dev/null +++ b/src/test/java/org/bukkit/GrassSpeciesTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class GrassSpeciesTest { + @Test + public void getByData() { + for (GrassSpecies grassSpecies : GrassSpecies.values()) { + assertThat(GrassSpecies.getByData(grassSpecies.getData()), is(grassSpecies)); + } + } +} diff --git a/src/test/java/org/bukkit/InstrumentTest.java b/src/test/java/org/bukkit/InstrumentTest.java new file mode 100644 index 00000000..f1a05705 --- /dev/null +++ b/src/test/java/org/bukkit/InstrumentTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class InstrumentTest { + @Test + public void getByType() { + for (Instrument instrument : Instrument.values()) { + assertThat(Instrument.getByType(instrument.getType()), is(instrument)); + } + } +} diff --git a/src/test/java/org/bukkit/MaterialTest.java b/src/test/java/org/bukkit/MaterialTest.java new file mode 100644 index 00000000..46be920b --- /dev/null +++ b/src/test/java/org/bukkit/MaterialTest.java @@ -0,0 +1,82 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class MaterialTest { + @Test + public void getByName() { + for (Material material : Material.values()) { + assertThat(Material.getMaterial(material.toString()), is(material)); + } + } + + @Test + public void getById() { + for (Material material : Material.values()) { + assertThat(Material.getMaterial(material.getId()), is(material)); + } + } + + @Test + public void isBlock() { + for (Material material : Material.values()) { + if (material.getId() > 255) continue; + + assertTrue(String.format("[%d] %s", material.getId(), material.toString()), material.isBlock()); + } + } + + @Test + public void getByOutOfRangeId() { + assertThat(Material.getMaterial(Integer.MAX_VALUE), is(nullValue())); + } + + @Test + public void getByNameNull() { + assertThat(Material.getMaterial(null), is(nullValue())); + } + + // [EB]: gawd we need better code >.> + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void getData() { + for (Material material : Material.values()) { + Class clazz = material.getData(); + + assertThat(material.getNewData((byte) 0), isA(clazz)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void matchMaterialByNull() { + Material.matchMaterial(null); + } + + @Test + public void matchMaterialById() { + for (Material material : Material.values()) { + assertThat(Material.matchMaterial(String.valueOf(material.getId())), is(material)); + } + } + + @Test + public void matchMaterialByName() { + for (Material material : Material.values()) { + assertThat(Material.matchMaterial(material.toString()), is(material)); + } + } + + @Test + public void matchMaterialByLowerCaseAndSpaces() { + for (Material material : Material.values()) { + String name = material.toString().replaceAll("_", " ").toLowerCase(); + assertThat(Material.matchMaterial(name), is(material)); + } + } +} diff --git a/src/test/java/org/bukkit/NoteTest.java b/src/test/java/org/bukkit/NoteTest.java new file mode 100644 index 00000000..34284b96 --- /dev/null +++ b/src/test/java/org/bukkit/NoteTest.java @@ -0,0 +1,107 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Collection; + +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class NoteTest { + @Test + public void getToneByDeprecated() { + for (Note.Tone tone : Note.Tone.values()) { + assertThat(Note.Tone.getToneById(tone.getId()), is(tone)); + } + } + + @Test + public void getToneByData() { + for (Note.Tone tone : Note.Tone.values()) { + assertThat(Note.Tone.getById(tone.getId()), is(tone)); + } + } + + @Test + public void verifySharpedData() { + for (Note.Tone tone : Note.Tone.values()) { + if (!tone.isSharpable()) return; + + assertFalse(tone.isSharped(tone.getId(false))); + assertTrue(tone.isSharped(tone.getId(true))); + } + } + + @Test + public void verifyUnknownToneData() { + Collection tones = Lists.newArrayList(); + for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { + tones.add((byte) i); + } + + for (Note.Tone tone : Note.Tone.values()) { + if (tone.isSharpable()) tones.remove(tone.getId(true)); + tones.remove(tone.getId()); + } + + for (Byte data : tones) { + assertThat(Note.Tone.getById(data), is(nullValue())); + + for (Note.Tone tone : Note.Tone.values()) { + try { + tone.isSharped(data); + + fail(data + " should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertNotNull(e); + } + } + } + } + + @Test(expected = IllegalArgumentException.class) + public void createNoteBelowMin() { + new Note((byte) -1); + } + + @Test(expected = IllegalArgumentException.class) + public void createNoteAboveMax() { + new Note((byte) 25); + } + + @Test(expected = IllegalArgumentException.class) + public void createNoteOctaveBelowMax() { + new Note((byte) -1, Note.Tone.A, true); + } + + @Test(expected = IllegalArgumentException.class) + public void createNoteOctaveAboveMax() { + new Note((byte) 3, Note.Tone.A, true); + } + + @Test(expected = IllegalArgumentException.class) + public void createNoteOctaveNonSharpable() { + new Note((byte) 0, Note.Tone.B, true); + } + + @Test + public void doo() { + for (int i = 1; i <= 24; i++) { + Note note = new Note((byte) i); + int octave = note.getOctave(); + Note.Tone tone = note.getTone(); + boolean sharped = note.isSharped(); + + Note newNote = new Note(octave, tone, sharped); + assertThat(newNote, is(note)); + assertThat(newNote.getId(), is(note.getId())); + } + } +} diff --git a/src/test/java/org/bukkit/StatisticTest.java b/src/test/java/org/bukkit/StatisticTest.java new file mode 100644 index 00000000..1c758fc3 --- /dev/null +++ b/src/test/java/org/bukkit/StatisticTest.java @@ -0,0 +1,22 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class StatisticTest { + @Test + public void getDeprecated() { + for (Statistic statistic : Statistic.values()) { + assertThat(Statistic.getStatistic(statistic.getId()), is(statistic)); + } + } + + @Test + public void getById() { + for (Statistic statistic : Statistic.values()) { + assertThat(Statistic.getById(statistic.getId()), is(statistic)); + } + } +} diff --git a/src/test/java/org/bukkit/TreeSpeciesTest.java b/src/test/java/org/bukkit/TreeSpeciesTest.java new file mode 100644 index 00000000..4a997306 --- /dev/null +++ b/src/test/java/org/bukkit/TreeSpeciesTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class TreeSpeciesTest { + @Test + public void getByData() { + for (TreeSpecies treeSpecies : TreeSpecies.values()) { + assertThat(TreeSpecies.getByData(treeSpecies.getData()), is(treeSpecies)); + } + } +} diff --git a/src/test/java/org/bukkit/WorldTypeTest.java b/src/test/java/org/bukkit/WorldTypeTest.java new file mode 100644 index 00000000..d31c2052 --- /dev/null +++ b/src/test/java/org/bukkit/WorldTypeTest.java @@ -0,0 +1,15 @@ +package org.bukkit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class WorldTypeTest { + @Test + public void getByName() { + for (WorldType worldType : WorldType.values()) { + assertThat(WorldType.getByName(worldType.getName()), is(worldType)); + } + } +} -- cgit v1.2.3