Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.55 KB | None | 0 0
  1. package tests;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.Type;
  8.  
  9. import static org.hamcrest.CoreMatchers.instanceOf;
  10.  
  11. import org.junit.Test;
  12.  
  13. import abstractClasses.Character;
  14. import abstractClasses.Enemy;
  15. import abstractClasses.Item;
  16. import abstractClasses.Place;
  17. import abstractClasses.Player;
  18. import exceptions.LocationException;
  19. import interfaces.CharacterActions;
  20. import models.Archer;
  21. import models.City;
  22. import models.Forest;
  23. import models.NPC;
  24. import models.Swordman;
  25. import models.Treasure;
  26. import models.enemies.Deviling;
  27. import models.items.Coin;
  28. import models.items.LifePotion;
  29. import models.items.ManaPotion;
  30. import models.items.Weapon;
  31. import utilities.Utilities;
  32. import utilities.hashTable.HashTable;
  33. import utilities.list.List;
  34. import utilities.list.Node;
  35. import utilities.list.Queue;
  36. import utilities.list.Stack;
  37.  
  38. public class HashTable2UnitTest {
  39.  
  40. private NPC npc1;
  41. private NPC npc2;
  42.  
  43. private Swordman swordman;
  44. private Archer archer;
  45.  
  46. private City city;
  47.  
  48. private Forest forest;
  49.  
  50. private Treasure treasure;
  51.  
  52. private List list;
  53.  
  54. private Stack stack;
  55.  
  56. private Deviling deviling;
  57.  
  58. private Queue queue;
  59.  
  60. private HashTable hashTable;
  61.  
  62. private Weapon weapon;
  63.  
  64. private Coin coin;
  65.  
  66. private LifePotion lifePotion;
  67.  
  68. private ManaPotion manaPotion;
  69.  
  70. public HashTable2UnitTest() {
  71. this.npc1 = new NPC("Blacksmith", 1, 2, "¿Que te arreglo?");
  72. this.npc2 = new NPC("Archer", 3, 4, "¿Quieres flechas?");
  73.  
  74. this.swordman = new Swordman("Emanie", 5, 5);
  75. this.archer = new Archer("Légolas", 3, 2);
  76.  
  77. this.city = new City("Prontera", this.npc1);
  78.  
  79. this.forest = new Forest("Payon", this.npc2);
  80.  
  81. this.treasure = new Treasure();
  82.  
  83. this.list = new List();
  84.  
  85. this.stack = new Stack();
  86.  
  87. this.deviling = new Deviling(5, 4);
  88.  
  89. this.queue = new Queue();
  90.  
  91. this.hashTable = new HashTable(5);
  92.  
  93. this.weapon = new Weapon(10, 200);
  94.  
  95. this.coin = new Coin(1);
  96.  
  97. this.lifePotion = new LifePotion(50);
  98.  
  99. this.manaPotion = new ManaPotion(10);
  100. }
  101.  
  102. /**
  103. * Test de Clases
  104. */
  105.  
  106. @Test
  107. public void testNPC1() {
  108. assertEquals(npc1.getName(), "Blacksmith");
  109. }
  110.  
  111. @Test
  112. public void testNPC2() {
  113. assertEquals(npc1.getX(), 1);
  114. }
  115.  
  116. @Test
  117. public void testNPC3() {
  118. assertEquals(npc1.getY(), 2);
  119. }
  120.  
  121. @Test
  122. public void testNPC4() {
  123. assertEquals(npc2.getSpeech(), "¿Quieres flechas?");
  124. }
  125.  
  126. @Test
  127. public void testPlayer() {
  128. assertEquals(swordman.getName(), "Emanie");
  129. }
  130.  
  131. @Test
  132. public void testPlace1() {
  133. city.setPlayer(swordman);
  134. assertEquals(city.getPlayer().getName(), "Emanie");
  135. }
  136.  
  137. @Test
  138. public void testPlace2() {
  139. forest.setNPC(npc2);
  140. NPC npc = forest.getNPC(npc2.getName());
  141. assertEquals(npc.getName(), "Archer");
  142. }
  143.  
  144. @Test
  145. public void testPlace3() {
  146. assertEquals(city.getName(), "Prontera");
  147. }
  148.  
  149. /**
  150. * Test de herencia
  151. */
  152.  
  153. @Test
  154. public void testInstance1() {
  155. assertThat(swordman, instanceOf(Character.class));
  156. }
  157.  
  158. @Test
  159. public void testInstance2() {
  160. assertThat(npc1, instanceOf(Character.class));
  161. }
  162.  
  163. @Test
  164. public void testInstance3() {
  165. assertThat(city, instanceOf(Place.class));
  166. }
  167.  
  168. @Test
  169. public void testInstance4() {
  170. assertThat(forest, instanceOf(Place.class));
  171. }
  172.  
  173. /**
  174. * Interfaces
  175. */
  176.  
  177. @Test
  178. public void testInterfaces1() {
  179. assertTrue(CharacterActions.class.isInterface());
  180. }
  181.  
  182. @Test
  183. public void testInterfaces2() {
  184. assertFalse(Place.class.isInterface());
  185. }
  186.  
  187. @Test
  188. public void testInterfaces3() {
  189. assertFalse(Player.class.isInterface());
  190. }
  191.  
  192. @Test
  193. public void testInterfaces4() {
  194. assertFalse(Archer.class.isInterface());
  195. }
  196.  
  197. @Test
  198. public void testInterfaces5() {
  199. assertFalse(City.class.isInterface());
  200. }
  201.  
  202. @Test
  203. public void testInterfaces6() {
  204. assertEquals(swordman.speech(0), "Grrrr");
  205. assertEquals(swordman.speech(1), "Tsk");
  206. assertEquals(swordman.speech(2), "¡Te clavaré mi espada!");
  207. }
  208.  
  209. @Test
  210. public void testInterfaces7() {
  211. assertEquals(archer.speech(0), "Hola mariposa");
  212. assertEquals(archer.speech(1), "Adiós orco");
  213. assertEquals(archer.speech(2), "¡Flecha va!");
  214. }
  215.  
  216. @Test
  217. public void testInterfaces8() {
  218. assertEquals(swordman.distance(), 1);
  219. }
  220.  
  221. @Test
  222. public void testInterfaces9() {
  223. assertEquals(archer.distance(), 5);
  224. }
  225.  
  226. @Test
  227. public void testInterfaces10() {
  228. assertEquals(swordman.attack(), 5);
  229. }
  230.  
  231. @Test
  232. public void testInterfaces11() {
  233. assertEquals(archer.attack(), 1);
  234. }
  235.  
  236. @Test
  237. public void testInterfaces12() {
  238. assertThat(swordman, instanceOf(CharacterActions.class));
  239. }
  240.  
  241. @Test
  242. public void testInterfaces13() {
  243. assertThat(archer, instanceOf(CharacterActions.class));
  244. }
  245.  
  246. /**
  247. * Clases abstractas
  248. */
  249.  
  250. @Test
  251. public void testAbstract1() {
  252. assertEquals(swordman.walk(), 1);
  253. }
  254.  
  255. @Test
  256. public void testAbstract2() {
  257. assertEquals(archer.walk(), 5);
  258. }
  259.  
  260. @Test
  261. public void testAbstract3() {
  262. assertEquals(npc1.walk(), 0);
  263. assertEquals(npc2.walk(), 0);
  264. }
  265.  
  266. /**
  267. * Sobrecarga de métodos
  268. */
  269.  
  270. @Test
  271. public void testOverload1() {
  272. assertNotNull(swordman.speech());
  273. assertNotNull(archer.speech());
  274. }
  275.  
  276. /**
  277. * Polimorfismo
  278. */
  279.  
  280. @Test
  281. public void testPolymorphism1() {
  282. assertEquals(swordman.getLifePoints(), 100);
  283. }
  284.  
  285. @Test
  286. public void testPolymorphism2() {
  287. assertEquals(archer.getLifePoints(), 60);
  288. }
  289.  
  290. @Test
  291. public void testPolymorphism3() {
  292. assertEquals(swordman.defend(30), 70);
  293. assertEquals(swordman.getLifePoints(), 70);
  294. }
  295.  
  296. @Test
  297. public void testPolymorphism4() {
  298. assertEquals(archer.defend(45), 15);
  299. assertEquals(archer.getLifePoints(), 15);
  300. }
  301.  
  302. /**
  303. * Encapsulamiento
  304. */
  305.  
  306. @Test(expected=NoSuchMethodException.class)
  307. public void testEncapsulation1() throws NoSuchMethodException {
  308. try{
  309. archer.getClass().getMethod("setLifePoints", Integer.class);
  310. }catch (SecurityException s) {
  311. System.out.println(s.getStackTrace());
  312. }
  313. }
  314.  
  315. @Test(expected=NoSuchMethodException.class)
  316. public void testEncapsulation2() throws NoSuchMethodException {
  317. try{
  318. swordman.getClass().getMethod("setLifePoints", Integer.class);
  319. }catch (SecurityException s) {
  320. System.out.println(s.getStackTrace());
  321. }
  322. }
  323.  
  324. @Test(expected=NoSuchMethodException.class)
  325. public void testEncapsulation3() throws NoSuchMethodException {
  326. try{
  327. archer.getClass().getMethod("setBaseLife");
  328. }catch (SecurityException s) {
  329. System.out.println(s.getStackTrace());
  330. }
  331. }
  332.  
  333. @Test(expected=NoSuchMethodException.class)
  334. public void testEncapsulation4() throws NoSuchMethodException {
  335. try{
  336. swordman.getClass().getMethod("setBaseLife");
  337. }catch (SecurityException s) {
  338. System.out.println(s.getStackTrace());
  339. }
  340. }
  341.  
  342. @Test(expected=NoSuchMethodException.class)
  343. public void testEncapsulation5() throws NoSuchMethodException {
  344. try{
  345. archer.getClass().getMethod("getBaseLife");
  346. }catch (SecurityException s) {
  347. System.out.println(s.getStackTrace());
  348. }
  349. }
  350.  
  351. @Test(expected=NoSuchMethodException.class)
  352. public void testEncapsulation6() throws NoSuchMethodException {
  353. try{
  354. swordman.getClass().getMethod("getBaseLife");
  355. }catch (SecurityException s) {
  356. System.out.println(s.getStackTrace());
  357. }
  358. }
  359.  
  360. @Test(expected=NoSuchMethodException.class)
  361. public void testEncapsulation7() throws NoSuchMethodException {
  362. try{
  363. Player.class.getMethod("setLifePoints", Integer.class);
  364. }catch (SecurityException s) {
  365. System.out.println(s.getStackTrace());
  366. }
  367. }
  368.  
  369. /**
  370. * Propiedad final
  371. */
  372.  
  373. @Test
  374. public void testFinalProperty1() {
  375. Field[] declaredFields = Archer.class.getDeclaredFields();
  376. Field finalField = null;
  377. for (Field field : declaredFields) {
  378. if (java.lang.reflect.Modifier.isFinal(field.getModifiers())) {
  379. if(field.getName() == "baseLife")
  380. finalField = field;
  381. }
  382. }
  383.  
  384. assertNotNull(finalField);
  385. assertTrue(finalField.getName() == "baseLife");
  386. assertTrue(finalField.getType() == int.class);
  387. }
  388.  
  389. @Test
  390. public void testFinalProperty2() {
  391. Field[] declaredFields = Swordman.class.getDeclaredFields();
  392. Field finalField = null;
  393. for (Field field : declaredFields) {
  394. if (java.lang.reflect.Modifier.isFinal(field.getModifiers())) {
  395. if(field.getName() == "baseLife")
  396. finalField = field;
  397. }
  398. }
  399.  
  400. assertNotNull(finalField);
  401. assertTrue(finalField.getName() == "baseLife");
  402. assertTrue(finalField.getType() == int.class);
  403. }
  404.  
  405. @Test
  406. public void testFinalProperty3() {
  407. Field[] declaredFields = Player.class.getDeclaredFields();
  408. Field finalField = null;
  409. for (Field field : declaredFields) {
  410. if (java.lang.reflect.Modifier.isFinal(field.getModifiers())) {
  411. if(field.getName() == "lifePoints")
  412. finalField = field;
  413. }
  414. }
  415.  
  416. assertNull(finalField);
  417. }
  418.  
  419. /**
  420. * Propiedad static
  421. */
  422.  
  423. @Test
  424. public void testStaticProperty1() {
  425. Field[] declaredFields = Archer.class.getDeclaredFields();
  426. Field staticField = null;
  427. for (Field field : declaredFields) {
  428. if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
  429. if(field.getName() == "baseLife")
  430. staticField = field;
  431. }
  432. }
  433.  
  434. assertNotNull(staticField);
  435. assertTrue(staticField.getName() == "baseLife");
  436. assertTrue(staticField.getType() == int.class);
  437. }
  438.  
  439. @Test
  440. public void testStaticProperty2() {
  441. Field[] declaredFields = Swordman.class.getDeclaredFields();
  442. Field staticField = null;
  443. for (Field field : declaredFields) {
  444. if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
  445. if(field.getName() == "baseLife")
  446. staticField = field;
  447. }
  448. }
  449.  
  450. assertNotNull(staticField);
  451. assertTrue(staticField.getName() == "baseLife");
  452. assertTrue(staticField.getType() == int.class);
  453. }
  454.  
  455. @Test
  456. public void testStaticProperty3() {
  457. Field[] declaredFields = Player.class.getDeclaredFields();
  458. Field staticField = null;
  459. for (Field field : declaredFields) {
  460. if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
  461. if(field.getName() == "lifePoints")
  462. staticField = field;
  463. }
  464. }
  465.  
  466. assertNull(staticField);
  467. }
  468.  
  469. @Test
  470. public void testStaticProperty4() {
  471. Method[] declaredMethods = Utilities.class.getMethods();
  472. Method staticMethod = null;
  473. for (Method method : declaredMethods) {
  474. if (java.lang.reflect.Modifier.isStatic(method.getModifiers())) {
  475. if(method.getName() == "calculateDamage")
  476. staticMethod = method;
  477. }
  478. }
  479.  
  480. Type[] methodParameters = staticMethod.getGenericParameterTypes();
  481. assertNotNull(staticMethod);
  482. assertTrue(staticMethod.getName() == "calculateDamage");
  483. assertTrue(staticMethod.getReturnType() == int.class);
  484. assertEquals(methodParameters.length, 2);
  485. assertEquals(methodParameters[0].getTypeName(), "int");
  486. assertEquals(methodParameters[1].getTypeName(), "int");
  487. }
  488.  
  489. /**
  490. * Excepciones
  491. */
  492. @Test(expected=LocationException.class)
  493. public void testException1() throws LocationException {
  494. throw new LocationException();
  495. }
  496.  
  497. @Test(expected=LocationException.class)
  498. public void testException2() throws LocationException {
  499. swordman.walk(5, 5);
  500. }
  501.  
  502. @Test(expected=LocationException.class)
  503. public void testException3() throws LocationException {
  504. archer.walk(3, 2);
  505. }
  506.  
  507. @Test
  508. public void testException4() {
  509. try {
  510. swordman.walk(5, 5);
  511. // Lanzando la excepción fuerzo a que si no salta lo anterior, salta aquí y falla el test
  512. throw new LocationException();
  513. } catch (LocationException e) {
  514. assertEquals(e.getMessage(), "Misma X e Y");
  515. }
  516. }
  517.  
  518. @Test
  519. public void testException5() {
  520. try {
  521. archer.walk(3, 2);
  522. throw new LocationException();
  523. } catch (LocationException e) {
  524. assertEquals(e.getMessage(), "Misma X e Y");
  525. }
  526. }
  527.  
  528. /**
  529. * IO
  530. */
  531.  
  532. /**
  533. * IO-Zip
  534. */
  535.  
  536. /**
  537. * IO-Serialization
  538. */
  539.  
  540. /**
  541. * Arrays
  542. */
  543. @Test
  544. public void testTreasure1() {
  545. assertTrue(treasure.getX() >= 0 && treasure.getX() <= 10);
  546. }
  547.  
  548. @Test
  549. public void testTreasure2() {
  550. assertTrue(treasure.getY() >= 0 && treasure.getY() <= 10);
  551. }
  552.  
  553. @Test
  554. public void testTreasure3() {
  555. if(treasure.isOpen() && treasure.getWeapon() == null){
  556. assertTrue(true);
  557. }else if(treasure.isOpen() && treasure.getWeapon() != null){
  558. assertTrue(false);
  559. }else if(!treasure.isOpen() && treasure.getWeapon() != null){
  560. assertTrue(true);
  561. }else{
  562. assertTrue(false);
  563. }
  564. }
  565.  
  566. @Test
  567. public void testTreasure4() {
  568. assertTrue(city.getTreasures().length == 5);
  569. }
  570.  
  571. @Test
  572. public void testTreasure5() {
  573. assertTrue(city.getTreasures().length == 5);
  574. }
  575.  
  576. /**
  577. * Lista
  578. */
  579.  
  580. @Test
  581. public void testList1() {
  582. assertEquals(0, list.getSize());
  583. assertEquals(list.getHead(), list.getTail());
  584. assertNull(list.getHead());
  585. assertNull(list.getTail());
  586. assertNull(list.getCurrent());
  587. }
  588.  
  589. @Test
  590. public void testList2() {
  591. list.add(5);
  592. assertEquals(1, list.getSize());
  593. assertEquals(list.getHead(), list.getTail());
  594. assertNotNull(list.getHead());
  595. assertNotNull(list.getTail());
  596. }
  597.  
  598. @Test
  599. public void testList3() {
  600. list.add(5);
  601. list.add(3);
  602. assertEquals(2, list.getSize());
  603. assertNotEquals(list.getHead(), list.getTail());
  604. assertEquals(5, list.getHead());
  605. assertEquals(3, list.getTail());
  606. }
  607.  
  608. @Test
  609. public void testList4() {
  610. list.add(5);
  611. list.add(3);
  612. list.add(7);
  613. list.delete(3);
  614. assertEquals(2, list.getSize());
  615. assertNotEquals(list.getHead(), list.getTail());
  616. assertEquals(5, list.getHead());
  617. assertEquals(7, list.getTail());
  618. }
  619.  
  620. @Test
  621. public void testList5() {
  622. assertEquals(0, list.getSize());
  623. list.delete(1);
  624. assertEquals(0, list.getSize());
  625. assertEquals(list.getHead(), list.getTail());
  626. assertNull(list.getHead());
  627. assertNull(list.getTail());
  628. }
  629.  
  630. @Test
  631. public void testList6() {
  632. list.add(1);
  633. list.add(2);
  634. list.add(3);
  635. list.add(4);
  636. list.add(5);
  637. assertNull(list.getCurrent());
  638. assertNotNull(list.getHead());
  639. assertEquals(list.getHead(), list.getCurrent());
  640. assertEquals(1, list.getCurrent());
  641. list.next();
  642. assertEquals(2, list.getCurrent());
  643. list.next();
  644. assertEquals(3, list.getCurrent());
  645. list.next();
  646. assertEquals(4, list.getCurrent());
  647. list.next();
  648. assertEquals(5, list.getCurrent());
  649. assertEquals(list.getTail(), list.getCurrent());
  650. list.next();
  651. assertNull(((Node) list.getCurrent()));
  652. }
  653.  
  654. /**
  655. * Uso de las listas
  656. */
  657.  
  658. @Test
  659. public void testList21() {
  660. city.setNPC(npc2);
  661.  
  662. NPC npc = new NPC("Foodseller", 2, 3, "Tengo carne");
  663. city.setNPC(npc);
  664.  
  665. assertEquals(3, city.getListNPC().getSize());
  666.  
  667. assertEquals(city.getNPC("Foodseller").getSpeech(), "Tengo carne");
  668. assertEquals(city.getNPC("Archer").getSpeech(), "¿Quieres flechas?");
  669. }
  670.  
  671. @Test
  672. public void testList22() {
  673. assertEquals(1, forest.getListNPC().getSize());
  674. }
  675.  
  676. @Test
  677. public void testList23() {
  678. NPC npc = new NPC("Foodseller", 2, 3, "Tengo carne");
  679. List listNPC = new List();
  680. listNPC.add(npc);
  681. listNPC.add(npc1);
  682. listNPC.add(npc2);
  683.  
  684. City city = new City("Aldebarán", listNPC);
  685. assertEquals(3, city.getListNPC().getSize());
  686. }
  687.  
  688. @Test
  689. public void testList24() {
  690. NPC npc = new NPC("Foodseller", 2, 3, "Tengo carne");
  691. List listNPC = new List();
  692. listNPC.add(npc);
  693. listNPC.add(npc1);
  694. listNPC.add(npc2);
  695.  
  696. Forest forest = new Forest("Izlude", listNPC);
  697. assertEquals(3, forest.getListNPC().getSize());
  698. }
  699.  
  700. /**
  701. * Pila
  702. */
  703.  
  704. @Test
  705. public void testStack1() {
  706. assertNull(stack.pop());
  707. assertNull(stack.getCurrent());
  708. }
  709.  
  710. @Test
  711. public void testStack2() {
  712. stack.push(swordman);
  713. assertEquals("Emanie", ((Player)stack.getCurrent()).getName());
  714. assertEquals(swordman, stack.getCurrent());
  715. }
  716.  
  717. @Test
  718. public void testStack3() {
  719. stack.push(swordman);
  720. stack.push(archer);
  721. stack.push(npc1);
  722. assertEquals("¿Que te arreglo?", ((NPC)stack.getCurrent()).getSpeech());
  723. }
  724.  
  725. @Test
  726. public void testStack4() {
  727. stack.push(npc1);
  728. stack.push(swordman);
  729. stack.push(archer);
  730. assertEquals("Légolas", ((Character)stack.getCurrent()).getName());
  731. assertEquals(archer, stack.pop());
  732. assertEquals("Emanie", ((Character)stack.getCurrent()).getName());
  733. assertEquals(swordman, stack.pop());
  734. assertEquals("Blacksmith", ((Character)stack.getCurrent()).getName());
  735. assertEquals(npc1, stack.pop());
  736. assertNull(stack.pop());
  737. assertNull(stack.getCurrent());
  738. assertNull(stack.pop());
  739. assertNull(stack.getCurrent());
  740. assertNull(stack.pop());
  741. }
  742.  
  743. @Test
  744. public void testStack5() {
  745. stack.push(archer);
  746. assertEquals("Légolas", ((Character)stack.getCurrent()).getName());
  747. assertEquals(archer, stack.pop());
  748. assertNull(stack.pop());
  749. assertNull(stack.getCurrent());
  750. stack.push(npc1);
  751. stack.push(swordman);
  752. assertEquals("Emanie", ((Character)stack.getCurrent()).getName());
  753. assertEquals(swordman, stack.pop());
  754. assertEquals("Blacksmith", ((Character)stack.getCurrent()).getName());
  755. assertEquals(npc1, stack.pop());
  756. assertNull(stack.pop());
  757. assertNull(stack.getCurrent());
  758. assertNull(stack.pop());
  759. stack.push(archer);
  760. assertEquals("Légolas", ((Character)stack.getCurrent()).getName());
  761. }
  762.  
  763. /**
  764. * Uso de las pilas
  765. */
  766. @Test
  767. public void testDeviling1() {
  768. assertEquals(deviling.getName(), "Deviling");
  769. }
  770.  
  771. @Test
  772. public void testDeviling2() {
  773. assertEquals(deviling.getX(), 5);
  774. }
  775.  
  776. @Test
  777. public void testDeviling3() {
  778. assertEquals(deviling.getY(), 4);
  779. }
  780.  
  781. @Test
  782. public void testDeviling4() {
  783. assertEquals(deviling.getRemainingLife(), 300);
  784. }
  785.  
  786. @Test
  787. public void testDeviling5() {
  788. assertEquals(deviling.walk(), 2);
  789. }
  790.  
  791. @Test(expected=NullPointerException.class)
  792. public void testDeviling6() throws LocationException {
  793. deviling.chase();
  794. }
  795.  
  796. @Test
  797. public void testDeviling7() throws LocationException {
  798. assertEquals(null, deviling.getCurrentObjective());
  799. deviling.see(swordman);
  800. deviling.see(archer);
  801. deviling.chase();
  802. assertEquals(archer, deviling.getCurrentObjective());
  803. }
  804.  
  805. @Test
  806. public void testDeviling8() throws LocationException {
  807. deviling.see(archer);
  808. deviling.see(swordman);
  809. deviling.chase();
  810. assertEquals(swordman, deviling.getCurrentObjective());
  811. swordman.setX(10);
  812. swordman.setY(10);
  813. deviling.chase();
  814. assertEquals(archer, deviling.getCurrentObjective());
  815. deviling.see(swordman);
  816. archer.setX(5);
  817. archer.setY(4);
  818. deviling.see(archer);
  819. deviling.chase();
  820. assertEquals(archer, deviling.getCurrentObjective());
  821. }
  822.  
  823. @Test(expected=LocationException.class)
  824. public void testDeviling9() throws LocationException {
  825. deviling.setX(5);
  826. deviling.setY(5);
  827. deviling.see(swordman);
  828. deviling.chase();
  829. }
  830.  
  831. @Test(expected=LocationException.class)
  832. public void testDeviling10() throws LocationException {
  833. deviling.see(swordman);
  834. deviling.chase();
  835. deviling.see(archer);
  836. deviling.chase();
  837. deviling.see(swordman);
  838. deviling.chase();
  839. deviling.see(swordman);
  840. deviling.chase();
  841. }
  842.  
  843. /**
  844. * Cola
  845. */
  846.  
  847. @Test
  848. public void testQueue1() {
  849. assertTrue(queue.isEmpty());
  850. assertTrue(queue.size() == 0);
  851. assertNull(queue.dequeue());
  852. }
  853.  
  854. @Test
  855. public void testQueue2() {
  856. queue.enqueue(swordman);
  857. assertFalse(queue.isEmpty());
  858. assertTrue(queue.size() == 1);
  859. Player player = ((Player)(queue.dequeue()));
  860. assertEquals("Emanie", player.getName());
  861. assertTrue(queue.isEmpty());
  862. assertTrue(queue.size() == 0);
  863. assertNull(queue.dequeue());
  864. }
  865.  
  866. @Test
  867. public void testQueue3() {
  868. queue.enqueue(swordman);
  869. queue.enqueue(archer);
  870. queue.enqueue(npc1);
  871. assertEquals("Emanie", ((Player)queue.dequeue()).getName());
  872. assertEquals("Légolas", ((Player)queue.dequeue()).getName());
  873. assertEquals("Blacksmith", ((Character)(queue.dequeue())).getName());
  874. assertNull(queue.dequeue());
  875. }
  876.  
  877. @Test
  878. public void testQueue4() {
  879. queue.enqueue(swordman);
  880. queue.enqueue(archer);
  881. queue.enqueue(npc1);
  882.  
  883. assertEquals("Emanie", ((Player)queue.dequeue()).getName());
  884. assertEquals("Légolas", ((Player)queue.dequeue()).getName());
  885.  
  886. queue.enqueue(npc2);
  887. assertEquals("Blacksmith", ((Character)(queue.dequeue())).getName());
  888.  
  889. queue.enqueue(swordman);
  890. assertEquals("Archer", ((Character)(queue.dequeue())).getName());
  891.  
  892. assertEquals("Emanie", ((Character)(queue.dequeue())).getName());
  893.  
  894. assertNull(queue.dequeue());
  895. assertNull(queue.dequeue());
  896. }
  897.  
  898. /**
  899. * Uso de las colas
  900. */
  901.  
  902. @Test
  903. public void testQueue21() {
  904. assertNull(swordman.killNextEnemy());
  905. assertTrue(swordman.getEnemiesToKill().isEmpty());
  906. assertEquals(0, swordman.getEnemiesToKill().size());
  907. }
  908.  
  909. @Test
  910. public void testQueue22() {
  911. archer.addNewEnemy(deviling);
  912. assertEquals(1, archer.getEnemiesToKill().size());
  913. Enemy enemy = archer.killNextEnemy();
  914. assertNotNull(enemy);
  915. assertEquals(deviling, enemy);
  916. assertTrue(archer.getEnemiesToKill().isEmpty());
  917.  
  918. assertNull(archer.killNextEnemy());
  919. assertTrue(archer.getEnemiesToKill().isEmpty());
  920. assertEquals(0, archer.getEnemiesToKill().size());
  921. assertEquals(null, archer.killNextEnemy());
  922.  
  923. assertNull(swordman.killNextEnemy());
  924. assertTrue(swordman.getEnemiesToKill().isEmpty());
  925. assertEquals(0, swordman.getEnemiesToKill().size());
  926. assertEquals(null, swordman.killNextEnemy());
  927. }
  928.  
  929. @Test
  930. public void testQueue23() {
  931. Deviling deviling2 = new Deviling(0, 0);
  932. Deviling deviling3 = new Deviling(1, 1);
  933. Deviling deviling4 = new Deviling(2, 2);
  934.  
  935. swordman.addNewEnemy(deviling);
  936. swordman.addNewEnemy(deviling2);
  937. swordman.addNewEnemy(deviling3);
  938. swordman.addNewEnemy(deviling4);
  939.  
  940. assertEquals(deviling, swordman.killNextEnemy());
  941. assertEquals(deviling2, swordman.killNextEnemy());
  942. assertEquals(deviling3, swordman.killNextEnemy());
  943. assertEquals(deviling4, swordman.killNextEnemy());
  944.  
  945. assertNull(swordman.killNextEnemy());
  946. }
  947.  
  948. @Test
  949. public void testQueue24() {
  950. Deviling deviling2 = new Deviling(0, 0);
  951. Deviling deviling3 = new Deviling(1, 1);
  952. Deviling deviling4 = new Deviling(2, 2);
  953. Deviling deviling5 = new Deviling(3,3);
  954.  
  955. swordman.addNewEnemy(deviling);
  956. swordman.addNewEnemy(deviling2);
  957.  
  958. archer.addNewEnemy(deviling3);
  959. archer.addNewEnemy(deviling4);
  960.  
  961. assertEquals(deviling, swordman.killNextEnemy());
  962. assertEquals(deviling3, archer.killNextEnemy());
  963.  
  964. swordman.addNewEnemy(deviling5);
  965.  
  966. assertEquals(deviling2, swordman.killNextEnemy());
  967. assertEquals(deviling4, archer.killNextEnemy());
  968.  
  969. assertEquals(deviling5, swordman.killNextEnemy());
  970. assertEquals(null, archer.killNextEnemy());
  971.  
  972. assertEquals(null, swordman.killNextEnemy());
  973. assertEquals(null, archer.killNextEnemy());
  974. }
  975.  
  976. /**
  977. * Tabla Hash
  978. */
  979.  
  980. @Test
  981. public void testHashTable1() {
  982. assertNull(hashTable.remove(swordman));
  983. assertNull(hashTable.search(swordman));
  984. assertTrue(hashTable.size() == 0);
  985. }
  986.  
  987. @Test(expected=NullPointerException.class)
  988. public void testHashTable2() {
  989. hashTable.put(null, swordman);
  990. }
  991.  
  992. @Test(expected=NullPointerException.class)
  993. public void testHashTable3() {
  994. hashTable.put(swordman.getName(), null);
  995. }
  996.  
  997. @Test
  998. public void testHashTable4() {
  999. hashTable.put(swordman.getName(), swordman);
  1000. hashTable.put(archer.getName(), archer);
  1001. assertEquals(2, hashTable.size());
  1002. hashTable.put(npc1.getName(), npc1);
  1003. hashTable.put(npc2.getName(), npc2);
  1004. assertEquals(4, hashTable.size());
  1005. hashTable.put(npc1.getName(), npc1);
  1006. hashTable.put(npc2.getName(), npc2);
  1007. assertEquals(6, hashTable.size());
  1008. }
  1009.  
  1010. @Test
  1011. public void testHashTable5() {
  1012. hashTable.put(swordman.getName(), swordman);
  1013. hashTable.put(archer.getName(), archer);
  1014. assertEquals(2, hashTable.size());
  1015. Player player = ((Player)hashTable.search(swordman.getName()));
  1016. assertEquals("Emanie", player.getName());
  1017. assertEquals(2, hashTable.size());
  1018. player = ((Player)hashTable.search("Légolas"));
  1019. assertEquals("Légolas", player.getName());
  1020. assertEquals(2, hashTable.size());
  1021. }
  1022.  
  1023. @Test
  1024. public void testHashTable6() {
  1025. hashTable.put(swordman.getName(), swordman);
  1026. hashTable.put(npc1.getName(), npc1);
  1027. assertEquals(2, hashTable.size());
  1028. Player player = ((Player)hashTable.remove(swordman.getName()));
  1029. assertEquals("Emanie", player.getName());
  1030. assertEquals(1, hashTable.size());
  1031. player = ((Player)hashTable.remove("House"));
  1032. assertNull(player);
  1033. assertEquals(1, hashTable.size());
  1034. NPC npc = ((NPC)hashTable.remove(npc1.getName()));
  1035. assertEquals("Blacksmith", npc.getName());
  1036. assertEquals(0, hashTable.size());
  1037. assertNull(hashTable.remove(swordman.getName()));
  1038. assertNull(hashTable.remove(archer.getName()));
  1039. assertNull(hashTable.remove(npc1.getName()));
  1040. assertNull(hashTable.remove("Test"));
  1041. }
  1042.  
  1043. @Test
  1044. public void testHashTable7() {
  1045. hashTable.put(swordman.getName(), swordman);
  1046. hashTable.put(archer.getName(), archer);
  1047. assertEquals(2, hashTable.size());
  1048. Player player = ((Player)hashTable.remove(swordman.getName()));
  1049. assertEquals("Emanie", player.getName());
  1050. assertEquals(1, hashTable.size());
  1051. player = ((Player)hashTable.remove("Légolas"));
  1052. assertEquals("Légolas", player.getName());
  1053. assertEquals(0, hashTable.size());
  1054.  
  1055. hashTable.put(archer.getName(), archer);
  1056. assertEquals(1, hashTable.size());
  1057. hashTable.put(swordman.getName(), swordman);
  1058. assertEquals(2, hashTable.size());
  1059. hashTable.put(npc1.getName(), npc1);
  1060. assertEquals(3, hashTable.size());
  1061.  
  1062. hashTable.put(5, 5);
  1063. assertEquals(4, hashTable.size());
  1064. hashTable.put("Cadena de texto", "Hola Mundo Cruel");
  1065. assertEquals(5, hashTable.size());
  1066. hashTable.put("Tesoro", "Espada+1");
  1067. assertEquals(6, hashTable.size());
  1068. hashTable.put(7, 7);
  1069. assertEquals(7, hashTable.size());
  1070. }
  1071.  
  1072. /**
  1073. * Uso de la Tabla Hash
  1074. */
  1075.  
  1076. @Test
  1077. public void testItem1() {
  1078. assertThat(coin, instanceOf(Item.class));
  1079. }
  1080.  
  1081. @Test
  1082. public void testItem2() {
  1083. assertThat(weapon, instanceOf(Item.class));
  1084. }
  1085.  
  1086. @Test
  1087. public void testItem3() {
  1088. assertThat(lifePotion, instanceOf(Item.class));
  1089. }
  1090.  
  1091. @Test
  1092. public void testItem4() {
  1093. assertThat(manaPotion, instanceOf(Item.class));
  1094. }
  1095.  
  1096. @Test
  1097. public void testCoin1() {
  1098. assertEquals(coin.getValue(), 1);
  1099. }
  1100.  
  1101. @Test
  1102. public void testCoin2() {
  1103. assertEquals("coin", coin.getName());
  1104. }
  1105.  
  1106. @Test
  1107. public void testPotion1() {
  1108. assertEquals(lifePotion.getQuantity(), 50);
  1109. }
  1110.  
  1111. @Test
  1112. public void testPotion2() {
  1113. assertEquals("lifePotion", lifePotion.getName());
  1114. }
  1115.  
  1116. @Test
  1117. public void testPotion3() {
  1118. assertEquals(manaPotion.getQuantity(), 10);
  1119. }
  1120.  
  1121. @Test
  1122. public void testPotion4() {
  1123. assertEquals("manaPotion", manaPotion.getName());
  1124. }
  1125.  
  1126. @Test
  1127. public void testWeapon() {
  1128. assertEquals("weapon", weapon.getName());
  1129. }
  1130.  
  1131. @Test
  1132. public void testHashTable21() {
  1133. assertNull(swordman.getItem("Algo"));
  1134. assertNull(archer.getItem("Algo"));
  1135. }
  1136.  
  1137. @Test
  1138. public void testHashTable22() {
  1139. assertEquals(0, swordman.getRucksack().size());
  1140. swordman.addItem(coin);
  1141. assertEquals(1, swordman.getRucksack().size());
  1142. swordman.addItem(weapon);
  1143. assertEquals(2, swordman.getRucksack().size());
  1144. swordman.addItem(lifePotion);
  1145. assertEquals(3, swordman.getRucksack().size());
  1146. swordman.addItem(manaPotion);
  1147. assertEquals(4, swordman.getRucksack().size());
  1148. }
  1149.  
  1150. @Test
  1151. public void testHashTable23() {
  1152. swordman.addItem(coin);
  1153. swordman.addItem(weapon);
  1154. swordman.addItem(lifePotion);
  1155. swordman.addItem(manaPotion);
  1156. assertEquals(lifePotion, swordman.getItem(lifePotion.getName()));
  1157. assertEquals(weapon, swordman.getItem(weapon.getName()));
  1158. assertEquals(coin, swordman.getItem(coin.getName()));
  1159. assertEquals(manaPotion, swordman.getItem(manaPotion.getName()));
  1160. }
  1161.  
  1162. @Test
  1163. public void testHashTable24() {
  1164. archer.addItem(coin);
  1165. archer.addItem(weapon);
  1166. swordman.addItem(lifePotion);
  1167. swordman.addItem(manaPotion);
  1168. Coin coin = new Coin(10);
  1169. swordman.addItem(coin);
  1170.  
  1171. assertEquals(1, (((Coin) archer.getItem(coin.getName())).getValue()));
  1172. assertEquals(10, (((Coin) swordman.getItem(coin.getName())).getValue()));
  1173.  
  1174. this.coin.setValue(2);
  1175. assertEquals(2, (((Coin) archer.getItem(coin.getName())).getValue()));
  1176.  
  1177. coin.setValue(5);
  1178. assertEquals(5, (((Coin) swordman.getItem(coin.getName())).getValue()));
  1179.  
  1180. assertNull(archer.getItem(lifePotion.getName()));
  1181. assertNull(archer.getItem(manaPotion.getName()));
  1182. assertNull(swordman.getItem(weapon.getName()));
  1183. }
  1184.  
  1185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement