PraktikumHolonsTestCanvasController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package tests;
  2. import classes.AbstractCanvasObject;
  3. import classes.Edge;
  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. HolonObject a = new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant"));
  46. a.setPosition(100, 100);
  47. controller.addNewObject(a);
  48. assertTrue("ID of the Object does not Match", mp.searchByID(1).getName().equals("Power Plant"));
  49. assertTrue("Type of the Object does not Match", mp.searchByID(1) instanceof HolonObject);
  50. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 1);
  51. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  52. /* trick drag&drop replace by moving a to 100 100 and then back to a, so
  53. * the rest of new objects won't know which one to replace
  54. */
  55. a.setPosition(0, 0);
  56. assertTrue("ID of the Object does not Match", mp.searchByID(2).getName().equals("Power Plant"));
  57. assertTrue("Type of the Object does not Match", mp.searchByID(2) instanceof HolonObject);
  58. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 2);
  59. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  60. assertTrue("ID of the Object does not Match", mp.searchByID(3).getName().equals("Power Plant"));
  61. assertTrue("Type of the Object does not Match", mp.searchByID(3) instanceof HolonObject);
  62. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 3);
  63. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  64. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 4);
  65. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  66. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 5);
  67. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  68. assertTrue("ID of the Object does not Match", mp.searchByID(6).getName().equals("House"));
  69. assertTrue("Type of the Object does not Match", mp.searchByID(6) instanceof HolonObject);
  70. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 6);
  71. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  72. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 7);
  73. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  74. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 8);
  75. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  76. assertTrue("ID of the Object does not Match", mp.searchByID(9).getName().equals("Switch"));
  77. assertTrue("Type of the Object does not Match", mp.searchByID(9) instanceof HolonSwitch);
  78. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 9);
  79. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  80. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 10);
  81. }
  82. /**
  83. * Tests deleting Objects.
  84. */
  85. @Test(expected = NullPointerException.class)
  86. public void testDeletingObject() {
  87. // Adding Objects on Canvas.. without Coordinates
  88. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  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 HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  92. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  93. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  94. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  95. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
  96. controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  97. controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
  98. controller.deleteObjectOnCanvas(mp.searchByID(4));
  99. assertTrue("Object:4 was not deleted", mp.searchByID(4) == null);
  100. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 9);
  101. controller.deleteObjectOnCanvas(mp.searchByID(5));
  102. assertTrue("Object:4 was not deleted", mp.searchByID(5) == null);
  103. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 8);
  104. controller.deleteObjectOnCanvas(mp.searchByID(6));
  105. assertTrue("Object:4 was not deleted", mp.searchByID(6) == null);
  106. assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 7);
  107. // deleting Non-Existant Object -> NullPointerexception
  108. controller.deleteObjectOnCanvas(mp.searchByID(4));
  109. }
  110. /**
  111. * Tests adding and deleting Edges.
  112. */
  113. @Test
  114. public void testAddingAndDeletingEdges() {
  115. HolonObject a = new HolonObject("A");
  116. controller.addNewObject(new HolonObject(a));
  117. int n = 0;
  118. // creates vertices A - Z
  119. for (int i = 2; i < 27; i++) {
  120. // adds Object on canvas
  121. HolonObject temp = new HolonObject(adapter.generate(i));
  122. HolonObject temp2 = new HolonObject(temp);
  123. temp2.setPosition(50+180 * (i-2)%5,50+ 180 * (i-2)/5);
  124. controller.addNewObject(temp2);
  125. // connect current vertice with all other vertices
  126. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  127. if (!cps.equals(mp.searchByID(i)))
  128. controller.addEdgeOnCanvas(new Edge(mp.searchByID(i), cps));
  129. }
  130. // test how many connections current vertice got
  131. assertTrue("Number of Connections does not Match: " + mp.searchByID(i).getConnectedTo().size()+ " != " + (i - 1), mp.searchByID(i).getConnectedTo().size() == i - 1);
  132. // actually just means if its a
  133. // complete graph -> all vertices connected all other vertices
  134. n = model.getObjectsOnCanvas().size();
  135. assertTrue("Number of Edges does not Match: "+model.getEdgesOnCanvas().size()+ " != "+ (n * (n - 1)) / 2, model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  136. }
  137. // same as above
  138. n = model.getObjectsOnCanvas().size();
  139. assertTrue("Number of Edges does not Match: "+model.getEdgesOnCanvas().size()+ " != "+ (n * (n - 1)) / 2, model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  140. // here starts the deleting
  141. controller.removeEdgesOnCanvas(mp.searchEdge(13, 14));
  142. assertTrue("Number of Connection of Vertice M does not Match",
  143. mp.searchByID(13).getConnections().size() == model.getObjectsOnCanvas().size() - 2);
  144. assertTrue("Edge-M-N was not deleted", mp.searchEdge(13, 14) == null);
  145. controller.deleteObjectOnCanvas(mp.searchByID(13));
  146. assertTrue("Object:13 was not deleted", mp.searchByID(13) == null);
  147. assertTrue("Edge-A-M was not deleted", mp.searchEdge(1, 13) == null);
  148. assertTrue("Edge-B-M was not deleted", mp.searchEdge(2, 13) == null);
  149. assertTrue("Edge-C-M was not deleted", mp.searchEdge(3, 13) == null);
  150. assertTrue("Edge-D-M was not deleted", mp.searchEdge(4, 13) == null);
  151. assertTrue("Edge-E-M was not deleted", mp.searchEdge(5, 13) == null);
  152. assertTrue("Edge-F-M was not deleted", mp.searchEdge(6, 13) == null);
  153. assertTrue("Edge-M-O was not deleted", mp.searchEdge(13, 16) == null);
  154. assertTrue("Edge-M-P was not deleted", mp.searchEdge(13, 17) == null);
  155. assertTrue("Edge-M-Q was not deleted", mp.searchEdge(13, 18) == null);
  156. assertTrue("Edge-M-R was not deleted", mp.searchEdge(13, 19) == null);
  157. assertTrue("Edge-M-S was not deleted", mp.searchEdge(13, 20) == null);
  158. }
  159. /**
  160. * Test copying, cutting and pasting Objects.
  161. */
  162. @Test
  163. public void testCutCopyPasteObjects() {
  164. HolonObject a = new HolonObject("A");
  165. HolonObject b = new HolonObject("B");
  166. HolonObject c = new HolonObject("C");
  167. a = new HolonObject(a);
  168. a.setPosition(1, 1);
  169. b = new HolonObject(b);
  170. b.setPosition(2, 2);
  171. c = new HolonObject(c);
  172. c.setPosition(3, 3);
  173. Edge edge1 = new Edge(a, b);
  174. Edge edge2 = new Edge(b, c);
  175. Edge edge3 = new Edge(c, a);
  176. controller.addNewObject(a);
  177. controller.addEdgeOnCanvas(edge1);
  178. controller.addEdgeOnCanvas(edge2);
  179. controller.addEdgeOnCanvas(edge3);
  180. assertTrue("Clipboard not empty", model.getClipboradObjects().isEmpty());
  181. model.getSelectedCpsObjects().add(a);
  182. model.getSelectedCpsObjects().add(b);
  183. model.getSelectedCpsObjects().add(c);
  184. //controller.copyObjects();
  185. //assertTrue("Clipboard empty", !model.getClipboradObjects().isEmpty());
  186. //assertTrue("Clipboard empty", !model.getClipboradObjects().isEmpty());
  187. controller.pasteObjects(new Point(1, 1));
  188. controller.addNewObject(a);
  189. controller.addNewObject(b);
  190. controller.addNewObject(c);
  191. controller.cutObjects();
  192. }
  193. }