From 85f8d4bfa8d569533a5650a56f29505cd5c68fa5 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Wed, 25 May 2016 11:01:07 -0400 Subject: [PATCH] Simplify the energy value test suite and remove more deprecated classes --- .../com/pahimar/ee3/EquivalentExchange3.java | 11 +- .../exchange/EnergyValueMappingsTester.java | 2 +- .../ee3/blacklist/BlacklistRegistry.java | 11 ++ .../pahimar/ee3/command/CommandRunTest.java | 26 ++- .../ee3/exchange/EnergyValueRegistry.java | 5 + .../ee3/exchange/EnergyValueStackMapping.java | 138 -------------- .../pahimar/ee3/exchange/JsonFluidStack.java | 107 ----------- .../pahimar/ee3/exchange/JsonItemStack.java | 109 ----------- .../pahimar/ee3/exchange/WrappedStack.java | 173 +----------------- .../java/com/pahimar/ee3/reference/Tests.java | 9 +- .../test/EnergyValueMappingsTestSuite.java | 130 ------------- .../ee3/test/EnergyValueTestSuite.java | 143 +++++++++++++++ ...gyValueTest.java => VanillaTestSuite.java} | 49 +++-- .../pahimar/ee3/util/SerializationHelper.java | 130 ++++--------- 14 files changed, 241 insertions(+), 802 deletions(-) delete mode 100644 src/main/java/com/pahimar/ee3/exchange/EnergyValueStackMapping.java delete mode 100644 src/main/java/com/pahimar/ee3/exchange/JsonFluidStack.java delete mode 100644 src/main/java/com/pahimar/ee3/exchange/JsonItemStack.java delete mode 100644 src/main/java/com/pahimar/ee3/test/EnergyValueMappingsTestSuite.java create mode 100644 src/main/java/com/pahimar/ee3/test/EnergyValueTestSuite.java rename src/main/java/com/pahimar/ee3/test/{VanillaEnergyValueTest.java => VanillaTestSuite.java} (97%) diff --git a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java index 927595eb..6c6c919c 100644 --- a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java +++ b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java @@ -14,7 +14,7 @@ import com.pahimar.ee3.recipe.RecipeRegistry; import com.pahimar.ee3.reference.Files; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Reference; -import com.pahimar.ee3.test.EnergyValueMappingsTestSuite; +import com.pahimar.ee3.test.EnergyValueTestSuite; import com.pahimar.ee3.util.FluidHelper; import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.SerializationHelper; @@ -167,13 +167,12 @@ public class EquivalentExchange3 return TileEntityDataHelper.getInstance(); } - public void runEnergyValueMappingTest(File file) + public void runEnergyValueTestSuite(File file) { - runEnergyValueMappingTest(file, false); + runEnergyValueTestSuite(file, false); } - public void runEnergyValueMappingTest(File file, boolean strict) - { - new EnergyValueMappingsTestSuite(file).runTestSuite(strict); + public void runEnergyValueTestSuite(File file, boolean strict) { + new EnergyValueTestSuite(file).run(strict); } } diff --git a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueMappingsTester.java b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueMappingsTester.java index cc22a041..e6ce772b 100644 --- a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueMappingsTester.java +++ b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueMappingsTester.java @@ -18,7 +18,7 @@ public class EnergyValueMappingsTester if (ee3Mod != null) { - EE3Wrapper.ee3mod.runEnergyValueMappingTest(file, strict); + EE3Wrapper.ee3mod.runEnergyValueTestSuite(file, strict); } } diff --git a/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java b/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java index 49d6f6b2..51d6317b 100644 --- a/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java +++ b/src/main/java/com/pahimar/ee3/blacklist/BlacklistRegistry.java @@ -161,6 +161,11 @@ public class BlacklistRegistry { } } + /** + * TODO Finish JavaDoc + * + * @param shouldSave + */ public void setShouldSave(boolean shouldSave) { this.shouldSave = shouldSave; } @@ -185,6 +190,12 @@ public class BlacklistRegistry { } } + /** + * TODO Finish JavaDoc + * + * @param blacklistSet + * @param blacklist + */ public void load(Set blacklistSet, Blacklist blacklist) { if (blacklist != null && blacklistSet != null) { diff --git a/src/main/java/com/pahimar/ee3/command/CommandRunTest.java b/src/main/java/com/pahimar/ee3/command/CommandRunTest.java index 7029e3b6..bd672c58 100644 --- a/src/main/java/com/pahimar/ee3/command/CommandRunTest.java +++ b/src/main/java/com/pahimar/ee3/command/CommandRunTest.java @@ -3,7 +3,7 @@ package com.pahimar.ee3.command; import com.pahimar.ee3.reference.Messages; import com.pahimar.ee3.reference.Names; import com.pahimar.ee3.reference.Reference; -import com.pahimar.ee3.test.EnergyValueMappingsTestSuite; +import com.pahimar.ee3.test.EnergyValueTestSuite; import com.pahimar.ee3.util.LogHelper; import cpw.mods.fml.common.FMLCommonHandler; import net.minecraft.command.ICommandSender; @@ -37,30 +37,26 @@ public class CommandRunTest extends CommandEE @Override public void processCommand(ICommandSender commandSender, String[] args) { - if (args.length == 2) - { + if (args.length == 2) { + File testCaseDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues" + File.separator + "testcases"); testCaseDirectory.mkdirs(); boolean testFound = false; - for (File testCaseFile : testCaseDirectory.listFiles()) - { - if (testCaseFile.isFile() && testCaseFile.getName().equalsIgnoreCase(args[1])) - { + for (File testCaseFile : testCaseDirectory.listFiles()) { + if (testCaseFile.isFile() && testCaseFile.getName().equalsIgnoreCase(args[1])) { testFound = true; - EnergyValueMappingsTestSuite energyValueMappingsTestSuite = new EnergyValueMappingsTestSuite(testCaseFile); - LogHelper.info(EnergyValueMappingsTestSuite.TEST_MARKER, "BEGIN TEST ({})", testCaseFile.getName()); - energyValueMappingsTestSuite.runTestSuite(); - LogHelper.info(EnergyValueMappingsTestSuite.TEST_MARKER, "END TEST ({})", testCaseFile.getName()); + EnergyValueTestSuite energyValueTestSuite = new EnergyValueTestSuite(testCaseFile); + LogHelper.info(EnergyValueTestSuite.TEST_MARKER, "BEGIN TEST ({})", testCaseFile.getName()); + energyValueTestSuite.run(); + LogHelper.info(EnergyValueTestSuite.TEST_MARKER, "END TEST ({})", testCaseFile.getName()); } } - if (testFound) - { + if (testFound) { commandSender.addChatMessage(new ChatComponentTranslation(Messages.Commands.RUN_TESTS_SUCCESS, args[1])); } - else - { + else { commandSender.addChatMessage(new ChatComponentTranslation(Messages.Commands.RUN_TESTS_NOT_FOUND, args[1])); } } diff --git a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java index fcfc5044..3993c161 100644 --- a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java @@ -526,6 +526,11 @@ public class EnergyValueRegistry { } } + /** + * TODO Finish JavaDoc + * + * @param shouldSave + */ public void setShouldSave(boolean shouldSave) { this.shouldSave = shouldSave; } diff --git a/src/main/java/com/pahimar/ee3/exchange/EnergyValueStackMapping.java b/src/main/java/com/pahimar/ee3/exchange/EnergyValueStackMapping.java deleted file mode 100644 index b7bfed6b..00000000 --- a/src/main/java/com/pahimar/ee3/exchange/EnergyValueStackMapping.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.pahimar.ee3.exchange; - -import com.google.gson.*; -import com.pahimar.ee3.api.exchange.EnergyValue; - -import java.lang.reflect.Type; - -@Deprecated -public class EnergyValueStackMapping implements JsonSerializer, JsonDeserializer -{ - public static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create(); - - public final WrappedStack wrappedStack; - public final EnergyValue energyValue; - - public EnergyValueStackMapping() - { - wrappedStack = null; - energyValue = null; - } - - public EnergyValueStackMapping(WrappedStack wrappedStack, EnergyValue energyValue) - { - this.wrappedStack = wrappedStack; - this.energyValue = energyValue; - } - - public static EnergyValueStackMapping createFromJson(String jsonStackValueMapping) - { - try - { - return jsonSerializer.fromJson(jsonStackValueMapping, EnergyValueStackMapping.class); - } - catch (JsonSyntaxException exception) - { - exception.printStackTrace(); - } - catch (JsonParseException exception) - { - exception.printStackTrace(); - } - - return null; - } - - public String toJson() - { - return jsonSerializer.toJson(this); - } - - /** - * Gson invokes this call-back method during deserialization when it encounters a field of the - * specified type. - *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects - * for any non-trivial field of the returned object. However, you should never invoke it on the - * the same type passing {@code jsonElement} since that will cause an infinite loop (Gson will call your - * call-back method again). - * - * @param jsonElement The Json data being deserialized - * @param typeOfT The type of the Object to deserialize to - * @param context - * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} - * @throws com.google.gson.JsonParseException if jsonElement is not in the expected format of {@code typeofT} - */ - @Override - public EnergyValueStackMapping deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - if (!jsonElement.isJsonPrimitive()) - { - JsonObject jsonStackValueMapping = (JsonObject) jsonElement; - - WrappedStack wrappedStack = null; - EnergyValue energyValue = null; - - if (jsonStackValueMapping.get("wrappedStack") != null) - { - try - { - wrappedStack = new WrappedStack().deserialize(jsonStackValueMapping.get("wrappedStack").getAsJsonObject(), typeOfT, context); - } - catch (JsonParseException e) - { - - } - } - - if (jsonStackValueMapping.get("energyValue") != null) - { - try - { - energyValue = new EnergyValue().deserialize(jsonStackValueMapping.get("energyValue").getAsJsonObject(), typeOfT, context); - } - catch (JsonParseException e) - { - - } - } - - if (wrappedStack != null && energyValue != null) - { - return new EnergyValueStackMapping(wrappedStack, energyValue); - } - else - { - return null; - } - } - - return null; - } - - /** - * Gson invokes this call-back method during serialization when it encounters a field of the - * specified type. - *

- *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to create JsonElements for any - * non-trivial field of the {@code src} object. However, you should never invoke it on the - * {@code src} object itself since that will cause an infinite loop (Gson will call your - * call-back method again).

- * - * @param energyValueStackMapping the object that needs to be converted to Json. - * @param typeOfSrc the actual type (fully genericized version) of the source object. - * @param context - * @return a JsonElement corresponding to the specified object. - */ - @Override - public JsonElement serialize(EnergyValueStackMapping energyValueStackMapping, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonStackValueMapping = new JsonObject(); - - jsonStackValueMapping.add("wrappedStack", jsonSerializer.toJsonTree(energyValueStackMapping.wrappedStack)); - jsonStackValueMapping.add("energyValue", jsonSerializer.toJsonTree(energyValueStackMapping.energyValue)); - - return jsonStackValueMapping; - } -} diff --git a/src/main/java/com/pahimar/ee3/exchange/JsonFluidStack.java b/src/main/java/com/pahimar/ee3/exchange/JsonFluidStack.java deleted file mode 100644 index 2878dc6a..00000000 --- a/src/main/java/com/pahimar/ee3/exchange/JsonFluidStack.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.pahimar.ee3.exchange; - -import com.google.gson.*; -import net.minecraft.nbt.JsonToNBT; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTException; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidRegistry; -import net.minecraftforge.fluids.FluidStack; - -import java.lang.reflect.Type; - -@Deprecated -public class JsonFluidStack implements JsonSerializer, JsonDeserializer -{ - public static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(JsonFluidStack.class, new JsonFluidStack()).create(); - - public Fluid fluid; - public int amount; - public NBTTagCompound tag; - - public JsonFluidStack() - { - this.fluid = null; - this.amount = 0; - this.tag = null; - } - - public JsonFluidStack(FluidStack fluidStack) - { - this.fluid = fluidStack.getFluid(); - this.amount = fluidStack.amount; - this.tag = fluidStack.tag; - } - - @Override - public JsonFluidStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - if (json.isJsonObject()) - { - JsonObject jsonObject = (JsonObject) json; - JsonFluidStack jsonFluidStack = new JsonFluidStack(); - - if (jsonObject.has("fluidName")) - { - jsonFluidStack.fluid = FluidRegistry.getFluid(jsonObject.get("fluidName").getAsString()); - } - else - { - throw new JsonParseException(""); // TODO Exception message - } - - if (jsonObject.has("amount")) - { - jsonFluidStack.amount = jsonObject.get("amount").getAsInt(); - } - else - { - throw new JsonParseException(""); // TODO Exception message - } - - if (jsonObject.has("tag")) - { - try - { - NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("tag").getAsString()); - - if (nbtBase instanceof NBTTagCompound) - { - jsonFluidStack.tag = (NBTTagCompound) nbtBase; - } - } - catch (NBTException e) - { - throw new JsonParseException(e.getMessage(), e.getCause()); - } - } - - return jsonFluidStack; - } - - return null; - } - - @Override - public JsonElement serialize(JsonFluidStack src, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonObject = new JsonObject(); - - jsonObject.addProperty("fluidName", src.fluid.getName()); - jsonObject.addProperty("amount", src.amount); - - if (src.tag != null) - { - jsonObject.addProperty("tag", src.tag.toString()); - } - - return jsonObject; - } - - @Override - public String toString() - { - return String.format("fluid: %s, amount: %s, tag: %s", fluid, amount, tag); - } -} diff --git a/src/main/java/com/pahimar/ee3/exchange/JsonItemStack.java b/src/main/java/com/pahimar/ee3/exchange/JsonItemStack.java deleted file mode 100644 index 7c60ba02..00000000 --- a/src/main/java/com/pahimar/ee3/exchange/JsonItemStack.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.pahimar.ee3.exchange; - -import com.google.gson.*; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.JsonToNBT; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTException; -import net.minecraft.nbt.NBTTagCompound; - -import java.lang.reflect.Type; - -@Deprecated -public class JsonItemStack implements JsonSerializer, JsonDeserializer -{ - public static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(JsonItemStack.class, new JsonItemStack()).create(); - - public String itemName; - public int itemDamage; - public NBTTagCompound itemNBTTagCompound; - - public JsonItemStack() - { - this.itemName = null; - this.itemDamage = 0; - this.itemNBTTagCompound = null; - } - - public JsonItemStack(ItemStack itemStack) - { - this.itemName = Item.itemRegistry.getNameForObject(itemStack.getItem()); - this.itemDamage = itemStack.getItemDamage(); - if (itemStack.stackTagCompound != null) - { - this.itemNBTTagCompound = itemStack.getTagCompound(); - } - } - - @Override - public JsonItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - if (json.isJsonObject()) - { - JsonObject jsonObject = (JsonObject) json; - JsonItemStack jsonItemStack = new JsonItemStack(); - - if (jsonObject.has("itemName")) - { - jsonItemStack.itemName = jsonObject.get("itemName").getAsString(); - } - else - { - throw new JsonParseException(""); // TODO Exception message - } - - if (jsonObject.has("itemDamage")) - { - jsonItemStack.itemDamage = jsonObject.get("itemDamage").getAsInt(); - } - else - { - throw new JsonParseException(""); // TODO Exception message - } - - if (jsonObject.has("itemNBTTagCompound")) - { - try - { - NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("itemNBTTagCompound").getAsString()); - - if (nbtBase instanceof NBTTagCompound) - { - jsonItemStack.itemNBTTagCompound = (NBTTagCompound) nbtBase; - } - } - catch (NBTException e) - { - throw new JsonParseException(e.getMessage(), e.getCause()); - } - } - - return jsonItemStack; - } - - return null; - } - - @Override - public JsonElement serialize(JsonItemStack src, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonObject = new JsonObject(); - - jsonObject.addProperty("itemName", src.itemName); - jsonObject.addProperty("itemDamage", src.itemDamage); - - if (src.itemNBTTagCompound != null) - { - jsonObject.addProperty("itemNBTTagCompound", src.itemNBTTagCompound.toString()); - } - - return jsonObject; - } - - @Override - public String toString() - { - return String.format("itemName: %s, itemDamage: %s, itemNBTTagCompound: %s", itemName, itemDamage, itemNBTTagCompound); - } -} diff --git a/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java b/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java index 51cce857..00fc93a5 100644 --- a/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java +++ b/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java @@ -1,6 +1,5 @@ package com.pahimar.ee3.exchange; -import com.google.gson.*; import com.pahimar.ee3.util.FluidHelper; import com.pahimar.ee3.util.ItemHelper; import net.minecraft.block.Block; @@ -9,14 +8,11 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; -import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -public class WrappedStack implements Comparable, JsonDeserializer, JsonSerializer -{ - public static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(WrappedStack.class, new WrappedStack()).create(); +public class WrappedStack implements Comparable { private final String objectType; private final Object wrappedStack; @@ -274,173 +270,6 @@ public class WrappedStack implements Comparable, JsonDeserializer< return null; } - public static WrappedStack createFromJson(String jsonWrappedObject) throws JsonParseException - { - try - { - return jsonSerializer.fromJson(jsonWrappedObject, WrappedStack.class); - } - catch (JsonSyntaxException exception) - { - exception.printStackTrace(); - } - catch (JsonParseException exception) - { - exception.printStackTrace(); - } - - return null; - } - - @SuppressWarnings("unused") - public String toJson() - { - return jsonSerializer.toJson(this); - } - - /** - * Gson invokes this call-back method during deserialization when it encounters a field of the - * specified type. - *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects - * for any non-trivial field of the returned object. However, you should never invoke it on the - * the same type passing {@code jsonElement} since that will cause an infinite loop (Gson will call your - * call-back method again). - * - * @param jsonElement The Json data being deserialized - * @param typeOfT The type of the Object to deserialize to - * @param context - * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} - * @throws com.google.gson.JsonParseException if jsonElement is not in the expected format of {@code typeofT} - */ - @Override - public WrappedStack deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - if (!jsonElement.isJsonPrimitive()) - { - JsonObject jsonWrappedStack = (JsonObject) jsonElement; - - int stackSize = 1; - String objectType = null; - Object stackObject = null; - - if (jsonWrappedStack.get("type") != null) - { - objectType = jsonWrappedStack.get("type").getAsString(); - } - - if (jsonWrappedStack.get("stackSize") != null) - { - stackSize = jsonWrappedStack.get("stackSize").getAsInt(); - } - - if (jsonWrappedStack.get("objectData") != null && !jsonWrappedStack.get("objectData").isJsonPrimitive()) - { - if (objectType != null) - { - if (objectType.equalsIgnoreCase("ItemStack")) - { - JsonItemStack jsonItemStack = JsonItemStack.jsonSerializer.fromJson(jsonWrappedStack.get("objectData"), JsonItemStack.class); - ItemStack itemStack = null; - Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName); - if (stackSize > 0 && item != null) - { - itemStack = new ItemStack(item, stackSize, jsonItemStack.itemDamage); - if (jsonItemStack.itemNBTTagCompound != null) - { - itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound; - } - } - stackObject = itemStack; - } - else if (objectType.equalsIgnoreCase("OreStack")) - { - OreStack oreStack = jsonSerializer.fromJson(jsonWrappedStack.get("objectData"), OreStack.class); - - if (stackSize > 0) - { - oreStack.stackSize = stackSize; - } - stackObject = oreStack; - } - else if (objectType.equalsIgnoreCase("FluidStack")) - { - JsonFluidStack jsonFluidStack = JsonFluidStack.jsonSerializer.fromJson(jsonWrappedStack.get("objectData"), JsonFluidStack.class); - FluidStack fluidStack = new FluidStack(jsonFluidStack.fluid, jsonFluidStack.amount, jsonFluidStack.tag); - - if (stackSize > 0) - { - fluidStack.amount = stackSize; - } - stackObject = fluidStack; - } - } - } - - if (stackObject != null) - { - return new WrappedStack(stackObject); - } - else - { - throw new JsonParseException(String.format("Unable to parse a wrappable stack object from the provided json: %s", jsonElement.toString())); - } - } - else - { - throw new JsonParseException(String.format("Unable to parse a wrappable stack object from the provided json: %s", jsonElement.toString())); - } - } - - /** - * Gson invokes this call-back method during serialization when it encounters a field of the - * specified type. - *

- *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to create JsonElements for any - * non-trivial field of the {@code wrappedStack} object. However, you should never invoke it on the - * {@code wrappedStack} object itself since that will cause an infinite loop (Gson will call your - * call-back method again).

- * - * @param wrappedStack the object that needs to be converted to Json. - * @param typeOfSrc the actual type (fully genericized version) of the source object. - * @param context - * @return a JsonElement corresponding to the specified object. - */ - @Override - public JsonElement serialize(WrappedStack wrappedStack, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonWrappedStack = new JsonObject(); - - Gson gson = new Gson(); - - jsonWrappedStack.addProperty("type", wrappedStack.objectType); - jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize); - - if (wrappedStack.wrappedStack instanceof ItemStack) - { - JsonItemStack jsonItemStack = new JsonItemStack(); - jsonItemStack.itemName = Item.itemRegistry.getNameForObject(((ItemStack) wrappedStack.wrappedStack).getItem()); - jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage(); - if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null) - { - jsonItemStack.itemNBTTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound; - } - jsonWrappedStack.add("objectData", JsonItemStack.jsonSerializer.toJsonTree(jsonItemStack, JsonItemStack.class)); - } - else if (wrappedStack.wrappedStack instanceof OreStack) - { - jsonWrappedStack.add("objectData", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class)); - } - else if (wrappedStack.wrappedStack instanceof FluidStack) - { - JsonFluidStack jsonFluidStack = new JsonFluidStack((FluidStack) wrappedStack.wrappedStack); - jsonWrappedStack.add("objectData", JsonFluidStack.jsonSerializer.toJsonTree(jsonFluidStack, JsonFluidStack.class)); - } - - return jsonWrappedStack; - } - @Override public boolean equals(Object object) { diff --git a/src/main/java/com/pahimar/ee3/reference/Tests.java b/src/main/java/com/pahimar/ee3/reference/Tests.java index 8d30f603..e5b49c44 100644 --- a/src/main/java/com/pahimar/ee3/reference/Tests.java +++ b/src/main/java/com/pahimar/ee3/reference/Tests.java @@ -1,9 +1,8 @@ package com.pahimar.ee3.reference; -import com.pahimar.ee3.test.EnergyValueMappingsTestSuite; -import com.pahimar.ee3.test.VanillaEnergyValueTest; +import com.pahimar.ee3.test.EnergyValueTestSuite; +import com.pahimar.ee3.test.VanillaTestSuite; -public class Tests -{ - public static final EnergyValueMappingsTestSuite vanillaEnergyValueTest = new VanillaEnergyValueTest(); +public class Tests { + public static final EnergyValueTestSuite vanillaTestSuite = new VanillaTestSuite(); } diff --git a/src/main/java/com/pahimar/ee3/test/EnergyValueMappingsTestSuite.java b/src/main/java/com/pahimar/ee3/test/EnergyValueMappingsTestSuite.java deleted file mode 100644 index d8b2fc06..00000000 --- a/src/main/java/com/pahimar/ee3/test/EnergyValueMappingsTestSuite.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.pahimar.ee3.test; - -import com.pahimar.ee3.api.exchange.EnergyValue; -import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy; -import com.pahimar.ee3.exchange.WrappedStack; -import com.pahimar.ee3.util.LogHelper; -import com.pahimar.ee3.util.SerializationHelper; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -public class EnergyValueMappingsTestSuite { - - public static final Marker TEST_MARKER = MarkerManager.getMarker("EE3_TEST", LogHelper.MOD_MARKER); - private static final Marker SUCCESS_MARKER = MarkerManager.getMarker("EE3_TEST_SUCCESS", TEST_MARKER); - private static final Marker FAILURE_MARKER = MarkerManager.getMarker("EE3_TEST_FAILURE", TEST_MARKER); - - Map testSuiteValueMap; - - public EnergyValueMappingsTestSuite() - { - testSuiteValueMap = new TreeMap<>(); - } - - public EnergyValueMappingsTestSuite(File jsonFile) { - - if (jsonFile != null && jsonFile.exists() && jsonFile.isFile()) { - testSuiteValueMap = SerializationHelper.readEnergyValueStackMapFromJsonFile(jsonFile); - } - } - - public void add(Object object, Object value) { - - if (WrappedStack.canBeWrapped(object)) { - - if (value instanceof Number) { - Number number = (Number) value; - WrappedStack wrappedStack = WrappedStack.wrap(object); - wrappedStack.setStackSize(1); - testSuiteValueMap.put(wrappedStack, new EnergyValue(number.floatValue())); - } - else if (value == null) { - - WrappedStack wrappedStack = WrappedStack.wrap(object); - wrappedStack.setStackSize(1); - testSuiteValueMap.put(wrappedStack, null); - } - } - } - - public void remove(Object object) { - - if (WrappedStack.canBeWrapped(object)) { - WrappedStack wrappedStack = WrappedStack.wrap(object); - wrappedStack.setStackSize(1); - testSuiteValueMap.remove(wrappedStack); - } - } - - public void loadTestSuite(File jsonFile) { - - if (jsonFile != null && jsonFile.exists() && jsonFile.isFile()) { - testSuiteValueMap = SerializationHelper.readEnergyValueStackMapFromJsonFile(jsonFile); - } - } - - public void saveTestSuite(File jsonFile) { - - if (jsonFile != null) { - SerializationHelper.writeEnergyValueStackMapToJsonFile(jsonFile, testSuiteValueMap); - } - } - - public void runTestSuite() { - runTestSuite(false); - } - - public void runTestSuite(boolean strict) { - - List successMessages = new ArrayList<>(); - List failureMessages = new ArrayList<>(); - - for (WrappedStack wrappedStack : testSuiteValueMap.keySet()) { - - EnergyValue registryEnergyValue = EnergyValueRegistryProxy.getEnergyValue(wrappedStack, strict); - EnergyValue testSuiteEnergyValue = testSuiteValueMap.get(wrappedStack); - - if (registryEnergyValue == null && testSuiteEnergyValue == null) { - /** - * Success - anticipated that no value was found and no value was found - */ - successMessages.add(String.format("SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)]", wrappedStack, testSuiteEnergyValue, registryEnergyValue)); - } - else if (registryEnergyValue == null) { - /** - * Failure - anticipated that a value would be found but no value was found - */ - failureMessages.add(String.format("FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)]", wrappedStack, testSuiteEnergyValue, registryEnergyValue)); - } - else if (registryEnergyValue != null && testSuiteEnergyValue != null) { - - if (registryEnergyValue.equals(testSuiteEnergyValue)) { - /** - * Success - anticipated that a specific value would be found and the anticipated value was found - */ - successMessages.add(String.format("SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)]", wrappedStack, testSuiteEnergyValue, registryEnergyValue)); - } - else { - /** - * Failure - anticipated that a specific value would be found and while a value was found it was not the anticipated one - */ - failureMessages.add(String.format("FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)]", wrappedStack, testSuiteEnergyValue, registryEnergyValue)); - } - } - } - - for (String successMessage : successMessages) { - LogHelper.info(SUCCESS_MARKER, successMessage); - } - - for (String failureMessage : failureMessages) { - LogHelper.warn(FAILURE_MARKER, failureMessage); - } - } -} diff --git a/src/main/java/com/pahimar/ee3/test/EnergyValueTestSuite.java b/src/main/java/com/pahimar/ee3/test/EnergyValueTestSuite.java new file mode 100644 index 00000000..7e38705c --- /dev/null +++ b/src/main/java/com/pahimar/ee3/test/EnergyValueTestSuite.java @@ -0,0 +1,143 @@ +package com.pahimar.ee3.test; + +import com.pahimar.ee3.api.exchange.EnergyValue; +import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy; +import com.pahimar.ee3.exchange.WrappedStack; +import com.pahimar.ee3.util.LogHelper; +import com.pahimar.ee3.util.SerializationHelper; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.*; + +public class EnergyValueTestSuite { + + public static final Marker TEST_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE_TEST", LogHelper.MOD_MARKER); + private static final Marker SUCCESS_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE_TEST_SUCCESS", TEST_MARKER); + private static final Marker FAILURE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE_TEST_FAILURE", TEST_MARKER); + + private Map testSuiteValueMap; + private transient Set stacksWithCorrectValue; + private transient Set failureStacksWithWrongValue; + private transient Set failureStacksWithNoValue; + + public EnergyValueTestSuite() { + testSuiteValueMap = new TreeMap<>(); + stacksWithCorrectValue = new TreeSet<>(); + failureStacksWithWrongValue = new TreeSet<>(); + failureStacksWithNoValue = new TreeSet<>(); + } + + public EnergyValueTestSuite(File file) { + load(file); + } + + public EnergyValueTestSuite add(Object object, Object value) { + + if (WrappedStack.canBeWrapped(object)) { + + WrappedStack wrappedStack = WrappedStack.wrap(object, 1); + if (value instanceof Number) { + testSuiteValueMap.put(wrappedStack, new EnergyValue((Number) value)); + } + else if (value instanceof EnergyValue) { + testSuiteValueMap.put(wrappedStack, (EnergyValue) value); + } + else if (value == null) { + testSuiteValueMap.put(wrappedStack, null); + } + } + + return this; + } + + public EnergyValueTestSuite remove(Object object) { + + if (WrappedStack.canBeWrapped(object)) { + testSuiteValueMap.remove(WrappedStack.wrap(object, 1)); + } + + return this; + } + + public void run() { + run(false); + } + + public void run(boolean strict) { + + stacksWithCorrectValue.clear(); + failureStacksWithWrongValue.clear(); + failureStacksWithNoValue.clear(); + + List successMessages = new ArrayList<>(); + List failureMessagesWrongValue = new ArrayList<>(); + List failureMessagesNoValue = new ArrayList<>(); + + for (WrappedStack wrappedStack : testSuiteValueMap.keySet()) { + + EnergyValue expectedValue = testSuiteValueMap.get(wrappedStack); + EnergyValue actualValue = EnergyValueRegistryProxy.getEnergyValue(wrappedStack, strict); + + if (actualValue == null && expectedValue == null) { + // Success - anticipated that no value was found and no value was found + successMessages.add(String.format("SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)]", wrappedStack, expectedValue, actualValue)); + stacksWithCorrectValue.add(wrappedStack); + } + else if (actualValue == null) { + // Failure - anticipated that a value would be found but no value was found + failureMessagesNoValue.add(String.format("FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)]", wrappedStack, expectedValue, actualValue)); + failureStacksWithNoValue.add(wrappedStack); + } + else if (actualValue != null && expectedValue != null) { + + if (actualValue.equals(expectedValue)) { + // Success - anticipated that a specific value would be found and the anticipated value was found + successMessages.add(String.format("SUCCESS: Object '%s' had the expected energy value [Expected (%s), Found (%s)]", wrappedStack, expectedValue, actualValue)); + stacksWithCorrectValue.add(wrappedStack); + } + else { + // Failure - anticipated that a specific value would be found and while a value was found it was not the anticipated one + failureMessagesWrongValue.add(String.format("FAILURE: Object '%s' did not have the expected energy value [Expected (%s), Found (%s)]", wrappedStack, expectedValue, actualValue)); + failureStacksWithWrongValue.add(wrappedStack); + } + } + } + + for (String successMessage : successMessages) { + LogHelper.info(SUCCESS_MARKER, successMessage); + } + + for (String failureMessage : failureMessagesWrongValue) { + LogHelper.warn(FAILURE_MARKER, failureMessage); + } + + for (String failureMessage : failureMessagesNoValue) { + LogHelper.warn(FAILURE_MARKER, failureMessage); + } + } + + public EnergyValueTestSuite load(File file) { + + try { + testSuiteValueMap = SerializationHelper.readMapFromFile(file); + } + catch (FileNotFoundException e) { + LogHelper.warn(TEST_MARKER, "Could not load test file from disk: " + e.getMessage()); + testSuiteValueMap = new TreeMap<>(); + } + + stacksWithCorrectValue = new TreeSet<>(); + failureStacksWithWrongValue = new TreeSet<>(); + failureStacksWithNoValue = new TreeSet<>(); + + return this; + } + + public EnergyValueTestSuite save(File file) { + SerializationHelper.writeMapToFile(testSuiteValueMap, file); + return this; + } +} diff --git a/src/main/java/com/pahimar/ee3/test/VanillaEnergyValueTest.java b/src/main/java/com/pahimar/ee3/test/VanillaTestSuite.java similarity index 97% rename from src/main/java/com/pahimar/ee3/test/VanillaEnergyValueTest.java rename to src/main/java/com/pahimar/ee3/test/VanillaTestSuite.java index 8e437cc7..c39fe527 100644 --- a/src/main/java/com/pahimar/ee3/test/VanillaEnergyValueTest.java +++ b/src/main/java/com/pahimar/ee3/test/VanillaTestSuite.java @@ -9,9 +9,9 @@ import net.minecraftforge.oredict.OreDictionary; import java.io.File; -public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite +public class VanillaTestSuite extends EnergyValueTestSuite { - public VanillaEnergyValueTest() + public VanillaTestSuite() { buildTestSuite(); } @@ -30,8 +30,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite addMaterialsTabTestCases(); } - private void addBuildingBlocksTabTestCases() - { + private void addBuildingBlocksTabTestCases() { + add(Blocks.stone, 1); add(Blocks.grass, 1); add(Blocks.dirt, 1); @@ -174,8 +174,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Blocks.packed_ice, null); } - private void addDecorationBlocksTabTestCases() - { + private void addDecorationBlocksTabTestCases() { + add(new ItemStack(Blocks.sapling, 1, 0), 32); add(new ItemStack(Blocks.sapling, 1, 1), 32); add(new ItemStack(Blocks.sapling, 1, 2), 32); @@ -280,8 +280,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(new ItemStack(Blocks.skull, 1, 4), null); } - private void addRedstoneTabTestCases() - { + private void addRedstoneTabTestCases() { + add(Blocks.dispenser, 87); add(Blocks.noteblock, 96); add(Blocks.sticky_piston, 340); @@ -310,8 +310,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.comparator, 367); } - private void addTransportationTabTestCases() - { + private void addTransportationTabTestCases() { + add(Blocks.golden_rail, 2054); add(Blocks.detector_rail, 261.667); add(Blocks.rail, 96.25); @@ -326,8 +326,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.hopper_minecart, 2624); } - private void addMiscellaneousTabTestCases() - { + private void addMiscellaneousTabTestCases() { + add(Blocks.beacon, 24773); add(Items.bucket, 768); add(Items.water_bucket, 769); @@ -387,8 +387,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.record_wait, 2048); } - private void addFoodstuffsTabTestCases() - { + private void addFoodstuffsTabTestCases() { + add(Items.apple, 24); add(Items.mushroom_stew, 70); add(Items.bread, 72); @@ -419,8 +419,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.pumpkin_pie, 208); } - private void addToolsTabTestCases() - { + private void addToolsTabTestCases() { + add(Items.iron_shovel, 264); add(Items.iron_pickaxe, 776); add(Items.iron_axe, 776); @@ -451,8 +451,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(new ItemStack(Items.enchanted_book, 1, OreDictionary.WILDCARD_VALUE), null); } - private void addCombatTabTestCases() - { + private void addCombatTabTestCases() { + add(Items.bow, 48); add(Items.arrow, 14); add(Items.iron_sword, 516); @@ -482,8 +482,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.golden_boots, 8192); } - private void addBrewingTabTestCases() - { + private void addBrewingTabTestCases() { + add(Items.ghast_tear, 4096); add(new ItemStack(Items.potionitem, 1, 0), 2); add(new ItemStack(Items.potionitem, 1, 16), 8.667); @@ -551,8 +551,8 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.speckled_melon, 1836.448); } - private void addMaterialsTabTestCases() - { + private void addMaterialsTabTestCases() { + add(new ItemStack(Items.coal, 1, 0), 32); add(new ItemStack(Items.coal, 1, 1), 32); add(Items.diamond, 8192); @@ -600,10 +600,9 @@ public class VanillaEnergyValueTest extends EnergyValueMappingsTestSuite add(Items.quartz, 256); } - public void save() - { + public void save() { File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues" + File.separator + "testcases"); energyValuesDataDirectory.mkdirs(); - this.saveTestSuite(new File(energyValuesDataDirectory, "minecraft-v1710-vanilla-test-suite.json")); + this.save(new File(energyValuesDataDirectory, "minecraft-v1710-vanilla-test-suite.json")); } } diff --git a/src/main/java/com/pahimar/ee3/util/SerializationHelper.java b/src/main/java/com/pahimar/ee3/util/SerializationHelper.java index b35fe6ad..89ea336d 100644 --- a/src/main/java/com/pahimar/ee3/util/SerializationHelper.java +++ b/src/main/java/com/pahimar/ee3/util/SerializationHelper.java @@ -4,10 +4,7 @@ import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; import com.pahimar.ee3.api.exchange.EnergyValue; -import com.pahimar.ee3.exchange.EnergyValueStackMapping; import com.pahimar.ee3.exchange.OreStack; import com.pahimar.ee3.exchange.WrappedStack; import com.pahimar.ee3.knowledge.PlayerKnowledge; @@ -68,63 +65,6 @@ public class SerializationHelper { instancePlayerDataDirectory.mkdirs(); } - public static Map readEnergyValueStackMapFromJsonFile(File jsonFile) - { - Map energyValueStackMap = new TreeMap(); - JsonReader jsonReader; - - try - { - jsonReader = new JsonReader(new FileReader(jsonFile)); - jsonReader.beginArray(); - while (jsonReader.hasNext()) - { - EnergyValueStackMapping energyValueStackMapping = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class); - if (energyValueStackMapping != null) - { - energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue); - } - } - jsonReader.endArray(); - jsonReader.close(); - } - catch (FileNotFoundException ignored) - { - } - catch (IOException e) - { - e.printStackTrace(); - } - - return energyValueStackMap; - } - - public static void writeEnergyValueStackMapToJsonFile(File jsonFile, Map energyValueMap) - { - JsonWriter jsonWriter; - - try - { - jsonWriter = new JsonWriter(new FileWriter(jsonFile)); - jsonWriter.setIndent(" "); - jsonWriter.beginArray(); - for (WrappedStack wrappedStack : energyValueMap.keySet()) - { - if (wrappedStack != null && wrappedStack.getWrappedObject() != null) - { - EnergyValueStackMapping.jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter); - } - } - - jsonWriter.endArray(); - jsonWriter.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } - } - public static Set readSetFromFile(File file) { Set wrappedStackSet = new TreeSet<>(); @@ -167,53 +107,55 @@ public class SerializationHelper { public static String readJsonFile(File file) throws FileNotFoundException { StringBuilder jsonStringBuilder = new StringBuilder(); - try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { + if (file != null) { + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { - jsonStringBuilder = new StringBuilder(); - String line; - while ((line = bufferedReader.readLine()) != null) { - jsonStringBuilder.append(line); + jsonStringBuilder = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + jsonStringBuilder.append(line); + } + } + catch (IOException exception) { + if (exception instanceof FileNotFoundException) { + throw (FileNotFoundException) exception; + } + else { + exception.printStackTrace(); // TODO Better logging of the exception + } } } - catch (IOException exception) { - if (exception instanceof FileNotFoundException) { - throw (FileNotFoundException) exception; - } - else { - exception.printStackTrace(); // TODO Better logging of the exception - } - } - return jsonStringBuilder.toString(); } public static void writeJsonFile(File file, String fileContents) { - file.getParentFile().mkdirs(); - File tempFile = new File(file.getAbsolutePath() + "_tmp"); + if (file != null) { - if (tempFile.exists()) { - tempFile.delete(); - } + file.getParentFile().mkdirs(); + File tempFile = new File(file.getAbsolutePath() + "_tmp"); - try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile))) { + if (tempFile.exists()) { + tempFile.delete(); + } - bufferedWriter.write(fileContents); - bufferedWriter.close(); - } - catch (IOException exception) { - exception.printStackTrace(); // TODO Better logging of the exception - } + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile))) { - if (file.exists()) { - file.delete(); - } + bufferedWriter.write(fileContents); + bufferedWriter.close(); + } catch (IOException exception) { + exception.printStackTrace(); // TODO Better logging of the exception + } - if (file.exists()) { - LogHelper.warn("Failed to delete " + file); - } - else { - tempFile.renameTo(file); + if (file.exists()) { + file.delete(); + } + + if (file.exists()) { + LogHelper.warn("Failed to delete " + file); + } else { + tempFile.renameTo(file); + } } } }