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