From d4f8cbf9dfc461c1add01e17ae74e3d2d80dd233 Mon Sep 17 00:00:00 2001 From: austin Date: Tue, 3 Sep 2024 23:12:46 -0400 Subject: [PATCH] attachutils npe bugfix --- docs/src/progress/renderertodo.md | 3 +- .../entity/EntityCreationUtils.java | 8 +++ .../entity/types/attach/AttachUtils.java | 8 ++- .../types/attach/AttachUtilsUnitTests.java | 49 +++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/test/java/electrosphere/entity/types/attach/AttachUtilsUnitTests.java diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index a3aff0d9..0dd6c4cc 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -678,7 +678,8 @@ Include jenkins dockerfile in repo Better model for gameobjects Server synchronization of sprint tree Fix potential bad path for item state lookup -Fix ItemUtils NPE bug + unit test +Fix ItemUtils NPE bug +Fix AttachUtils NPE bug # TODO diff --git a/src/main/java/electrosphere/entity/EntityCreationUtils.java b/src/main/java/electrosphere/entity/EntityCreationUtils.java index a8297ee8..c4724869 100644 --- a/src/main/java/electrosphere/entity/EntityCreationUtils.java +++ b/src/main/java/electrosphere/entity/EntityCreationUtils.java @@ -125,4 +125,12 @@ public class EntityCreationUtils { Globals.clientScene.registerEntityToTag(entity, EntityTags.DRAWABLE); } + /** + * Creates an entity for testing + * @return The entity + */ + public static Entity TEST_createEntity(){ + return new Entity(); + } + } diff --git a/src/main/java/electrosphere/entity/types/attach/AttachUtils.java b/src/main/java/electrosphere/entity/types/attach/AttachUtils.java index a50beb38..ce2766d8 100644 --- a/src/main/java/electrosphere/entity/types/attach/AttachUtils.java +++ b/src/main/java/electrosphere/entity/types/attach/AttachUtils.java @@ -633,7 +633,13 @@ public class AttachUtils { * @return true if attached, false otherwise */ public static boolean isAttached(Entity e){ - return e.containsKey(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED); + if(e == null){ + return false; + } + if(!e.containsKey(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED)){ + return false; + } + return (boolean)e.getData(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED); } /** diff --git a/src/test/java/electrosphere/entity/types/attach/AttachUtilsUnitTests.java b/src/test/java/electrosphere/entity/types/attach/AttachUtilsUnitTests.java new file mode 100644 index 00000000..27125e21 --- /dev/null +++ b/src/test/java/electrosphere/entity/types/attach/AttachUtilsUnitTests.java @@ -0,0 +1,49 @@ +package electrosphere.entity.types.attach; + +import org.junit.jupiter.api.Assertions; + +import annotations.FastTest; +import annotations.UnitTest; +import electrosphere.entity.Entity; +import electrosphere.entity.EntityCreationUtils; +import electrosphere.entity.EntityDataStrings; + +/** + * Unit tests for attach utils + */ +public class AttachUtilsUnitTests { + + @UnitTest + @FastTest + public void isAttached_NullEntity_False(){ + boolean result = AttachUtils.isAttached(null); + Assertions.assertEquals(false, result); + } + + @UnitTest + @FastTest + public void isAttached_FalseValue_False(){ + Entity testEnt = EntityCreationUtils.TEST_createEntity(); + testEnt.putData(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED, false); + boolean result = AttachUtils.isAttached(testEnt); + Assertions.assertEquals(false, result); + } + + @UnitTest + @FastTest + public void isAttached_NullValue_False(){ + Entity testEnt = EntityCreationUtils.TEST_createEntity(); + boolean result = AttachUtils.isAttached(testEnt); + Assertions.assertEquals(false, result); + } + + @UnitTest + @FastTest + public void isAttached_TrueValue_True(){ + Entity testEnt = EntityCreationUtils.TEST_createEntity(); + testEnt.putData(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED, true); + boolean result = AttachUtils.isAttached(testEnt); + Assertions.assertEquals(true, result); + } + +}