1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package complex.dbaccess;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.sdb.CommandType;
import com.sun.star.sdb.XParametersSupplier;
import com.sun.star.sdb.XResultSetAccess;
import com.sun.star.sdb.XRowSetApproveBroadcaster;
import com.sun.star.sdbc.SQLException;
import com.sun.star.sdbc.XParameters;
import com.sun.star.sdbc.XPreparedStatement;
import com.sun.star.sdbc.XResultSet;
import com.sun.star.sdbc.XResultSetUpdate;
import com.sun.star.sdbc.XRow;
import com.sun.star.sdbc.XRowSet;
import com.sun.star.sdbc.XRowUpdate;
import com.sun.star.sdbcx.XColumnsSupplier;
import com.sun.star.sdbcx.XDeleteRows;
import com.sun.star.sdbcx.XRowLocate;
import com.sun.star.uno.UnoRuntime;
import connectivity.tools.CRMDatabase;
import connectivity.tools.DataSource;
import connectivity.tools.HsqlDatabase;
import connectivity.tools.sdb.Connection;
import java.lang.reflect.Method;
import java.util.Random;
// ---------- junit imports -----------------
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
public class RowSet extends TestCase
{
static final int MAX_TABLE_ROWS = 100;
static final int MAX_FETCH_ROWS = 10;
private static final String NEXT = "next";
private static final String TEST21 = "Test21";
HsqlDatabase m_database;
DataSource m_dataSource;
XRowSet m_rowSet;
XResultSet m_resultSet;
XResultSetUpdate m_resultSetUpdate;
XRow m_row;
XRowLocate m_rowLocate;
XPropertySet m_rowSetProperties;
XParametersSupplier m_paramsSupplier;
private final Object failedResultSetMovementStressGuard = new Object();
private String failedResultSetMovementStressMessages = "";
private class ResultSetMovementStress implements Runnable
{
private final XResultSet m_resultSet;
private final XRow m_row;
private final int m_id;
private ResultSetMovementStress(XResultSet _resultSet, int _id) throws java.lang.Exception
{
m_resultSet = _resultSet;
m_row = UnoRuntime.queryInterface( XRow.class, m_resultSet );
m_id = _id;
}
public void run()
{
int i=-1;
try
{
m_resultSet.beforeFirst();
for (i = 0; m_resultSet.next(); ++i)
{
int pos = m_resultSet.getRow();
testPosition(m_resultSet, m_row, i + 1, "clone move(" + m_id + ")");
int pos2 = m_resultSet.getRow();
assertTrue("ResultSetMovementStress wrong position: " + i + " Pos1: " + pos + " Pos2: " + pos2, pos == pos2);
}
}
catch (Exception e)
{
synchronized (failedResultSetMovementStressGuard) {
failedResultSetMovementStressMessages
+= "ResultSetMovementStress(" + m_id + ") failed at i="
+ i + ": " + e + "\n";
}
}
}
}
private void createTestCase(boolean _defaultRowSet) throws Exception
{
if (m_database == null)
{
final CRMDatabase database = new CRMDatabase( getMSF(), false );
m_database = database.getDatabase();
m_dataSource = m_database.getDataSource();
}
createStructure();
if (_defaultRowSet)
{
createRowSet("TEST1", CommandType.TABLE, true, true);
}
}
@After public final void closeAndDeleteDatabase() {
if (m_database != null) {
m_database.closeAndDelete();
}
}
/** creates a com.sun.star.sdb.RowSet to use during the test
* @param command
* the command to use for the RowSet
* @param commandType
* the command type to use for the RowSet
* @param execute
* determines whether the RowSet should be executed
*/
private void createRowSet(String command, int commandType, boolean execute) throws com.sun.star.uno.Exception
{
createRowSet(command, commandType, execute, false);
}
/** creates a com.sun.star.sdb.RowSet to use during the test
* @param command
* the command to use for the RowSet
* @param commandType
* the command type to use for the RowSet
* @param execute
* determines whether the RowSet should be executed
* @param limitFetchSize
* determines whether the fetch size of the RowSet should be limited to MAX_FETCH_ROWS
*/
private void createRowSet(String command, int commandType, boolean execute, boolean limitFetchSize) throws com.sun.star.uno.Exception
{
m_rowSet = UnoRuntime.queryInterface( XRowSet.class, getMSF().createInstance( "com.sun.star.sdb.RowSet" ) );
final XPropertySet rowSetProperties = UnoRuntime.queryInterface( XPropertySet.class, m_rowSet );
rowSetProperties.setPropertyValue("Command", command);
rowSetProperties.setPropertyValue("CommandType", Integer.valueOf(commandType));
rowSetProperties.setPropertyValue("ActiveConnection", m_database.defaultConnection().getXConnection());
if (limitFetchSize)
{
rowSetProperties.setPropertyValue("FetchSize", Integer.valueOf(MAX_FETCH_ROWS));
}
m_resultSet = UnoRuntime.queryInterface( XResultSet.class, m_rowSet );
m_resultSetUpdate = UnoRuntime.queryInterface( XResultSetUpdate.class, m_rowSet );
m_row = UnoRuntime.queryInterface( XRow.class, m_rowSet );
m_rowLocate = UnoRuntime.queryInterface( XRowLocate.class, m_resultSet );
m_rowSetProperties = UnoRuntime.queryInterface( XPropertySet.class, m_rowSet );
m_paramsSupplier = UnoRuntime.queryInterface( XParametersSupplier.class, m_rowSet );
if (execute)
{
m_rowSet.execute();
}
}
@Test
public void testRowSet() throws java.lang.Exception
{
System.out.println("testing testRowSet");
createTestCase(true);
// sequential positioning
m_resultSet.beforeFirst();
testSequentialPositining(m_resultSet, m_row);
// absolute positioning
testAbsolutePositioning(m_resultSet, m_row);
// position during modify
testModifyPosition(m_resultSet, m_row);
// 3rd test
test3(createClone(), m_resultSet);
// 4th test
test4(m_resultSet);
// concurrent (multi threaded) access to the row set and its clones
testConcurrentAccess(m_resultSet);
}
XResultSet createClone() throws SQLException
{
final XResultSetAccess rowAcc = UnoRuntime.queryInterface( XResultSetAccess.class, m_rowSet );
return rowAcc.createResultSet();
}
void createStructure() throws SQLException
{
m_database.executeSQL("DROP TABLE \"TEST1\" IF EXISTS");
m_database.executeSQL("CREATE TABLE \"TEST1\" (\"ID\" integer not null primary key, \"col2\" varchar(50) )");
final Connection connection = m_database.defaultConnection();
final XPreparedStatement prep = connection.prepareStatement("INSERT INTO \"TEST1\" values (?,?)");
final XParameters para = UnoRuntime.queryInterface( XParameters.class, prep );
for (int i = 1; i <= MAX_TABLE_ROWS; ++i)
{
para.setInt(1, i);
para.setString(2, "Test" + i);
prep.executeUpdate();
}
connection.refreshTables();
}
void testPosition(XResultSet resultSet, XRow row, int expectedValue, String location) throws SQLException
{
final int val = row.getInt(1);
final int pos = resultSet.getRow();
assertTrue(location + ": value/position do not match: " + pos + " (pos) != " + val + " (val)", val == pos);
assertTrue(location + ": value/position are not as expected: " + val + " (val) != " + expectedValue + " (expected)", val == expectedValue);
}
void testSequentialPositining(XResultSet _resultSet, XRow _row) throws com.sun.star.uno.Exception
{
// 1st test
int i = 1;
while (_resultSet.next())
{
testPosition(_resultSet, _row, i, "testSequentialPositining");
++i;
}
}
void testAbsolutePositioning(XResultSet _resultSet, XRow _row) throws com.sun.star.uno.Exception
{
for (int i = 1; i <= MAX_FETCH_ROWS; ++i)
{
final int calcPos = (MAX_TABLE_ROWS % i) + 1;
assertTrue("testAbsolutePositioning failed", _resultSet.absolute(calcPos));
testPosition(_resultSet, _row, calcPos, "testAbsolutePositioning");
}
}
void testModifyPosition(XResultSet _resultSet, XRow _row) throws com.sun.star.uno.Exception
{
final int testPos = 3;
assertTrue("testModifyPosition wants at least " + (testPos+1) + " rows", MAX_FETCH_ROWS >= testPos+1);
assertTrue("testModifyPosition failed on moving to row " + testPos, _resultSet.absolute(testPos));
UnoRuntime.queryInterface( XRowUpdate.class, _row ).updateString(2, TEST21);
testPosition(_resultSet, _row, testPos, "testModifyPosition");
UnoRuntime.queryInterface( XResultSetUpdate.class, _resultSet ).cancelRowUpdates();
}
void test3(XResultSet clone, XResultSet _resultSet) throws com.sun.star.uno.Exception
{
final XRow _row = UnoRuntime.queryInterface( XRow.class, _resultSet );
final XRow cloneRow = UnoRuntime.queryInterface( XRow.class, clone );
for (int i = 1; i <= MAX_FETCH_ROWS; ++i)
{
final int calcPos = (MAX_TABLE_ROWS % i) + 1;
if (clone.absolute(calcPos))
{
testPosition(clone, cloneRow, calcPos, "test3");
testAbsolutePositioning(_resultSet, _row);
testAbsolutePositioning(clone, cloneRow);
}
}
}
void test4(XResultSet _resultSet) throws com.sun.star.uno.Exception
{
final XRow _row = UnoRuntime.queryInterface( XRow.class, _resultSet );
_resultSet.beforeFirst();
for (int i = 1; i <= MAX_TABLE_ROWS; ++i)
{
_resultSet.next();
final XResultSet clone = createClone();
final XRow cloneRow = UnoRuntime.queryInterface( XRow.class, clone );
final int calcPos = MAX_TABLE_ROWS - 1;
if (calcPos != 0 && clone.absolute(calcPos))
{
testPosition(clone, cloneRow, calcPos, "test4: clone");
testPosition(_resultSet, _row, i, "test4: rowset");
}
}
}
void testConcurrentAccess(XResultSet _resultSet) throws Exception
{
System.out.println("testing Thread");
_resultSet.beforeFirst();
final int numberOfThreads = 10;
final Thread threads[] = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; ++i)
{
threads[i] = new Thread(new ResultSetMovementStress(createClone(), i));
System.out.println("starting thread " + (i + 1) + " of " + (numberOfThreads));
threads[i].start();
}
for (int i = 0; i < numberOfThreads; ++i)
{
threads[i].join();
}
synchronized (failedResultSetMovementStressGuard) {
assertEquals("", failedResultSetMovementStressMessages);
}
}
@Test
public void testRowSetEvents() throws java.lang.Exception
{
System.out.println("testing RowSet Events");
createTestCase(true);
// first we create our RowSet object
final RowSetEventListener pRow = new RowSetEventListener();
final XColumnsSupplier colSup = UnoRuntime.queryInterface( XColumnsSupplier.class, m_rowSet );
final XPropertySet col = UnoRuntime.queryInterface( XPropertySet.class, colSup.getColumns().getByName( "ID" ) );
col.addPropertyChangeListener("Value", pRow);
m_rowSetProperties.addPropertyChangeListener("IsModified", pRow);
m_rowSetProperties.addPropertyChangeListener("IsNew", pRow);
m_rowSetProperties.addPropertyChangeListener("IsRowCountFinal", pRow);
m_rowSetProperties.addPropertyChangeListener("RowCount", pRow);
final XRowSetApproveBroadcaster xApBroad = UnoRuntime.queryInterface( XRowSetApproveBroadcaster.class, m_resultSet );
xApBroad.addRowSetApproveListener(pRow);
m_rowSet.addRowSetListener(pRow);
// do some movements to check if we got all notifications
final Class cResSet = Class.forName("com.sun.star.sdbc.XResultSet");
final boolean moves[] = new boolean[9];
for (int i = 0; i < moves.length; ++i)
{
moves[i] = false;
}
moves[RowSetEventListener.APPROVE_CURSOR_MOVE] = true;
moves[RowSetEventListener.COLUMN_VALUE] = true;
moves[RowSetEventListener.CURSOR_MOVED] = true;
moves[RowSetEventListener.IS_ROW_COUNT_FINAL] = true;
moves[RowSetEventListener.ROW_COUNT] = true;
testCursorMove(m_resultSet, cResSet.getMethod("afterLast", (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_ROW_COUNT_FINAL] = false;
moves[RowSetEventListener.ROW_COUNT] = false;
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod("last", (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod("first", (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod("previous", (Class[]) null), pRow, moves, null);
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_MODIFIED] = true;
final XRowUpdate updRow = UnoRuntime.queryInterface( XRowUpdate.class, m_resultSet );
updRow.updateString(2, TEST21);
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_MODIFIED] = false;
updRow.updateString(2, m_row.getString(2));
testCursorMove(m_resultSet, cResSet.getMethod(NEXT, (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_MODIFIED] = false;
final Class cupd = Class.forName("com.sun.star.sdbc.XResultSetUpdate");
final XResultSetUpdate upd = UnoRuntime.queryInterface( XResultSetUpdate.class, m_resultSet );
testCursorMove(upd, cupd.getMethod("moveToInsertRow", (Class[]) null), pRow, moves, null);
updRow.updateInt(1, MAX_TABLE_ROWS + 2);
updRow.updateString(2, "HHHH");
moves[RowSetEventListener.APPROVE_CURSOR_MOVE] = false;
moves[RowSetEventListener.CURSOR_MOVED] = false;
moves[RowSetEventListener.IS_MODIFIED] = true;
moves[RowSetEventListener.IS_NEW] = true;
moves[RowSetEventListener.ROW_COUNT] = true;
moves[RowSetEventListener.APPROVE_ROW_CHANGE] = true;
moves[RowSetEventListener.ROW_CHANGED] = true;
testCursorMove(upd, cupd.getMethod("insertRow", (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_NEW] = false;
moves[RowSetEventListener.ROW_COUNT] = false;
m_resultSet.first();
updRow.updateInt(1, MAX_TABLE_ROWS + 3);
updRow.updateString(2, "__");
testCursorMove(upd, cupd.getMethod("updateRow", (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_NEW] = true;
moves[RowSetEventListener.ROW_COUNT] = true;
m_resultSet.first();
testCursorMove(upd, cupd.getMethod("deleteRow", (Class[]) null), pRow, moves, null);
moves[RowSetEventListener.IS_NEW] = false;
moves[RowSetEventListener.COLUMN_VALUE] = true;
moves[RowSetEventListener.ROW_COUNT] = false;
m_resultSet.first();
updRow.updateString(2, TEST21);
testCursorMove(m_resultSet, cResSet.getMethod("refreshRow", (Class[]) null), pRow, moves, null);
m_resultSet.first();
updRow.updateString(2, TEST21);
testCursorMove(upd, cupd.getMethod("cancelRowUpdates", (Class[]) null), pRow, moves, null);
for (int i = 0; i < moves.length; ++i)
{
moves[i] = false;
}
moves[RowSetEventListener.APPROVE_CURSOR_MOVE] = true;
moves[RowSetEventListener.COLUMN_VALUE] = true;
moves[RowSetEventListener.CURSOR_MOVED] = true;
final Class cloc = Class.forName("com.sun.star.sdbcx.XRowLocate");
m_resultSet.first();
final Object bookmark = m_rowLocate.getBookmark();
m_resultSet.next();
final Object temp[] = new Object[1];
temp[0] = bookmark;
Class ctemp[] = new Class[1];
ctemp[0] = Object.class;
testCursorMove(m_rowLocate, cloc.getMethod("moveToBookmark", ctemp), pRow, moves, temp);
final Object temp2[] = new Object[2];
temp2[0] = bookmark;
temp2[1] = Integer.valueOf(1);
final Class ctemp2[] = new Class[2];
ctemp2[0] = Object.class;
ctemp2[1] = int.class;
testCursorMove(m_rowLocate, cloc.getMethod("moveRelativeToBookmark", ctemp2), pRow, moves, temp2);
for (int i = 0; i < moves.length; ++i)
{
moves[i] = false;
}
moves[RowSetEventListener.APPROVE_ROW_CHANGE] = true;
moves[RowSetEventListener.ROW_CHANGED] = true;
moves[RowSetEventListener.ROW_COUNT] = true;
final Class cdelRows = Class.forName("com.sun.star.sdbcx.XDeleteRows");
ctemp[0] = Object[].class;
final XDeleteRows delRows = UnoRuntime.queryInterface( XDeleteRows.class, m_resultSet );
final Object bookmarks[] = new Object[5];
m_resultSet.first();
for (int i = 0; i < bookmarks.length; ++i)
{
m_resultSet.next();
bookmarks[i] = m_rowLocate.getBookmark();
}
temp[0] = bookmarks;
testCursorMove(delRows, cdelRows.getMethod("deleteRows", ctemp), pRow, moves, temp);
// now destroy the RowSet
final XComponent xComp = UnoRuntime.queryInterface( XComponent.class, m_resultSet );
xComp.dispose();
}
private void testCursorMove(Object res, Method _method, RowSetEventListener _evt, boolean _must[], Object args[]) throws java.lang.Exception
{
_evt.clearCalling();
_method.invoke(res, args);
System.out.println("testing events for " + _method.getName());
final int calling[] = _evt.getCalling();
int pos = 1;
assertTrue("Callings are not in the correct order for APPROVE_CURSOR_MOVE ",
(!_must[RowSetEventListener.APPROVE_CURSOR_MOVE] || calling[RowSetEventListener.APPROVE_CURSOR_MOVE] == -1) || calling[RowSetEventListener.APPROVE_CURSOR_MOVE] == pos++);
assertTrue("Callings are not in the correct order for APPROVE_ROW_CHANGE",
(!_must[RowSetEventListener.APPROVE_ROW_CHANGE] || calling[RowSetEventListener.APPROVE_ROW_CHANGE] == -1) || calling[RowSetEventListener.APPROVE_ROW_CHANGE] == pos++);
assertTrue("Callings are not in the correct order for COLUMN_VALUE",
(!_must[RowSetEventListener.COLUMN_VALUE] || calling[RowSetEventListener.COLUMN_VALUE] == -1) || calling[RowSetEventListener.COLUMN_VALUE] == pos++);
assertTrue("Callings are not in the correct order for CURSOR_MOVED",
(!_must[RowSetEventListener.CURSOR_MOVED] || calling[RowSetEventListener.CURSOR_MOVED] == -1) || calling[RowSetEventListener.CURSOR_MOVED] == pos++);
assertTrue("Callings are not in the correct order for ROW_CHANGED",
(!_must[RowSetEventListener.ROW_CHANGED] || calling[RowSetEventListener.ROW_CHANGED] == -1) || calling[RowSetEventListener.ROW_CHANGED] == pos++);
assertTrue("Callings are not in the correct order for IS_MODIFIED",
(!_must[RowSetEventListener.IS_MODIFIED] || calling[RowSetEventListener.IS_MODIFIED] == -1) || calling[RowSetEventListener.IS_MODIFIED] == pos++);
assertTrue("Callings are not in the correct order for IS_NEW",
(!_must[RowSetEventListener.IS_NEW] || calling[RowSetEventListener.IS_NEW] == -1) || calling[RowSetEventListener.IS_NEW] == pos++);
assertTrue("Callings are not in the correct order for ROW_COUNT",
(!_must[RowSetEventListener.ROW_COUNT] || calling[RowSetEventListener.ROW_COUNT] == -1) || calling[RowSetEventListener.ROW_COUNT] == pos++);
assertTrue("Callings are not in the correct order for IS_ROW_COUNT_FINAL",
(!_must[RowSetEventListener.IS_ROW_COUNT_FINAL] || calling[RowSetEventListener.IS_ROW_COUNT_FINAL] == -1) || calling[RowSetEventListener.IS_ROW_COUNT_FINAL] == pos);
_evt.clearCalling();
}
/** returns the current row count of the RowSet
*/
private int currentRowCount() throws UnknownPropertyException, WrappedTargetException
{
final Integer rowCount = (Integer) m_rowSetProperties.getPropertyValue("RowCount");
return rowCount.intValue();
}
/** positions the row set at an arbitrary position between 2 and (current row count - 1)
*/
private int positionRandom() throws SQLException, UnknownPropertyException, WrappedTargetException
{
// note: obviously this should subtract 2 but actually subtract 3
// because if we have just deleted the current row then
// ORowSetBase::impl_getRowCount() will lie and currentRowCount()
// returns 1 more than the actual number of rows and then
// positionRandom() followed by deleteRow() deletes *last* row
final int position = (new Random()).nextInt(currentRowCount() - 3) + 2;
assertTrue("sub task failed: could not position to row no. " + (Integer.valueOf(position)).toString(),
m_resultSet.absolute(position));
return m_resultSet.getRow();
}
/** moves the result set to a random record between 2 and (current row count - 1), and deletes this record
*
* After returning from this method, the row set is still positioned at the deleted record
* @return
* the number/position of the record which has been deleted
*/
private int deleteRandom() throws SQLException, UnknownPropertyException, WrappedTargetException
{
// check if the current position and the row count in the result set is changed by a deletion (it should not)
final int positionBefore = positionRandom();
final int rowCountBefore = currentRowCount();
m_resultSetUpdate.deleteRow();
final int positionAfter = m_resultSet.getRow();
final int rowCountAfter = currentRowCount();
assertTrue("position changed during |deleteRow| (it should not)", positionAfter == positionBefore);
assertTrue("row count changed with a |deleteRow| (it should not)", rowCountBefore == rowCountAfter);
assertTrue("RowSet does not report the current row as deleted after |deleteRow|", m_resultSet.rowDeleted());
return positionBefore;
}
@Test
public void testDeleteBehavior() throws Exception
{
createTestCase(true);
// ensure that all records are known
m_resultSet.last();
final int initialRowCount = currentRowCount();
// delete a random row
int deletedRow = deleteRandom();
// asking for the bookmark of a deleted row should fail
boolean caughtException = false;
try
{
m_rowLocate.getBookmark();
}
catch (SQLException e)
{
caughtException = true;
}
assertTrue("asking for the bookmark of a deleted row should throw an exception", caughtException);
// isXXX methods should return |false| on a deleted row
assertTrue("one of the isFoo failed after |deleteRow|", !m_resultSet.isBeforeFirst() && !m_resultSet.isAfterLast() && !m_resultSet.isFirst() && !m_resultSet.isLast());
// note that we can assume that isFirst / isLast also return |false|, since deleteRandom did
// not position on the first or last record, but inbetween
// check if moving away from this row in either direction yields the expected results
assertTrue("|previous| after |deleteRow| failed", m_resultSet.previous());
final int positionPrevious = m_resultSet.getRow();
assertTrue("position after |previous| after |deleteRow| is not as expected", positionPrevious == deletedRow - 1);
deletedRow = deleteRandom();
assertTrue("|next| after |deleteRow| failed", m_resultSet.next());
final int positionAfter = m_resultSet.getRow();
assertTrue("position after |next| after |deleteRow| is not as expected", positionAfter == deletedRow);
// since the deleted record "vanishes" as soon as the cursor is moved away from it, the absolute position does
// not change with a |next| call here
// check if the deleted rows really vanished after moving away from them
assertTrue("row count did not change as expected after two deletions", initialRowCount - 2 == currentRowCount());
// check if the deleted row vanishes after moving to the insertion row
final int rowCountBefore = currentRowCount();
final int deletedPos = deleteRandom();
m_resultSetUpdate.moveToInsertRow();
assertTrue("moving to the insertion row immediately after |deleteRow| does not adjust the row count", rowCountBefore == currentRowCount() + 1);
m_resultSetUpdate.moveToCurrentRow();
assertTrue("|moveToCurrentRow| after |deleteRow| + |moveToInsertRow| results in unexpected position",
(m_resultSet.getRow() == deletedPos) && !m_resultSet.rowDeleted());
// the same, but this time with deleting the first row (which is not covered by deleteRandom)
m_resultSet.last();
m_resultSetUpdate.deleteRow();
m_resultSetUpdate.moveToInsertRow();
m_resultSetUpdate.moveToCurrentRow();
assertTrue("|last| + |deleteRow| + |moveToInsertRow| + |moveToCurrentRow| results in wrong state", m_resultSet.isAfterLast());
// check if deleting a deleted row fails as expected
deleteRandom();
caughtException = false;
try
{
m_resultSetUpdate.deleteRow();
}
catch (SQLException e)
{
caughtException = true;
}
assertTrue("deleting a deleted row succeeded - it shouldn't", caughtException);
// check if deleteRows fails if it contains the bookmark of a previously-deleted row
m_resultSet.first();
final Object firstBookmark = m_rowLocate.getBookmark();
positionRandom();
final Object deleteBookmark = m_rowLocate.getBookmark();
m_resultSetUpdate.deleteRow();
final XDeleteRows multiDelete = UnoRuntime.queryInterface( XDeleteRows.class, m_resultSet );
final int[] deleteSuccess = multiDelete.deleteRows(new Object[]
{
firstBookmark, deleteBookmark
});
assertTrue("XDeleteRows::deleteRows with the bookmark of an already-deleted row failed",
(deleteSuccess.length == 2) && (deleteSuccess[0] != 0) && (deleteSuccess[1] == 0));
// check if refreshing a deleted row fails as expected
deleteRandom();
caughtException = false;
try
{
m_resultSet.refreshRow();
}
catch (SQLException e)
{
caughtException = true;
}
assertTrue("refreshing a deleted row succeeded - it shouldn't", caughtException);
// rowUpdated/rowDeleted
deleteRandom();
assertTrue("rowDeleted and/or rowUpdated are wrong on a deleted row", !m_resultSet.rowUpdated() && !m_resultSet.rowInserted());
// updating values in a deleted row should fail
deleteRandom();
final XRowUpdate rowUpdated = UnoRuntime.queryInterface( XRowUpdate.class, m_resultSet );
caughtException = false;
try
{
rowUpdated.updateString(2, TEST21);
}
catch (SQLException e)
{
caughtException = true;
}
assertTrue("updating values in a deleted row should not succeed", caughtException);
}
/** checks whether deletions on the main RowSet properly interfere (or don't interfere) with the movement
* on a clone of the RowSet
*/
@Test
public void testCloneMovesPlusDeletions() throws Exception
{
createTestCase(true);
// ensure that all records are known
m_resultSet.last();
final XResultSet clone = createClone();
final XRowLocate cloneRowLocate = UnoRuntime.queryInterface( XRowLocate.class, clone );
positionRandom();
// move the clone to the same record as the RowSet, and delete this record
cloneRowLocate.moveToBookmark(m_rowLocate.getBookmark());
final int clonePosition = clone.getRow();
m_resultSetUpdate.deleteRow();
assertTrue("clone doesn't know that its current row has been deleted via the RowSet", clone.rowDeleted());
assertTrue("clone's position changed somehow during deletion", clonePosition == clone.getRow());
// move the row set away from the deleted record. This should still not touch the state of the clone
m_resultSet.previous();
assertTrue("clone doesn't know (anymore) that its current row has been deleted via the RowSet", clone.rowDeleted());
assertTrue("clone's position changed somehow during deletion and RowSet-movement", clonePosition == clone.getRow());
// move the clone away from the deleted record
clone.next();
assertTrue("clone still assumes that its row is deleted - but we already moved it", !clone.rowDeleted());
// check whether deleting the extremes (first / last) work
m_resultSet.first();
cloneRowLocate.moveToBookmark(m_rowLocate.getBookmark());
m_resultSetUpdate.deleteRow();
clone.previous();
assertTrue("deleting the first record left the clone in a strange state (after |previous|)", clone.isBeforeFirst());
clone.next();
assertTrue("deleting the first record left the clone in a strange state (after |previous| + |next|)", clone.isFirst());
m_resultSet.last();
cloneRowLocate.moveToBookmark(m_rowLocate.getBookmark());
m_resultSetUpdate.deleteRow();
clone.next();
assertTrue("deleting the last record left the clone in a strange state (after |next|)", clone.isAfterLast());
clone.previous();
assertTrue("deleting the first record left the clone in a strange state (after |next| + |previous|)", clone.isLast());
// check whether movements of the clone interfere with movements of the RowSet, if the latter is on a deleted row
final int positionBefore = positionRandom();
m_resultSetUpdate.deleteRow();
assertTrue("|deleteRow|, but no |rowDeleted| (this should have been found much earlier!)", m_resultSet.rowDeleted());
clone.beforeFirst();
while (clone.next()) {}
assertTrue("row set forgot that the current row is deleted", m_resultSet.rowDeleted());
assertTrue("moving to the next record after |deleteRow| and clone moves failed", m_resultSet.next());
assertTrue("wrong position after |deleteRow| and clone movement", !m_resultSet.isAfterLast() && !m_resultSet.isBeforeFirst());
assertTrue("wrong absolute position after |deleteRow| and clone movement", m_resultSet.getRow() == positionBefore);
}
/** checks whether insertions on the main RowSet properly interfere (or don't interfere) with the movement
* on a clone of the RowSet
*/
@Test
public void testCloneMovesPlusInsertions() throws Exception
{
createTestCase(true);
// ensure that all records are known
m_rowSetProperties.setPropertyValue("FetchSize", Integer.valueOf(10));
final XResultSet clone = createClone();
final XRow cloneRow = UnoRuntime.queryInterface( XRow.class, clone );
// first check the basic scenario without the |moveToInsertRow| |moveToCurrentRow|, to ensure that
// really those are broken, if at all
m_resultSet.last();
clone.first();
clone.absolute(11);
clone.first();
final int rowValue1 = m_row.getInt(1);
final int rowPos = m_resultSet.getRow();
final int rowValue2 = m_row.getInt(1);
assertTrue("repeated query for the same column value delivers different values (" + rowValue1 + " and " + rowValue2 + ") on row: " + rowPos,
rowValue1 == rowValue2);
testPosition(clone, cloneRow, 1, "mixed clone/rowset move: clone check");
testPosition(m_resultSet, m_row, MAX_TABLE_ROWS, "mixed clone/rowset move: rowset check");
// now the complete scenario
m_resultSet.last();
m_resultSetUpdate.moveToInsertRow();
clone.first();
clone.absolute(11);
clone.first();
m_resultSetUpdate.moveToCurrentRow();
testPosition(clone, cloneRow, 1, "mixed clone/rowset move/insertion: clone check");
testPosition(m_resultSet, m_row, 100, "mixed clone/rowset move/insertion: rowset check");
}
private void testTableParameters() throws com.sun.star.uno.Exception
{
// for a row set simply based on a table, there should be not parameters at all
createRowSet("products", CommandType.TABLE, false);
verifyParameters(new String[]
{
}, "testTableParameters");
}
private void testParametersAfterNormalExecute() throws com.sun.star.uno.Exception
{
createRowSet("SELECT * FROM \"customers\"", CommandType.COMMAND, true);
m_rowSetProperties.setPropertyValue("Command", "SELECT * FROM \"customers\" WHERE \"City\" = :city");
final XParameters rowsetParams = UnoRuntime.queryInterface( XParameters.class, m_rowSet );
rowsetParams.setString(1, "London");
m_rowSet.execute();
}
private void verifyParameters(String[] _paramNames, String _context) throws com.sun.star.uno.Exception
{
final XIndexAccess params = m_paramsSupplier.getParameters();
final int expected = _paramNames.length;
final int found = params != null ? params.getCount() : 0;
assertTrue("wrong number of parameters (expected: " + expected + ", found: " + found + ") in " + _context,
found == expected);
if (found == 0)
{
return;
}
for (int i = 0; i < expected; ++i)
{
final XPropertySet parameter = UnoRuntime.queryInterface( XPropertySet.class, params.getByIndex( i ) );
final String expectedName = _paramNames[i];
final String foundName = (String) parameter.getPropertyValue("Name");
assertTrue("wrong parameter name (expected: " + expectedName + ", found: " + foundName + ") in" + _context,
expectedName.equals(foundName));
}
}
private void testParametrizedQuery() throws com.sun.star.uno.Exception
{
// for a row set based on a parametrized query, those parameters should be properly
// recognized
m_dataSource.createQuery("products like", "SELECT * FROM \"products\" WHERE \"Name\" LIKE :product_name");
createRowSet("products like", CommandType.QUERY, false);
verifyParameters(new String[]
{
"product_name"
}, "testParametrizedQuery");
}
private void testParametersInteraction() throws com.sun.star.uno.Exception
{
createRowSet("products like", CommandType.QUERY, false);
// let's fill in a parameter value via XParameters, and see whether it is respected by the parameters container
final XParameters rowsetParams = UnoRuntime.queryInterface(XParameters.class, m_rowSet);
rowsetParams.setString(1, "Apples");
XIndexAccess params = m_paramsSupplier.getParameters();
XPropertySet firstParam = UnoRuntime.queryInterface( XPropertySet.class, params.getByIndex( 0 ) );
Object firstParamValue = firstParam.getPropertyValue("Value");
assertTrue("XParameters and the parameters container do not properly interact",
"Apples".equals(firstParamValue));
// let's see whether this also survives an execute of the row set
rowsetParams.setString(1, "Oranges");
m_rowSet.execute();
{
// TODO: the following would not be necessary if the parameters container would *survive*
// the execution of the row set. It currently doesn't (though the values it represents do).
// It would be nice, but not strictly necessary, if it would.
params = m_paramsSupplier.getParameters();
firstParam = UnoRuntime.queryInterface( XPropertySet.class, params.getByIndex( 0 ) );
}
firstParamValue = firstParam.getPropertyValue("Value");
assertTrue("XParameters and the parameters container do not properly interact, after the row set has been executed",
"Oranges".equals(firstParamValue));
}
private void testParametersInFilter() throws com.sun.star.uno.Exception
{
createRowSet("SELECT * FROM \"customers\"", CommandType.COMMAND, false);
m_rowSetProperties.setPropertyValue("Filter", "\"City\" = :city");
m_rowSetProperties.setPropertyValue("ApplyFilter", Boolean.TRUE);
verifyParameters(new String[]
{
"city"
}, "testParametersInFilter");
m_rowSetProperties.setPropertyValue("ApplyFilter", Boolean.FALSE);
verifyParameters(new String[]
{
}, "testParametersInFilter");
}
/** checks the XParametersSupplier functionality of a RowSet
*/
@Test
public void testParameters() throws Exception
{
createTestCase(false);
// use an own RowSet instance, not the one which is also used for the other cases
testTableParameters();
testParametrizedQuery();
testParametersInFilter();
testParametersAfterNormalExecute();
testParametersInteraction();
}
}
|