PraktikumHolonsTestLoadAndStoreController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package tests;
  2. import classes.IdCounter;
  3. import ui.controller.CanvasController;
  4. import ui.controller.CategoryController;
  5. import ui.controller.GlobalController;
  6. import ui.controller.LoadController;
  7. import ui.controller.MultiPurposeController;
  8. import ui.controller.ObjectController;
  9. import ui.controller.SaveController;
  10. import ui.model.Model;
  11. /**
  12. * Tests for LoadAndStoreController.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class PraktikumHolonsTestLoadAndStoreController {
  17. protected PraktikumHolonsAdapter adapter;
  18. protected Model model;
  19. protected MultiPurposeController mp;
  20. protected CategoryController cg;
  21. protected CanvasController cvs;
  22. protected ObjectController obj;
  23. protected GlobalController global;
  24. protected SaveController storeController;
  25. protected LoadController loadController;
  26. protected IdCounter id;
  27. protected String path = System.getProperty("user.home") + "/HolonGUI/Test/";
  28. // /**
  29. // * Setup for the Tests.
  30. // */
  31. // @Before
  32. // public void setUp() {
  33. // adapter = new PraktikumHolonsAdapter();
  34. // model = new Model();
  35. // mp = new MultiPurposeController(model);
  36. // cg = new CategoryController(model, mp);
  37. // cvs = new CanvasController(model, mp);
  38. // obj = new ObjectController(model, mp);
  39. // storeController = new StoreController(model);
  40. // loadController = new LoadController(model, cg, cvs, obj, global, mp);
  41. // // cg.initCategories();
  42. // // obj.initHolonElements();
  43. // File file = new File(path);
  44. // file.mkdirs();
  45. // }
  46. //
  47. // /**
  48. // * minimal tests for saving.
  49. // */
  50. // @Test
  51. // public void testStoreMinimal() {
  52. // try {
  53. // File sav = new File(path + "TestSavMinimal.json");
  54. // storeController.writeSaveFile(sav.getAbsolutePath());
  55. // assertTrue("Save File was not created", new File(path + "TestSavMinimal.json").exists());
  56. //
  57. // File cat = new File(path + "TestCategoryMinimal.json");
  58. // //storeController.writeCategoryFile(cat.getAbsolutePath());
  59. // assertTrue("Category File was not created", new File(path + "TestCategoryMinimal.json").exists());
  60. //
  61. // File canvas = new File(path + "TestCanvasMinimal.json");
  62. // storeController.writeCanvasFile(canvas.getAbsolutePath());
  63. // assertTrue("Canvas File was not created", new File(path + "TestCanvasMinimal.json").exists());
  64. //
  65. // assertTrue("Non Existant File exist", !new File(path + "TestDummyMinimal.json").exists());
  66. //
  67. // } catch (IOException e) {
  68. // // TODO Auto-generated catch block
  69. // e.printStackTrace();
  70. // }
  71. // }
  72. //
  73. // /**
  74. // * basic tests for saving.
  75. // */
  76. // @Test
  77. // public void testStoreBasic() {
  78. //
  79. // for (int i = 1; i <= 26; i++) {
  80. // cg.addNewCategory(adapter.generate(i));
  81. // assertTrue("Number of Categories does not match", model.getCategories().size() == i + 3);
  82. // assertTrue("Category was not created", mp.searchCat(adapter.generate(i)) != null);
  83. // for (int j = 1; j <= 10; j++) {
  84. // cg.addNewHolonObject(mp.searchCat(adapter.generate(i)), adapter.generate(j),
  85. // new ArrayList<HolonElement>(), "");
  86. // assertTrue("Number of Objects does not match",
  87. // mp.searchCat(adapter.generate(i)).getObjects().size() == j);
  88. // assertTrue("Object was not created", mp.searchCat(adapter.generate(i)).getObjects().get(j - 1) != null);
  89. // }
  90. // }
  91. //
  92. // // here some Objects were dropped on the canvas
  93. // for (int i = 1; i <= 10; i++) {
  94. // cvs.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
  95. // assertTrue("Size of Objects on Canvas does not match", model.getObjectsOnCanvas().size() == i);
  96. // assertTrue("Object was not added", model.getObjectsOnCanvas().get(i - 1) != null
  97. // && model.getObjectsOnCanvas().get(i - 1).getObjName().equals("House"));
  98. // }
  99. //
  100. // HolonSwitch sw = new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch"));
  101. // sw.setPosition(77, 88);
  102. // cvs.addNewObject(sw);
  103. //
  104. // try {
  105. // File sav = new File(path + "TestSavBasic.json");
  106. // storeController.writeSaveFile(sav.getAbsolutePath());
  107. // assertTrue("Save File was not created", new File(path + "TestSavBasic.json").exists());
  108. //
  109. // File canvas = new File(path + "TestCanvasBasic.json");
  110. // storeController.writeCanvasFile(canvas.getAbsolutePath());
  111. // assertTrue("Canvas File was not created", new File(path + "TestCanvasBasic.json").exists());
  112. //
  113. // assertTrue("Non Existant File exist", !new File(path + "TestDummyMinimal.json").exists());
  114. //
  115. // } catch (IOException e) {
  116. // // TODO Auto-generated catch block
  117. // e.printStackTrace();
  118. // }
  119. // }
  120. //
  121. // /**
  122. // * advanced tests for saving.
  123. // */
  124. // @Test
  125. // public void testStoreAdvanced() {
  126. //
  127. // setGraphPoints((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"));
  128. //
  129. // int n = 0;
  130. // for (int i = 0; i < 10; i++) {
  131. // for (int j = 0; j < 10; j++) {
  132. // HolonObject h = new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House"));
  133. // h.setPosition(j * 50, i * 50);
  134. // cvs.addNewObject(h);
  135. // setGraphPoints(h);
  136. // for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  137. // if (!cps.equals(h))
  138. // cvs.addEdgeOnCanvas(new CpsEdge(h, cps));
  139. // }
  140. //
  141. // // vollständiger Graph
  142. // n = model.getObjectsOnCanvas().size();
  143. // assertTrue("Number of Edges does not Match", model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  144. // }
  145. // }
  146. //
  147. // try {
  148. // File sav = new File(path + "TestSavAdvanced.json");
  149. // storeController.writeSaveFile(sav.getAbsolutePath());
  150. // assertTrue("Save File was not created", new File(path + "TestSavAdvanced.json").exists());
  151. //
  152. // } catch (IOException e) {
  153. // // TODO Auto-generated catch block
  154. // e.printStackTrace();
  155. // }
  156. //
  157. // }
  158. //
  159. // /**
  160. // * minimal tests for loading a save file.
  161. // */
  162. // @Test
  163. // public void testLoadMinimal() {
  164. // try {
  165. // // Category Tests
  166. // Category building = mp.searchCat("Building");
  167. // assertTrue("Number of Categories does not match", model.getCategories().size() == 3);
  168. // assertTrue("TestFile was not found", new File(path + "TestCategoryMinimal.json").exists());
  169. // loadController.readJson(path + "TestCategoryMinimal.json");
  170. // assertTrue("Number of Categories does not match", model.getCategories().size() == 3);
  171. // // Tests if its same instance and if Name is the same
  172. // assertTrue("Same instance of Category:Building", building != mp.searchCat("Building")
  173. // && building.getName().equals(mp.searchCat("Building").getName()));
  174. //
  175. // // Canvas Tests.. basically nothing happens because nothing is on
  176. // // the Canvas
  177. // assertTrue("Number of Objects on Canvas does not match", model.getObjectsOnCanvas().size() == 0);
  178. // assertTrue("TestFile was not found", new File(path + "TestCanvasMinimal.json").exists());
  179. // loadController.readJson(path + "TestCanvasMinimal.json");
  180. // assertTrue("Number of Objects on Canvas does not match", model.getObjectsOnCanvas().size() == 0);
  181. //
  182. // // Save File tests basically both Test from Above Combined
  183. // building = mp.searchCat("Building");
  184. // assertTrue("Number of Objects in Energy does not Match", mp.searchCat("Energy").getObjects().size() == 1);
  185. // assertTrue("TestFile was not found", new File(path + "TestSavMinimal.json").exists());
  186. // loadController.readJson(path + "TestSavMinimal.json");
  187. // assertTrue("Number of Objects in Energy does not Match", mp.searchCat("Energy").getObjects().size() == 1);
  188. // assertTrue("Number of Objects on Canvas does not match", model.getObjectsOnCanvas().size() == 0);
  189. // assertTrue("Same instance of Category:Building", building != mp.searchCat("Building")
  190. // && building.getName().equals(mp.searchCat("Building").getName()));
  191. //
  192. // } catch (IOException e) {
  193. // // TODO Auto-generated catch block
  194. // e.printStackTrace();
  195. // }
  196. // }
  197. //
  198. // /**
  199. // * basic tests for loading a save file.
  200. // */
  201. // @Test
  202. // public void testLoadBasic() {
  203. // assertTrue("Non-Existant Category Exists", mp.searchCat("J") == null);
  204. // assertTrue("Non-Existant Category Exists", mp.searchCat("U") == null);
  205. // assertTrue("Non-Existant Category Exists", mp.searchCat("L") == null);
  206. // assertTrue("Non-Existant Category Exists", mp.searchCat("I") == null);
  207. // assertTrue("Non-Existant Category Exists", mp.searchCat("A") == null);
  208. // assertTrue("Non-Existant Category Exists", mp.searchCat("N") == null);
  209. //
  210. // try {
  211. // assertTrue("Objects on Canvas is not 0", model.getObjectsOnCanvas().size() == 0);
  212. // assertTrue("Canvas File was not found", new File(path + "TestCanvasBasic.json").exists());
  213. // loadController.readJson(path + "TestCanvasBasic.json");
  214. //
  215. // assertTrue("NUmber of Objects on Canvas does not match", model.getObjectsOnCanvas().size() == 11);
  216. // for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
  217. // assertTrue("Not instance of HolonObject", obj instanceof HolonObject || obj instanceof HolonSwitch);
  218. //
  219. // }
  220. //
  221. // assertTrue("NUmber of Categories not match", model.getCategories().size() == 3);
  222. // assertTrue("Canvas File was not found", new File(path + "TestSavBasic.json").exists());
  223. // loadController.readJson(path + "TestSavBasic.json");
  224. // assertTrue("NUmber of Categories not match", model.getCategories().size() == 29);
  225. // assertTrue("Existant Category dont Exists", mp.searchCat("J") != null);
  226. // assertTrue("Existant Category dont Exists", mp.searchCat("U") != null);
  227. // assertTrue("Existant Category dont Exists", mp.searchCat("L") != null);
  228. // assertTrue("Existant Category dont Exists", mp.searchCat("I") != null);
  229. // assertTrue("Existant Category dont Exists", mp.searchCat("A") != null);
  230. // assertTrue("Existant Category dont Exists", mp.searchCat("N") != null);
  231. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "A") != null);
  232. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "B") != null);
  233. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "C") != null);
  234. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "D") != null);
  235. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "E") != null);
  236. // assertTrue("Existant Object dont Exists", mp.searchCatObj(mp.searchCat("Z"), "F") != null);
  237. //
  238. // } catch (IOException e) {
  239. // // TODO Auto-generated catch block
  240. // e.printStackTrace();
  241. // }
  242. // }
  243. //
  244. // /**
  245. // * advanced tests for loading a save file.
  246. // */
  247. // @Test
  248. // public void testLoadAdvanced() {
  249. //
  250. // try {
  251. // assertTrue("Objects on Canvas is not 0", model.getObjectsOnCanvas().size() == 0);
  252. // assertTrue("Save File was not found", new File(path + "TestSavAdvanced.json").exists());
  253. // loadController.readJson(path + "TestSavAdvanced.json");
  254. // assertTrue("Objects on Canvas is not 100", model.getObjectsOnCanvas().size() == 100);
  255. //
  256. // int n = model.getObjectsOnCanvas().size();
  257. // assertTrue("Number of Edges does not Match", model.getEdgesOnCanvas().size() == (n * (n - 1)) / 2);
  258. // assertTrue("Element has no UnitGraph", !mp
  259. // .searchEle((HolonObject) model.getObjectsOnCanvas().get(77), "Fridge").getGraphPoints().isEmpty());
  260. // assertTrue("Points in UnitGraph does not Match",
  261. // mp.searchEle((HolonObject) model.getObjectsOnCanvas().get(77), "Fridge").getGraphPoints()
  262. // .size() == 3);
  263. //
  264. // } catch (IOException e) {
  265. // // TODO Auto-generated catch block
  266. // e.printStackTrace();
  267. // }
  268. // }
  269. //
  270. // /**
  271. // * Test for FileNotFound.
  272. // *
  273. // * @throws IOException
  274. // * FileNotFoundException
  275. // */
  276. // @Test(expected = FileNotFoundException.class)
  277. // public void testLoadException() throws IOException {
  278. // assertTrue("Save File was not found", !new File(path + "TestSavDummy.json").exists());
  279. // loadController.readJson(path + "TestSavDummy.json");
  280. // }
  281. //
  282. // /**
  283. // * sets the graph points in all elements of an Object.
  284. // *
  285. // * @param obj
  286. // * the Object
  287. // */
  288. // public void setGraphPoints(HolonObject obj) {
  289. // LinkedList<Point> list = new LinkedList<>();
  290. // list.add(new Point(0, 0));
  291. // list.add(new Point(125, 50));
  292. // list.add(new Point(249, 0));
  293. // UnitGraph u = new UnitGraph(model, null);
  294. // u.repaintWithNewElement(obj.getElements());
  295. // u.fillArrayofValue();
  296. // for (HolonElement ele : obj.getElements()) {
  297. // ele.setGraphPoints(list);
  298. // }
  299. //
  300. // }
  301. }