PraktikumHolonsTestClasses.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package tests;
  2. import classes.*;
  3. import static org.junit.Assert.assertTrue;
  4. import java.awt.Color;
  5. import java.util.ArrayList;
  6. import org.junit.Test;
  7. /**
  8. * Test for the Classes.
  9. *
  10. * @author Gruppe14
  11. */
  12. public class PraktikumHolonsTestClasses {
  13. /**
  14. * Test for the Categories.
  15. */
  16. @Test
  17. public void testCategory() {
  18. Category test1 = new Category("Test1");
  19. Category test2 = new Category("Test2");
  20. HolonObject obj1 = new HolonObject("Test Object");
  21. ArrayList<AbstractCpsObject> arr = new ArrayList<>();
  22. assertTrue("Name not correct", test1.getName().equals("Test1"));
  23. assertTrue("Name not correct", test2.getName().equals("Test2"));
  24. assertTrue("Name should not be the same", !test1.getName().equals(test2.getName()));
  25. assertTrue("Should be empty", test1.getObjects().size() == 0);
  26. test1.getObjects().add(obj1);
  27. assertTrue("Should not be empty", test1.getObjects().size() == 1);
  28. assertTrue("Should be empty", test2.getObjects().size() == 0);
  29. test1.setObjects(arr);
  30. assertTrue("Should be empty", test1.getObjects().size() == 0);
  31. arr.add(obj1);
  32. test2.setObjects(arr);
  33. arr = new ArrayList<>();
  34. assertTrue("Shoud be empty", arr.isEmpty());
  35. arr = test2.getObjects();
  36. assertTrue("Shoud not be empty", !arr.isEmpty());
  37. }
  38. /**
  39. * Test for HolonObject.
  40. */
  41. @Test
  42. public void testHolonObject() {
  43. HolonObject test1 = new HolonObject("Test1");
  44. HolonObject test2 = new HolonObject("Test2");
  45. HolonObject test3 = new HolonObject(test1);
  46. HolonElement ele = new HolonElement("Element", 1, 10);
  47. ArrayList<HolonElement> arr = new ArrayList<>();
  48. assertTrue("Should be Empty", test1.getElements().isEmpty());
  49. test1.setElements(arr);
  50. assertTrue("Should be Empty", test1.getElements().isEmpty());
  51. arr.add(ele);
  52. arr.add(ele);
  53. assertTrue("Should be Empty", test2.getElements().isEmpty());
  54. assertTrue("Current Energy not corrent", test2.getCurrentEnergy() == 0);
  55. test2.setElements(arr);
  56. assertTrue("Should not be Empty", !test2.getElements().isEmpty());
  57. assertTrue("Current Energy not corrent", test2.getCurrentEnergy() == 20);
  58. assertTrue("Current Energy not corrent", test2.getCurrentEnergyAtTimeStep(20) == 20);
  59. String str = test2.toStringElements();
  60. assertTrue("String not corrent", str.equals("Element, Element"));
  61. test2.deleteElement(0);
  62. test2.addElements(ele);
  63. assertTrue("Current Energy not corrent", test2.getCurrentEnergy() == 20);
  64. assertTrue("Should be Empty", test3.getElements().isEmpty());
  65. test3.setElements(test2.copyElements(test2.getElements()));
  66. assertTrue("Should be Empty", !test3.getElements().isEmpty());
  67. assertTrue("Should be state 0", test3.getState() == 0);
  68. test3.setState(1);
  69. assertTrue("Should be state 1", test3.getState() == 1);
  70. test3.setState(2);
  71. assertTrue("Should be state 2", test3.getState() == 2);
  72. test3.setState(3);
  73. assertTrue("Should be state 3", test3.getState() == 3);
  74. test3.setState(4);
  75. assertTrue("Should be state 4", test3.getState() == 4);
  76. assertTrue("Element not Found", test3.searchElement("Element") != null);
  77. test1.setElements(new ArrayList<HolonElement>());
  78. assertTrue("Not Empty", !test1.checkIfPartiallySupplied(1));
  79. test3.addElements(new HolonElement("Element2", 1, -10));
  80. assertTrue("Not Partially Supplied", test3.checkIfPartiallySupplied(1));
  81. test3.addElements(new HolonElement("Element2", 2, -10));
  82. assertTrue("Not Partially Supplied", test3.checkIfPartiallySupplied(1));
  83. test1.addElements(new HolonElement("Element2", 2, -10));
  84. assertTrue("minSupply < 0", !test1.checkIfPartiallySupplied(1));
  85. Color color = test3.getColor();
  86. test3.setColor(new Color(0, 255, 255));
  87. assertTrue(color.getBlue() != test3.getColor().getBlue());
  88. test3.addElements(new HolonElement("Element3", 3, 50));
  89. test3.setState();
  90. assertTrue("Should be state 3", test3.getState() == 3);
  91. test1.setState();
  92. assertTrue("Should be state 0", test1.getState() == 0);
  93. test2.setState();
  94. test2.setBorderColor(color);
  95. assertTrue("Color not Same", color == test2.getBorderColor());
  96. }
  97. /**
  98. * Test for HolonSwitch.
  99. */
  100. // @Test
  101. // public void testHolonSwitch() {
  102. // HolonSwitch test1 = new HolonSwitch("Test1");
  103. // HolonSwitch test2 = new HolonSwitch(test1);
  104. //
  105. // assertTrue("Manuel Mode is on", !test2.getManualMode());
  106. // test2.switchState();
  107. // assertTrue(test2.getState());
  108. // assertTrue(test2.getState(1));
  109. // test2.setManualMode(true);
  110. // test2.switchState();
  111. // test2.switchState();
  112. // assertTrue(test2.getState());
  113. // assertTrue(test2.getState(1));
  114. // assertTrue("Manuel Mode is off", test2.getManualMode());
  115. // assertTrue("ManuelActive is off", test2.getActiveManual());
  116. // test2.switchState();
  117. // assertTrue("ManuelActive is on", !test2.getActiveManual());
  118. // test2.switchState();
  119. // assertTrue("ManuelActive is off", test2.getActiveManual());
  120. // assertTrue(test1.getGraphPoints() != test2.getGraphPoints());
  121. // test2.setGraphPoints(test1.getGraphPoints());
  122. // assertTrue(test1.getGraphPoints() == test2.getGraphPoints());
  123. // }
  124. /**
  125. * Test for CpsEdge.
  126. */
  127. @Test
  128. public void testCpsEdge() {
  129. CpsNode node1 = new CpsNode("Node1");
  130. CpsNode node2 = new CpsNode("Node2");
  131. CpsNode node3 = new CpsNode("Node3");
  132. CpsEdge edge1 = new CpsEdge(node1, node2, 100);
  133. CpsEdge edge2 = new CpsEdge(node2, node3);
  134. assertTrue("Flow not 0", edge1.getFlow() == 0);
  135. edge1.setFlow(50);
  136. assertTrue("Capacity not right", edge1.getCapacity() == 100);
  137. edge2.setFlow(50);
  138. edge1.setCapacity(200);
  139. assertTrue("Flow was not changed", edge1.getFlow() == 50);
  140. assertTrue("Capacity not right", edge1.getCapacity() == 200);
  141. assertTrue("line broken", edge2.getState());
  142. edge2.calculateState();
  143. assertTrue("line broken", edge2.getState());
  144. edge2.setFlow(200);
  145. edge2.calculateState();
  146. assertTrue("line not broken", !edge2.getState());
  147. edge1.setCapacity(-1);
  148. edge1.calculateState();
  149. edge1.setCapacity(500);
  150. edge1.calculateState();
  151. node1 = (CpsNode) edge1.getB();
  152. node2 = (CpsNode) edge2.getA();
  153. assertTrue("Not Same", node1 == node2);
  154. assertTrue("State not right", edge1.getState());
  155. edge1.setState(false);
  156. assertTrue("State not right", !edge1.getState());
  157. edge2.setTags(new ArrayList<Integer>());
  158. edge1.setTags(new ArrayList<Integer>());
  159. assertTrue("Tags not Empty", edge2.getTags().isEmpty());
  160. edge2.addTag(1);
  161. assertTrue("Tags not Empty", edge1.getTags().isEmpty());
  162. assertTrue("Tags Empty", !edge2.getTags().isEmpty());
  163. edge1.setTags(edge2.getTags());
  164. assertTrue("Tags Empty", !edge1.getTags().isEmpty());
  165. }
  166. /**
  167. * Test for HolonElement.
  168. */
  169. @Test
  170. public void testHolonElement() {
  171. HolonElement ele1 = new HolonElement("TV", 2, -20);
  172. HolonElement ele2 = new HolonElement("Fridge", 1, -50);
  173. HolonElement ele3 = new HolonElement(ele2);
  174. assertTrue("Array not empty", ele1.getEnergyAt()[0] == -20);
  175. assertTrue("Array not empty", ele1.getEnergyAt()[2] == -20);
  176. ele1.setEnergyAt(2, -10);
  177. assertTrue("Array not empty", ele1.getEnergyAt()[2] == -10);
  178. assertTrue("Name not correct", ele1.getEleName().equals("TV"));
  179. ele1.setEleName(ele2.getEleName());
  180. assertTrue("Name not correct", ele1.getEleName().equals("Fridge"));
  181. assertTrue("Amount not correct", ele2.getAmount() == 1);
  182. ele2.setAmount(5);
  183. assertTrue("Amount not correct", ele2.getAmount() == 5);
  184. assertTrue("Total Energy not Correct", ele2.getTotalEnergy() == ele2.getAmount() * ele2.getEnergy());
  185. assertTrue("Sign not correct", ele2.getSign() == '-');
  186. // ele3.setSav("CVS");
  187. // assertTrue("SAV not correct", ele3.getSav().equals("CVS"));
  188. }
  189. /**
  190. * Test for Position.
  191. */
  192. @Test
  193. public void testPosition() {
  194. Position pos1 = new Position(100, 200);
  195. Position pos2 = new Position();
  196. assertTrue("Wrong coordinates", pos1.x == 100 && pos1.y == 200);
  197. assertTrue("Are the Same", pos1.x != pos2.x);
  198. assertTrue("not (-1,-1)", pos2.x == -1 && pos2.y == -1);
  199. }
  200. /**
  201. * Test SubNet.
  202. */
  203. @Test
  204. public void testSubNet() {
  205. PraktikumHolonsAdapter adapter = new PraktikumHolonsAdapter();
  206. ArrayList<HolonObject> obj = new ArrayList<>();
  207. ArrayList<CpsEdge> edge = new ArrayList<>();
  208. ArrayList<HolonSwitch> sw = new ArrayList<>();
  209. assertTrue("Not Empty", obj.isEmpty());
  210. assertTrue("Not Empty", sw.isEmpty());
  211. assertTrue("Not Empty", edge.isEmpty());
  212. for (int i = 1; i < 10; i++) {
  213. HolonObject o = new HolonObject(adapter.generate(i));
  214. HolonSwitch s = new HolonSwitch(adapter.generate(i));
  215. o = new HolonObject(o);
  216. s = new HolonSwitch(s);
  217. obj.add(o);
  218. sw.add(s);
  219. edge.add(new CpsEdge(o, s));
  220. }
  221. SubNet sub = new SubNet(obj, edge, sw);
  222. assertTrue("Empty", !sub.getObjects().isEmpty() && sub.getObjects().size() == 9);
  223. assertTrue("Empty", !sub.getSwitches().isEmpty() && sub.getSwitches().size() == 9);
  224. assertTrue("Empty", !sub.getEdges().isEmpty() && sub.getEdges().size() == 9);
  225. assertTrue("Wrong Obj", sub.getObjects().get(5).getObjName().equals("F"));
  226. }
  227. }