1 package org.xmlBlaster.test.classtest.msgstore;
2
3 import java.util.logging.Logger;
4 import java.util.logging.Level;
5 import org.xmlBlaster.engine.ServerScope;
6 import org.xmlBlaster.util.XmlBlasterException;
7 import org.xmlBlaster.util.MsgUnit;
8 import org.xmlBlaster.util.queue.StorageId;
9 import org.xmlBlaster.engine.msgstore.I_MapEntry;
10 import org.xmlBlaster.engine.msgstore.I_Map;
11 import org.xmlBlaster.util.qos.storage.MsgUnitStoreProperty;
12 import org.xmlBlaster.util.qos.storage.QueuePropertyBase;
13 import org.xmlBlaster.engine.qos.PublishQosServer;
14 import org.xmlBlaster.engine.MsgUnitWrapper;
15
16 import java.util.ArrayList;
17
18 import junit.framework.*;
19 import org.xmlBlaster.engine.msgstore.StoragePluginManager;
20 import org.xmlBlaster.util.plugin.PluginInfo;
21
22 /**
23 * Test I_Map e.g. MapPlugin which allows to store randomly messages.
24 * <p>
25 * Invoke: java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.classtest.msgstore.I_MapTest
26 * </p>
27 * @see org.xmlBlaster.engine.msgstore.I_Map
28 * @see org.xmlBlaster.engine.msgstore.ram.MapPlugin
29 * @see org.xmlBlaster.util.queue.jdbc.JdbcQueuePlugin
30 */
31 public class I_MapTest extends TestCase {
32 private String ME = "I_MapTest";
33 protected ServerScope glob;
34 private static Logger log = Logger.getLogger(I_MapTest.class.getName());
35
36 private final boolean IS_DURABLE = true;
37 private final boolean IS_TRANSIENT = false;
38
39 private I_Map currMap;
40 private int currImpl;
41 /*
42 static I_Map[] IMPL = {
43 new org.xmlBlaster.engine.msgstore.ram.MapPlugin(),
44 new org.xmlBlaster.util.queue.jdbc.JdbcQueuePlugin(),
45 new org.xmlBlaster.engine.msgstore.cache.PersistenceCachePlugin()
46 };
47 */
48 static String[] PLUGIN_TYPES = { new String("RAM"),
49 new String("JDBC"),
50 new String("CACHE") };
51
52 public I_MapTest(String name, int currImpl) {
53 super(name);
54 this.currImpl = currImpl;
55
56 String[] args = { //configure the cache
57 "-persistence.persistentQueue", "JDBC,1.0",
58 "-persistence.transientQueue", "RAM,1.0" };
59
60 this.glob = new ServerScope(args);
61
62 //this.ME = "I_MapTest[" + this.currMap.getClass().getName() + "]";
63 }
64
65 protected void setUp() {
66 try {
67 glob.getProperty().set("topic.queue.persistent.tableNamePrefix", "TEST");
68
69 String type = PLUGIN_TYPES[this.currImpl];
70 StoragePluginManager pluginManager = this.glob.getStoragePluginManager();
71 // Overwrite JDBC settings from xmlBlaster.properties
72 PluginInfo pluginInfo = new PluginInfo(glob, pluginManager, "JDBC", "1.0");
73 java.util.Properties prop = (java.util.Properties)pluginInfo.getParameters();
74 prop.put("tableNamePrefix", "TEST");
75 prop.put("entriesTableName", "_entries");
76 this.glob.getProperty().set("QueuePlugin[JDBC][1.0]", pluginInfo.dumpPluginParameters());
77
78 if (!"JDBC".equals(type))
79 pluginInfo = new PluginInfo(glob, pluginManager, type, "1.0");
80
81 MsgUnitStoreProperty storeProp = new MsgUnitStoreProperty(glob, "/node/test");
82 StorageId queueId = new StorageId(glob, "msgUnitStore", "SomeMapId");
83
84 this.currMap = pluginManager.getPlugin(pluginInfo, queueId, storeProp);
85 this.currMap.clear();
86 this.currMap.shutdown(); // to allow to initialize again
87 }
88 catch (Exception ex) {
89 log.severe("setUp: error when setting the property 'topic.queue.persistent.tableNamePrefix' to 'TEST': " + ex.getMessage());
90 }
91
92 // cleaning up the database from previous runs ...
93 /*
94 QueuePropertyBase prop = null;
95 try {
96 prop = new MsgUnitStoreProperty(glob, "/node/test");
97
98 StorageId queueId = new StorageId("msgUnitStore", "SetupMap");
99 JdbcMapPlugin jdbcMap = new JdbcMapPlugin();
100 jdbcMap.initialize(queueId, prop);
101 jdbcMap.destroy();
102 }
103 catch (Exception ex) {
104 log.severe("could not propertly set up the database: " + ex.getMessage());
105 }
106 */
107 }
108
109 private MsgUnit createMsgUnit(boolean persistent) {
110 return createMsgUnit(persistent, -1);
111 }
112
113 private MsgUnit createMsgUnit(boolean persistent, long contentLen_) {
114 try {
115 int contentLen = (int)contentLen_;
116 PublishQosServer publishQosServer = new PublishQosServer(glob, "<qos/>");
117 publishQosServer.getData().setPersistent(persistent);
118 String contentStr = "content";
119 if (contentLen >= 0) {
120 StringBuffer content = new StringBuffer(contentLen);
121 for (int i=0; i<contentLen; i++) {
122 content.append("X");
123 }
124 contentStr = content.toString();
125 }
126 return new MsgUnit(glob, "<key oid='Hi'/>", contentStr.getBytes(), publishQosServer.toXml());
127 }
128 catch (XmlBlasterException ex) {
129 fail("msgUnit not constructed: " + ex.getMessage());
130 }
131 return null;
132 }
133
134
135 /**
136 * Tests QueuePropertyBase() and getStorageId()
137 * @param queueTypeList A space separated list of names for the
138 * implementations to be tested. Valid names are:
139 * RamMapPlugin JdbcMapPlugin
140 */
141 public void testConfig() {
142 config(this.currMap);
143 }
144
145 /**
146 * Tests initialize(), getProperties(), setProperties() and capacity()
147 * @param queue !!!Is not initialized in this case!!!!
148 */
149 private void config(I_Map i_map) {
150 ME = "I_MapTest.config(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
151 System.out.println("***" + ME);
152
153 QueuePropertyBase prop1 = null;
154 QueuePropertyBase prop = null;
155 try {
156 // test initialize()
157 prop1 = new MsgUnitStoreProperty(glob, "/node/test");
158 int max = 12;
159 prop1.setMaxEntries(max);
160 prop1.setMaxEntriesCache(max);
161 assertEquals(ME+": Wrong capacity", max, prop1.getMaxEntries());
162 assertEquals(ME+": Wrong cache capacity", max, prop1.getMaxEntriesCache());
163 StorageId queueId = new StorageId(glob, "msgUnitStore", "SomeMapId");
164
165 i_map.initialize(queueId, prop1);
166 assertEquals(ME+": Wrong queue ID", queueId, i_map.getStorageId());
167
168 try {
169 prop = new MsgUnitStoreProperty(glob, "/node/test");
170 prop.setMaxEntries(99);
171 prop.setMaxEntriesCache(99);
172 i_map.setProperties(prop);
173 }
174 catch(XmlBlasterException e) {
175 fail("Changing properties failed: " + e.getMessage());
176 }
177
178 }
179 catch(XmlBlasterException e) {
180 fail(ME + ": Exception thrown: " + e.getMessage());
181 }
182
183 long len = prop.getMaxEntries();
184 assertEquals(ME+": Wrong capacity", prop.getMaxEntries(), i_map.getMaxNumOfEntries());
185 assertEquals(ME+": Wrong capacity", prop.getMaxEntries(), ((QueuePropertyBase)i_map.getProperties()).getMaxEntries());
186 assertEquals(ME+": Wrong size", 0, i_map.getNumOfEntries());
187
188 try {
189 for (int ii=0; ii<len; ii++) {
190 i_map.put(new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId()));
191 }
192 assertEquals(ME+": Wrong total size", len, i_map.getNumOfEntries());
193
194 try {
195 MsgUnitWrapper queueEntry = new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId());
196 i_map.put(queueEntry);
197 i_map.put(queueEntry);
198 fail("Did expect an exception on overflow getMaxNumOfEntries=" + i_map.getMaxNumOfEntries() + " size=" + i_map.getNumOfEntries());
199 }
200 catch(XmlBlasterException e) {
201 log.info("SUCCESS the exception is OK: " + e.getMessage());
202 }
203
204 log.info("toXml() test:" + i_map.toXml(""));
205 log.info("usage() test:" + i_map.usage());
206
207 assertEquals(ME+": should not be shutdown", false, i_map.isShutdown());
208 i_map.shutdown();
209 assertEquals(ME+": should be shutdown", true, i_map.isShutdown());
210
211 log.info("#2 Success, filled " + i_map.getNumOfEntries() + " messages into queue");
212 System.out.println("***" + ME + " [SUCCESS]");
213 i_map.shutdown();
214 }
215 catch(XmlBlasterException e) {
216 fail(ME + ": Exception thrown: " + e.getMessage());
217 }
218 }
219
220 //------------------------------------
221 public void testPutMsg() {
222 String queueType = "unknown";
223 try {
224 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
225 queueType = this.currMap.toString();
226 StorageId queueId = new StorageId(glob, "msgUnitStore", "MapPlugin/putMsg");
227 this.currMap.initialize(queueId, prop);
228 this.currMap.clear();
229 assertEquals(ME + "wrong size before starting ", 0L, this.currMap.getNumOfEntries());
230 putMsg(this.currMap);
231 }
232 catch (XmlBlasterException ex) {
233 fail("Exception when testing PutMsg probably due to failed initialization of the queue of type " + queueType + ": " + ex.getMessage());
234 }
235 }
236
237 /**
238 * Tests put(MsgMapEntry[]) and put(MsgMapEntry) and clear()
239 */
240 private void putMsg(I_Map i_map) {
241 ME = "I_MapTest.putMsg(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
242 System.out.println("***" + ME);
243 try {
244 //========== Test 1: put(I_MapEntry[])
245 int numLoop = 10;
246 ArrayList list = new ArrayList();
247 for (int ii=0; ii<numLoop; ii++) {
248 MsgUnitWrapper[] queueEntries = {
249 new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId()),
250 new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId()),
251 new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId())};
252
253 for(int i=0; i<queueEntries.length; i++)
254 i_map.put(queueEntries[i]);
255
256 for (int i=0; i < 3; i++) list.add(queueEntries[i]);
257
258 this.checkSizeAndEntries(" put(I_MapEntry[]) ", list, i_map);
259 assertEquals(ME+": Wrong size", (ii+1)*queueEntries.length, i_map.getNumOfEntries());
260 }
261 int total = numLoop*3;
262 assertEquals(ME+": Wrong total size", total, i_map.getNumOfEntries());
263 log.info("#1 Success, filled " + i_map.getNumOfEntries() + " messages into queue");
264
265
266 //========== Test 2: put(I_MapEntry)
267 for (int ii=0; ii<numLoop; ii++) {
268 MsgUnitWrapper queueEntry = new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId());
269 list.add(queueEntry);
270 i_map.put(queueEntry);
271 }
272 assertEquals(ME+": Wrong total size", numLoop+total, i_map.getNumOfEntries());
273 this.checkSizeAndEntries(" put(I_MapEntry) ", list, i_map);
274 log.info("#2 Success, filled " + i_map.getNumOfEntries() + " messages into queue");
275
276 i_map.clear();
277 checkSizeAndEntries("Test 2 put()", new I_MapEntry[0], i_map);
278 assertEquals(ME+": Wrong empty size", 0L, i_map.getNumOfEntries());
279
280 System.out.println("***" + ME + " [SUCCESS]");
281 i_map.shutdown();
282 }
283 catch(XmlBlasterException e) {
284 fail(ME + ": Exception thrown: " + e.getMessage());
285 }
286 }
287
288
289 /**
290 * Tests overflow of maxNumOfBytes() of a CACHE.
291 */
292 public void testByteOverflow() {
293 I_Map i_map = this.currMap;
294 ME = "I_MapTest.testByteOverflow(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
295 System.out.println("***" + ME);
296 try {
297 StorageId storageId = new StorageId(glob, "msgUnitStore", "ByteOverflowMapId");
298 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
299
300 MsgUnitWrapper mu = new MsgUnitWrapper(glob, createMsgUnit(false, 0), storageId);
301 long sizeEmpty = mu.getSizeInBytes();
302
303 MsgUnitWrapper[] queueEntries = {
304 new MsgUnitWrapper(glob, createMsgUnit(false, 0), storageId),
305 new MsgUnitWrapper(glob, createMsgUnit(false, 0), storageId),
306 new MsgUnitWrapper(glob, createMsgUnit(false, 0), storageId),
307 // Each above entry has 3,311 bytes = 9,922, the next one has 9,932 bytes
308 // so when it is entered two of the above need to be swapped away
309 // as maxBytes=13,244
310 new MsgUnitWrapper(glob, createMsgUnit(false, 2*sizeEmpty-1), storageId),
311 new MsgUnitWrapper(glob, createMsgUnit(false, 0), storageId)};
312
313 final long maxBytesCache = 4*sizeEmpty;
314 prop.setMaxBytes(1000000);
315 prop.setMaxBytesCache(maxBytesCache);
316 assertEquals(ME+": Wrong capacity", 1000000, prop.getMaxBytes());
317 assertEquals(ME+": Wrong cache capacity", maxBytesCache, prop.getMaxBytesCache());
318 i_map.initialize(storageId, prop);
319 assertEquals(ME+": Wrong queue ID", storageId, i_map.getStorageId());
320
321 long numOfBytes = 0;
322 for(int i=0; i<queueEntries.length; i++) {
323 i_map.put(queueEntries[i]);
324 numOfBytes += queueEntries[i].getSizeInBytes();
325 }
326
327 assertEquals(ME+": Wrong size", queueEntries.length, i_map.getNumOfEntries());
328 assertEquals(ME+": Wrong bytes", numOfBytes, i_map.getNumOfBytes());
329
330 System.out.println("***" + ME + " [SUCCESS]");
331 i_map.clear();
332 i_map.shutdown();
333 }
334 catch(XmlBlasterException e) {
335 log.severe("Exception thrown: " + e.getMessage());
336 fail(ME + ": Exception thrown: " + e.getMessage());
337 }
338 }
339
340
341 //------------------------------------
342 public void testGetMsg() {
343
344 String queueType = "unknown";
345 try {
346 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
347 queueType = this.currMap.toString();
348 StorageId queueId = new StorageId(glob, "msgUnitStore", "MapPlugin/getMsg");
349 this.currMap.initialize(queueId, prop);
350 this.currMap.clear();
351 assertEquals(ME + "wrong size before starting ", 0, this.currMap.getNumOfEntries());
352 getMsg(this.currMap);
353 }
354 catch (XmlBlasterException ex) {
355 log.severe("Exception when testing getMsg probably due to failed initialization of the queue " + queueType + ": " + ex.getMessage());
356 }
357
358 }
359
360 /**
361 * Tests get() and get(int num) and remove()
362 * For a discussion of the sorting order see Javadoc of this class
363 */
364 private void getMsg(I_Map i_map) {
365 ME = "I_MapTest.getMsg(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
366 System.out.println("***" + ME);
367 try {
368 //========== Test 1: get()
369 {
370 MsgUnitWrapper[] queueEntries = {
371 new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId()),
372 new MsgUnitWrapper(glob, createMsgUnit(true), i_map.getStorageId()),
373 new MsgUnitWrapper(glob, createMsgUnit(true), i_map.getStorageId())
374 };
375 for(int i=0; i<queueEntries.length; i++) {
376 i_map.put(queueEntries[i]);
377 log.info("#" + i + " id=" + queueEntries[i].getUniqueId() + " numSizeBytes()=" + queueEntries[i].getSizeInBytes());
378 }
379 log.info("storage bytes sum=" + i_map.getNumOfBytes() + " with persistent bytes=" + i_map.getNumOfPersistentBytes());
380
381 assertEquals("", 3, i_map.getNumOfEntries());
382 assertEquals("", 2, i_map.getNumOfPersistentEntries());
383
384 for (int ii=0; ii<10; ii++) {
385 I_MapEntry result = i_map.get(queueEntries[0].getUniqueId());
386 assertTrue("Missing entry", result != null);
387 assertEquals(ME+": Wrong result", queueEntries[0].getUniqueId(), result.getUniqueId());
388
389 result = i_map.get(queueEntries[1].getUniqueId());
390 assertTrue("Missing entry", result != null);
391 assertEquals(ME+": Wrong result", queueEntries[1].getUniqueId(), result.getUniqueId());
392
393 result = i_map.get(queueEntries[2].getUniqueId());
394 assertTrue("Missing entry", result != null);
395 assertEquals(ME+": Wrong result", queueEntries[2].getUniqueId(), result.getUniqueId());
396 }
397 assertEquals("", 3, i_map.getNumOfEntries());
398 assertEquals("", 2, i_map.getNumOfPersistentEntries());
399
400 log.info("storage before remove [0], bytes sum=" + i_map.getNumOfBytes() + " with persistent bytes=" + i_map.getNumOfPersistentBytes());
401 i_map.remove(queueEntries[0]); // Remove one
402 log.info("storage after remove [0], bytes sum=" + i_map.getNumOfBytes() + " with persistent bytes=" + i_map.getNumOfPersistentBytes());
403 ArrayList list = new ArrayList();
404 list.add(queueEntries[1]);
405 list.add(queueEntries[2]);
406 this.checkSizeAndEntries(" getMsg() ", list, i_map);
407
408 for (int ii=0; ii<10; ii++) {
409 I_MapEntry result = i_map.get(queueEntries[1].getUniqueId());
410 assertTrue("Missing entry", result != null);
411 assertEquals(ME+": Wrong result", queueEntries[1].getUniqueId(), result.getUniqueId());
412 }
413 i_map.remove(queueEntries[1].getUniqueId()); // Remove one
414 assertEquals("", 1, i_map.getNumOfEntries());
415 assertEquals("", 1, i_map.getNumOfPersistentEntries());
416
417 for (int ii=0; ii<10; ii++) {
418 I_MapEntry result = i_map.get(queueEntries[2].getUniqueId());
419 assertTrue("Missing entry", result != null);
420 assertEquals(ME+": Wrong result", queueEntries[2].getUniqueId(), result.getUniqueId());
421 }
422 i_map.remove(queueEntries[2]); // Remove one
423 for (int ii=0; ii<10; ii++) {
424 I_MapEntry result = i_map.get(queueEntries[0].getUniqueId());
425 assertTrue("Unexpected entry", result == null);
426 }
427 assertEquals("", 0, i_map.getNumOfEntries());
428 assertEquals("", 0, i_map.getNumOfPersistentEntries());
429 log.info("#1 Success, get()");
430 }
431
432 System.out.println("***" + ME + " [SUCCESS]");
433 i_map.clear();
434 i_map.shutdown();
435 }
436 catch(XmlBlasterException e) {
437 e.printStackTrace();
438 fail(ME + ": Exception thrown: " + e.getMessage());
439 }
440 }
441
442
443
444 //------------------------------------
445 public void testGetAllMsgs() {
446
447 String queueType = "unknown";
448 try {
449 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
450 queueType = this.currMap.toString();
451 StorageId queueId = new StorageId(glob, "msgUnitStore", "MapPlugin/getAllMsgs");
452 this.currMap.initialize(queueId, prop);
453 this.currMap.clear();
454 assertEquals(ME + "wrong size before starting ", 0, this.currMap.getNumOfEntries());
455 getAllMsgs(this.currMap);
456 }
457 catch (XmlBlasterException ex) {
458 log.severe("Exception when testing getAllMsgs probably due to failed initialization of the queue " + queueType + ": " + ex.getMessage());
459 }
460
461 }
462
463 /**
464 * Tests get() and get(int num) and remove()
465 * NOTE: Currently the MapPlugin returns getAll() sorted (it uses a TreeMap)
466 * But we haven't yet forced this in the I_Map#getAll() Javadoc!
467 * This test assumes sorting order and needs to be changed if we once
468 * decide to specify the exact behaviour in I_Map#getAll() javadoc
469 */
470 private void getAllMsgs(I_Map i_map) {
471 ME = "I_MapTest.getAllMsgs(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
472 System.out.println("***" + ME);
473 try {
474 //========== Test 1: getAll()
475 {
476 MsgUnitWrapper[] queueEntries = {
477 new MsgUnitWrapper(glob, createMsgUnit(IS_TRANSIENT), i_map.getStorageId()),
478 new MsgUnitWrapper(glob, createMsgUnit(IS_DURABLE), i_map.getStorageId()),
479 new MsgUnitWrapper(glob, createMsgUnit(IS_DURABLE), i_map.getStorageId())
480 };
481 for(int i=0; i<queueEntries.length; i++) {
482 i_map.put(queueEntries[i]);
483 log.info("#" + i + " id=" + queueEntries[i].getUniqueId() + " numSizeBytes()=" + queueEntries[i].getSizeInBytes());
484 }
485 log.info("storage bytes sum=" + i_map.getNumOfBytes() + " with persistent bytes=" + i_map.getNumOfPersistentBytes());
486
487 assertEquals("", 3, i_map.getNumOfEntries());
488 assertEquals("", 2, i_map.getNumOfPersistentEntries());
489
490 for (int ii=0; ii<10; ii++) {
491 I_MapEntry[] results = i_map.getAll(null);
492 assertEquals("Missing entry", queueEntries.length, results.length);
493 assertEquals(ME+": Wrong result", queueEntries[0].getUniqueId(), results[0].getUniqueId());
494 assertEquals(ME+": Wrong result", queueEntries[1].getUniqueId(), results[1].getUniqueId());
495 assertEquals(ME+": Wrong result", queueEntries[2].getUniqueId(), results[2].getUniqueId());
496 }
497 assertEquals("", 3, i_map.getNumOfEntries());
498 assertEquals("", 2, i_map.getNumOfPersistentEntries());
499 /*
500 I_MapEntry[] results = i_map.getAll(new I_EntryFilter() {
501 public I_Entry intercept(I_Entry entry, I_Storage storage) {
502 assertTrue("NULL storage", storage!=null);
503 if (!storage.isTransient()) return entry;
504 entryCounter++;
505 if (entryCounter == 2)
506 return null;
507 return entry;
508 }
509 });
510 assertEquals("Missing entry", queueEntries.length-1, results.length);
511 assertEquals(ME+": Wrong result", queueEntries[0].getUniqueId(), results[0].getUniqueId());
512 assertEquals(ME+": Wrong result", queueEntries[2].getUniqueId(), results[1].getUniqueId());
513 */
514 i_map.clear();
515 log.info("#1 Success, getAll()");
516 }
517
518 System.out.println("***" + ME + " [SUCCESS]");
519 }
520 catch(XmlBlasterException e) {
521 e.printStackTrace();
522 fail(ME + ": Exception thrown: " + e.getMessage());
523 }
524 finally {
525 try {
526 i_map.clear();
527 i_map.shutdown();
528 }
529 catch(XmlBlasterException e) {
530 e.printStackTrace();
531 fail(ME + ": Exception thrown in cleanup: " + e.getMessage());
532 }
533 }
534 }
535
536
537 //------------------------------------
538 public void testGetAllSwappedMsgs() {
539
540 String queueType = "unknown";
541 try {
542 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
543 queueType = this.currMap.toString();
544 StorageId queueId = new StorageId(glob, "msgUnitStore", "MapPlugin/getAllSwappedMsgs");
545 prop.setMaxEntries(10); // Overall size (RAM or JDBC or CACHE)
546 prop.setMaxEntriesCache(2); // Is only interpreted for cache implementations (-> the size of the RAM map)
547 this.currMap.initialize(queueId, prop);
548 this.currMap.clear();
549 assertEquals(ME + "wrong size before starting ", 0, this.currMap.getNumOfEntries());
550 getAllSwappedMsgs(this.currMap);
551 }
552 catch (XmlBlasterException ex) {
553 log.severe("Exception when testing getAllSwappedMsgs probably due to failed initialization of the queue " + queueType + ": " + ex.getMessage());
554 }
555
556 }
557
558 /**
559 * Tests getAll() and the entries are swapped as the RAM size is only 2
560 * NOTE: see NOTE of getAllMsgs(I_Map)
561 */
562 private void getAllSwappedMsgs(I_Map i_map) {
563 ME = "I_MapTest.getAllSwappedMsgs(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
564 System.out.println("***" + ME);
565
566 QueuePropertyBase prop = (QueuePropertyBase)i_map.getProperties();
567 assertEquals(ME+": Wrong capacity", 10, prop.getMaxEntries());
568 assertEquals(ME+": Wrong cache capacity", 2, prop.getMaxEntriesCache());
569 log.info("Current settings: " + prop.toXml());
570
571 try {
572 //========== Test 1: getAllSwapped()
573 {
574 MsgUnitWrapper[] queueEntries = {
575 new MsgUnitWrapper(glob, createMsgUnit(IS_TRANSIENT), i_map.getStorageId()),
576 new MsgUnitWrapper(glob, createMsgUnit(IS_TRANSIENT), i_map.getStorageId()),
577 new MsgUnitWrapper(glob, createMsgUnit(IS_TRANSIENT), i_map.getStorageId()),
578 new MsgUnitWrapper(glob, createMsgUnit(IS_TRANSIENT), i_map.getStorageId())
579 };
580 for(int i=0; i<queueEntries.length; i++) {
581 i_map.put(queueEntries[i]);
582 log.info("#" + i + " id=" + queueEntries[i].getUniqueId() + " numSizeBytes()=" + queueEntries[i].getSizeInBytes());
583 }
584 //log.info("storage bytes sum=" + i_map.getNumOfBytes() + " with persistent bytes=" + i_map.getNumOfPersistentBytes());
585 log.info("storage state=" + i_map.toXml(""));
586
587 assertEquals("", queueEntries.length, i_map.getNumOfEntries());
588
589 for (int ii=0; ii<10; ii++) {
590 I_MapEntry[] results = i_map.getAll(null);
591 for(int jj=0; jj<results.length; jj++) {
592 log.info("#" + jj + ": " + results[jj].getUniqueId());
593 }
594 assertEquals("Missing entry", queueEntries.length, results.length);
595 assertEquals(ME+": Wrong result", queueEntries[0].getUniqueId(), results[0].getUniqueId());
596 assertEquals(ME+": Wrong result", queueEntries[1].getUniqueId(), results[1].getUniqueId());
597 assertEquals(ME+": Wrong result", queueEntries[2].getUniqueId(), results[2].getUniqueId());
598 assertEquals(ME+": Wrong result", queueEntries[3].getUniqueId(), results[3].getUniqueId());
599 }
600 assertEquals("", 4, i_map.getNumOfEntries());
601 assertEquals("", 0, i_map.getNumOfPersistentEntries());
602 log.info("#1 Success, getAllSwapped()");
603 }
604
605 System.out.println("***" + ME + " [SUCCESS]");
606 }
607 catch(XmlBlasterException e) {
608 e.printStackTrace();
609 fail(ME + ": Exception thrown: " + e.getMessage());
610 }
611 finally {
612 try {
613 i_map.clear();
614 i_map.shutdown();
615 }
616 catch(XmlBlasterException e) {
617 e.printStackTrace();
618 fail(ME + ": Exception thrown in cleanup: " + e.getMessage());
619 }
620 }
621 }
622
623
624
625 public void testPutEntriesTwice() {
626 String queueType = "unknown";
627 try {
628 QueuePropertyBase prop = new MsgUnitStoreProperty(glob, "/node/test");
629 queueType = this.currMap.toString();
630 StorageId queueId = new StorageId(glob, "msgUnitStore", "MapPlugin/putEntriesTwice");
631 this.currMap.initialize(queueId, prop);
632 this.currMap.clear();
633 assertEquals(ME + " wrong size before starting ", 0, this.currMap.getNumOfEntries());
634 putEntriesTwice(this.currMap);
635 }
636 catch (XmlBlasterException ex) {
637 log.severe("Exception when testing putEntriesTwice probably due to failed initialization of the queue " + queueType + ": " + ex.getMessage());
638 }
639 }
640
641 private void putEntriesTwice(I_Map i_map) {
642 ME = "I_MapTest.putEntriesTwice(" + i_map.getStorageId() + ")[" + i_map.getClass().getName() + "]";
643 System.out.println("***" + ME);
644 try {
645 //========== Test 1: checks if entries are returned in the correct
646 // order even if they are inserted in the wrong order
647 {
648 log.fine("putEntriesTwice test 1");
649 int imax = 5;
650 long size = 0L;
651
652 MsgUnitWrapper[] entries = new MsgUnitWrapper[imax];
653 for (int i=0; i < entries.length; i++) {
654 entries[i] = new MsgUnitWrapper(glob, createMsgUnit(false), i_map.getStorageId());
655 size += entries[i].getSizeInBytes();
656 }
657
658 for(int i=0; i<entries.length; i++) {
659 int numPut = i_map.put(entries[i]);
660 assertEquals("Putting first entry should be OK", 1, numPut);
661 numPut = i_map.put(entries[i]);
662 assertEquals("Putting entries twices should fail", 0, numPut);
663 }
664
665 assertEquals(ME+": Wrong number of entries after putting same entries twice", entries.length, i_map.getNumOfEntries());
666 assertEquals(ME+": Wrong size after putting same entries twice", size, i_map.getNumOfBytes());
667 i_map.clear();
668 assertEquals(ME+": Wrong num entries after cleaning", i_map.getNumOfEntries(), 0);
669 }
670 System.out.println("***" + ME + " [SUCCESS]");
671 }
672 catch(XmlBlasterException e) {
673 fail(ME + ": Exception thrown: " + e.getMessage());
674 }
675 }
676
677 public void tearDown() {
678 try {
679 this.currMap.destroy();
680 }
681 catch (Exception ex) {
682 ex.printStackTrace();
683 log.severe("error when tearing down " + ex.getMessage());
684 }
685 }
686
687 /**
688 * @see checkSizeAndEntries(String, I_MapEntry[], I_Map)
689 */
690 private void checkSizeAndEntries(String txt, ArrayList queueEntries, I_Map map) {
691 checkSizeAndEntries(txt, (I_MapEntry[])queueEntries.toArray(new I_MapEntry[queueEntries.size()]), map);
692 }
693
694 /**
695 * Helper method to do a generic size check (size and number of entries)
696 */
697 private void checkSizeAndEntries(String txt, I_MapEntry[] queueEntries, I_Map i_map) {
698 long sizeOfTransients = 0L;
699 long numOfPersistents = 0;
700 long numOfTransients = 0;
701 long sizeOfPersistents = 0L;
702 for (int i=0; i < queueEntries.length; i++) {
703 I_MapEntry entry = queueEntries[i];
704 if (entry.isPersistent()) {
705 sizeOfPersistents += entry.getSizeInBytes();
706 numOfPersistents++;
707 }
708 else {
709 sizeOfTransients += entry.getSizeInBytes();
710 numOfTransients++;
711 }
712 }
713
714 long queueNumOfPersistents = i_map.getNumOfPersistentEntries();
715 long queueNumOfTransients = i_map.getNumOfEntries() - queueNumOfPersistents;
716 long queueSizeOfPersistents = i_map.getNumOfPersistentBytes();
717 long queueSizeOfTransients = i_map.getNumOfBytes() - queueSizeOfPersistents;
718
719 txt += " getNumOfPersistentEntries=" + queueNumOfPersistents + " NumOfTransients=" + queueNumOfTransients;
720 txt += " getNumOfPersistentBytes=" + queueSizeOfPersistents + " SizeOfTransients=" + queueSizeOfTransients;
721
722 assertEquals(ME + ": " + txt + " wrong number of persistents ", numOfPersistents, queueNumOfPersistents);
723 assertEquals(ME + ": " + txt + " wrong number of transients ", numOfTransients, queueNumOfTransients);
724 assertEquals(ME + ": " + txt + " wrong size of persistents ", sizeOfPersistents, queueSizeOfPersistents);
725 assertEquals(ME + ": " + txt + " wrong size of transients ", sizeOfTransients, queueSizeOfTransients);
726 }
727
728 /**
729 * Method is used by TestRunner to load these tests
730 */
731 public static Test suite()
732 {
733 TestSuite suite= new TestSuite();
734 ServerScope glob = new ServerScope();
735 suite.addTest(new I_MapTest("testByteOverflow", 2)); // For CACHE only
736 for (int i=0; i<PLUGIN_TYPES.length; i++) {
737 suite.addTest(new I_MapTest("testConfig", i));
738 suite.addTest(new I_MapTest("testPutMsg", i));
739 suite.addTest(new I_MapTest("testGetMsg", i));
740 suite.addTest(new I_MapTest("testGetAllMsgs", i));
741 suite.addTest(new I_MapTest("testGetAllSwappedMsgs", i));
742 suite.addTest(new I_MapTest("testPutEntriesTwice", i));
743 }
744 return suite;
745 }
746
747 /**
748 * <pre>
749 * java -Dtrace=true org.xmlBlaster.test.classtest.msgstore.I_MapTest > test.log
750 * </pre>
751 */
752 public static void main(String args[]) {
753
754 ServerScope glob = new ServerScope(args);
755
756 I_MapTest testSub = new I_MapTest("I_MapTest", 1); // JDBC check
757 //I_MapTest testSub = new I_MapTest("I_MapTest", 2); // CACHE check
758 long startTime = System.currentTimeMillis();
759 testSub.setUp();
760 testSub.testGetAllMsgs();
761 testSub.tearDown();
762 long usedTime = System.currentTimeMillis() - startTime;
763 testSub.log.info("time used for tests: " + usedTime/1000 + " seconds");
764
765 for (int i=0; i < PLUGIN_TYPES.length; i++) {
766 testSub = new I_MapTest("I_MapTest", i);
767
768 startTime = System.currentTimeMillis();
769
770 testSub.setUp();
771 testSub.testConfig();
772 testSub.tearDown();
773
774 testSub.setUp();
775 testSub.testPutMsg();
776 testSub.tearDown();
777
778 testSub.setUp();
779 testSub.testGetMsg();
780 testSub.tearDown();
781
782 // already tested outside
783 // testSub.setUp();
784 // testSub.testGetAllMsgs();
785 // testSub.tearDown();
786
787 testSub.setUp();
788 testSub.testGetAllSwappedMsgs();
789 testSub.tearDown();
790 testSub.setUp();
791 testSub.testPutEntriesTwice();
792 testSub.tearDown();
793
794 usedTime = System.currentTimeMillis() - startTime;
795 testSub.log.info("time used for tests: " + usedTime/1000 + " seconds");
796 }
797 }
798 }
syntax highlighted by Code2HTML, v. 0.9.1