feature: resolve #1282 for Lockable Object pattern. (#1702)

* Added Lockable-Object pattern. Closes #1282.

* Refactor method name.

* Refactor sonar lint bugs.

* Added tests and enum Constants.

* Increase coverage.

* Changed @Data to Getters and Setters.

* Iluwatar's comment on pull request #1702.

* Fixed codes mells.

* Incremented wait time to 3 seconds.

* Reduced wait time to 2 seconds.

* Cleaned Code Smells.

* Incremented wait time, removed cool down.

* Refactored README.md file.

Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
Noam Greenshtain
2021-05-14 18:56:41 +03:00
committed by GitHub
parent ea3c9d955e
commit 122e6edb38
22 changed files with 1118 additions and 0 deletions

View File

@ -0,0 +1,79 @@
package com.iluwatar.lockableobject;
import com.iluwatar.lockableobject.domain.Creature;
import com.iluwatar.lockableobject.domain.CreatureStats;
import com.iluwatar.lockableobject.domain.CreatureType;
import com.iluwatar.lockableobject.domain.Elf;
import com.iluwatar.lockableobject.domain.Orc;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CreatureTest {
private Creature orc;
private Creature elf;
private Lockable sword;
@BeforeEach
void init() {
elf = new Elf("Elf test");
orc = new Orc("Orc test");
sword = new SwordOfAragorn();
}
@Test
void baseTest() {
Assertions.assertEquals("Elf test", elf.getName());
Assertions.assertEquals(CreatureType.ELF, elf.getType());
Assertions.assertThrows(NullPointerException.class, () -> new Elf(null));
Assertions.assertThrows(NullPointerException.class, () -> elf.acquire(null));
Assertions.assertThrows(NullPointerException.class, () -> elf.attack(null));
Assertions.assertThrows(IllegalArgumentException.class, () -> elf.hit(-10));
}
@Test
void hitTest() {
elf.hit(CreatureStats.ELF_HEALTH.getValue() / 2);
Assertions.assertEquals(CreatureStats.ELF_HEALTH.getValue() / 2, elf.getHealth());
elf.hit(CreatureStats.ELF_HEALTH.getValue() / 2);
Assertions.assertFalse(elf.isAlive());
Assertions.assertEquals(0, orc.getInstruments().size());
Assertions.assertTrue(orc.acquire(sword));
Assertions.assertEquals(1, orc.getInstruments().size());
orc.kill();
Assertions.assertEquals(0, orc.getInstruments().size());
}
@Test
void testFight() throws InterruptedException {
killCreature(elf, orc);
Assertions.assertTrue(elf.isAlive());
Assertions.assertFalse(orc.isAlive());
Assertions.assertTrue(elf.getHealth() > 0);
Assertions.assertTrue(orc.getHealth() <= 0);
}
@Test
void testAcqusition() throws InterruptedException {
Assertions.assertTrue(elf.acquire(sword));
Assertions.assertEquals(elf.getName(), sword.getLocker().getName());
Assertions.assertTrue(elf.getInstruments().contains(sword));
Assertions.assertFalse(orc.acquire(sword));
killCreature(orc, elf);
Assertions.assertTrue(orc.acquire(sword));
Assertions.assertEquals(orc, sword.getLocker());
}
void killCreature(Creature source, Creature target) throws InterruptedException {
while (target.isAlive()) {
source.attack(target);
}
}
@Test
void invalidDamageTest(){
Assertions.assertThrows(IllegalArgumentException.class, () -> elf.hit(-50));
}
}