|
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 |
|
30 /** |
|
31 * StorageSession write test cases. See test methods for test case details. |
|
32 */ |
|
33 public class TestAppendTable extends TestCase 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 TestAppendTable("TestAppendTableArguments", new TestMethod() |
|
53 { |
|
54 public void run(TestCase tc) |
|
55 { |
|
56 ((TestAppendTable)tc).TestAppendTableArguments(); |
|
57 } |
|
58 })); |
|
59 |
|
60 suite.addTest(new TestAppendTable("TestAppendInvalid", new TestMethod() |
|
61 { |
|
62 public void run(TestCase tc) |
|
63 { |
|
64 ((TestAppendTable)tc).TestAppendInvalid(); |
|
65 } |
|
66 })); |
|
67 |
|
68 suite.addTest(new TestAppendTable("TestAppendTable", new TestMethod() |
|
69 { |
|
70 public void run(TestCase tc) |
|
71 { |
|
72 ((TestAppendTable)tc).TestAppendTable(); |
|
73 } |
|
74 })); |
|
75 |
|
76 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
77 } |
|
78 |
|
79 public TestAppendTable() |
|
80 { |
|
81 } |
|
82 |
|
83 public TestAppendTable(String aTestName, TestMethod aTestMethod) |
|
84 { |
|
85 super(aTestName, aTestMethod); |
|
86 } |
|
87 |
|
88 protected void setUp() |
|
89 { |
|
90 iSession = StorageFactory.createSession(); |
|
91 iJtu = new StorageSessionTestUtils(); |
|
92 } |
|
93 |
|
94 protected void tearDown() |
|
95 { |
|
96 if (iSession != null) |
|
97 { |
|
98 try |
|
99 { |
|
100 iSession.destroySession(); |
|
101 } |
|
102 catch (StorageException se) |
|
103 { |
|
104 // No can do |
|
105 System.out.println("TearDown failed: " + se.toString()); |
|
106 } |
|
107 } |
|
108 } |
|
109 |
|
110 /** |
|
111 * Test append table arguments. |
|
112 * 1. Test empty tableName throws SE. |
|
113 * 2. Test null tableName throws SE. |
|
114 * 3. Test empty entry throws SE. |
|
115 * 4. Test null entry throws SE. |
|
116 * 5. Test append no connection throws SE. |
|
117 */ |
|
118 public void TestAppendTableArguments() |
|
119 { |
|
120 String tableName = null; |
|
121 StorageEntry entry = null; |
|
122 String appUid = null; |
|
123 |
|
124 // 1. Test empty tableName throws SE. |
|
125 try |
|
126 { |
|
127 iSession.open(); |
|
128 tableName = ""; |
|
129 appUid = "Not_Used"; |
|
130 entry = new StorageEntry(); |
|
131 |
|
132 entry.addAttribute( |
|
133 new StorageAttribute("HomeTown", |
|
134 "", |
|
135 StorageAttribute.STRING_TYPE)); |
|
136 |
|
137 entry.addAttribute( |
|
138 new StorageAttribute("Country", |
|
139 "", |
|
140 StorageAttribute.INT_TYPE)); |
|
141 |
|
142 iSession.appendTable(tableName, entry); |
|
143 assertTrue("No exp. when appending empty table name", false); |
|
144 } |
|
145 catch (StorageException se) |
|
146 { |
|
147 // PASSSED |
|
148 } |
|
149 catch (Throwable t) |
|
150 { |
|
151 assertTrue("UnExpected exp1: " + t.toString(), false); |
|
152 } |
|
153 |
|
154 iSession.close(); |
|
155 |
|
156 // 2. Test null tableName throws SE. |
|
157 try |
|
158 { |
|
159 iSession.open(); |
|
160 tableName = null; |
|
161 appUid = "Not_Used"; |
|
162 entry = new StorageEntry(); |
|
163 |
|
164 entry.addAttribute( |
|
165 new StorageAttribute("HomeTown", |
|
166 "", |
|
167 StorageAttribute.STRING_TYPE)); |
|
168 |
|
169 entry.addAttribute( |
|
170 new StorageAttribute("Country", |
|
171 "", |
|
172 StorageAttribute.INT_TYPE)); |
|
173 |
|
174 iSession.appendTable(tableName, entry); |
|
175 assertTrue("No exp. when appending null table name", false); |
|
176 } |
|
177 catch (StorageException se) |
|
178 { |
|
179 // PASSSED |
|
180 } |
|
181 catch (Throwable t) |
|
182 { |
|
183 assertTrue("UnExpected exp2: " + t.toString(), false); |
|
184 } |
|
185 |
|
186 iSession.close(); |
|
187 |
|
188 // 3. Test empty entry throws SE. |
|
189 try |
|
190 { |
|
191 iSession.open(); |
|
192 tableName = APPLICATION_PACKAGE_TABLE; |
|
193 entry = new StorageEntry(); |
|
194 |
|
195 iSession.appendTable(tableName, entry); |
|
196 assertTrue("No exp. when appending empty entry", false); |
|
197 } |
|
198 catch (StorageException se) |
|
199 { |
|
200 // PASSSED |
|
201 } |
|
202 catch (Throwable t) |
|
203 { |
|
204 assertTrue("UnExpected exp3: " + t.toString(), false); |
|
205 } |
|
206 |
|
207 iSession.close(); |
|
208 |
|
209 // 4. Test null entry throws SE. |
|
210 try |
|
211 { |
|
212 iSession.open(); |
|
213 tableName = APPLICATION_PACKAGE_TABLE; |
|
214 entry = null; |
|
215 |
|
216 iSession.appendTable(tableName, entry); |
|
217 assertTrue("No exp. when appending null entry", false); |
|
218 } |
|
219 catch (StorageException se) |
|
220 { |
|
221 // PASSSED |
|
222 } |
|
223 catch (Throwable t) |
|
224 { |
|
225 assertTrue("UnExpected exp4: " + t.toString(), false); |
|
226 } |
|
227 |
|
228 iSession.close(); |
|
229 |
|
230 // 5. Test createTable no connection throws SE. |
|
231 try |
|
232 { |
|
233 tableName = APPLICATION_PACKAGE_TABLE; |
|
234 appUid = "Not_Used"; |
|
235 entry = new StorageEntry(); |
|
236 |
|
237 entry.addAttribute( |
|
238 new StorageAttribute("HomeTown", |
|
239 "", |
|
240 StorageAttribute.STRING_TYPE)); |
|
241 |
|
242 entry.addAttribute( |
|
243 new StorageAttribute("Country", |
|
244 "", |
|
245 StorageAttribute.INT_TYPE)); |
|
246 |
|
247 iSession.appendTable(tableName, entry); |
|
248 assertTrue("No exp. when appending no connection", false); |
|
249 } |
|
250 catch (StorageException se) |
|
251 { |
|
252 // PASSSED |
|
253 } |
|
254 catch (Throwable t) |
|
255 { |
|
256 assertTrue("UnExpected exp5: " + t.toString(), false); |
|
257 } |
|
258 } |
|
259 |
|
260 /** |
|
261 * Test append invalid table structues. |
|
262 * 1. Test append no table to append throws SE. |
|
263 * 2. Test append existing column throws SE. |
|
264 * 3. Test create invalid attribute. Name contains '-char |
|
265 * StorageException is thrown. |
|
266 */ |
|
267 public void TestAppendInvalid() |
|
268 { |
|
269 String tableName = null; |
|
270 StorageEntry entry = null; |
|
271 String appUid = null; |
|
272 |
|
273 // 1. Test append no table to append throws SE. |
|
274 try |
|
275 { |
|
276 iSession.open(); |
|
277 tableName = "I_DO_NOT_EXISTS"; |
|
278 entry = new StorageEntry(); |
|
279 |
|
280 entry.addAttribute( |
|
281 new StorageAttribute("Home", |
|
282 "", |
|
283 StorageAttribute.STRING_TYPE)); |
|
284 |
|
285 entry.addAttribute( |
|
286 new StorageAttribute("StreetNumber", |
|
287 "", |
|
288 StorageAttribute.INT_TYPE)); |
|
289 |
|
290 iSession.appendTable(tableName, entry); |
|
291 |
|
292 assertTrue("No exp. thrown appending non-existent table", false); |
|
293 } |
|
294 catch (StorageException se) |
|
295 { |
|
296 // PASSSED |
|
297 } |
|
298 catch (Throwable t) |
|
299 { |
|
300 assertTrue("UnExpected exp1: " + t.toString(), false); |
|
301 } |
|
302 |
|
303 iSession.close(); |
|
304 |
|
305 // 2. Test append existing column throws SE. |
|
306 try |
|
307 { |
|
308 iSession.open(); |
|
309 tableName = APPLICATION_PACKAGE_TABLE; |
|
310 entry = new StorageEntry(); |
|
311 |
|
312 entry.addAttribute( |
|
313 new StorageAttribute(StorageAttribute.PACKAGE_NAME, |
|
314 "", |
|
315 StorageAttribute.STRING_TYPE)); |
|
316 entry.addAttribute( |
|
317 new StorageAttribute("StreetNumber", |
|
318 "", |
|
319 StorageAttribute.INT_TYPE)); |
|
320 |
|
321 iSession.appendTable(tableName, entry); |
|
322 |
|
323 assertTrue("No exp. thrown appending already existent attribute", |
|
324 false); |
|
325 } |
|
326 catch (StorageException se) |
|
327 { |
|
328 // PASSED |
|
329 } |
|
330 catch (Throwable t) |
|
331 { |
|
332 assertTrue("UnExpected exp2: " + t.toString(), false); |
|
333 } |
|
334 |
|
335 iSession.close(); |
|
336 |
|
337 // 3. Test create invalid attribute. Name contains '-char |
|
338 // StorageException is thrown. |
|
339 try |
|
340 { |
|
341 iSession.open(); |
|
342 tableName = APPLICATION_PACKAGE_TABLE; |
|
343 entry = new StorageEntry(); |
|
344 |
|
345 entry.addAttribute( |
|
346 new StorageAttribute("Column '1", |
|
347 "", |
|
348 StorageAttribute.INT_TYPE)); |
|
349 entry.addAttribute( |
|
350 new StorageAttribute("SameTwice", |
|
351 "", |
|
352 StorageAttribute.INT_TYPE)); |
|
353 |
|
354 iSession.appendTable(tableName, entry); |
|
355 |
|
356 assertTrue("No exp. thrown appending invalid attribute", |
|
357 false); |
|
358 } |
|
359 catch (StorageException se) |
|
360 { |
|
361 // PASSED |
|
362 } |
|
363 catch (Throwable t) |
|
364 { |
|
365 assertTrue("UnExpected exp3: " + t.toString(), false); |
|
366 } |
|
367 |
|
368 iSession.close(); |
|
369 } |
|
370 |
|
371 /** |
|
372 * Test append table. |
|
373 * 1. Test append table. Data can be written to it. |
|
374 * 2. Test append one column. Data can be written to it. |
|
375 * 3. Test append string and int columns. Data can be written to them. |
|
376 */ |
|
377 public void TestAppendTable() |
|
378 { |
|
379 String tableName = null; |
|
380 StorageEntry entry = null; |
|
381 String appUid = null; |
|
382 |
|
383 // 1. Test append table. Data can be written to it. |
|
384 // 3. Test append string and int columns. Data can be written to them. |
|
385 try |
|
386 { |
|
387 iSession.open(); |
|
388 iSession.startTransaction(); |
|
389 tableName = APPLICATION_PACKAGE_TABLE; |
|
390 entry = new StorageEntry(); |
|
391 |
|
392 entry.addAttribute( |
|
393 new StorageAttribute("ADDRESS", |
|
394 "Home town 11", |
|
395 StorageAttribute.STRING_TYPE)); |
|
396 |
|
397 entry.addAttribute( |
|
398 new StorageAttribute("AGE", |
|
399 "12", |
|
400 StorageAttribute.INT_TYPE)); |
|
401 |
|
402 iSession.appendTable(tableName, entry); |
|
403 |
|
404 StorageAttribute idAttr = new StorageAttribute( |
|
405 StorageAttribute.ID, "888"); |
|
406 |
|
407 entry.addAttribute(idAttr); |
|
408 |
|
409 iSession.write(tableName, entry); |
|
410 |
|
411 assertTrue("Check write", iJtu.checkParams(iSession, |
|
412 tableName, |
|
413 entry, |
|
414 1)); |
|
415 iSession.rollbackTransaction(); |
|
416 } |
|
417 catch (Throwable t) |
|
418 { |
|
419 iSession.rollbackTransaction(); |
|
420 assertTrue("UnExpected exp1: " + t.toString(), false); |
|
421 } |
|
422 |
|
423 iSession.close(); |
|
424 |
|
425 // 2. Test append one column. Data can be written to it. |
|
426 try |
|
427 { |
|
428 iSession.open(); |
|
429 iSession.startTransaction(); |
|
430 tableName = APPLICATION_PACKAGE_TABLE; |
|
431 entry = new StorageEntry(); |
|
432 |
|
433 entry.addAttribute( |
|
434 new StorageAttribute("ADDRESS", |
|
435 "Home town 11", |
|
436 StorageAttribute.STRING_TYPE)); |
|
437 |
|
438 iSession.appendTable(tableName, entry); |
|
439 |
|
440 StorageAttribute idAttr = new StorageAttribute( |
|
441 StorageAttribute.ID, "888"); |
|
442 |
|
443 entry.addAttribute(idAttr); |
|
444 |
|
445 iSession.write(tableName, entry); |
|
446 |
|
447 assertTrue("Check write", iJtu.checkParams(iSession, |
|
448 tableName, |
|
449 entry, |
|
450 1)); |
|
451 iSession.rollbackTransaction(); |
|
452 } |
|
453 catch (Throwable t) |
|
454 { |
|
455 iSession.rollbackTransaction(); |
|
456 assertTrue("UnExpected exp2: " + t.toString(), false); |
|
457 } |
|
458 |
|
459 iSession.close(); |
|
460 } |
|
461 } |