|
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.impl.storage; |
|
20 |
|
21 import com.nokia.mj.test.storage.utils.StorageSessionTestUtils; |
|
22 |
|
23 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
24 import j2meunit.framework.Test; |
|
25 import j2meunit.framework.TestCase; |
|
26 import j2meunit.framework.TestMethod; |
|
27 import j2meunit.framework.TestSuite; |
|
28 |
|
29 /** |
|
30 * StorageAttirubte test cases. See test methods for test case details. |
|
31 */ |
|
32 public class TestStorageAttribute extends TestCase |
|
33 implements InstallerMain, StorageNames |
|
34 { |
|
35 /** |
|
36 * Directory for JavaStorage tests. |
|
37 */ |
|
38 private static final String iTestRoot = "./jstest"; |
|
39 |
|
40 /** |
|
41 * Directory for JavaStorage journal and temp files. |
|
42 */ |
|
43 private static final String iIsRoot = iTestRoot + "/js"; |
|
44 |
|
45 private StorageSession iSession = null; |
|
46 private StorageSessionTestUtils iJtu = null; |
|
47 |
|
48 public void installerMain(String[] args) |
|
49 { |
|
50 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
51 |
|
52 suite.addTest(new TestStorageAttribute("testNameValueConstructor", new TestMethod() |
|
53 { |
|
54 public void run(TestCase tc) |
|
55 { |
|
56 ((TestStorageAttribute)tc).testNameValueConstructor(); |
|
57 } |
|
58 })); |
|
59 |
|
60 suite.addTest(new TestStorageAttribute("testNameValueTypeConstructor", new TestMethod() |
|
61 { |
|
62 public void run(TestCase tc) |
|
63 { |
|
64 ((TestStorageAttribute)tc).testNameValueTypeConstructor(); |
|
65 } |
|
66 })); |
|
67 |
|
68 suite.addTest(new TestStorageAttribute("testAttribute", new TestMethod() |
|
69 { |
|
70 public void run(TestCase tc) |
|
71 { |
|
72 ((TestStorageAttribute)tc).testAttribute(); |
|
73 } |
|
74 })); |
|
75 |
|
76 suite.addTest(new TestStorageAttribute("testtoString", new TestMethod() |
|
77 { |
|
78 public void run(TestCase tc) |
|
79 { |
|
80 ((TestStorageAttribute)tc).testtoString(); |
|
81 } |
|
82 })); |
|
83 |
|
84 suite.addTest(new TestStorageAttribute("testEscape", new TestMethod() |
|
85 { |
|
86 public void run(TestCase tc) |
|
87 { |
|
88 ((TestStorageAttribute)tc).testEscape(); |
|
89 } |
|
90 })); |
|
91 |
|
92 suite.addTest(new TestStorageAttribute("testEscapeToStorage", new TestMethod() |
|
93 { |
|
94 public void run(TestCase tc) |
|
95 { |
|
96 ((TestStorageAttribute)tc).testEscapeToStorage(); |
|
97 } |
|
98 })); |
|
99 |
|
100 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
101 } |
|
102 |
|
103 public TestStorageAttribute() |
|
104 { |
|
105 } |
|
106 |
|
107 public TestStorageAttribute(String aTestName, TestMethod aTestMethod) |
|
108 { |
|
109 super(aTestName, aTestMethod); |
|
110 } |
|
111 |
|
112 protected void setUp() |
|
113 { |
|
114 iSession = StorageFactory.createSession(); |
|
115 iJtu = new StorageSessionTestUtils(); |
|
116 } |
|
117 |
|
118 protected void tearDown() |
|
119 { |
|
120 if (iSession != null) |
|
121 { |
|
122 try |
|
123 { |
|
124 iSession.destroySession(); |
|
125 } |
|
126 catch (StorageException se) |
|
127 { |
|
128 // No can do |
|
129 System.out.println("TearDown failed: " + se.toString()); |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 /** |
|
135 * Test constructor with name and value. |
|
136 * 2. Test with Name and Value. |
|
137 * 3. Test Name null throws StorageException |
|
138 * 4. Test Name "" throws StorageException |
|
139 * 5. Test value null. Type is not set SE thrown. |
|
140 * 6. Test value "". |
|
141 * 7. Test one len name and value. |
|
142 * 8. Test value null, type STRING throws StorageException. |
|
143 */ |
|
144 public void testNameValueConstructor() |
|
145 { |
|
146 StorageAttribute sa = new StorageAttribute("N/A", "N/A"); |
|
147 // 2. Test with Name and Value |
|
148 try |
|
149 { |
|
150 String name = "AttrName"; |
|
151 String value = "AttrValue"; |
|
152 sa = new StorageAttribute(name, value); |
|
153 |
|
154 assertTrue(name.equals(sa.getName())); |
|
155 assertTrue(value.equals(sa.getValue())); |
|
156 // Default value is set if not defined. |
|
157 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
158 } |
|
159 catch (StorageException se) |
|
160 { |
|
161 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
162 } |
|
163 |
|
164 // 3. Test null name |
|
165 try |
|
166 { |
|
167 String name = null; |
|
168 String value = "AttrValue"; |
|
169 sa = new StorageAttribute(name, value); |
|
170 |
|
171 assertTrue("Null name does not throw exception", false); |
|
172 } |
|
173 catch (StorageException se) |
|
174 { |
|
175 // PASSED |
|
176 } |
|
177 |
|
178 // 4. Test empty name |
|
179 try |
|
180 { |
|
181 String name = ""; |
|
182 String value = "AttrValue"; |
|
183 sa = new StorageAttribute(name, value); |
|
184 |
|
185 assertTrue("Empty name does not throw exception", false); |
|
186 } |
|
187 catch (StorageException se) |
|
188 { |
|
189 // PASSED |
|
190 } |
|
191 |
|
192 // 5. Test null value. |
|
193 try |
|
194 { |
|
195 String name = "AttrName"; |
|
196 String value = null; |
|
197 sa = new StorageAttribute(name, value); |
|
198 assertTrue("No exp when value Null", false); |
|
199 } |
|
200 catch (StorageException se) |
|
201 { |
|
202 // PASSED |
|
203 } |
|
204 catch (Throwable t) |
|
205 { |
|
206 assertTrue("Wrong exp thrown: " + t.toString(), false); |
|
207 |
|
208 } |
|
209 |
|
210 // 6. Test empty value |
|
211 try |
|
212 { |
|
213 String name = "AttrName"; |
|
214 String value = ""; |
|
215 sa = new StorageAttribute(name, value); |
|
216 |
|
217 assertTrue(name.equals(sa.getName())); |
|
218 assertTrue(sa.getValue() == ""); |
|
219 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
220 } |
|
221 catch (StorageException se) |
|
222 { |
|
223 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
224 } |
|
225 |
|
226 // 7. Test one len arguments |
|
227 try |
|
228 { |
|
229 String name = "A"; |
|
230 String value = "V"; |
|
231 sa = new StorageAttribute(name, value); |
|
232 |
|
233 assertTrue(name.equals(sa.getName())); |
|
234 assertTrue(value.equals(sa.getValue())); |
|
235 // Default value is set if not defined. |
|
236 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
237 } |
|
238 catch (StorageException se) |
|
239 { |
|
240 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
241 } |
|
242 |
|
243 // 8. Test value null, type STRING throws StorageException. |
|
244 try |
|
245 { |
|
246 String name = "A"; |
|
247 String value = null; |
|
248 sa = new StorageAttribute(name, value); |
|
249 assertTrue("No exp thrown when null value, string type", false); |
|
250 } |
|
251 catch (StorageException se) |
|
252 { |
|
253 // PASSED |
|
254 } |
|
255 catch (Throwable t) |
|
256 { |
|
257 assertTrue("Wrong exception thrown: " + t.toString(), false); |
|
258 } |
|
259 |
|
260 } |
|
261 |
|
262 /** |
|
263 * Test constructor with name, value and type. |
|
264 * 1. Test with Name, Value and Type. |
|
265 * 2. Test Name null throws StorageException |
|
266 * 3. Test Name "" throws StorageException |
|
267 * 4. Test value null. If value is not set null is returned. |
|
268 * 5. Test value "". |
|
269 * 6. Test TYPE is set. |
|
270 * 7. Test one len arguments. |
|
271 * 8. Test not supported type throws StorageException. |
|
272 */ |
|
273 public void testNameValueTypeConstructor() |
|
274 { |
|
275 StorageAttribute sa = null; |
|
276 |
|
277 // 1. Test with Name, Value and Type. |
|
278 try |
|
279 { |
|
280 String name = "AttrName"; |
|
281 String value = "AttrValue"; |
|
282 sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE); |
|
283 |
|
284 assertTrue(name.equals(sa.getName())); |
|
285 assertTrue(value.equals(sa.getValue())); |
|
286 assertTrue(sa.getType() == StorageAttribute.INT_TYPE); |
|
287 } |
|
288 catch (StorageException se) |
|
289 { |
|
290 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
291 } |
|
292 |
|
293 // 2. Test null name |
|
294 try |
|
295 { |
|
296 String name = null; |
|
297 String value = "AttrValue"; |
|
298 sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE); |
|
299 |
|
300 assertTrue("Null name does not throw exception", false); |
|
301 } |
|
302 catch (StorageException se) |
|
303 { |
|
304 // PASSED |
|
305 } |
|
306 |
|
307 // 3. Test empty name |
|
308 try |
|
309 { |
|
310 String name = ""; |
|
311 String value = "AttrValue"; |
|
312 sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE); |
|
313 |
|
314 assertTrue("Empty name does not throw exception", false); |
|
315 } |
|
316 catch (StorageException se) |
|
317 { |
|
318 // PASSED |
|
319 } |
|
320 |
|
321 // 4. Test null value. |
|
322 try |
|
323 { |
|
324 String name = "AttrName"; |
|
325 String value = null; |
|
326 sa = new StorageAttribute(name, value, StorageAttribute.NULL_TYPE); |
|
327 |
|
328 assertTrue(name.equals(sa.getName())); |
|
329 assertTrue(sa.getValue() == null); |
|
330 assertTrue(sa.getType() == StorageAttribute.NULL_TYPE); |
|
331 } |
|
332 catch (StorageException se) |
|
333 { |
|
334 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
335 } |
|
336 |
|
337 // 5. Test empty value |
|
338 try |
|
339 { |
|
340 String name = "AttrName"; |
|
341 String value = ""; |
|
342 sa = new StorageAttribute(name, value, StorageAttribute.STRING_TYPE); |
|
343 |
|
344 assertTrue(name.equals(sa.getName())); |
|
345 assertTrue(sa.getValue() == ""); |
|
346 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
347 } |
|
348 catch (StorageException se) |
|
349 { |
|
350 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
351 } |
|
352 |
|
353 // 6. and 7. Test one len arguments. |
|
354 try |
|
355 { |
|
356 String name = "A"; |
|
357 String value = "V"; |
|
358 sa = new StorageAttribute( |
|
359 name, value, StorageAttribute.STRING_TYPE); |
|
360 |
|
361 assertTrue(name.equals(sa.getName())); |
|
362 assertTrue(value.equals(sa.getValue())); |
|
363 // Default value is set if not defined. |
|
364 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
365 } |
|
366 catch (StorageException se) |
|
367 { |
|
368 assertTrue("Attribute creation failed: " + se.getMessage(), false); |
|
369 } |
|
370 |
|
371 // 8. Test not supported type throws StorageException. |
|
372 try |
|
373 { |
|
374 String name = NAME; |
|
375 String value = "AttrValue"; |
|
376 sa = new StorageAttribute(name, value, 8); |
|
377 |
|
378 assertTrue("Not defined type do not throw exp", false); |
|
379 } |
|
380 catch (StorageException se) |
|
381 { |
|
382 // PASSED |
|
383 } |
|
384 } |
|
385 |
|
386 /** |
|
387 * Test constructor with name, value and type. |
|
388 * Getters are used to verify values. |
|
389 * |
|
390 * 1. Test with Name, Value and Type. |
|
391 * 2. Test Name null throws StorageException |
|
392 * 3. Test Name "" throws StorageException |
|
393 * 4. Test value null. If value is not set null is returned. |
|
394 * 5. Test value "". |
|
395 * 6. Test one len arguments. |
|
396 */ |
|
397 public void testAttribute() |
|
398 { |
|
399 StorageAttribute sa = new StorageAttribute("N/A", "N/A"); |
|
400 |
|
401 // 1. Test with Name, Value and Type. |
|
402 try |
|
403 { |
|
404 String name = "AttrName"; |
|
405 String value = "AttrValue"; |
|
406 sa.setAttribute(name, value, StorageAttribute.INT_TYPE); |
|
407 |
|
408 assertTrue(name.equals(sa.getName())); |
|
409 assertTrue(value.equals(sa.getValue())); |
|
410 assertTrue(sa.getType() == StorageAttribute.INT_TYPE); |
|
411 } |
|
412 catch (StorageException se) |
|
413 { |
|
414 assertTrue("Attribute set failed: " + se.getMessage(), false); |
|
415 } |
|
416 |
|
417 // 2. Test null name |
|
418 try |
|
419 { |
|
420 String name = null; |
|
421 String value = "AttrValue"; |
|
422 sa.setAttribute(name, value, StorageAttribute.INT_TYPE); |
|
423 |
|
424 assertTrue("Null name does not throw exception", false); |
|
425 } |
|
426 catch (StorageException se) |
|
427 { |
|
428 // PASSED |
|
429 } |
|
430 |
|
431 // 3. Test empty name |
|
432 try |
|
433 { |
|
434 String name = ""; |
|
435 String value = "AttrValue"; |
|
436 sa.setAttribute(name, value, StorageAttribute.INT_TYPE); |
|
437 |
|
438 assertTrue("Empty name does not throw exception", false); |
|
439 } |
|
440 catch (StorageException se) |
|
441 { |
|
442 // PASSED |
|
443 } |
|
444 |
|
445 // 4. Test null value. |
|
446 try |
|
447 { |
|
448 String name = "AttrName"; |
|
449 String value = null; |
|
450 sa.setAttribute(name, value, StorageAttribute.NULL_TYPE); |
|
451 |
|
452 assertTrue(name.equals(sa.getName())); |
|
453 assertTrue(sa.getValue() == null); |
|
454 assertTrue(sa.getType() == StorageAttribute.NULL_TYPE); |
|
455 } |
|
456 catch (StorageException se) |
|
457 { |
|
458 assertTrue("Attribute set failed: " + se.getMessage(), false); |
|
459 } |
|
460 |
|
461 // 5. Test empty value. |
|
462 try |
|
463 { |
|
464 String name = "AttrName"; |
|
465 String value = ""; |
|
466 sa.setAttribute(name, value, StorageAttribute.INT_TYPE); |
|
467 |
|
468 assertTrue(name.equals(sa.getName())); |
|
469 assertTrue(sa.getValue() == ""); |
|
470 assertTrue(sa.getType() == StorageAttribute.INT_TYPE); |
|
471 } |
|
472 catch (StorageException se) |
|
473 { |
|
474 assertTrue("Attribute set failed: " + se.getMessage(), false); |
|
475 } |
|
476 |
|
477 // 6. Test one len arguments |
|
478 try |
|
479 { |
|
480 String name = "N"; |
|
481 String value = "V"; |
|
482 sa.setAttribute(name, value, StorageAttribute.STRING_TYPE); |
|
483 |
|
484 assertTrue(name.equals(sa.getName())); |
|
485 assertTrue(value.equals(sa.getValue())); |
|
486 assertTrue(sa.getType() == StorageAttribute.STRING_TYPE); |
|
487 } |
|
488 catch (StorageException se) |
|
489 { |
|
490 assertTrue("Attribute set failed: " + se.getMessage(), false); |
|
491 } |
|
492 } |
|
493 |
|
494 /** |
|
495 * Test toString method. |
|
496 * 1. Test with Name, Value and Type. |
|
497 * 2. Test value null. |
|
498 * 3. Test value "". |
|
499 */ |
|
500 public void testtoString() |
|
501 { |
|
502 try |
|
503 { |
|
504 // 1. Test with Name, Value and Type. |
|
505 String name = "AttrName"; |
|
506 String value = "AttrValue"; |
|
507 StorageAttribute sa = |
|
508 new StorageAttribute(name, value, StorageAttribute.INT_TYPE); |
|
509 |
|
510 String refString = "AttrName='AttrValue' Type: 1"; |
|
511 assertTrue(sa.toString().equals(refString)); |
|
512 |
|
513 // 2. Test value null |
|
514 value = null; |
|
515 sa.setAttribute(name, value, StorageAttribute.NULL_TYPE); |
|
516 refString = "AttrName='NULL' Type: 3"; |
|
517 assertTrue(sa.toString().equals(refString)); |
|
518 |
|
519 // 3. Test value empty |
|
520 value = ""; |
|
521 sa.setAttribute(name, value, StorageAttribute.INT_TYPE); |
|
522 |
|
523 refString = "AttrName='' Type: 1"; |
|
524 assertTrue(sa.toString().equals(refString)); |
|
525 } |
|
526 catch (Throwable t) |
|
527 { |
|
528 assertTrue("Unexpected exp: " + t.toString(), false); |
|
529 } |
|
530 } |
|
531 |
|
532 /** |
|
533 * Test character escape. |
|
534 * |
|
535 * 1. Test ' char is escaped. |
|
536 * 2. Test '' chars are escaped. |
|
537 * 3. Test 'a' chars are escaped. |
|
538 * 4. Test ' only escape char. |
|
539 * 5. Test 'a escape char first. |
|
540 * 6. Test a' escape char last. |
|
541 */ |
|
542 public void testEscape() |
|
543 { |
|
544 // 1. Test ' char is escaped. |
|
545 executeEscapeTest("Tes't", "Tes''t"); |
|
546 |
|
547 // 2. Test '' chars are escaped. |
|
548 executeEscapeTest("Tes''t", "Tes''''t"); |
|
549 |
|
550 // 3. Test 'a' chars are escaped. |
|
551 executeEscapeTest("'a'", "''a''"); |
|
552 |
|
553 // 4. ' char only. |
|
554 executeEscapeTest("'", "''"); |
|
555 |
|
556 // 5. ' char start. |
|
557 executeEscapeTest("'Hello", "''Hello"); |
|
558 |
|
559 // 6. ' char end. |
|
560 executeEscapeTest("Hello'", "Hello''"); |
|
561 } |
|
562 |
|
563 /** |
|
564 * Test character escape. Attributes are written, searched and removed |
|
565 * using storage. |
|
566 * |
|
567 * 1. Test ' char is escaped. |
|
568 * 2. Test '' chars are escaped. |
|
569 * 3. Test 'a' chars are escaped. |
|
570 * 4. Test ' only escape char. |
|
571 * 5. Test 'a escape char first. |
|
572 * 6. Test a' escape char last. |
|
573 */ |
|
574 public void testEscapeToStorage() |
|
575 { |
|
576 // 1. Test escape charcter is escaped. |
|
577 executeToStorage("escT1", "Tes't"); |
|
578 |
|
579 // 2. Test '' chars are escaped. |
|
580 executeToStorage("escT2", "Tes''t"); |
|
581 |
|
582 // 3. Test 'a' chars are escaped. |
|
583 executeToStorage("escT3", "'a'"); |
|
584 |
|
585 // 4. Only ' char. |
|
586 executeToStorage("escT4", "'"); |
|
587 |
|
588 // 5. Escape char first. |
|
589 executeToStorage("escT5", "'a'"); |
|
590 |
|
591 // 6. Escape char last. |
|
592 executeToStorage("escT6", "a'"); |
|
593 } |
|
594 |
|
595 private void executeEscapeTest(String aValue, String aExpectedVal) |
|
596 { |
|
597 String name = "AttrName"; |
|
598 StorageAttribute sa = |
|
599 new StorageAttribute(name, aValue, StorageAttribute.INT_TYPE); |
|
600 |
|
601 try |
|
602 { |
|
603 assertTrue(name.equals(sa.getName())); |
|
604 assertTrue(sa.getEscapedValue().equals(aExpectedVal)); |
|
605 } |
|
606 catch (StorageException se) |
|
607 { |
|
608 assertTrue("Escape fail: " + se.getMessage() + "Val: '" |
|
609 + aValue + "'", false); |
|
610 } |
|
611 } |
|
612 |
|
613 private void executeToStorage(String aUid, String aValue) |
|
614 { |
|
615 try |
|
616 { |
|
617 iSession.open(); |
|
618 iSession.startTransaction(); |
|
619 |
|
620 String tableName = APPLICATION_PACKAGE_TABLE; |
|
621 StorageEntry entry = new StorageEntry(); |
|
622 |
|
623 entry.addAttribute( |
|
624 new StorageAttribute(VENDOR, aValue)); |
|
625 |
|
626 assertTrue("Populate failed", |
|
627 iJtu.populate(iSession, tableName, aUid, entry)); |
|
628 |
|
629 assertTrue("Check failed", |
|
630 iJtu.checkParams(iSession, tableName, entry, 1)); |
|
631 |
|
632 // Utils add ID to entry |
|
633 entry.removeAttribute(ID); |
|
634 |
|
635 assertTrue("Remove failed", |
|
636 iJtu.remove(iSession, tableName, aUid, entry)); |
|
637 |
|
638 iSession.rollbackTransaction(); |
|
639 iSession.close(); |
|
640 } |
|
641 catch (Throwable t) |
|
642 { |
|
643 iSession.rollbackTransaction(); |
|
644 iSession.close(); |
|
645 assertTrue("UnExp exp1: " + t.toString(), false); |
|
646 } |
|
647 } |
|
648 } |