PraktikumHolonsTestCanvasController.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package tests;
  2. import classes.AbstractCpsObject;
  3. import classes.CpsEdge;
  4. import classes.HolonObject;
  5. import classes.HolonSwitch;
  6. import classes.IdCounter;
  7. import ui.controller.CanvasController;
  8. import ui.controller.CategoryController;
  9. import ui.controller.MultiPurposeController;
  10. import ui.model.Model;
  11. import org.junit.Test;
  12. import org.junit.Before;
  13. import static org.junit.Assert.assertTrue;
  14. import java.awt.Point;
  15. /**
  16. * Tests for the CanvasController.
  17. *
  18. * @author Gruppe14
  19. */
  20. public class PraktikumHolonsTestCanvasController {
  21. protected PraktikumHolonsAdapter adapter;
  22. protected Model model;
  23. protected MultiPurposeController mp;
  24. protected CategoryController cg;
  25. protected CanvasController controller;
  26. /**
  27. * Setup for the tests.
  28. */
  29. @Before
  30. public void setUp() {
  31. adapter = new PraktikumHolonsAdapter();
  32. model = new Model();
  33. mp = new MultiPurposeController(model);
  34. cg = new CategoryController(model, mp);
  35. controller = new CanvasController(model, mp);
  36. IdCounter.setCounter(1);
  37. }
  38. /**
  39. * Tests adding objects.
  40. */
  41. @Test
  42. public void testAddingObjects() {
  43. // just adding a few things
  44. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 0);
  45. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  46. assertTrue("ID of the Object does not Match", mp.searchByID(1).getName().equals("Power Plant"));
  47. assertTrue("Type of the Object does not Match", mp.searchByID(1) instanceof HolonObject);
  48. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 1);
  49. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  50. assertTrue("ID of the Object does not Match", mp.searchByID(2).getName().equals("Power Plant"));
  51. assertTrue("Type of the Object does not Match", mp.searchByID(2) instanceof HolonObject);
  52. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 2);
  53. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  54. assertTrue("ID of the Object does not Match", mp.searchByID(3).getName().equals("Power Plant"));
  55. assertTrue("Type of the Object does not Match", mp.searchByID(3) instanceof HolonObject);
  56. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 3);
  57. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  58. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 4);
  59. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  60. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 5);
  61. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  62. assertTrue("ID of the Object does not Match", mp.searchByID(6).getName().equals("House"));
  63. assertTrue("Type of the Object does not Match", mp.searchByID(6) instanceof HolonObject);
  64. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 6);
  65. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  66. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 7);
  67. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  68. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 8);
  69. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  70. assertTrue("ID of the Object does not Match", mp.searchByID(9).getName().equals("Switch"));
  71. assertTrue("Type of the Object does not Match", mp.searchByID(9) instanceof HolonSwitch);
  72. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 9);
  73. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  74. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 10);
  75. }
  76. /**
  77. * Tests deleting Objects.
  78. */
  79. @Test(expected = NullPointerException.class)
  80. public void testDeletingObject() {
  81. // Adding Objects on Canvas.. without Coordinates
  82. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  83. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  84. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  85. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  86. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  87. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  88. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  89. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  90. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  91. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  92. controller.deleteObjectOnCanvas(mp.searchByID(4));
  93. assertTrue("Object:4 was not deleted", mp.searchByID(4) == null);
  94. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 9);
  95. controller.deleteObjectOnCanvas(mp.searchByID(5));
  96. assertTrue("Object:4 was not deleted", mp.searchByID(5) == null);
  97. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 8);
  98. controller.deleteObjectOnCanvas(mp.searchByID(6));
  99. assertTrue("Object:4 was not deleted", mp.searchByID(6) == null);
  100. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 7);
  101. // deleting Non-Existant Object -> NullPointerexception
  102. controller.deleteObjectOnCanvas(mp.searchByID(4));
  103. }
  104. /**
  105. * Tests adding and deleting Edges.
  106. */
  107. @Test
  108. public void testAddingAndDeletingEdges() {
  109. HolonObject a = new HolonObject("A");
  110. controller.addNewObject(new HolonObject(a));
  111. int n = 0;
  112. // creates vertices A - Z
  113. for (int i = 2; i < 27; i++) {
  114. // adds Object on canvas
  115. HolonObject temp = new HolonObject(adapter.generate(i));
  116. controller.addNewObject(new HolonObject(temp));
  117. // connect current vertice with all other vertices
  118. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  119. if (!cps.equals(mp.searchByID(i)))
  120. controller.addEdgeOnCanvas(new CpsEdge(mp.searchByID(i), cps));
  121. }
  122. // test how many connections current vertice got
  123. assertTrue("Number of Connections does not Match", mp.searchByID(i).getConnectedTo().size() == i - 1);
  124. // actually just means if its a
  125. // complete graph -> all vertices connected all other vertices
  126. n = model.getObjectsOnCanvas().size();
  127. assertTrue("Number of Edges does not Match", model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  128. }
  129. // same as above
  130. n = model.getObjectsOnCanvas().size();
  131. assertTrue("Number of Edges does not Match", model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  132. // here starts the deleting
  133. controller.removeEdgesOnCanvas(mp.searchEdge(13, 14));
  134. assertTrue("Number of Connection of Vertice M does not Match",
  135. mp.searchByID(13).getConnections().size() == model.getObjectsOnCanvas().size() - 2);
  136. assertTrue("Edge-M-N was not deleted", mp.searchEdge(13, 14) == null);
  137. controller.deleteObjectOnCanvas(mp.searchByID(13));
  138. assertTrue("Object:13 was not deleted", mp.searchByID(13) == null);
  139. assertTrue("Edge-A-M was not deleted", mp.searchEdge(1, 13) == null);
  140. assertTrue("Edge-B-M was not deleted", mp.searchEdge(2, 13) == null);
  141. assertTrue("Edge-C-M was not deleted", mp.searchEdge(3, 13) == null);
  142. assertTrue("Edge-D-M was not deleted", mp.searchEdge(4, 13) == null);
  143. assertTrue("Edge-E-M was not deleted", mp.searchEdge(5, 13) == null);
  144. assertTrue("Edge-F-M was not deleted", mp.searchEdge(6, 13) == null);
  145. assertTrue("Edge-M-O was not deleted", mp.searchEdge(13, 16) == null);
  146. assertTrue("Edge-M-P was not deleted", mp.searchEdge(13, 17) == null);
  147. assertTrue("Edge-M-Q was not deleted", mp.searchEdge(13, 18) == null);
  148. assertTrue("Edge-M-R was not deleted", mp.searchEdge(13, 19) == null);
  149. assertTrue("Edge-M-S was not deleted", mp.searchEdge(13, 20) == null);
  150. }
  151. /**
  152. * Test copying, cutting and pasting Objects.
  153. */
  154. @Test
  155. public void testCutCopyPasteObjects() {
  156. HolonObject a = new HolonObject("A");
  157. HolonObject b = new HolonObject("B");
  158. HolonObject c = new HolonObject("C");
  159. a = new HolonObject(a);
  160. a.setPosition(1, 1);
  161. b = new HolonObject(b);
  162. b.setPosition(2, 2);
  163. c = new HolonObject(c);
  164. c.setPosition(3, 3);
  165. CpsEdge edge1 = new CpsEdge(a, b);
  166. CpsEdge edge2 = new CpsEdge(b, c);
  167. CpsEdge edge3 = new CpsEdge(c, a);
  168. controller.addNewObject(a);
  169. controller.addEdgeOnCanvas(edge1);
  170. controller.addEdgeOnCanvas(edge2);
  171. controller.addEdgeOnCanvas(edge3);
  172. assertTrue("Clipboard not empty", model.getClipboradObjects().isEmpty());
  173. model.getSelectedCpsObjects().add(a);
  174. model.getSelectedCpsObjects().add(b);
  175. model.getSelectedCpsObjects().add(c);
  176. //controller.copyObjects();
  177. //assertTrue("Clipboard empty", !model.getClipboradObjects().isEmpty());
  178. //assertTrue("Clipboard empty", !model.getClipboradObjects().isEmpty());
  179. controller.pasteObjects(new Point(1, 1));
  180. controller.addNewObject(a);
  181. controller.addNewObject(b);
  182. controller.addNewObject(c);
  183. controller.cutObjects();
  184. }
  185. }