|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.test.storage; |
|
20 |
|
21 import com.nokia.mj.test.storage.utils.StorageSessionTestUtils; |
|
22 import com.nokia.mj.impl.storage.*; |
|
23 |
|
24 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
25 import j2meunit.framework.Test; |
|
26 import j2meunit.framework.TestCase; |
|
27 import j2meunit.framework.TestMethod; |
|
28 import j2meunit.framework.TestSuite; |
|
29 import java.util.Enumeration; |
|
30 |
|
31 /** |
|
32 * StorageSession connection test cases. See test methods for test case details. |
|
33 */ |
|
34 public class TestStorageEntry extends TestCase implements InstallerMain |
|
35 { |
|
36 /** |
|
37 * Directory for JavaStorage tests. |
|
38 */ |
|
39 private static final String iTestRoot = "./jstest"; |
|
40 |
|
41 /** |
|
42 * Directory for JavaStorage journal and temp files. |
|
43 */ |
|
44 private static final String iIsRoot = iTestRoot + "/js"; |
|
45 |
|
46 |
|
47 public void installerMain(String[] args) |
|
48 { |
|
49 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
50 |
|
51 suite.addTest(new TestStorageEntry("testAddAttribute", new TestMethod() |
|
52 { |
|
53 public void run(TestCase tc) |
|
54 { |
|
55 ((TestStorageEntry)tc).testAddAttribute(); |
|
56 } |
|
57 })); |
|
58 |
|
59 suite.addTest(new TestStorageEntry("testElements", new TestMethod() |
|
60 { |
|
61 public void run(TestCase tc) |
|
62 { |
|
63 ((TestStorageEntry)tc).testElements(); |
|
64 } |
|
65 })); |
|
66 |
|
67 suite.addTest(new TestStorageEntry("testGetAttribute", new TestMethod() |
|
68 { |
|
69 public void run(TestCase tc) |
|
70 { |
|
71 ((TestStorageEntry)tc).testGetAttribute(); |
|
72 } |
|
73 })); |
|
74 |
|
75 suite.addTest(new TestStorageEntry("testRemoveByObject", new TestMethod() |
|
76 { |
|
77 public void run(TestCase tc) |
|
78 { |
|
79 ((TestStorageEntry)tc).testRemoveByObject(); |
|
80 } |
|
81 })); |
|
82 |
|
83 suite.addTest(new TestStorageEntry("testRemoveByName", new TestMethod() |
|
84 { |
|
85 public void run(TestCase tc) |
|
86 { |
|
87 ((TestStorageEntry)tc).testRemoveByName(); |
|
88 } |
|
89 })); |
|
90 |
|
91 suite.addTest(new TestStorageEntry("testToString", new TestMethod() |
|
92 { |
|
93 public void run(TestCase tc) |
|
94 { |
|
95 ((TestStorageEntry)tc).testToString(); |
|
96 } |
|
97 })); |
|
98 |
|
99 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
100 } |
|
101 |
|
102 public TestStorageEntry() |
|
103 { |
|
104 } |
|
105 |
|
106 public TestStorageEntry(String aTestName, TestMethod aTestMethod) |
|
107 { |
|
108 super(aTestName, aTestMethod); |
|
109 } |
|
110 |
|
111 protected void setUp() |
|
112 { |
|
113 } |
|
114 |
|
115 protected void tearDown() |
|
116 { |
|
117 } |
|
118 |
|
119 /** |
|
120 * Test connection open |
|
121 * 1. Test adding null throws StorageException. |
|
122 * 2. Test add attribute. |
|
123 * 3. Test add second attribute. |
|
124 * 4. Test add same attribute again does not override previous. |
|
125 * 5. Test remove all. |
|
126 */ |
|
127 public void testAddAttribute() |
|
128 { |
|
129 StorageAttribute sa = null; |
|
130 StorageEntry entry = new StorageEntry(); |
|
131 |
|
132 // 1. Test add null. |
|
133 try |
|
134 { |
|
135 entry.addAttribute(sa); |
|
136 assertTrue("Adding null attribute does not throw exp.", false); |
|
137 } |
|
138 catch (StorageException se) |
|
139 { |
|
140 // PASSED |
|
141 } |
|
142 |
|
143 // 2. Add attribute |
|
144 try |
|
145 { |
|
146 StorageAttribute attr = new StorageAttribute( |
|
147 "Name", "Value", StorageAttribute.STRING_TYPE); |
|
148 entry.addAttribute(attr); |
|
149 |
|
150 assertTrue("Invalid size after adding one attr", |
|
151 entry.size() == 1); |
|
152 |
|
153 StorageAttribute added = entry.getAttribute(attr.getName()); |
|
154 assertTrue("Attribute was not succesfully added", |
|
155 attr.equals(added)); |
|
156 } |
|
157 catch (Throwable t) |
|
158 { |
|
159 assertTrue("Unexpected exp1: " + t.toString(), false); |
|
160 } |
|
161 |
|
162 // 3. Add second attribute |
|
163 try |
|
164 { |
|
165 StorageAttribute attr = new StorageAttribute( |
|
166 "Name2", "Value2", StorageAttribute.INT_TYPE); |
|
167 entry.addAttribute(attr); |
|
168 |
|
169 assertTrue("Invalid size after adding second attr", |
|
170 entry.size() == 2); |
|
171 |
|
172 StorageAttribute added = entry.getAttribute(attr.getName()); |
|
173 assertTrue("Attribute was not succesfully added", |
|
174 attr.equals(added)); |
|
175 } |
|
176 catch (Throwable t) |
|
177 { |
|
178 assertTrue("Unexpected exp2: " + t.toString(), false); |
|
179 } |
|
180 |
|
181 // 4. Add second attribute again |
|
182 try |
|
183 { |
|
184 StorageAttribute attr = new StorageAttribute( |
|
185 "Name3", "Value3", StorageAttribute.INT_TYPE); |
|
186 entry.addAttribute(attr); |
|
187 |
|
188 assertTrue("Invalid size after adding third attr", |
|
189 entry.size() == 3); |
|
190 |
|
191 StorageAttribute added = entry.getAttribute(attr.getName()); |
|
192 |
|
193 assertTrue("No match", attr.equals(added)); |
|
194 } |
|
195 catch (Throwable t) |
|
196 { |
|
197 assertTrue("Unexpected exp3: " + t.toString(), false); |
|
198 } |
|
199 |
|
200 // 5. Clean i.e. test remove all. |
|
201 entry.removeAll(); |
|
202 assertTrue(entry.size() == 0); |
|
203 } |
|
204 |
|
205 /** |
|
206 * Test connection open |
|
207 * 1. Test elements with empty entry. |
|
208 * 2. Test elements when one attribute. |
|
209 * 3. Test elements when two attributes |
|
210 * 4. Test elements after remove all. |
|
211 */ |
|
212 public void testElements() |
|
213 { |
|
214 StorageEntry entry = new StorageEntry(); |
|
215 |
|
216 // 1. Elements when empty entry. |
|
217 try |
|
218 { |
|
219 Enumeration attrs = entry.elements(); |
|
220 assertTrue(attrs.hasMoreElements() == false); |
|
221 } |
|
222 catch (Throwable t) |
|
223 { |
|
224 assertTrue("Unexpected exp1: " + t.toString(), false); |
|
225 } |
|
226 |
|
227 // 2. Test elements when one entry. |
|
228 try |
|
229 { |
|
230 StorageAttribute attr = new StorageAttribute( |
|
231 "Name", "Value", StorageAttribute.STRING_TYPE); |
|
232 entry.addAttribute(attr); |
|
233 |
|
234 assertTrue("Invalid size after adding one attr", |
|
235 entry.size() == 1); |
|
236 |
|
237 Enumeration attrs = entry.elements(); |
|
238 assertTrue(attrs.hasMoreElements() == true); |
|
239 } |
|
240 catch (Throwable t) |
|
241 { |
|
242 assertTrue("Unexpected exp2: " + t.toString(), false); |
|
243 } |
|
244 |
|
245 // 3. Test elements when two attrs. |
|
246 try |
|
247 { |
|
248 entry.removeAll(); |
|
249 StorageAttribute attr = new StorageAttribute( |
|
250 "Name", "Value", StorageAttribute.STRING_TYPE); |
|
251 entry.addAttribute(attr); |
|
252 |
|
253 StorageAttribute attr2 = new StorageAttribute( |
|
254 "Name2", "Value2", StorageAttribute.STRING_TYPE); |
|
255 entry.addAttribute(attr2); |
|
256 |
|
257 assertTrue("Invalid size after adding one attr", |
|
258 entry.size() == 2); |
|
259 |
|
260 Enumeration attrs = entry.elements(); |
|
261 assertTrue(attrs.hasMoreElements() == true); |
|
262 StorageAttribute sa = (StorageAttribute) attrs.nextElement(); |
|
263 assertTrue(sa.equals(attr)); |
|
264 |
|
265 assertTrue(attrs.hasMoreElements() == true); |
|
266 sa = (StorageAttribute) attrs.nextElement(); |
|
267 assertTrue(sa.equals(attr2)); |
|
268 |
|
269 assertTrue(attrs.hasMoreElements() == false); |
|
270 } |
|
271 catch (Throwable t) |
|
272 { |
|
273 assertTrue("Unexp exp3: " + t.toString(), false); |
|
274 } |
|
275 |
|
276 // 4. Test after remove all |
|
277 try |
|
278 { |
|
279 entry.removeAll(); |
|
280 Enumeration attrs = entry.elements(); |
|
281 assertTrue(attrs.hasMoreElements() == false); |
|
282 } |
|
283 catch (Throwable t) |
|
284 { |
|
285 assertTrue("Unexp exp4: " + t.toString(), false); |
|
286 } |
|
287 } |
|
288 |
|
289 /** |
|
290 * Test getAttribute. |
|
291 * 1. Get attribute empty entry returns null. |
|
292 * 2. Get attribute null name throws StorageException. |
|
293 * 3. Get attribute empty name throws StorageException. |
|
294 * 4. Get attribute no match returns null. |
|
295 * 5. Get attribute. |
|
296 * 6. Two attributes. Get attributes. |
|
297 * 7. Remove attribute. Get attribute. Other one returns null. |
|
298 * 8. Remove all. Get attribute returns null. |
|
299 */ |
|
300 public void testGetAttribute() |
|
301 { |
|
302 // 1. Get attribute empty entry returns null. |
|
303 StorageEntry entry = new StorageEntry(); |
|
304 assertTrue(entry.getAttribute("Name") == null); |
|
305 |
|
306 // 2. Get attribute null name throws StorageException. |
|
307 try |
|
308 { |
|
309 entry.getAttribute(null); |
|
310 assertTrue("getAttribute null name does not throw exp.", |
|
311 false); |
|
312 } |
|
313 catch (StorageException se) |
|
314 { |
|
315 // PASSED |
|
316 } |
|
317 catch (Throwable t) |
|
318 { |
|
319 assertTrue("Unexpected exp2: " + t.toString(), false); |
|
320 } |
|
321 |
|
322 // 3. Get attribute empty name throws StorageException. |
|
323 try |
|
324 { |
|
325 entry.getAttribute(""); |
|
326 assertTrue("getAttribute empty name does not throw exp.", |
|
327 false); |
|
328 } |
|
329 catch (StorageException se) |
|
330 { |
|
331 // PASSED |
|
332 } |
|
333 catch (Throwable t) |
|
334 { |
|
335 assertTrue("Unexpected exp3: " + t.toString(), false); |
|
336 } |
|
337 |
|
338 // 4. Get attribute no match returns null. |
|
339 entry = new StorageEntry(); |
|
340 StorageAttribute attribute = new StorageAttribute( |
|
341 "Name", |
|
342 "Value", |
|
343 StorageAttribute.INT_TYPE); |
|
344 |
|
345 entry.addAttribute(attribute); |
|
346 assertTrue(entry.size() == 1); |
|
347 |
|
348 try |
|
349 { |
|
350 assertTrue(entry.getAttribute("I Do not exists!") == null); |
|
351 } |
|
352 catch (Throwable t) |
|
353 { |
|
354 assertTrue("Unexpected exp4: " + t.toString(), false); |
|
355 } |
|
356 |
|
357 // 5. Get attribute. |
|
358 try |
|
359 { |
|
360 StorageAttribute resAttr = entry.getAttribute("Name"); |
|
361 assertTrue(attribute.equals(resAttr)); |
|
362 } |
|
363 catch (Throwable t) |
|
364 { |
|
365 assertTrue("Unexpected exp5: " + t.toString(), false); |
|
366 } |
|
367 |
|
368 // 6. Two attributes. Get attributes. |
|
369 StorageAttribute attribute2 = new StorageAttribute( |
|
370 "Name2", |
|
371 "Value2", |
|
372 StorageAttribute.INT_TYPE); |
|
373 |
|
374 entry.addAttribute(attribute2); |
|
375 assertTrue(entry.size() == 2); |
|
376 |
|
377 try |
|
378 { |
|
379 StorageAttribute resAttr = entry.getAttribute("Name"); |
|
380 assertTrue(attribute.equals(resAttr)); |
|
381 |
|
382 resAttr = entry.getAttribute("Name2"); |
|
383 assertTrue(attribute2.equals(resAttr)); |
|
384 } |
|
385 catch (Throwable t) |
|
386 { |
|
387 assertTrue("Unexpected exp6: " + t.toString(), false); |
|
388 } |
|
389 |
|
390 // 7. Remove attribute. Get attribute. Other one returns null. |
|
391 try |
|
392 { |
|
393 assertTrue(entry.removeAttribute(attribute)); |
|
394 assertTrue(entry.size() == 1); |
|
395 |
|
396 StorageAttribute resAttr = entry.getAttribute("Name"); |
|
397 assertTrue(resAttr == null); |
|
398 |
|
399 resAttr = entry.getAttribute("Name2"); |
|
400 assertTrue(attribute2.equals(resAttr)); |
|
401 } |
|
402 catch (Throwable t) |
|
403 { |
|
404 assertTrue("Unexpected exp7: " + t.toString(), false); |
|
405 } |
|
406 |
|
407 // 8. Remove all. Get attribute returns null. |
|
408 try |
|
409 { |
|
410 entry.removeAll(); |
|
411 assertTrue(entry.getAttribute("Name") == null); |
|
412 } |
|
413 catch (Throwable t) |
|
414 { |
|
415 assertTrue("Unexpected exp8: " + t.toString(), false); |
|
416 } |
|
417 } |
|
418 |
|
419 /** |
|
420 * Test atturibute removal. |
|
421 * 1. Test when empty entry returns false. |
|
422 * 2. Test remove null object returns false. |
|
423 * 3. Test remove. |
|
424 * 4. Test remove between. |
|
425 * 5. Test remove when attribute has null value. |
|
426 * 6. Test remove when attribute has empty value. |
|
427 */ |
|
428 public void testRemoveByObject() |
|
429 { |
|
430 // 1. Test when empty entry returns false. |
|
431 StorageEntry entry = new StorageEntry(); |
|
432 StorageAttribute attribute = new StorageAttribute( |
|
433 "Name", |
|
434 "Value", |
|
435 StorageAttribute.INT_TYPE); |
|
436 assertTrue(entry.removeAttribute(attribute) == false); |
|
437 |
|
438 // 2. Test remove null object returns false. |
|
439 StorageAttribute nullAttr = null; |
|
440 assertTrue(entry.removeAttribute(nullAttr) == false); |
|
441 |
|
442 |
|
443 // 3. Test remove. |
|
444 try |
|
445 { |
|
446 entry.addAttribute(attribute); |
|
447 assertTrue(entry.size() == 1); |
|
448 assertTrue(entry.removeAttribute(attribute)); |
|
449 assertTrue(entry.size() == 0); |
|
450 } |
|
451 catch (Throwable t) |
|
452 { |
|
453 assertTrue("Unexpected exp3: " + t.toString(), false); |
|
454 } |
|
455 |
|
456 // 4. Test remove between. |
|
457 try |
|
458 { |
|
459 entry.addAttribute(attribute); |
|
460 assertTrue(entry.size() == 1); |
|
461 |
|
462 StorageAttribute attribute2 = new StorageAttribute( |
|
463 "Name2", |
|
464 "Value2", |
|
465 StorageAttribute.INT_TYPE); |
|
466 |
|
467 entry.addAttribute(attribute2); |
|
468 assertTrue(entry.size() == 2); |
|
469 |
|
470 StorageAttribute attribute3 = new StorageAttribute( |
|
471 "Name3", |
|
472 "Value3", |
|
473 StorageAttribute.INT_TYPE); |
|
474 |
|
475 entry.addAttribute(attribute3); |
|
476 assertTrue(entry.size() == 3); |
|
477 |
|
478 assertTrue(entry.removeAttribute(attribute2)); |
|
479 assertTrue(entry.size() == 2); |
|
480 // Check both remaining attrs. |
|
481 assertTrue(entry.getAttribute("Name").equals(attribute)); |
|
482 assertTrue(entry.getAttribute("Name3").equals(attribute3)); |
|
483 } |
|
484 catch (Throwable t) |
|
485 { |
|
486 assertTrue("Unexp exp4: " + t.toString(), false); |
|
487 } |
|
488 |
|
489 // 5. Test remove when attribute has null value. |
|
490 try |
|
491 { |
|
492 entry.removeAll(); |
|
493 String name = null; |
|
494 StorageAttribute attrNullVal = new StorageAttribute( |
|
495 "Name2", |
|
496 name, |
|
497 StorageAttribute.NULL_TYPE); |
|
498 entry.addAttribute(attrNullVal); |
|
499 assertTrue(entry.size() == 1); |
|
500 |
|
501 // Null is converted to empty string by StorageAttribute. |
|
502 StorageAttribute refAttr = new StorageAttribute( |
|
503 "Name2", |
|
504 null, |
|
505 StorageAttribute.NULL_TYPE); |
|
506 |
|
507 assertTrue(entry.removeAttribute(refAttr)); |
|
508 assertTrue(entry.size() == 0); |
|
509 } |
|
510 catch (Throwable t) |
|
511 { |
|
512 assertTrue("Unexpected exp5: " + t.toString(), false); |
|
513 } |
|
514 |
|
515 // 6. Test remove when attribute has empty value. |
|
516 try |
|
517 { |
|
518 StorageAttribute attrEmptyVal = new StorageAttribute( |
|
519 "Name2", |
|
520 "", |
|
521 StorageAttribute.INT_TYPE); |
|
522 entry.addAttribute(attrEmptyVal); |
|
523 assertTrue(entry.size() == 1); |
|
524 |
|
525 assertTrue(entry.removeAttribute(attrEmptyVal)); |
|
526 assertTrue(entry.size() == 0); |
|
527 } |
|
528 catch (Throwable t) |
|
529 { |
|
530 assertTrue("Unexpected exp6: " + t.toString(), false); |
|
531 } |
|
532 } |
|
533 |
|
534 /** |
|
535 * Test attribute removal by name. |
|
536 * 1. Test when empty entry returns false. |
|
537 * 2. Test remove by null name returns false. |
|
538 * 3. Test remove by empty name returns false. |
|
539 * 4. Test remove. |
|
540 */ |
|
541 public void testRemoveByName() |
|
542 { |
|
543 // 1. Test when empty entry returns false. |
|
544 StorageEntry entry = new StorageEntry(); |
|
545 StorageAttribute attribute = new StorageAttribute( |
|
546 "Name", |
|
547 "Value", |
|
548 StorageAttribute.INT_TYPE); |
|
549 |
|
550 assertTrue(entry.removeAttribute(attribute) == false); |
|
551 |
|
552 // 2. Test remove by null name returns false. |
|
553 String name = null; |
|
554 assertTrue(entry.removeAttribute(name) == false); |
|
555 |
|
556 // 3. Test remove by empty name returns false. |
|
557 name = ""; |
|
558 assertTrue(entry.removeAttribute(name) == false); |
|
559 |
|
560 // 4. Test remove. |
|
561 try |
|
562 { |
|
563 name = "Name"; |
|
564 entry.addAttribute(attribute); |
|
565 assertTrue(entry.size() == 1); |
|
566 assertTrue(entry.removeAttribute(name)); |
|
567 assertTrue(entry.size() == 0); |
|
568 } |
|
569 catch (Throwable t) |
|
570 { |
|
571 assertTrue("Unexpected exp4: " + t.toString(), false); |
|
572 } |
|
573 |
|
574 entry.removeAll(); |
|
575 } |
|
576 |
|
577 /** |
|
578 * Test toString method. |
|
579 * 1. Test empty. |
|
580 * 2. Test one attribute. |
|
581 * 3. Test two attributes. |
|
582 */ |
|
583 public void testToString() |
|
584 { |
|
585 // 1. Test empty. |
|
586 StorageEntry entry = new StorageEntry(); |
|
587 String refString = "{}"; |
|
588 assertTrue(entry.toString().equals(refString)); |
|
589 |
|
590 // 2. Test one attribute. |
|
591 StorageAttribute attr = new StorageAttribute( |
|
592 "Na", "Va", StorageAttribute.INT_TYPE); |
|
593 entry.addAttribute(attr); |
|
594 |
|
595 refString = "{Na=Na='Va' Type: 1}"; |
|
596 assertTrue(entry.toString().equals(refString)); |
|
597 |
|
598 // 3. Test two attributes. |
|
599 attr = new StorageAttribute( |
|
600 "Na2", "Va2", StorageAttribute.STRING_TYPE); |
|
601 entry.addAttribute(attr); |
|
602 |
|
603 refString = "{Na=Na='Va' Type: 1, Na2=Na2='Va2' Type: 2}"; |
|
604 assertTrue(entry.toString().equals(refString)); |
|
605 } |
|
606 } |