1
|
package eu.dnetlib.xml.database.exist;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.*;
|
5
|
|
6
|
import eu.dnetlib.xml.database.XMLDatabase;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.junit.After;
|
10
|
import org.junit.Before;
|
11
|
import org.junit.Test;
|
12
|
import org.xmldb.api.base.XMLDBException;
|
13
|
|
14
|
import static org.junit.Assert.*;
|
15
|
|
16
|
/**
|
17
|
* Test the exist wrapper.
|
18
|
*
|
19
|
* @author marko
|
20
|
*
|
21
|
*/
|
22
|
public class ExistDatabaseTest { // NOPMD by marko on 11/24/08 5:01 PM
|
23
|
/**
|
24
|
* number of values in stress test profile.
|
25
|
*/
|
26
|
private static final int STRESS_VALUES = 100;
|
27
|
|
28
|
/**
|
29
|
* number of times the values are repeated in the stress test.
|
30
|
*/
|
31
|
private static final int STRESS_PROF_LEN = 100;
|
32
|
|
33
|
/**
|
34
|
* number of profile writes in stress test.
|
35
|
*/
|
36
|
private static final int STRESS_TIMES = 2000000;
|
37
|
|
38
|
/**
|
39
|
* number of collections for stress test.
|
40
|
*/
|
41
|
private static final int STRESS_COLLS = 40;
|
42
|
|
43
|
/**
|
44
|
* number of profiles in stress test.
|
45
|
*/
|
46
|
private static final int STRESS_PROFILES = 1000;
|
47
|
|
48
|
/**
|
49
|
* parallel tuning.
|
50
|
*/
|
51
|
private static final int VALUE_SCATTER = 7;
|
52
|
|
53
|
/**
|
54
|
* parallel tuning.
|
55
|
*/
|
56
|
private static final int QUERY_SCATTER = 5;
|
57
|
|
58
|
/**
|
59
|
* test trigger name.
|
60
|
*/
|
61
|
private static final String TEST_TRIGGER = "testTrigger";
|
62
|
|
63
|
/**
|
64
|
* number of collections for parallel test.
|
65
|
*/
|
66
|
private static final int PAR_COLLS = 20;
|
67
|
|
68
|
/**
|
69
|
* iterations per thread.
|
70
|
*/
|
71
|
private static final int PAR_ITERATIONS = 100;
|
72
|
|
73
|
/**
|
74
|
* parallel job executions.
|
75
|
*/
|
76
|
private static final int PAR_TIMES = 100;
|
77
|
|
78
|
/**
|
79
|
* logger.
|
80
|
*/
|
81
|
public static final Log log = LogFactory.getLog(ExistDatabaseTest.class); // NOPMD by marko on 11/24/08 5:01 PM
|
82
|
|
83
|
/**
|
84
|
* test xml string.
|
85
|
*/
|
86
|
private static final String HELLO_XML = "<hello/>";
|
87
|
|
88
|
/**
|
89
|
* test xml content.
|
90
|
*/
|
91
|
private static final String HELLO_XML_CONTENT = "<hello>content</hello>";
|
92
|
|
93
|
/**
|
94
|
* test file name.
|
95
|
*/
|
96
|
private static final String EXAMPLE = "example";
|
97
|
|
98
|
/**
|
99
|
* trigger test file name.
|
100
|
*/
|
101
|
private static final String TRIGGER_TEST_FILE = "shouldTrigger";
|
102
|
|
103
|
/**
|
104
|
* root collection prefix.
|
105
|
*/
|
106
|
private static final String DB_ROOT = "/db";
|
107
|
|
108
|
/**
|
109
|
* test collection name.
|
110
|
*/
|
111
|
private static final String DB_TEST = "/db/testCollection";
|
112
|
|
113
|
/**
|
114
|
* test other collection name.
|
115
|
*/
|
116
|
private static final String DB_OTHER = "/db/otherCollection";
|
117
|
|
118
|
/**
|
119
|
* test sub collection.
|
120
|
*/
|
121
|
private static final String DB_SUB = DB_TEST + "/sub";
|
122
|
|
123
|
/**
|
124
|
* xml database under test.
|
125
|
*/
|
126
|
private transient XMLDatabase database;
|
127
|
|
128
|
/**
|
129
|
* eXist database under test, viewed only in setUp and tearDown.
|
130
|
*/
|
131
|
private transient ExistDatabase edb;
|
132
|
|
133
|
/**
|
134
|
* prepares the database.
|
135
|
*
|
136
|
* @throws Exception
|
137
|
* exist error
|
138
|
*/
|
139
|
@Before
|
140
|
public void setUp() throws Exception {
|
141
|
edb = new TemporaryExistDatabase();
|
142
|
edb.start();
|
143
|
database = edb;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* shuts down the database.
|
148
|
*
|
149
|
* @throws IOException
|
150
|
* happens
|
151
|
*/
|
152
|
@After
|
153
|
public void tearDown() throws IOException {
|
154
|
if (edb != null)
|
155
|
edb.stop();
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* test create.
|
160
|
*
|
161
|
* @throws XMLDBException
|
162
|
* happens
|
163
|
*/
|
164
|
@Test
|
165
|
public void create() throws XMLDBException {
|
166
|
database.create(EXAMPLE, DB_ROOT, HELLO_XML);
|
167
|
assertEquals("database resource created", true, true);
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* correct behavior is to throw exception on empty file name, because otherwise the file is invisible but the data
|
172
|
* is inserted in the xml db.
|
173
|
*
|
174
|
* @throws XMLDBException
|
175
|
* expected
|
176
|
*/
|
177
|
@Test(expected = XMLDBException.class)
|
178
|
public void createEmptyName() throws XMLDBException {
|
179
|
database.create("", DB_ROOT, "<shouldnt_exist/>");
|
180
|
|
181
|
// code to repeat the bug, shouldn't be executed because of the expected exception
|
182
|
final Iterator<String> res = database.xquery("collection('')/shouldnt_exist");
|
183
|
|
184
|
assertTrue("shouldn't exist, but it exists", res.hasNext());
|
185
|
log.info(database.list(DB_ROOT));
|
186
|
assertEquals("file should be listed, with empty file name", 1, database.list(DB_ROOT).size());
|
187
|
assertEquals("file should be listed, with empty file name", "", database.list(DB_ROOT).get(0));
|
188
|
|
189
|
assertEquals("the file can be retrieved", "<shouldnt_exist/>", database.read("", DB_ROOT));
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* test read.
|
194
|
*
|
195
|
* @throws XMLDBException
|
196
|
* shouldn't happen
|
197
|
*/
|
198
|
@Test
|
199
|
public void read() throws XMLDBException {
|
200
|
create();
|
201
|
final String res = database.read(EXAMPLE, DB_ROOT);
|
202
|
assertEquals("simple reading", HELLO_XML, res);
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* test read of non existing file.
|
207
|
*
|
208
|
* @throws XMLDBException
|
209
|
* shouldn't throw exception
|
210
|
*/
|
211
|
@Test
|
212
|
public void readError() throws XMLDBException {
|
213
|
final String res = database.read("nonExisting", DB_ROOT);
|
214
|
assertEquals("expecting null on unexisting", null, res);
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* test remove.
|
219
|
*
|
220
|
* @throws XMLDBException
|
221
|
* shouldn't throw exception
|
222
|
*/
|
223
|
@Test
|
224
|
public void remove() throws XMLDBException {
|
225
|
database.create(EXAMPLE, DB_ROOT, HELLO_XML);
|
226
|
String res = database.read(EXAMPLE, DB_ROOT);
|
227
|
assertEquals("reading", HELLO_XML, res);
|
228
|
|
229
|
database.remove(EXAMPLE, DB_ROOT);
|
230
|
res = database.read(EXAMPLE, DB_ROOT);
|
231
|
assertEquals("now it doesn't exist", null, res);
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* delete unexisting file.
|
236
|
*
|
237
|
* @throws XMLDBException
|
238
|
* could happen
|
239
|
*/
|
240
|
@Test
|
241
|
public void doubleDelete() throws XMLDBException {
|
242
|
database.create(EXAMPLE, DB_ROOT, HELLO_XML);
|
243
|
String res = database.read(EXAMPLE, DB_ROOT);
|
244
|
assertEquals("reading", HELLO_XML, res);
|
245
|
|
246
|
database.remove(EXAMPLE, DB_ROOT);
|
247
|
res = database.read(EXAMPLE, DB_ROOT);
|
248
|
assertEquals("now it doesn't exist", null, res);
|
249
|
|
250
|
assertFalse("already exists", database.remove(EXAMPLE, DB_ROOT));
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* update an xml file.
|
255
|
*
|
256
|
* @throws XMLDBException
|
257
|
* shouldn't happen
|
258
|
*/
|
259
|
@Test
|
260
|
public void update() throws XMLDBException {
|
261
|
database.create(EXAMPLE, DB_ROOT, HELLO_XML);
|
262
|
String res = database.read(EXAMPLE, DB_ROOT);
|
263
|
assertEquals("reading", HELLO_XML, res);
|
264
|
|
265
|
database.update(EXAMPLE, DB_ROOT, "<world/>");
|
266
|
res = database.read(EXAMPLE, DB_ROOT);
|
267
|
assertEquals("now it doesn't exist", "<world/>", res);
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* update an unexisting file.
|
272
|
*
|
273
|
* @throws XMLDBException
|
274
|
* expected
|
275
|
*/
|
276
|
@Test(expected = XMLDBException.class)
|
277
|
public void updateError() throws XMLDBException {
|
278
|
database.create(EXAMPLE, DB_ROOT, HELLO_XML);
|
279
|
database.remove(EXAMPLE, DB_ROOT);
|
280
|
final String res = database.read(EXAMPLE, DB_ROOT);
|
281
|
assertEquals("check non existing", null, res);
|
282
|
|
283
|
database.update(EXAMPLE, DB_ROOT, "<world/>"); // throws
|
284
|
}
|
285
|
|
286
|
/**
|
287
|
* test xquery.
|
288
|
*
|
289
|
* @throws XMLDBException
|
290
|
* shouldn't happen
|
291
|
*/
|
292
|
@Test()
|
293
|
public void query() throws XMLDBException {
|
294
|
database.create(EXAMPLE, DB_ROOT, "<queryTest><one><sub/></one><two/></queryTest>");
|
295
|
final Iterator<String> res = database.xquery("collection('/db')//queryTest/one");
|
296
|
assertTrue("finds only one result", res.hasNext());
|
297
|
|
298
|
final Iterator<String> res2 = database.xquery("collection('/db')//queryTest/two");
|
299
|
assertTrue("finds only one result",res2.hasNext());
|
300
|
assertEquals("check the correct result", "<two/>", res2.next());
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* create a collection.
|
305
|
*
|
306
|
* @throws XMLDBException
|
307
|
* shouldn't happen
|
308
|
*/
|
309
|
@Test()
|
310
|
public void createCollection() throws XMLDBException {
|
311
|
database.createCollection(DB_TEST);
|
312
|
database.create(EXAMPLE, DB_TEST, "<col/>");
|
313
|
|
314
|
final String res = database.read(EXAMPLE, DB_TEST);
|
315
|
assertEquals("check another collection", "<col/>", res);
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* check the existence of a collection.
|
320
|
*
|
321
|
* @throws XMLDBException
|
322
|
* shouldn't happen
|
323
|
*/
|
324
|
@Test()
|
325
|
public void checkCollection() throws XMLDBException {
|
326
|
createCollection();
|
327
|
|
328
|
assertTrue("check root", database.collectionExists(DB_ROOT));
|
329
|
assertTrue("check test collection", database.collectionExists(DB_TEST));
|
330
|
assertFalse("check non existing", database.collectionExists("/db/testNonExistingCollection"));
|
331
|
}
|
332
|
|
333
|
/**
|
334
|
* shows that a spurious collection create is legal.
|
335
|
*
|
336
|
* @throws XMLDBException
|
337
|
* shouldn't throw
|
338
|
*/
|
339
|
@Test()
|
340
|
public void createCollectionDuplicate() throws XMLDBException {
|
341
|
database.createCollection(DB_TEST);
|
342
|
database.createCollection(DB_TEST);
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* remove a collection.
|
347
|
*
|
348
|
* @throws XMLDBException
|
349
|
* shouldn't throw
|
350
|
*/
|
351
|
@Test()
|
352
|
public void removeCollection() throws XMLDBException {
|
353
|
database.createCollection(DB_TEST);
|
354
|
assertTrue("check before remove", database.collectionExists(DB_TEST));
|
355
|
database.removeCollection(DB_TEST);
|
356
|
assertFalse("check after remove", database.collectionExists(DB_TEST));
|
357
|
}
|
358
|
|
359
|
/**
|
360
|
* check useless contract with spring.
|
361
|
*/
|
362
|
@Test
|
363
|
public void testIsRunning() {
|
364
|
assertFalse("contract with spring", edb.isRunning());
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* Test a scenario where exist fails to create a database.
|
369
|
*
|
370
|
* @throws IOException
|
371
|
* happens
|
372
|
* @throws XMLDBException
|
373
|
* happens
|
374
|
*/
|
375
|
@Test(expected = IllegalStateException.class)
|
376
|
public void checkWrongConfigFile() throws IOException, XMLDBException {
|
377
|
edb.stop();
|
378
|
edb = new ExistDatabase();
|
379
|
try {
|
380
|
edb.setConfigFile("/tmp/unexistingfile");
|
381
|
edb.start();
|
382
|
database = edb;
|
383
|
|
384
|
create();
|
385
|
} finally {
|
386
|
edb = null; // NOPMD - just for junit
|
387
|
}
|
388
|
}
|
389
|
|
390
|
/**
|
391
|
* get child collections.
|
392
|
*
|
393
|
* @throws XMLDBException
|
394
|
* shouldn't happen
|
395
|
*/
|
396
|
@Test
|
397
|
public void testListChildCollections() throws XMLDBException {
|
398
|
database.createCollection(DB_TEST);
|
399
|
assertTrue("check that collection exists", database.collectionExists(DB_TEST));
|
400
|
|
401
|
final String[] expectedNames = new String[] { "child1", "child2", "child3" };
|
402
|
for (String name : expectedNames)
|
403
|
database.createCollection(DB_TEST + "/" + name);
|
404
|
|
405
|
final List<String> res = database.listChildCollections(DB_TEST);
|
406
|
// the db doesn't return then in the same order
|
407
|
Collections.sort(res);
|
408
|
assertArrayEquals("check list", expectedNames, res.toArray());
|
409
|
}
|
410
|
|
411
|
/**
|
412
|
* check list resources.
|
413
|
*
|
414
|
* @throws XMLDBException
|
415
|
* shouldn't happen
|
416
|
*/
|
417
|
@Test
|
418
|
public void testList() throws XMLDBException {
|
419
|
database.createCollection(DB_TEST);
|
420
|
assertTrue("collection should exist", database.collectionExists(DB_TEST));
|
421
|
|
422
|
final String[] expectedNames = new String[] { "name1", "name2", "name3" };
|
423
|
for (String name : expectedNames)
|
424
|
database.create(name, DB_TEST, HELLO_XML);
|
425
|
|
426
|
final List<String> res = database.list(DB_TEST);
|
427
|
// the db doesn't return then in the same order
|
428
|
Collections.sort(res);
|
429
|
assertArrayEquals("check list", expectedNames, res.toArray());
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
* test low-level set trigger.
|
434
|
*
|
435
|
* @throws XMLDBException
|
436
|
* shouldn't happen
|
437
|
*/
|
438
|
@Test
|
439
|
public void testSetExistTrigger() throws XMLDBException {
|
440
|
final Map<String, String> params = new HashMap<String, String>();
|
441
|
params.put("listenerBean", "pippo");
|
442
|
|
443
|
database.createCollection(DB_TEST);
|
444
|
edb.setExistTrigger(TestExistTrigger.class, DB_TEST, Arrays.asList(new String[] { "store", "update", "delete" }), params);
|
445
|
|
446
|
// assertNotNull("check that the conf is stored", database.read(ExistDatabase.COLLECTION_XCONF, DB_ROOT + "/system/config" + DB_TEST));
|
447
|
|
448
|
database.create(TRIGGER_TEST_FILE, DB_TEST, HELLO_XML_CONTENT);
|
449
|
assertEquals("check the the write happened", HELLO_XML_CONTENT, database.read(TRIGGER_TEST_FILE, DB_TEST));
|
450
|
|
451
|
database.update(TRIGGER_TEST_FILE, DB_TEST, "<hello>new content</hello>");
|
452
|
|
453
|
database.xquery("for $x in collection('')/hello where $x/text() = 'new content' return update value $x/text() with 'xupdate'");
|
454
|
assertEquals("check the the xupdate happened", "<hello>xupdate</hello>", database.read(TRIGGER_TEST_FILE, DB_TEST));
|
455
|
}
|
456
|
|
457
|
/**
|
458
|
* Test high level addTrigger.
|
459
|
*
|
460
|
* @throws XMLDBException
|
461
|
* shouldn't happen
|
462
|
*/
|
463
|
@Test
|
464
|
public void testRegisterTrigger() throws XMLDBException {
|
465
|
final TestTrigger trigger = new TestTrigger();
|
466
|
trigger.setName(TEST_TRIGGER);
|
467
|
|
468
|
database.createCollection(DB_TEST);
|
469
|
database.registerTrigger(trigger, DB_TEST);
|
470
|
|
471
|
assertFalse("check that the tester state is correct", trigger.isCreated());
|
472
|
|
473
|
database.create(TRIGGER_TEST_FILE, DB_TEST, HELLO_XML_CONTENT);
|
474
|
assertEquals("check the the write happened", HELLO_XML_CONTENT, database.read(TRIGGER_TEST_FILE, DB_TEST));
|
475
|
assertFalse("check that the trigger is not invoked for the update event", trigger.isUpdated());
|
476
|
assertFalse("check that the trigger is not invoked for the deletion event", trigger.isDeleted());
|
477
|
assertTrue("check that the trigger is invoked create", trigger.isCreated());
|
478
|
assertEquals("check file name for create", TRIGGER_TEST_FILE, trigger.getLastFile());
|
479
|
assertEquals("check collection name for create", DB_TEST, trigger.getLastCollection());
|
480
|
|
481
|
trigger.reset();
|
482
|
assertFalse("check that the tester state is correct", trigger.isUpdated());
|
483
|
database.update(TRIGGER_TEST_FILE, DB_TEST, "<hello>new content</hello>");
|
484
|
assertFalse("check that the trigger is not invoked for the creation event", trigger.isCreated());
|
485
|
assertFalse("check that the trigger is not invoked for the deletion event", trigger.isDeleted());
|
486
|
assertTrue("check that the trigger is invoked for update", trigger.isUpdated());
|
487
|
assertEquals("check file name for update", TRIGGER_TEST_FILE, trigger.getLastFile());
|
488
|
assertEquals("check collection name for update", DB_TEST, trigger.getLastCollection());
|
489
|
|
490
|
trigger.reset();
|
491
|
database.xquery("for $x in collection('')/hello where $x/text() = 'new content' return update value $x/text() with 'xupdate'");
|
492
|
assertTrue("check that the trigger is invoked for xupdate", trigger.isUpdated());
|
493
|
assertEquals("check file name for xupdate", TRIGGER_TEST_FILE, trigger.getLastFile());
|
494
|
assertEquals("check collection name for xupdate", DB_TEST, trigger.getLastCollection());
|
495
|
|
496
|
trigger.reset();
|
497
|
assertFalse("check that the tester state is correct", trigger.isDeleted());
|
498
|
database.remove(TRIGGER_TEST_FILE, DB_TEST);
|
499
|
assertFalse("check that the trigger is not invoked for the creation event", trigger.isCreated());
|
500
|
assertFalse("check that the trigger is not invoked for the update event", trigger.isUpdated());
|
501
|
assertNull("check that the file is removed", database.read(TRIGGER_TEST_FILE, DB_TEST));
|
502
|
assertTrue("check that the trigger is invoked for delete", trigger.isDeleted());
|
503
|
assertEquals("check file name for delete", TRIGGER_TEST_FILE, trigger.getLastFile());
|
504
|
assertEquals("check collection name for delete", DB_TEST, trigger.getLastCollection());
|
505
|
}
|
506
|
|
507
|
/**
|
508
|
* a trigger configuration file should not be listed as a xml file resource.
|
509
|
*
|
510
|
* @throws XMLDBException
|
511
|
* shouldn't happen
|
512
|
*/
|
513
|
@Test
|
514
|
public void testListWithTriggerConf() throws XMLDBException {
|
515
|
database.createCollection(DB_TEST);
|
516
|
assertTrue("collection should exist", database.collectionExists(DB_TEST));
|
517
|
|
518
|
final TestTrigger trigger = new TestTrigger();
|
519
|
trigger.setName(TEST_TRIGGER);
|
520
|
|
521
|
database.registerTrigger(trigger, DB_TEST);
|
522
|
|
523
|
final String[] expectedNames = new String[] { "name1", "name2", "name3" };
|
524
|
for (String name : expectedNames)
|
525
|
database.create(name, DB_TEST, HELLO_XML);
|
526
|
|
527
|
assertTrue("check that trigger was invoked", trigger.isCreated());
|
528
|
|
529
|
final List<String> res = database.list(DB_TEST);
|
530
|
// the db doesn't return then in the same order
|
531
|
Collections.sort(res);
|
532
|
assertArrayEquals("check list", expectedNames, res.toArray());
|
533
|
}
|
534
|
|
535
|
/**
|
536
|
* test trigger with several collections.
|
537
|
*
|
538
|
* @throws XMLDBException
|
539
|
* shoudn't happen
|
540
|
*/
|
541
|
@Test
|
542
|
public void testTriggerDifferentCollections() throws XMLDBException {
|
543
|
final TestTrigger trigger = new TestTrigger();
|
544
|
trigger.setName(TEST_TRIGGER);
|
545
|
|
546
|
database.createCollection(DB_TEST);
|
547
|
database.createCollection(DB_OTHER);
|
548
|
database.createCollection(DB_SUB);
|
549
|
|
550
|
database.registerTrigger(trigger, DB_TEST);
|
551
|
database.create(TRIGGER_TEST_FILE, DB_TEST, HELLO_XML);
|
552
|
assertTrue("trigger was registered for this collection", trigger.isCreated());
|
553
|
|
554
|
trigger.reset();
|
555
|
database.create(TRIGGER_TEST_FILE, DB_OTHER, HELLO_XML);
|
556
|
assertFalse("trigger was not registered for this collection", trigger.isCreated());
|
557
|
|
558
|
trigger.reset();
|
559
|
database.registerTrigger(trigger, DB_OTHER);
|
560
|
database.remove(TRIGGER_TEST_FILE, DB_OTHER);
|
561
|
assertTrue("trigger is now registered for this collection", trigger.isDeleted());
|
562
|
|
563
|
trigger.reset();
|
564
|
database.create(TRIGGER_TEST_FILE, DB_SUB, HELLO_XML);
|
565
|
assertTrue("trigger is automatically registered for the sub collection", trigger.isCreated());
|
566
|
|
567
|
}
|
568
|
|
569
|
/**
|
570
|
* fails because eXist wants that all collection paths begin with /db.
|
571
|
*
|
572
|
* @throws XMLDBException
|
573
|
* expected
|
574
|
*/
|
575
|
@Test(expected = XMLDBException.class)
|
576
|
public void testGetCollection() throws XMLDBException {
|
577
|
edb.getCollection("/something");
|
578
|
assertNotNull("dummy", edb);
|
579
|
}
|
580
|
|
581
|
/**
|
582
|
* simple parallel job.
|
583
|
*
|
584
|
* @author marko
|
585
|
*
|
586
|
*/
|
587
|
class SimpleParallelJob extends Thread { // NOPMD
|
588
|
/**
|
589
|
* some argument.
|
590
|
*/
|
591
|
private final transient int name;
|
592
|
/**
|
593
|
* record eventual exceptions.
|
594
|
*/
|
595
|
private transient Throwable throwable = null;
|
596
|
|
597
|
/**
|
598
|
* pass some argument to the job.
|
599
|
*
|
600
|
* @param name
|
601
|
* some argument
|
602
|
*/
|
603
|
SimpleParallelJob(final int name) {
|
604
|
super();
|
605
|
this.name = name;
|
606
|
}
|
607
|
|
608
|
/**
|
609
|
* {@inheritDoc}
|
610
|
*
|
611
|
* @see java.lang.Runnable#run()
|
612
|
*/
|
613
|
@Override
|
614
|
public void run() {
|
615
|
try {
|
616
|
parallelJob(name);
|
617
|
} catch (XMLDBException e) {
|
618
|
log.fatal("parallel job failing", e);
|
619
|
throwable = e;
|
620
|
} catch (Throwable e) { // NOPMD
|
621
|
log.fatal("other exception", e);
|
622
|
throwable = e;
|
623
|
}
|
624
|
}
|
625
|
|
626
|
public Throwable getThrowable() {
|
627
|
return throwable;
|
628
|
}
|
629
|
}
|
630
|
|
631
|
/**
|
632
|
* stress test the eXist db.
|
633
|
*
|
634
|
* @throws Throwable
|
635
|
* could
|
636
|
*/
|
637
|
@Test
|
638
|
public void testParallel() throws Throwable {
|
639
|
final TestTrigger trigger = new TestTrigger();
|
640
|
trigger.setName(TEST_TRIGGER);
|
641
|
|
642
|
database.registerTrigger(trigger, DB_ROOT);
|
643
|
|
644
|
final List<SimpleParallelJob> threads = new ArrayList<SimpleParallelJob>(); // NOPMD
|
645
|
for (int i = 0; i < PAR_TIMES; i++)
|
646
|
threads.add(new SimpleParallelJob(i)); // NOPMD
|
647
|
for (Thread thread : threads) { // NOPMD
|
648
|
thread.start();
|
649
|
}
|
650
|
for (Thread thread : threads) { // NOPMD
|
651
|
thread.join();
|
652
|
}
|
653
|
|
654
|
for (SimpleParallelJob thread : threads) { // NOPMD
|
655
|
if (thread.getThrowable() != null)
|
656
|
throw thread.getThrowable();
|
657
|
}
|
658
|
|
659
|
assertNotNull("dummy", threads);
|
660
|
}
|
661
|
|
662
|
/**
|
663
|
* Stress test for eXist WAL logs.
|
664
|
*
|
665
|
* @throws XMLDBException
|
666
|
* could happen
|
667
|
*/
|
668
|
@Test
|
669
|
@Ignore
|
670
|
public void testManueleStress() throws XMLDBException {
|
671
|
final StringBuffer childTemplate = new StringBuffer(200); // NOPMD
|
672
|
|
673
|
for (int i = 0; i < STRESS_VALUES; i++) {
|
674
|
childTemplate.append("<key>" + i + "-VAL</key><value>ITER</value>");
|
675
|
}
|
676
|
|
677
|
final StringBuffer hugeTemplate = new StringBuffer(200);
|
678
|
hugeTemplate.append("<root>");
|
679
|
for (int i = 0; i < STRESS_PROF_LEN; i++) {
|
680
|
final String child = childTemplate.toString().replace("VAL", Integer.toString(i));
|
681
|
hugeTemplate.append(child);
|
682
|
}
|
683
|
hugeTemplate.append("</root>");
|
684
|
|
685
|
for (int i = 0; i < STRESS_TIMES; i++) {
|
686
|
final String huge = hugeTemplate.toString().replace("ITER", Integer.toString(i));
|
687
|
database.create("stressTest-" + (i % STRESS_PROFILES), DB_ROOT + "/" + (i % STRESS_COLLS), huge);
|
688
|
}
|
689
|
|
690
|
assertNotNull("dummy", hugeTemplate);
|
691
|
}
|
692
|
|
693
|
/**
|
694
|
* one parallel job execution.
|
695
|
*
|
696
|
* @param arg
|
697
|
* some argument
|
698
|
* @throws XMLDBException
|
699
|
* could happen.
|
700
|
*/
|
701
|
protected void parallelJob(final int arg) throws XMLDBException {
|
702
|
final String name = Integer.toString(arg);
|
703
|
final String coll = DB_ROOT + "/" + Integer.toString(arg % PAR_COLLS);
|
704
|
database.create(name, coll, "<a" + name + "/>");
|
705
|
|
706
|
for (int i = 0; i < PAR_ITERATIONS; i++) {
|
707
|
database.update(name, coll, "<a" + name + " value=\"" + i % VALUE_SCATTER + "\"/>");
|
708
|
final Iterator<String> res = database.xquery("collection('')//*[@value='" + i % QUERY_SCATTER + "']");
|
709
|
while (res.hasNext())
|
710
|
res.next();
|
711
|
}
|
712
|
}
|
713
|
|
714
|
}
|