PraktikumHolonsTestCanvasController.java 10 KB

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