From 319d65a0c334f055122e6d55b8d7cefd5ee415f0 Mon Sep 17 00:00:00 2001 From: P0nk Date: Wed, 7 Sep 2022 18:58:19 +0200 Subject: [PATCH] Add tests for MobSkillFactory --- pom.xml | 12 +- .../java/server/life/MobSkillFactoryTest.java | 70 +++ src/test/resources/MobSkill-test.img.xml | 522 ++++++++++++++++++ 3 files changed, 603 insertions(+), 1 deletion(-) create mode 100644 src/test/java/server/life/MobSkillFactoryTest.java create mode 100644 src/test/resources/MobSkill-test.img.xml diff --git a/pom.xml b/pom.xml index 82536389b4..1a333333a6 100644 --- a/pom.xml +++ b/pom.xml @@ -28,11 +28,12 @@ 2.18.0 22.2.0 4.1.79.Final - 5.9.0 1.15 1.0 5.0.1 8.0.30 + 5.9.0 + 4.7.0 @@ -120,16 +121,25 @@ org.junit.jupiter junit-jupiter-api ${junit.version} + test org.junit.jupiter junit-jupiter-engine ${junit.version} + test org.junit.jupiter junit-jupiter-params ${junit.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test diff --git a/src/test/java/server/life/MobSkillFactoryTest.java b/src/test/java/server/life/MobSkillFactoryTest.java new file mode 100644 index 0000000000..2917ab6a5c --- /dev/null +++ b/src/test/java/server/life/MobSkillFactoryTest.java @@ -0,0 +1,70 @@ +package server.life; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.MockitoAnnotations; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; + +class MobSkillFactoryTest { + + @TempDir + private Path wzPath; + + @BeforeEach + void setWzPath() { + MockitoAnnotations.openMocks(this); + writeTestFileToTempDir(); + System.setProperty("wz-path", "%s/wz".formatted(wzPath.toString())); + } + + private void writeTestFileToTempDir() { + try { + String testFileContents = readTestFileContents(); + writeTempDirFile(testFileContents); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private String readTestFileContents() throws IOException { + return new String(getClass() + .getClassLoader() + .getResourceAsStream("MobSkill-test.img.xml") + .readAllBytes() + ); + } + + private void writeTempDirFile(String fileContents) throws IOException { + Path tempDirDirectory = wzPath.resolve("wz/Skill.wz"); + Files.createDirectories(tempDirDirectory); + Path tempDirFile = Files.createFile(tempDirDirectory.resolve("MobSkill.img.xml")); + Files.writeString(tempDirFile, fileContents); + } + + @Test + void shouldLoadExistingMobSkill() { + MobSkill mobSkill = MobSkillFactory.getMobSkill(MobSkillType.ATTACK_UP, 1); + + assertNotNull(mobSkill); + assertAll("MobSkill", + () -> assertEquals(115, mobSkill.getX()), + () -> assertEquals(5, mobSkill.getMpCon()), + () -> assertEquals(40_000, mobSkill.getCoolTime()), + () -> assertEquals(30_000, mobSkill.getDuration()) + ); + } + + @Test + void shouldReturnNullForNonExistingMobSkill() { + MobSkill mobSkill = MobSkillFactory.getMobSkill(MobSkillType.DEFENSE_UP, 1); + + assertNull(mobSkill); + } + +} \ No newline at end of file diff --git a/src/test/resources/MobSkill-test.img.xml b/src/test/resources/MobSkill-test.img.xml new file mode 100644 index 0000000000..3a2658d0be --- /dev/null +++ b/src/test/resources/MobSkill-test.img.xml @@ -0,0 +1,522 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +