/test/sixthsense/CategoryTest.java

https://bitbucket.org/Skriglitz/sixthsense-java · Java · 237 lines · 219 code · 14 blank · 4 comment · 48 complexity · b66e4f091c343b01bd6802d9a20c2d13 MD5 · raw file

  1. /**
  2. * Authors: Aroop Ganguly, Dhaivat Pandya
  3. */
  4. package sixthsense;
  5. import static org.junit.Assert.*;
  6. import java.util.ArrayList;
  7. import org.junit.Test;
  8. public class CategoryTest {
  9. @Test
  10. public void testCtorForNameMemberInit() {
  11. Category c = new Category("c1");
  12. if (!c.getName().equals("c1")) {
  13. fail("Category constructor Name value incorrect");
  14. }
  15. }
  16. @Test
  17. public void testCtorForNameGestureMember() {
  18. Category c = null;
  19. try {
  20. c = new Category("circle", new Gesture("circle1",
  21. new ArrayList<PointR>()));
  22. }
  23. catch (Exception e) {
  24. fail("Category constructor Name value incorrect, Exception Details: "
  25. + e.getMessage());
  26. }
  27. try {
  28. if (c != null && !c.getName().equals("circle")
  29. && c.getExamples() != 1
  30. && !c.getGesture(0).getName().equals("circle1")) {
  31. fail("Category constructor Name value incorrect");
  32. }
  33. }
  34. catch (Exception e) {
  35. fail("Category constructor Name value incorrect, Exception Details: "
  36. + e.getMessage());
  37. }
  38. }
  39. @Test
  40. public void testParseName() {
  41. String testStr = "abc1121bcd";
  42. if (!Category.parseName(testStr).equals("abc")) {
  43. fail("ParseName is incorrect");
  44. }
  45. }
  46. @Test
  47. public void testName() {
  48. Category c = new Category("test");
  49. if (!c.getName().equals("test")) {
  50. fail("Name not retrived correctly");
  51. }
  52. }
  53. @Test
  54. public void testGetGesttureWithNullPrototypes() {
  55. Category c = new Category("c");
  56. try {
  57. c.getGesture(5);
  58. }
  59. catch (Exception e) {
  60. if (e.getMessage().indexOf("prototypes not initialised") != -1) {
  61. return;
  62. }
  63. else
  64. fail("get gesture mal functioning!!");
  65. }
  66. }
  67. @Test
  68. public void getGestureWithPrototypes() {
  69. ArrayList<Gesture> prototypes = new ArrayList<Gesture>();
  70. for (int i = 0; i < 5; i++) {
  71. Gesture g = new Gesture("test" + i, new ArrayList<PointR>());
  72. prototypes.add(g);
  73. }
  74. Category c = null;
  75. try {
  76. c = new Category("test", prototypes);
  77. }
  78. catch (Exception e1) {
  79. fail("exception in initialising a category with prototypes");
  80. }
  81. try {
  82. if (!c.getGesture(4).getName().equals("test4"))
  83. fail("get gesture not working properly");
  84. }
  85. catch (Exception e) {
  86. fail("get gesture not working properly");
  87. }
  88. }
  89. @Test
  90. public void testExample() {
  91. ArrayList<String> data = new ArrayList<String>(5);
  92. for (@SuppressWarnings("unused")
  93. String s : data) {
  94. s = "t";
  95. }
  96. Category c = null;
  97. try {
  98. c = new Category("test", data);
  99. }
  100. catch (Exception e) {
  101. if (e.getMessage().indexOf("Prototypes name") != -1) {
  102. return;
  103. }
  104. }
  105. if (!(c.getExamples() == data.size())) {
  106. fail("Example() not working properly");
  107. }
  108. }
  109. @Test
  110. public void testAddExample() {
  111. Category c = new Category("test");
  112. try {
  113. c.addExample(new Gesture("test1", null));
  114. }
  115. catch (Exception e) {
  116. if (e.getMessage().indexOf("Prototype name") != -1) {
  117. return;
  118. }
  119. else
  120. fail("error in adding examples." + e.getMessage());
  121. }
  122. if (c.getExamples() != 1) {
  123. fail("error in adding examples");
  124. }
  125. }
  126. @Test
  127. public void testAddExampleWithExisitngprototypes() {
  128. ArrayList<Gesture> prototypes = new ArrayList<Gesture>();
  129. for (int i = 0; i < 5; i++) {
  130. Gesture g = new Gesture("test" + i, new ArrayList<PointR>());
  131. prototypes.add(g);
  132. }
  133. Category c = null;
  134. try {
  135. c = new Category("test", prototypes);
  136. }
  137. catch (Exception e1) {
  138. fail("exception in initialising a category with prototypes");
  139. }
  140. try {
  141. c.addExample(new Gesture("test5", null));
  142. }
  143. catch (Exception e) {
  144. if (e.getMessage().indexOf("Prototype name") != -1) {
  145. return;
  146. }
  147. else
  148. fail("error in adding examples." + e.getMessage());
  149. }
  150. if (c.getExamples() != 6) {
  151. fail("error in adding examples");
  152. }
  153. try {
  154. c.addExample(new Gesture("test5", null));
  155. }
  156. catch (Exception e) {
  157. if (e.getMessage().indexOf("Prototype name") != -1) {
  158. return;
  159. }
  160. else
  161. fail("error in adding examples." + e.getMessage());
  162. }
  163. if (c.getExamples() != 6) {
  164. fail("error in adding examples");
  165. }
  166. }
  167. @Test
  168. public void testCtorForNameAndObjectMembersInit() {
  169. Category c = null;
  170. try {
  171. Gesture g = new Gesture("c1", null);
  172. c = new Category("c1", g);
  173. }
  174. catch (Exception e) {
  175. if (e.getMessage().toLowerCase().indexOf("prototype name") != -1) {
  176. return;// it is a valid system generated exception for not
  177. // initializing the prototypes
  178. }
  179. else
  180. fail("failed to add example, " + e.getMessage());
  181. }
  182. if (c == null || c.getExamples() == 0) {
  183. fail("no examples added in constructor");
  184. }
  185. else if (c.getExamples() > 1) {
  186. fail("garbage values for examples inside Category");
  187. }
  188. }
  189. @Test
  190. public void testCtorForGenericParamsInit() {
  191. Category c = null;
  192. ArrayList<String> data = new ArrayList<String>(5);
  193. for (@SuppressWarnings("unused")
  194. String s : data) {
  195. s = "t";
  196. }
  197. try {
  198. c = new Category("test", data);
  199. for (int i = 0; i < 5; i++) {
  200. try {
  201. if (c.getGesture(i) == null) {
  202. fail("Examples not added properly as Gestures in Contructor with generic params");
  203. break;
  204. }
  205. else if (!c.getGesture(i).getClass().equals(Gesture.class)) {
  206. fail("Examples not added properly as Gestures in Contructor with generic params");
  207. break;
  208. }
  209. }
  210. catch (Exception e) {
  211. if (e.getMessage().toLowerCase()
  212. .indexOf("prototypes not initialized") != -1) {
  213. }
  214. else {
  215. fail("Examples not added properly as Gestures in Contructor with generic params");
  216. }
  217. }
  218. }
  219. }
  220. catch (Exception e) {
  221. }
  222. }
  223. }