* 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:
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.lockableobject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationAsRunnableWithoutException() {
|
||||
assertDoesNotThrow(() -> (new App()).run());
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.iluwatar.lockableobject;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ExceptionsTest {
|
||||
|
||||
private String msg = "test";
|
||||
|
||||
@Test
|
||||
void testException(){
|
||||
Exception e;
|
||||
try{
|
||||
throw new LockingException(msg);
|
||||
}
|
||||
catch(LockingException ex){
|
||||
e = ex;
|
||||
}
|
||||
Assertions.assertEquals(msg, e.getMessage());
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.iluwatar.lockableobject;
|
||||
|
||||
import com.iluwatar.lockableobject.domain.Creature;
|
||||
import com.iluwatar.lockableobject.domain.Elf;
|
||||
import com.iluwatar.lockableobject.domain.Feind;
|
||||
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 FeindTest {
|
||||
|
||||
private Creature elf;
|
||||
private Creature orc;
|
||||
private Lockable sword;
|
||||
|
||||
@BeforeEach
|
||||
void init(){
|
||||
elf = new Elf("Nagdil");
|
||||
orc = new Orc("Ghandar");
|
||||
sword = new SwordOfAragorn();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTests(){
|
||||
Assertions.assertThrows(NullPointerException.class, () -> new Feind(null, null));
|
||||
Assertions.assertThrows(NullPointerException.class, () -> new Feind(elf, null));
|
||||
Assertions.assertThrows(NullPointerException.class, () -> new Feind(null, sword));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBaseCase() throws InterruptedException {
|
||||
var base = new Thread(new Feind(orc, sword));
|
||||
Assertions.assertNull(sword.getLocker());
|
||||
base.start();
|
||||
base.join();
|
||||
Assertions.assertEquals(orc, sword.getLocker());
|
||||
var extend = new Thread(new Feind(elf, sword));
|
||||
extend.start();
|
||||
extend.join();
|
||||
Assertions.assertTrue(sword.isLocked());
|
||||
|
||||
sword.unlock(elf.isAlive() ? elf : orc);
|
||||
Assertions.assertNull(sword.getLocker());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.iluwatar.lockableobject;
|
||||
|
||||
import com.iluwatar.lockableobject.domain.CreatureStats;
|
||||
import com.iluwatar.lockableobject.domain.Elf;
|
||||
import com.iluwatar.lockableobject.domain.Human;
|
||||
import com.iluwatar.lockableobject.domain.Orc;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SubCreaturesTests {
|
||||
|
||||
@Test
|
||||
void statsTest(){
|
||||
var elf = new Elf("Limbar");
|
||||
var orc = new Orc("Dargal");
|
||||
var human = new Human("Jerry");
|
||||
Assertions.assertEquals(CreatureStats.ELF_HEALTH.getValue(), elf.getHealth());
|
||||
Assertions.assertEquals(CreatureStats.ELF_DAMAGE.getValue(), elf.getDamage());
|
||||
Assertions.assertEquals(CreatureStats.ORC_DAMAGE.getValue(), orc.getDamage());
|
||||
Assertions.assertEquals(CreatureStats.ORC_HEALTH.getValue(), orc.getHealth());
|
||||
Assertions.assertEquals(CreatureStats.HUMAN_DAMAGE.getValue(), human.getDamage());
|
||||
Assertions.assertEquals(CreatureStats.HUMAN_HEALTH.getValue(), human.getHealth());
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.iluwatar.lockableobject;
|
||||
|
||||
import com.iluwatar.lockableobject.domain.Human;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TheSwordOfAragornTest {
|
||||
|
||||
@Test
|
||||
void basicSwordTest() {
|
||||
var sword = new SwordOfAragorn();
|
||||
Assertions.assertNotNull(sword.getName());
|
||||
Assertions.assertNull(sword.getLocker());
|
||||
Assertions.assertFalse(sword.isLocked());
|
||||
var human = new Human("Tupac");
|
||||
Assertions.assertTrue(human.acquire(sword));
|
||||
Assertions.assertEquals(human, sword.getLocker());
|
||||
Assertions.assertTrue(sword.isLocked());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidLockerTest(){
|
||||
var sword = new SwordOfAragorn();
|
||||
Assertions.assertThrows(NullPointerException.class, () -> sword.lock(null));
|
||||
Assertions.assertThrows(NullPointerException.class, () -> sword.unlock(null));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user