summaryrefslogtreecommitdiff
path: root/src/test/java/com/amazon/carbonado/TestStorables.java
blob: 90048d737e0cf46827ea7e3ef1a0a8641a9f5be3 (plain)
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
/*
 * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado;

import java.util.Comparator;
import java.util.List;

import junit.framework.TestCase;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.amazon.carbonado.ConstraintException;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.FetchNoneException;
import com.amazon.carbonado.OptimisticLockException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.PersistMultipleException;
import com.amazon.carbonado.PersistNoneException;
import com.amazon.carbonado.PrimaryKey;
import com.amazon.carbonado.Query;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Trigger;
import com.amazon.carbonado.UniqueConstraintException;

import com.amazon.carbonado.cursor.SortedCursor;

import com.amazon.carbonado.spi.RepairExecutor;
import com.amazon.carbonado.spi.WrappedSupport;

import com.amazon.carbonado.stored.*;

/**
 * Runs an extensive set of acceptance tests for a repository. Must be
 * subclassed to specify a repository to use.
 *
 * @author Don Schneider
 * @author Brian S O'Neill
 */
public abstract class TestStorables extends TestCase {

    public static final long sSetId = 0x1L << 0;           // 0x0001
    public static final long sGetStringProp = 0x1L << 1;   // 0x0002
    public static final long sSetStringProp = 0x1L << 2;   // 0x0004
    public static final long sGetIntProp = 0x1L << 3;      // 0x0008
    public static final long sSetIntProp = 0x1L << 4;      // 0x0010
    public static final long sGetLongProp = 0x1L << 5;     // 0x0020
    public static final long sSetLongProp = 0x1L << 6;     // 0x0040
    public static final long sGetDoubleProp = 0x1L << 7;   // 0x0080
    public static final long sSetDoubleProp = 0x1L << 8;   // 0x0100
    public static final long sLoad = 0x1L << 9;            // 0x0200
    public static final long sTryLoad = 0x1L << 10;        // 0x0400
    public static final long sInsert = 0x1L << 11;         // 0x0800
    public static final long sTryInsert = 0x1L << 31;      // 0x8000 0000
    public static final long sUpdate = 0x1L << 32;         // 0x0001 0000 0000
    public static final long sTryUpdate = 0x1L << 12;      // 0x1000
    public static final long sDelete = 0x1L << 33;         // 0x0002 0000 0000
    public static final long sTryDelete = 0x1L << 13;      // 0x2000
    public static final long sStorage = 0x1L << 14;        // 0x4000
    public static final long sCopy = 0x1L << 15;           // 0x8000
    public static final long sToStringKeyOnly = 0x1L << 16;          // 0x0001 0000
    public static final long sGetId = 0x1L << 17;                    // 0x0002 0000
    public static final long sCopyAllProperties = 0x1L << 18;        // 0x0004 0000
    public static final long sCopyPrimaryKeyProperties = 0x1L << 19; // 0x0080 0000
    public static final long sCopyUnequalProperties = 0x1L << 20;    // 0x0010 0000
    public static final long sCopyDirtyProperties = 0x1L << 21;      // 0x0020 0000
    public static final long sHasDirtyProperties = 0x1L << 25;       // 0x0040 0000
    public static final long sEqualKeys = 0x1L << 22;                // 0x0080 0000
    public static final long sEqualProperties = 0x1L << 23;          // 0x0100 0000
    public static final long sCopyVersionProperty = 0x1L << 24;      // 0x0200 0000
    public static final long sMarkPropertiesClean = 0x1L << 26;      // 0x0400 0000
    public static final long sMarkAllPropertiesClean = 0x1L << 27;   // 0x0800 0000
    public static final long sMarkPropertiesDirty = 0x1L << 28;      // 0x1000 0000
    public static final long sMarkAllPropertiesDirty = 0x1L << 29;   // 0x2000 0000
    public static final long sStorableType = 0x1L << 30;             // 0x4000 0000

    public static final long ALL_SET_METHODS =       // 0x00000155;
            sSetId + sSetStringProp + sSetIntProp + sSetLongProp + sSetDoubleProp;
    public static final long ALL_GET_METHODS =       // 0x000200AA;
            sGetId + sGetStringProp + sGetIntProp + sGetLongProp + sGetDoubleProp;
    public static final long ALL_PRIMARY_KEYS = sSetId;     // 0x00000001;
    public static final long ALL_COPY_PROP_METHODS =  // 0x003C0000;
            sCopyAllProperties + sCopyPrimaryKeyProperties + sCopyUnequalProperties +
            sCopyDirtyProperties + sCopyVersionProperty;
    public static final long ALL_INTERFACE_METHODS = // 0x43C1FE00;
            sLoad + sTryLoad + sInsert + sUpdate + sDelete + sStorage + sCopy + sToStringKeyOnly +
            ALL_COPY_PROP_METHODS + sHasDirtyProperties + sEqualKeys + sEqualProperties +
            sMarkPropertiesClean + sMarkPropertiesDirty +
            sMarkAllPropertiesClean + sMarkAllPropertiesDirty + sStorableType;

    private Repository mRepository;
    private static int s_Ids = 0;

    public TestStorables(String s) {
        super(s);
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        if (mRepository != null) {
            // The test may have thrown exceptions which cause some
            // repositories to kick off asynchronous repairs. They will
            // immediately fail since the repository is about to be
            // closed. This just eliminates uninteresting errors from being
            // logged.
            try {
                RepairExecutor.waitForRepairsToFinish(10000);
            }
            catch (InterruptedException e) {
            }

            mRepository.close();
            mRepository = null;
        }
    }

    /**
     * Subclasses must implement this method to specify a repository.
     */
    protected abstract Repository newRepository(boolean isMaster)
        throws RepositoryException;

    /**
     * provide subsequent access to the repository so the tests can do fancy things if
     * interested.
     * @return
     */
    protected Repository getRepository() throws RepositoryException {
        if (mRepository == null) {
            mRepository = newRepository(true);
        }
        return mRepository;
    }

    /**
     * Create a random ID to eliminate optimistic lock conflicts due to ID collisions
     * @param seed
     * @return
     */
    private int generateId(int seed) {
        return seed*10000 + ((int)System.currentTimeMillis()) + s_Ids++;
    }

    public void test_createAndRetrieve() throws Exception {
        Storage<StorableTestBasic> storageSB =
            getRepository().storageFor(StorableTestBasic.class);

        StorableTestBasic sb = storageSB.prepare();
        final int id = generateId(0);
        sb.setId(id);
        sb.setIntProp(1);
        sb.setLongProp(1);
        sb.setDoubleProp(1.1);
        sb.setStringProp("one");
        sb.setDate(new DateTime("2005-08-26T08:09:00.000"));
        sb.insert();

        StorableTestBasic sb_load = storageSB.prepare();
        sb_load.setId(id);
        sb_load.load();
        assertEquals(sb, sb_load);

        // Try re-inserting
        // First, make sure the system disallows setting pk for loaded object
        try {
            sb.setId(id);
            fail("successfully set pk on loaded object");
        }
        catch (Exception e) {
        }

        // Then the more common way: just create an identical new one
        StorableTestBasic sbr = storageSB.prepare();
        sbr.setId(id);
        sbr.setIntProp(1);
        sbr.setLongProp(1);
        sbr.setDoubleProp(1.1);
        sbr.setStringProp("one");
        sb.setDate(new DateTime("2005-08-26T08:09:00.000"));
        try {
            sbr.insert();
            fail("PK constraint violation ignored");
        }
        catch (UniqueConstraintException e) {
        }


        Storage<StorableTestMultiPK> storageMPK =
                getRepository().storageFor(StorableTestMultiPK.class);
        StorableTestMultiPK smpk = storageMPK.prepare();
        smpk.setIdPK(0);
        smpk.setStringPK("zero");
        smpk.setStringData("and some data");
        smpk.insert();

        StorableTestMultiPK smpk_load = storageMPK.prepare();
        smpk_load.setIdPK(0);
        smpk_load.setStringPK("zero");
        smpk_load.load();
        assertEquals(smpk, smpk_load);
    }

    public void test_storableStorableStates() throws Exception {

        Storage<StorableTestKeyValue> storageMinimal =
            getRepository().storageFor(StorableTestKeyValue.class);

        // Start by just putting some targets in the repository
        for (int i = 0; i < 10; i++) {
            insert(storageMinimal, 100+i, 200+i);
        }

        StorableTestKeyValue s = storageMinimal.prepare();
        StorableTestKeyValue s2 = storageMinimal.prepare();

        // State: unloaded
        // pk incomplete
        assertInoperable(s, "new - untouched");
        assertFalse(s.hasDirtyProperties());

        // new --set(pk)--> loadable incomplete
        s.setKey1(0);
        assertInoperable(s, "Loadable Incomplete");
        assertFalse(s.hasDirtyProperties());

        s.setKey1(101);
        assertInoperable(s, "loadable incomplete (2nd)");
        assertFalse(s.hasDirtyProperties());

        // loadable incomplete --pkFilled--> loadable ready
        s.setKey2(201);
        assertEquals(true, s.tryDelete());
        assertFalse(s.hasDirtyProperties());

        s.setKey1(102);
        s.setKey2(202);
        assertEquals(true, s.tryDelete());
        assertFalse(s.hasDirtyProperties());

        // loadable ready --load()--> loaded
        s.setKey1(103);
        s.setKey2(203);
        s.load();
        assertEquals(s.getValue1(), 1030);
        assertEquals(s.getValue2(), 20300);
        assertNoInsert(s, "written");
        assertNoSetPK(s, "written");
        assertFalse(s.hasDirtyProperties());

        s2.setKey1(103);
        s2.setKey2(203);
        assertFalse(s2.hasDirtyProperties());
        s2.load();
        assertFalse(s2.hasDirtyProperties());

        assertTrue(s.equalPrimaryKeys(s2));
        assertTrue(s.equalProperties(s2));
        assertEquals(s.storableType(), s2.storableType());
        assertTrue(s.equals(s2));
        assertEquals(s, s2);
        s.setValue1(11);
        s.setValue2(11111);
        assertEquals(true, s.tryUpdate());
        assertEquals(11, s.getValue1());
        assertEquals(11111, s.getValue2());
        s2.load();
        assertEquals(s, s2);

        StorableTestKeyValue s3 = storageMinimal.prepare();
        s.copyPrimaryKeyProperties(s3);
        s3.tryUpdate();
        assertEquals(s, s3);

        s.setValue2(222222);
        assertTrue(s.hasDirtyProperties());
        s.load();
        assertFalse(s.hasDirtyProperties());
        assertEquals(s, s2);

        // Update should return true, even though it probably didn't actually
        // touch the storage layer.
        assertEquals(true, s.tryUpdate());

        s.tryDelete();
        assertNoLoad(s, "deleted");
        // After delete, saved properties remain dirty.
        assertTrue(s.hasDirtyProperties());

        s.insert();
        assertFalse(s.hasDirtyProperties());
        s.load();
        assertFalse(s.hasDirtyProperties());
        assertEquals(s, s2);
    }

    public void test_storableInteractions() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);
        StorableTestBasic s = storage.prepare();
        final int id = generateId(111);
        s.setId(id);
        s.initBasicProperties();
        assertTrue(s.hasDirtyProperties());
        s.insert();
        assertFalse(s.hasDirtyProperties());

        StorableTestBasic s2 = storage.prepare();
        s2.setId(id);
        s2.load();
        assertTrue(s2.equalPrimaryKeys(s));
        assertTrue(s2.equalProperties(s));
        assertTrue(s2.equals(s));

        StorableTestBasic s3 = storage.prepare();
        s3.setId(id);
        s3.tryDelete();

        // Should be able to re-insert.
        s2.insert();
        s2.load();
        assertTrue(s2.equalPrimaryKeys(s));
        assertTrue(s2.equalProperties(s));
        assertTrue(s2.equals(s));

        // Delete in preparation for next test.
        s3.tryDelete();

        s.setStringProp("updated by s");
        // The object is gone, we can't update it
        assertEquals(false, s.tryUpdate());
        // ...or load it
        try {
            s2.load();
            fail("shouldn't be able to load a deleted object");
        }
        catch (FetchException e) {
        }
    }

    public void test_assymetricStorable() throws Exception {
        try {
            Storage<StorableTestAssymetric> storage =
                    getRepository().storageFor(StorableTestAssymetric.class);
        }
        catch (Exception e) {
            fail("exception creating storable with assymetric concrete getter?" + e);
        }
        try {
            Storage<StorableTestAssymetricGet> storage =
                    getRepository().storageFor(StorableTestAssymetricGet.class);
            fail("Created assymetric storabl`e");
        }
        catch (Exception e) {
        }
        try {
            Storage<StorableTestAssymetricSet> storage =
                    getRepository().storageFor(StorableTestAssymetricSet.class);
            fail("Created assymetric storable");
        }
        catch (Exception e) {
        }
        try {
            Storage<StorableTestAssymetricGetSet> storage =
                    getRepository().storageFor(StorableTestAssymetricGetSet.class);
            fail("Created assymetric storable");
        }
        catch (Exception e) {
        }
    }

    private void assertNoSetPK(StorableTestKeyValue aStorableTestKeyValue,
                               String aState)
    {
        try {
            aStorableTestKeyValue.setKey1(1111);
            fail("'set pk' for '" + aState + "' succeeded");
        }
        catch (Exception e) {
        }
    }

    private void assertInoperable(Storable aStorable, String aState) {
        assertNoInsert(aStorable, aState);
        assertNoUpdate(aStorable, aState);
        assertNoLoad(aStorable, aState);
        assertNoDelete(aStorable, aState);
    }

    private void assertNoDelete(Storable aStorable, String aState) {
        try {
            aStorable.tryDelete();
            fail("'delete' for '" + aState + "' succeeded");
        }
        catch (PersistException e) {
        }
        catch (IllegalStateException e) {
        }
    }

    private void assertNoLoad(Storable aStorable, String aState) {
        try {
            aStorable.load();
            fail("'load' for '" + aState + "' succeeded");
        }
        catch (FetchException e) {
        }
        catch (IllegalStateException e) {
        }
    }

    private void assertNoInsert(Storable aStorable, String aState) {
        try {
            aStorable.insert();
            fail("'insert' for '" + aState + "' succeeded");
        }
        catch (PersistException e) {
        }
        catch (IllegalStateException e) {
        }
    }

    private void assertNoUpdate(Storable aStorable, String aState) {
        try {
            aStorable.tryUpdate();
            fail("'update' for '" + aState + "' succeeded");
        }
        catch (PersistException e) {
        }
        catch (IllegalStateException e) {
        }
    }

    private void insert(Storage<StorableTestKeyValue> aStorageMinimal,
                        int key1,
                        int key2) throws PersistException
    {
        StorableTestKeyValue s = aStorageMinimal.prepare();
        s.setKey1(key1);
        s.setKey2(key2);
        s.setValue1(key1*10);
        s.setValue2(key2*100);
        s.insert();
    }

    public void test_copyStorableProperties() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);
        StorableTestBasic storable = storage.prepare();

        InvocationTracker tracker = new InvocationTracker("tracker", false);
        storable.copyAllProperties(tracker);
        // unloaded, untouched; nothing happens
        tracker.assertTrack(0);

        storable.setId(generateId(1));
        storable.setIntProp(1);
        storable.copyAllProperties(tracker);
        tracker.assertTrack(0x1 + 0x10);
        tracker.clearTracks();

        storable.initBasicProperties();
        storable.copyAllProperties(tracker);
        tracker.assertTrack(ALL_SET_METHODS);
        tracker.clearTracks();

        storable = storage.prepare();
        storable.copyPrimaryKeyProperties(tracker);
        tracker.assertTrack(0);
        storable.initPrimaryKeyProperties();
        storable.copyPrimaryKeyProperties(tracker);
        tracker.assertTrack(ALL_PRIMARY_KEYS);
        tracker.clearTracks();

        storable = storage.prepare();
        storable.copyUnequalProperties(tracker);
        tracker.assertTrack(0);
        storable.setIntProp(0);  // this will now be dirty, and equal
        storable.copyUnequalProperties(tracker);
        tracker.assertTrack(0x8);
        storable.setIntProp(1);  // this will now be dirty and not equal
        storable.copyUnequalProperties(tracker);
        tracker.assertTrack(0x8 | 0x10);

        // get a fresh one
        storable = storage.prepare();
        storable.setStringProp("hi");
        storable.setId(22);
        storable.copyPrimaryKeyProperties(tracker);
        storable.copyDirtyProperties(tracker);
        tracker.assertTrack(0x05);
    }

    public void test_copy() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);
        StorableTestBasic storable = storage.prepare();

        storable.setId(5);
        storable.setStringProp("hello");
        storable.setIntProp(512);
        storable.setLongProp(45354L);
        storable.setDoubleProp(56734.234);

        Storable copy = storable.copy();

        assertEquals(storable.getClass(), copy.getClass());
        assertEquals(storable, copy);

        StorableTestBasic castedCopy = (StorableTestBasic) copy;

        assertEquals(storable.getId(), castedCopy.getId());
        assertEquals(storable.getStringProp(), castedCopy.getStringProp());
        assertEquals(storable.getIntProp(), castedCopy.getIntProp());
        assertEquals(storable.getLongProp(), castedCopy.getLongProp());
        assertEquals(storable.getDoubleProp(), castedCopy.getDoubleProp());
    }

    public void test_invalidStorables() throws Exception {
        try {
            getRepository().storageFor(StorableTestInvalid.class);
            fail("prepared invalid storable");
        }
        catch (RepositoryException e) {
        }
    }

    public void test_invalidPatterns() throws Exception {
        // Minimal -- try setting with no PK
        Storage<StorableTestMinimal> storageMinimal =
                getRepository().storageFor(StorableTestMinimal.class);
        StorableTestMinimal s = storageMinimal.prepare();
        assertNoInsert(s, "new (minimal)");

        s.setId(generateId(0));
        s.insert();

        // Basic -- try combinations of PK, fields
        // First, fill in all the fields but no PK
        Storage<StorableTestBasic> storageBasic =
                getRepository().storageFor(StorableTestBasic.class);
        StorableTestBasic sb = storageBasic.prepare();
        assertNoInsert(sb, "new (basic)");;

        sb.setIntProp(0);
        sb.setIntProp(1);
        sb.setLongProp(1);
        sb.setDoubleProp(1.1);
        sb.setStringProp("one");
        sb.setDate(new DateTime("2005-08-26T08:09:00.000"));
        assertNoInsert(sb, "SB: Storable incomplete (pkMissing)");

        sb.setId(generateId(2));
        sb.insert();

        // Now try leaving one of the fields empty.
        sb = storageBasic.prepare();
        final int id = generateId(3);
        sb.setId(id);
        sb.setIntProp(0);
        sb.setIntProp(1);
        sb.setLongProp(1);
        sb.setDoubleProp(1.1);
        try {
            sb.insert();
            fail();
        } catch (ConstraintException e) {
        }

        sb = storageBasic.prepare();
        sb.setId(id);
        try {
            sb.load();
            fail();
        } catch (FetchNoneException e) {
        }
    }

    public void test_nonDestructiveUpdate() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);
        StorableTestBasic s = storage.prepare();

        int id = generateId(3943945);
        s.setId(id);
        s.setStringProp("hello");
        s.setIntProp(56);
        s.setLongProp(99999999999999999L);
        s.setDoubleProp(Double.NaN);

        s.insert();

        s = storage.prepare();

        s.setId(id);
        s.setIntProp(100);
        assertEquals(true, s.tryUpdate());

        assertEquals("hello", s.getStringProp());
        assertEquals(100, s.getIntProp());
        assertEquals(99999999999999999L, s.getLongProp());
        assertEquals(Double.NaN, s.getDoubleProp());

        s = storage.prepare();

        s.setId(id);
        s.load();

        assertEquals("hello", s.getStringProp());
        assertEquals(100, s.getIntProp());
        assertEquals(99999999999999999L, s.getLongProp());
        assertEquals(Double.NaN, s.getDoubleProp());
    }

    public void test_updateLoadSideEffect() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);

        StorableTestBasic s = storage.prepare();
        final int id = generateId(500);
        s.setId(id);
        s.setStringProp("hello");
        s.setIntProp(10);
        s.setLongProp(123456789012345L);
        s.setDoubleProp(Double.POSITIVE_INFINITY);
        s.insert();

        s = storage.prepare();
        s.setId(id);

        assertEquals(id, s.getId());
        assertEquals(0, s.getIntProp());
        assertEquals(0L, s.getLongProp());
        assertEquals(0.0, s.getDoubleProp());

        assertFalse(s.hasDirtyProperties());

        // Even if nothing was updated, must load fresh copy.
        assertTrue(s.tryUpdate());
        assertEquals(id, s.getId());
        assertEquals(10, s.getIntProp());
        assertEquals(123456789012345L, s.getLongProp());
        assertEquals(Double.POSITIVE_INFINITY, s.getDoubleProp());
    }

    public void test_versioning() throws Exception {
        Storage<StorableVersioned> storage = getRepository().storageFor(StorableVersioned.class);

        StorableVersioned s = storage.prepare();
        s.setID(500);
        s.setValue("hello");
        try {
            // Require version property to be set.
            s.tryUpdate();
            fail();
        } catch (IllegalStateException e) {
        }

        s.setVersion(1);
        try {
            // Cannot update that which does not exist.
            s.update();
            fail();
        } catch (PersistNoneException e) {
        }

        s.insert();

        s.setVersion(2);
        try {
            // Record version mismatch.
            s.tryUpdate();
            fail();
        } catch (OptimisticLockException e) {
        }

        s.setVersion(1);
        s.setValue("world");
        s.tryUpdate();

        assertEquals(2, s.getVersion());
        assertEquals("world", s.getValue());

        // Since no properties changed, update does not increase version.
        assertEquals(true, s.tryUpdate());
        assertEquals(2, s.getVersion());

        // Simple test to ensure that version property doesn't need to be
        // dirtied.
        s = storage.prepare();
        s.setID(500);
        s.load();
        s.setValue("hello");
        assertTrue(s.tryUpdate());
    }

    public void test_versioningWithLong() throws Exception {
        Storage<StorableVersionedWithLong> storage =
            getRepository().storageFor(StorableVersionedWithLong.class);

        StorableVersionedWithLong s = storage.prepare();
        s.setID(500);
        s.setValue("hello");
        try {
            // Require version property to be set.
            s.tryUpdate();
            fail();
        } catch (IllegalStateException e) {
        }

        s.setVersion(1);
        try {
            // Cannot update that which does not exist.
            s.update();
            fail();
        } catch (PersistNoneException e) {
        }

        s.insert();

        s.setVersion(2);
        try {
            // Record version mismatch.
            s.tryUpdate();
            fail();
        } catch (OptimisticLockException e) {
        }

        s.setVersion(1);
        s.setValue("world");
        s.tryUpdate();

        assertEquals(2, s.getVersion());
        assertEquals("world", s.getValue());

        // Since no properties changed, update does not increase version.
        assertEquals(true, s.tryUpdate());
        assertEquals(2, s.getVersion());
    }

    public void test_versioningWithObj() throws Exception {
        Storage<StorableVersionedWithObj> storage =
            getRepository().storageFor(StorableVersionedWithObj.class);

        StorableVersionedWithObj s = storage.prepare();
        s.setID(generateId(500));
        s.setValue("hello");
        try {
            // Require version property to be set.
            s.tryUpdate();
            fail();
        } catch (IllegalStateException e) {
        }

        s.setVersion(null);
        try {
            // Cannot update that which does not exist.
            s.update();
            fail();
        } catch (PersistNoneException e) {
        }

        s.insert();

        assertNull(s.getVersion());

        s.setVersion(2);
        try {
            // Record version mismatch.
            s.tryUpdate();
            fail();
        } catch (OptimisticLockException e) {
        }

        s.setVersion(null);
        s.setValue("world");
        s.tryUpdate();

        assertEquals((Integer) 1, s.getVersion());
        assertEquals("world", s.getValue());

        s.setValue("value");
        s.tryUpdate();

        assertEquals((Integer) 2, s.getVersion());
        assertEquals("value", s.getValue());

        // Since no properties changed, update does not increase version.
        assertEquals(true, s.tryUpdate());
        assertEquals((Integer) 2, s.getVersion());
    }

    public void test_versioningWithLongObj() throws Exception {
        Storage<StorableVersionedWithLongObj> storage =
            getRepository().storageFor(StorableVersionedWithLongObj.class);

        StorableVersionedWithLongObj s = storage.prepare();
        s.setID(500);
        s.setValue("hello");
        try {
            // Require version property to be set.
            s.tryUpdate();
            fail();
        } catch (IllegalStateException e) {
        }

        s.setVersion(null);
        try {
            // Cannot update that which does not exist.
            s.update();
            fail();
        } catch (PersistNoneException e) {
        }

        s.insert();

        assertNull(s.getVersion());

        s.setVersion(2L);
        try {
            // Record version mismatch.
            s.tryUpdate();
            fail();
        } catch (OptimisticLockException e) {
        }

        s.setVersion(null);
        s.setValue("world");
        s.tryUpdate();

        assertEquals((Long) 1L, s.getVersion());
        assertEquals("world", s.getValue());

        s.setValue("value");
        s.tryUpdate();

        assertEquals((Long) 2L, s.getVersion());
        assertEquals("value", s.getValue());

        // Since no properties changed, update does not increase version.
        assertEquals(true, s.tryUpdate());
        assertEquals((Long) 2L, s.getVersion());
    }

    public void test_initialVersion() throws Exception {
        Storage<StorableVersioned> storage = getRepository().storageFor(StorableVersioned.class);

        StorableVersioned s = storage.prepare();
        s.setID(987);
        s.setValue("hello");
        assertEquals(0, s.getVersion());
        s.insert();
        assertEquals(1, s.getVersion());

        s = storage.prepare();
        s.setID(12345);
        s.setValue("world");
        assertEquals(0, s.getVersion());
        s.setVersion(56);
        assertEquals(56, s.getVersion());
        s.insert();
        assertEquals(56, s.getVersion());
    }

    public void test_initialVersionWithLong() throws Exception {
        Storage<StorableVersionedWithLong> storage =
            getRepository().storageFor(StorableVersionedWithLong.class);

        StorableVersionedWithLong s = storage.prepare();
        s.setID(987);
        s.setValue("hello");
        assertEquals(0, s.getVersion());
        s.insert();
        assertEquals(1, s.getVersion());

        s = storage.prepare();
        s.setID(12345);
        s.setValue("world");
        assertEquals(0, s.getVersion());
        s.setVersion(56);
        assertEquals(56, s.getVersion());
        s.insert();
        assertEquals(56, s.getVersion());
    }

    public void test_initialVersionWithObj() throws Exception {
        Storage<StorableVersionedWithObj> storage =
            getRepository().storageFor(StorableVersionedWithObj.class);

        StorableVersionedWithObj s = storage.prepare();
        s.setID(987);
        s.setValue("hello");
        assertNull(s.getVersion());
        s.insert();
        assertEquals((Integer) 1, s.getVersion());

        s = storage.prepare();
        s.setID(12345);
        s.setValue("world");
        assertNull(s.getVersion());
        s.setVersion(56);
        assertEquals((Integer) 56, s.getVersion());
        s.insert();
        assertEquals((Integer) 56, s.getVersion());
    }

    public void test_initialVersionWithLongObj() throws Exception {
        Storage<StorableVersionedWithLongObj> storage =
            getRepository().storageFor(StorableVersionedWithLongObj.class);

        StorableVersionedWithLongObj s = storage.prepare();
        s.setID(987);
        s.setValue("hello");
        assertNull(s.getVersion());
        s.insert();
        assertEquals((Long) 1L, s.getVersion());

        s = storage.prepare();
        s.setID(12345);
        s.setValue("world");
        assertNull(s.getVersion());
        s.setVersion(56L);
        assertEquals((Long) 56L, s.getVersion());
        s.insert();
        assertEquals((Long) 56L, s.getVersion());
    }

    public void test_versioningMissingRecord() throws Exception {
        Storage<StorableVersioned> storage = getRepository().storageFor(StorableVersioned.class);

        StorableVersioned s = storage.prepare();
        s.setID(500);
        s.setValue("hello");
        s.insert();

        // Now delete it from under our feet.
        StorableVersioned s2 = storage.prepare();
        s2.setID(500);
        s2.delete();

        s.setValue("world");
        s.tryUpdate();

        s.insert();

        // Delete it again.
        s2.delete();

        // Update without changing and properties must still reload, which should fail.
        assertFalse(s.tryUpdate());
    }

    public void test_versioningDisabled() throws Exception {
        // Make sure repository works properly when configured as non-master.
        Repository repo = newRepository(false);
        Storage<StorableVersioned> storage = repo.storageFor(StorableVersioned.class);

        StorableVersioned s = storage.prepare();
        s.setID(500);
        s.setValue("hello");
        try {
            // Require version property to be set.
            s.tryUpdate();
            fail();
        } catch (IllegalStateException e) {
        }

        s.setVersion(1);
        assertEquals(false, s.tryUpdate());

        s.insert();

        s.setVersion(2);
        assertEquals(true, s.tryUpdate());

        s.setVersion(1);
        s.setValue("world");
        s.tryUpdate();

        assertEquals(1, s.getVersion());
        assertEquals("world", s.getValue());

        RepairExecutor.waitForRepairsToFinish(10000);

        repo.close();
        repo = null;
    }

    public void test_sequences() throws Exception {
        Storage<StorableSequenced> storage = getRepository().storageFor(StorableSequenced.class);

        StorableSequenced seq = storage.prepare();
        seq.setData("hello");
        seq.insert();

        assertEquals(1L, seq.getID());
        assertEquals(1, seq.getSomeInt());
        assertEquals(Integer.valueOf(1), seq.getSomeIntegerObj());
        assertEquals(1L, seq.getSomeLong());
        assertEquals(Long.valueOf(1L), seq.getSomeLongObj());
        assertEquals("1", seq.getSomeString());
        assertEquals("hello", seq.getData());

        seq = storage.prepare();
        seq.setData("foo");
        seq.insert();

        assertEquals(2L, seq.getID());
        assertEquals(2, seq.getSomeInt());
        assertEquals(Integer.valueOf(2), seq.getSomeIntegerObj());
        assertEquals(2L, seq.getSomeLong());
        assertEquals(Long.valueOf(2L), seq.getSomeLongObj());
        assertEquals("2", seq.getSomeString());
        assertEquals("foo", seq.getData());
        
        seq = storage.prepare();
        seq.setSomeInt(100);
        seq.setSomeLongObj(null);
        seq.setData("data");
        seq.insert();

        assertEquals(3L, seq.getID());
        assertEquals(100, seq.getSomeInt());
        assertEquals(null, seq.getSomeLongObj());

        seq = storage.prepare();
        seq.setData("world");
        seq.insert();

        assertEquals(4L, seq.getID());
        assertEquals(3, seq.getSomeInt());
        assertEquals(Integer.valueOf(4), seq.getSomeIntegerObj());
        assertEquals(4L, seq.getSomeLong());
        assertEquals(Long.valueOf(3L), seq.getSomeLongObj());
        assertEquals("4", seq.getSomeString());
    }

    public void test_oldIndexEntryDeletion() throws Exception {
        // Very simple test that ensures that old index entries are deleted
        // when the master record is updated. There is no guarantee that the
        // chosen repository supports indexes, and there is no guarantee that
        // it is selecting the desired index. Since the index set is simple and
        // so are the queries, I think it is safe to assume that the selected
        // index is what I expect it to be. Repositories that support
        // custom indexing should have more rigorous tests.

        Storage<StorableTestBasicIndexed> storage =
            getRepository().storageFor(StorableTestBasicIndexed.class);

        StorableTestBasicIndexed s = storage.prepare();
        final int id1 = generateId(1);
        s.setId(id1);
        s.setStringProp("hello");
        s.setIntProp(3);
        s.setLongProp(4);
        s.setDoubleProp(5);
        s.insert();

        s = storage.prepare();
        final int id6 = generateId(6);
        s.setId(id6);
        s.setStringProp("hello");
        s.setIntProp(8);
        s.setLongProp(9);
        s.setDoubleProp(10);
        s.insert();

        s = storage.prepare();
        final int id11 = generateId(11);
        s.setId(id11);
        s.setStringProp("world");
        s.setIntProp(3);
        s.setLongProp(14);
        s.setDoubleProp(15);
        s.insert();

        // First verify that queries report what we expect. Don't perform an
        // orderBy on query, as that might interfere with index selection.
        Query<StorableTestBasicIndexed> q = storage.query("stringProp = ?").with("hello");
        List<StorableTestBasicIndexed> list = q.fetch().toList();
        assertEquals(2, list.size());
        if (list.get(0).getId() == id1) {
            assertEquals(id6, list.get(1).getId());
        } else {
            assertEquals(id6, list.get(0).getId());
        }

        q = storage.query("stringProp = ?").with("world");
        list = q.fetch().toList();
        assertEquals(1, list.size());
        assertEquals(id11, list.get(0).getId());

        q = storage.query("intProp = ?").with(3);
        list = q.fetch().toList();
        assertEquals(2, list.size());
        if (list.get(0).getId() == id1) {
            assertEquals(id11, list.get(1).getId());
        } else {
            assertEquals(id11, list.get(0).getId());
        }

        // Now update and verify changes to query results.
        s = storage.prepare();
        s.setId(id1);
        s.load();
        s.setStringProp("world");
        s.tryUpdate();

        q = storage.query("stringProp = ?").with("hello");
        list = q.fetch().toList();
        assertEquals(1, list.size());
        assertEquals(id6, list.get(0).getId());

        q = storage.query("stringProp = ?").with("world");
        list = q.fetch().toList();
        assertEquals(2, list.size());
        if (list.get(0).getId() == id1) {
            assertEquals(id11, list.get(1).getId());
        } else {
            assertEquals(id11, list.get(0).getId());
        }

        q = storage.query("intProp = ?").with(3);
        list = q.fetch().toList();
        assertEquals(2, list.size());
        if (list.get(0).getId() == id1) {
            assertEquals(id11, list.get(1).getId());
        } else {
            assertEquals(id11, list.get(0).getId());
        }
    }

    public void test_falseDoubleUpdate() throws Exception {
        Storage<StorableTestBasic> storage = getRepository().storageFor(StorableTestBasic.class);

        StorableTestBasic s = storage.prepare();
        s.setId(56789);

        assertFalse(s.tryUpdate());
        assertFalse(s.tryUpdate());
    }

    public void test_dateTimeIndex() throws Exception {
        Storage<StorableDateIndex> storage = getRepository().storageFor(StorableDateIndex.class);

        DateTimeZone original = DateTimeZone.getDefault();
        // Set time zone different than defined in storable.
        DateTimeZone.setDefault(DateTimeZone.forID("America/Los_Angeles"));
        try {
            DateTime now = new DateTime();

            StorableDateIndex sdi = storage.prepare();
            sdi.setID(1);
            sdi.setOrderDate(now);
            sdi.insert();

            sdi.load();

            assertEquals(now.getMillis(), sdi.getOrderDate().getMillis());
            // Time zones will differ, since adapter is applied upon load.
            assertFalse(now.equals(sdi.getOrderDate()));

            Query<StorableDateIndex> query = storage.query("orderDate=?").with(now);
            // Tests that adapter is applied to index. Otherwise, consistency
            // check will reject loaded storable.
            StorableDateIndex sdi2 = query.tryLoadOne();
            assertNotNull(sdi2);
        } finally {
            DateTimeZone.setDefault(original);
        }
    }

    public void test_joinCache() throws Exception {
        Storage<UserAddress> uaStorage = getRepository().storageFor(UserAddress.class);
        Storage<UserInfo> uiStorage = getRepository().storageFor(UserInfo.class);

        UserAddress addr = uaStorage.prepare();
        addr.setAddressID(5);
        addr.setLine1("123");
        addr.setCity("Seattle");
        addr.setState("WA");
        addr.setCountry("USA");
        addr.insert();

        UserInfo user = uiStorage.prepare();
        user.setUserID(1);
        user.setStateID(1);
        user.setFirstName("John");
        user.setLastName("Smith");
        user.setAddress(addr);

        assertEquals("Seattle", user.getAddress().getCity());

        user.insert();

        addr.setCity("Bellevue");
        addr.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Bellevue", user.getAddress().getCity());

        UserAddress addr2 = uaStorage.prepare();
        addr2.setAddressID(5);
        addr2.setCity("Kirkland");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Bellevue", user.getAddress().getCity());

        // Force reload of user should flush cache.
        user.load();

        assertEquals("Kirkland", user.getAddress().getCity());

        addr2.setCity("Redmond");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Kirkland", user.getAddress().getCity());

        // Update of user should flush cache (even if nothing changed)
        assertEquals(true, user.tryUpdate());

        assertEquals("Redmond", user.getAddress().getCity());

        addr2.setCity("Renton");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Redmond", user.getAddress().getCity());

        // Update of user should flush cache (when something changed)
        user.setFirstName("Jim");
        assertEquals(true, user.tryUpdate());

        assertEquals("Renton", user.getAddress().getCity());

        addr2.setCity("Tacoma");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Renton", user.getAddress().getCity());

        // Delete of user should flush cache
        assertEquals(true, user.tryDelete());

        assertEquals("Tacoma", user.getAddress().getCity());

        addr2.setCity("Shoreline");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Tacoma", user.getAddress().getCity());

        // Failed load of user should flush cache
        assertEquals(false, user.tryLoad());

        assertEquals("Shoreline", user.getAddress().getCity());

        addr2.setCity("Vancouver");
        addr2.tryUpdate();

        // Should still refer to same address instance.
        assertEquals("Shoreline", user.getAddress().getCity());

        // Insert of user should flush cache
        user.insert();

        assertEquals("Vancouver", user.getAddress().getCity());
    }

    public void test_updateReload() throws Exception {
        Storage<UserInfo> uiStorage = getRepository().storageFor(UserInfo.class);

        UserInfo user = uiStorage.prepare();
        user.setUserID(1);
        user.setStateID(1);
        user.setFirstName("John");
        user.setLastName("Smith");
        user.setAddressID(0);
        user.insert();

        UserInfo user2 = uiStorage.prepare();
        user2.setUserID(1);
        user2.setFirstName("Jim");
        user2.tryUpdate();

        assertEquals("John", user.getFirstName());
        user.tryUpdate();
        assertEquals("Jim", user.getFirstName());

        user2.setFirstName("Bob");
        user2.tryUpdate();

        assertEquals("Jim", user.getFirstName());
        user.setLastName("Jones");
        user.tryUpdate();
        assertEquals("Bob", user.getFirstName());
        assertEquals("Jones", user.getLastName());
    }

    public void test_deleteState() throws Exception {
        Storage<UserInfo> uiStorage = getRepository().storageFor(UserInfo.class);

        UserInfo user = uiStorage.prepare();
        user.setUserID(1);
        user.setStateID(1);
        user.setFirstName("John");
        user.setLastName("Smith");
        user.setAddressID(0);
        user.insert();

        UserInfo user2 = uiStorage.prepare();
        user2.setUserID(1);
        user2.tryDelete();

        assertFalse(user.tryLoad());

        // Should be able to change pk now.
        user.setUserID(2);
        assertFalse(user.tryUpdate());
        user.setUserID(1);
        user.insert();

        user2.tryDelete();

        assertFalse(user.tryUpdate());

        // Should be able to change pk now.
        user.setUserID(2);
        assertFalse(user.tryUpdate());
        user.setUserID(1);
        user.insert();
        user2.tryDelete();

        user.setFirstName("Jim");
        assertFalse(user.tryUpdate());

        // Should be able to change pk now.
        user.setUserID(2);
        assertFalse(user.tryUpdate());
        user.setUserID(1);
        user.insert();
        assertEquals("Jim", user.getFirstName());
    }

    public void test_deleteUpdate() throws Exception {
        Storage<UserInfo> uiStorage = getRepository().storageFor(UserInfo.class);

        UserInfo user = uiStorage.prepare();
        user.setUserID(1);
        user.setStateID(1);
        user.setFirstName("John");
        user.setLastName("Smith");
        user.setAddressID(0);
        user.insert();

        // Just want to change first name now
        user.setFirstName("Bob");

        // Concurrently, someone else deletes the user
        UserInfo user2 = uiStorage.prepare();
        user2.setUserID(1);
        assertTrue(user2.tryDelete());

        assertFalse(user.tryUpdate());

        // Update failed... perhaps we should try again... (wrong decision)

        // Concurrently, someone inserts a different user with re-used id. (wrong decision)
        user2 = uiStorage.prepare();
        user2.setUserID(1);
        user2.setStateID(1);
        user2.setFirstName("Mike");
        user2.setLastName("Jones");
        user2.setAddressID(0);
        user2.insert();

        // Trying the failed update again should totally blow away the sneaked in insert.
        assertTrue(user.tryUpdate());

        assertEquals("Bob", user.getFirstName());
        assertEquals("Smith", user.getLastName());

        // The failed update earlier dirtied all the properties, including ones
        // that were not modified. As a result, the second update replaces all
        // properties. This is a special edge case destructive update for which
        // there is no clear solution. If just the one property was changed as
        // instructed earlier, then the record would have been corrupted. This
        // feels much worse. This can still happen if the record was not
        // originally fully clean.

        // The cause of the error is the user creating a new record with a
        // re-used id. This problem could be prevented using transactions, or
        // it might be detected with optimistic locking.
    }

    public void test_deleteOne() throws Exception {
        Storage<UserInfo> uiStorage = getRepository().storageFor(UserInfo.class);

        UserInfo user = uiStorage.prepare();
        user.setUserID(1);
        user.setStateID(1);
        user.setFirstName("Bob");
        user.setLastName("Smith");
        user.setAddressID(0);
        user.insert();

        user = uiStorage.prepare();
        user.setUserID(2);
        user.setStateID(1);
        user.setFirstName("Bob");
        user.setLastName("Jones");
        user.setAddressID(0);
        user.insert();
        
        user = uiStorage.prepare();
        user.setUserID(3);
        user.setStateID(1);
        user.setFirstName("Indiana");
        user.setLastName("Jones");
        user.setAddressID(0);
        user.insert();

        try {
            uiStorage.query("lastName = ?").with("Jones").deleteOne();
            fail();
        } catch (PersistMultipleException e) {
        }

        List<UserInfo> list = uiStorage.query().fetch().toList();
        assertEquals(3, list.size());

        uiStorage.query("lastName = ? & firstName = ?").with("Jones").with("Bob").deleteOne();

        list = uiStorage.query().orderBy("userID").fetch().toList();
        assertEquals(2, list.size());

        assertEquals("Bob", list.get(0).getFirstName());
        assertEquals("Smith", list.get(0).getLastName());
        assertEquals("Indiana", list.get(1).getFirstName());
        assertEquals("Jones", list.get(1).getLastName());
    }

    public void test_triggers() throws Exception {
        Storage<StorableVersioned> storage = getRepository().storageFor(StorableVersioned.class);

        class InsertTrigger extends Trigger<StorableVersioned> {
            Object mState;

            @Override
            public Object beforeInsert(StorableVersioned s) {
                assertEquals(1, s.getID());
                mState = new Object();
                return mState;
            }

            @Override
            public void afterInsert(StorableVersioned s, Object state) {
                assertEquals(1, s.getID());
                assertEquals(mState, state);
            }
        };

        InsertTrigger it = new InsertTrigger();

        assertTrue(storage.addTrigger(it));

        StorableVersioned s = storage.prepare();
        s.setID(1);
        s.setValue("value");
        s.insert();

        assertTrue(it.mState != null);

        assertTrue(storage.removeTrigger(it));

        class UpdateTrigger extends Trigger<StorableVersioned> {
            Object mState;
            int mVersion;

            @Override
            public Object beforeUpdate(StorableVersioned s) {
                assertEquals(1, s.getID());
                mState = new Object();
                mVersion = s.getVersion();
                return mState;
            }

            @Override
            public void afterUpdate(StorableVersioned s, Object state) {
                assertEquals(1, s.getID());
                assertEquals(mState, state);
                assertEquals(mVersion + 1, s.getVersion());
            }
        };

        UpdateTrigger ut = new UpdateTrigger();

        assertTrue(storage.addTrigger(ut));

        s.setValue("value2");
        s.update();

        assertTrue(ut.mState != null);

        assertTrue(storage.removeTrigger(ut));

        class DeleteTrigger extends Trigger<StorableVersioned> {
            Object mState;

            @Override
            public Object beforeDelete(StorableVersioned s) {
                assertEquals(1, s.getID());
                mState = new Object();
                return mState;
            }

            @Override
            public void afterDelete(StorableVersioned s, Object state) {
                assertEquals(1, s.getID());
                assertEquals(mState, state);
            }
        };

        DeleteTrigger dt = new DeleteTrigger();

        assertTrue(storage.addTrigger(dt));

        s.delete();

        assertTrue(dt.mState != null);

        assertTrue(storage.removeTrigger(dt));
    }

    public void test_triggerFailure() throws Exception {
        Storage<StorableVersioned> storage = getRepository().storageFor(StorableVersioned.class);

        class InsertTrigger extends Trigger<StorableVersioned> {
            boolean failed;

            @Override
            public Object beforeInsert(StorableVersioned s) {
                throw new RuntimeException();
            }

            @Override
            public void afterInsert(StorableVersioned s, Object state) {
                fail();
            }

            @Override
            public void failedInsert(StorableVersioned s, Object state) {
                failed = true;
            }
        };

        InsertTrigger it = new InsertTrigger();

        assertTrue(storage.addTrigger(it));

        StorableVersioned s = storage.prepare();
        s.setID(1);
        s.setValue("value");
        try {
            s.insert();
            fail();
        } catch (RuntimeException e) {
        }

        assertTrue(it.failed);

        class UpdateTrigger extends Trigger<StorableVersioned> {
            boolean failed;

            @Override
            public Object beforeUpdate(StorableVersioned s) {
                throw new RuntimeException();
            }

            @Override
            public void afterUpdate(StorableVersioned s, Object state) {
                fail();
            }

            @Override
            public void failedUpdate(StorableVersioned s, Object state) {
                failed = true;
            }
        };

        UpdateTrigger ut = new UpdateTrigger();

        assertTrue(storage.addTrigger(ut));

        s = storage.prepare();
        s.setID(1);
        s.setVersion(3);
        s.setValue("value");
        try {
            s.update();
            fail();
        } catch (RuntimeException e) {
        }

        assertTrue(ut.failed);

        class DeleteTrigger extends Trigger<StorableVersioned> {
            boolean failed;

            @Override
            public Object beforeDelete(StorableVersioned s) {
                throw new RuntimeException();
            }

            @Override
            public void afterDelete(StorableVersioned s, Object state) {
                fail();
            }

            @Override
            public void failedDelete(StorableVersioned s, Object state) {
                failed = true;
            }
        };

        DeleteTrigger dt = new DeleteTrigger();

        assertTrue(storage.addTrigger(dt));

        s = storage.prepare();
        s.setID(1);
        try {
            s.delete();
            fail();
        } catch (RuntimeException e) {
        }

        assertTrue(dt.failed);
    }

    public void test_triggerChecks() throws Exception {
        Storage<StorableTimestamped> storage =
            getRepository().storageFor(StorableTimestamped.class);

        StorableTimestamped st = storage.prepare();
        st.setId(1);
        st.setValue("value");

        try {
            st.insert();
            fail();
        } catch (ConstraintException e) {
            // We forgot to set submitDate and modifyDate.
        }

        // Install trigger that sets the timestamp properties.

        storage.addTrigger(new Trigger<Timestamped>() {
            @Override
            public Object beforeInsert(Timestamped st) {
                DateTime now = new DateTime();
                st.setSubmitDateTime(now);
                st.setModifyDateTime(now);
                return null;
            }

            @Override
            public Object beforeUpdate(Timestamped st) {
                DateTime now = new DateTime();
                st.setModifyDateTime(now);
                return null;
            }
        });

        st.insert();

        assertNotNull(st.getSubmitDateTime());
        assertNotNull(st.getModifyDateTime());

        DateTime dt = st.getModifyDateTime();

        Thread.sleep(500);

        st.setValue("foo");
        st.update();

        assertTrue(st.getModifyDateTime().getMillis() >= dt.getMillis() + 450);
    }

    public void test_hashCode() throws Exception {
        Storage<StorableTestBasic> storage =
            getRepository().storageFor(StorableTestBasic.class);

        StorableTestBasic stb = storage.prepare();
        // Just tests to make sure generated code doesn't throw an error.
        int hashCode = stb.hashCode();
    }

    public void test_stateMethods() throws Exception {
        Storage<Order> storage = getRepository().storageFor(Order.class);

        Order order = storage.prepare();

        assertUninitialized(true, order, "orderID", "orderNumber", "orderTotal", "orderComments");
        assertDirty(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");
        assertClean(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");

        order.setOrderID(1);

        assertUninitialized(false, order, "orderID");
        assertUninitialized(true, order, "orderNumber", "orderTotal", "orderComments");
        assertDirty(false, order, "orderNumber", "orderTotal", "orderComments");
        assertDirty(true, order, "orderID");
        assertClean(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");

        order.setOrderNumber("123");
        order.setOrderTotal(456);
        order.setAddressID(789);

        assertUninitialized(false, order, "orderID", "orderNumber", "orderTotal");
        assertUninitialized(true, order, "orderComments");
        assertDirty(true, order, "orderID", "orderNumber", "orderTotal");
        assertDirty(false, order, "orderComments");
        assertClean(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");

        // Get unknown property
        try {
            order.isPropertyUninitialized("foo");
            fail();
        } catch (IllegalArgumentException e) {
        }

        // Get unknown property
        try {
            order.isPropertyDirty("foo");
            fail();
        } catch (IllegalArgumentException e) {
        }

        // Get unknown property
        try {
            order.isPropertyClean("foo");
            fail();
        } catch (IllegalArgumentException e) {
        }

        // Get join property
        try {
            order.isPropertyUninitialized("address");
            fail();
        } catch (IllegalArgumentException e) {
        }

        // Get join property
        try {
            order.isPropertyDirty("address");
            fail();
        } catch (IllegalArgumentException e) {
        }

        // Get join property
        try {
            order.isPropertyClean("address");
            fail();
        } catch (IllegalArgumentException e) {
        }

        order.insert();

        assertUninitialized(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");
        assertDirty(false, order, "orderID", "orderNumber", "orderTotal", "orderComments");
        assertClean(true, order, "orderID", "orderNumber", "orderTotal", "orderComments");
    }

    public void test_count() throws Exception {
        test_count(false);
    }
    public void test_countIndexed() throws Exception {
        test_count(true);
    }

    private void test_count(boolean indexed) throws Exception {
        Storage<? extends StorableTestBasic> storage;
        if (indexed) {
            storage = getRepository().storageFor(StorableTestBasicIndexed.class);
        } else {
            storage = getRepository().storageFor(StorableTestBasic.class);
        }

        assertEquals(0, storage.query().count());

        StorableTestBasic sb = storage.prepare();
        sb.setId(1);
        sb.setIntProp(1);
        sb.setLongProp(1);
        sb.setDoubleProp(1.1);
        sb.setStringProp("one");
        sb.insert();

        assertEquals(1, storage.query().count());
        assertEquals(0, storage.query().not().count());
        assertEquals(1, storage.query("stringProp = ?").with("one").count());
        assertEquals(0, storage.query("stringProp = ?").with("two").count());

        sb = storage.prepare();
        sb.setId(2);
        sb.setIntProp(2);
        sb.setLongProp(2);
        sb.setDoubleProp(2.1);
        sb.setStringProp("two");
        sb.insert();

        sb = storage.prepare();
        sb.setId(3);
        sb.setIntProp(3);
        sb.setLongProp(3);
        sb.setDoubleProp(3.1);
        sb.setStringProp("three");
        sb.insert();

        assertEquals(3, storage.query().count());
        assertEquals(0, storage.query().not().count());
        assertEquals(1, storage.query("stringProp = ?").with("one").count());
        assertEquals(1, storage.query("stringProp = ?").with("two").count());
        assertEquals(1, storage.query("stringProp = ?").with("three").count());
        assertEquals(2, storage.query("stringProp = ?").not().with("one").count());
        assertEquals(2, storage.query("stringProp = ?").not().with("two").count());
        assertEquals(2, storage.query("stringProp = ?").not().with("three").count());
        assertEquals(2, storage.query("stringProp > ?").with("one").count());
        assertEquals(0, storage.query("stringProp > ?").with("two").count());
        assertEquals(1, storage.query("stringProp > ?").with("three").count());
    }

    public void test_fetchAfter() throws Exception {
        Storage<ManyKeys2> storage = getRepository().storageFor(ManyKeys2.class);

        final int groupSize = 4;
        final int aliasing = groupSize + 1;

        int total = 0;
        for (int a=0; a<groupSize; a++) {
            for (int b=0; b<groupSize; b++) {
                for (int c=0; c<groupSize; c++) {
                    for (int d=0; d<groupSize; d++) {
                        for (int e=0; e<groupSize; e++) {
                            for (int f=0; f<groupSize; f++) {
                                ManyKeys2 obj = storage.prepare();
                                obj.setA(a);
                                obj.setB(b);
                                obj.setC(c);
                                obj.setD(d);
                                obj.setE(e);
                                obj.setF(f);
                                obj.insert();
                                total++;
                            }
                        }
                    }
                }
            }
        }

        String[] orderBy = {"a", "b", "c", "d", "e", "f"};
        Query<ManyKeys2> query = storage.query().orderBy(orderBy);
        Cursor<ManyKeys2> cursor = query.fetch();
        Comparator<ManyKeys2> comparator = SortedCursor.createComparator(ManyKeys2.class, orderBy);

        int actual = 0;
        ManyKeys2 last = null;
        while (cursor.hasNext()) {
            actual++;
            ManyKeys2 obj = cursor.next();
            if (last != null) {
                assertTrue(comparator.compare(last, obj) < 0);
            }
            if (actual % aliasing == 0) {
                cursor.close();
                cursor = query.fetchAfter(obj);
            }
            last = obj;
        }

        assertEquals(total, actual);

        // Try again in reverse

        orderBy = new String[] {"-a", "-b", "-c", "-d", "-e", "-f"};
        query = storage.query().orderBy(orderBy);
        cursor = query.fetch();

        actual = 0;
        last = null;
        while (cursor.hasNext()) {
            actual++;
            ManyKeys2 obj = cursor.next();
            if (last != null) {
                assertTrue(comparator.compare(last, obj) > 0);
            }
            if (actual % aliasing == 0) {
                cursor.close();
                cursor = query.fetchAfter(obj);
            }
            last = obj;
        }

        assertEquals(total, actual);

        // Try again with funny mix of orderings. This will likely cause sort
        // operations to be performed, thus making it very slow.

        orderBy = new String[] {"-a", "b", "-c", "d", "-e", "f"};
        query = storage.query().orderBy(orderBy);
        cursor = query.fetch();
        comparator = SortedCursor.createComparator(ManyKeys2.class, orderBy);

        actual = 0;
        last = null;
        while (cursor.hasNext()) {
            actual++;
            ManyKeys2 obj = cursor.next();
            if (last != null) {
                assertTrue(comparator.compare(last, obj) < 0);
            }
            if (actual % aliasing == 0) {
                cursor.close();
                cursor = query.fetchAfter(obj);
            }
            last = obj;
        }

        assertEquals(total, actual);
    }

    private void assertUninitialized(boolean expected, Storable storable, String... properties) {
        for (String property : properties) {
            assertEquals(expected, storable.isPropertyUninitialized(property));
        }
    }

    private void assertDirty(boolean expected, Storable storable, String... properties) {
        for (String property : properties) {
            assertEquals(expected, storable.isPropertyDirty(property));
        }
    }

    private void assertClean(boolean expected, Storable storable, String... properties) {
        for (String property : properties) {
            assertEquals(expected, storable.isPropertyClean(property));
        }
    }

    @PrimaryKey("id")
    public interface StorableTestAssymetricGet extends Storable{
        public abstract int getId();
        public abstract void setId(int id);

        public abstract int getAssymetricGET();
    }

    @PrimaryKey("id")
    public interface StorableTestAssymetricSet extends Storable{
        public abstract int getId();
        public abstract void setId(int id);

        public abstract int setAssymetricSET();
    }

    @PrimaryKey("id")
    public interface StorableTestAssymetricGetSet extends Storable{
        public abstract int getId();
        public abstract void setId(int id);

        public abstract int getAssymetricGET();

        public abstract int setAssymetricSET();
    }

    public static class InvocationTracker extends StorableTestBasic implements WrappedSupport {
        String mName;
        long mInvocationTracks;

        boolean mTrace;

        public InvocationTracker(String name) {
            this(name, false);
        }

        public InvocationTracker(final String name, boolean trace) {
            mName = name;
            mTrace = trace;
            clearTracks();
        }

        public void clearTracks() {
            mInvocationTracks = 0;
        }

        public long getTracks() {
            return mInvocationTracks;
        }

        public void assertTrack(long value) {
            assertEquals(value, getTracks());
            clearTracks();
        }


        public void setId(int id) {
            if (mTrace) System.out.println("setId");
            mInvocationTracks |= sSetId;
        }

        // Basic coverage of the primitives
        public String getStringProp() {
            if (mTrace) System.out.println("getStringProp");
            mInvocationTracks |= sGetStringProp;  // 0x2
            return null;
        }

        public void setStringProp(String aStringThing) {
            if (mTrace) System.out.println("setStringProp");
            mInvocationTracks |= sSetStringProp;  // 0x4
        }

        public int getIntProp() {
            if (mTrace) System.out.println("getIntProp");
            mInvocationTracks |= sGetIntProp; // 0x8
            return 0;
        }

        public void setIntProp(int anInt) {
            if (mTrace) System.out.println("setIntProp");
            mInvocationTracks |= sSetIntProp; // 0x10
        }

        public long getLongProp() {
            if (mTrace) System.out.println("getLongProp");
            mInvocationTracks |= sGetLongProp;  // 0x20
            return 0;
        }

        public void setLongProp(long aLong) {
            if (mTrace) System.out.println("setLongProp");
            mInvocationTracks |= sSetLongProp;   // 0x40
        }

        public double getDoubleProp() {
            if (mTrace) System.out.println("getDoubleProp");
            mInvocationTracks |= sGetDoubleProp;   // 0x80
            return 0;
        }

        public void setDoubleProp(double aDouble) {
            if (mTrace) System.out.println("setDoubleProp");
            mInvocationTracks |= sSetDoubleProp;   // 0x100
        }

        public DateTime getDate() {
            return null;
        }

        public void setDate(DateTime date) {
        }

        public void load() throws FetchException {
            if (mTrace) System.out.println("load");
            mInvocationTracks |= sLoad;  // 0x200
        }

        public boolean tryLoad() throws FetchException {
            if (mTrace) System.out.println("tryLoad");
            mInvocationTracks |= sTryLoad;  // 0x400
            return false;
        }

        public void insert() throws PersistException {
            if (mTrace) System.out.println("insert");
            mInvocationTracks |= sInsert;  // 0x800
        }

        public boolean tryInsert() throws PersistException {
            if (mTrace) System.out.println("tryInsert");
            mInvocationTracks |= sTryInsert;
            return false;
        }

        public void update() throws PersistException {
            if (mTrace) System.out.println("update");
            mInvocationTracks |= sUpdate;
        }

        public boolean tryUpdate() throws PersistException {
            if (mTrace) System.out.println("tryUpdate");
            mInvocationTracks |= sTryUpdate;    // 0x1000
            return false;
        }

        public void delete() throws PersistException {
            if (mTrace) System.out.println("delete");
            mInvocationTracks |= sDelete;
        }

        public boolean tryDelete() throws PersistException {
            if (mTrace) System.out.println("tryDelete");
            mInvocationTracks |= sTryDelete;    // 0x2000
            return false;
        }

        public WrappedSupport createSupport(Storable storable) {
            return new InvocationTracker(mName, mTrace);
        }

        public Storage storage() {
            if (mTrace) System.out.println("storage");
            mInvocationTracks |= sStorage;    // 0x4000
            return null;
        }

        public Storable copy() {
            if (mTrace) System.out.println("copy");
            mInvocationTracks |= sCopy;    // 0x8000
            return null;
        }

        public String toStringKeyOnly() {
            if (mTrace) System.out.println("toStringKeyOnly");
            mInvocationTracks |= sToStringKeyOnly;    // 0x10000
            return null;
        }

        public int getId() {
            if (mTrace) System.out.println("getId");
            mInvocationTracks |= sGetId;   // 0x20000
            return 0;
        }

        public void copyAllProperties(Storable storable) {
            if (mTrace) System.out.println("copyAllProperties");
            mInvocationTracks |= sCopyAllProperties;   // 0x40000
        }

        public void copyPrimaryKeyProperties(Storable storable) {
            if (mTrace) System.out.println("copyPrimaryKeyProperties");
            mInvocationTracks |= sCopyPrimaryKeyProperties;   // 0x80000
        }

        public void copyUnequalProperties(Storable storable) {
            if (mTrace) System.out.println("copyUnequalProperties");
            mInvocationTracks |= sCopyUnequalProperties;   // 0x10 0000
        }

        public void copyDirtyProperties(Storable storable) {
            if (mTrace) System.out.println("copyDirtyProperties");
            mInvocationTracks |= sCopyDirtyProperties;   // 0x20 0000
        }

        public boolean hasDirtyProperties() {
            if (mTrace) System.out.println("hasDirtyProperties");
            mInvocationTracks |= sHasDirtyProperties;   // 0x200 0000
            return false;
        }

        public boolean equalPrimaryKeys(Object obj) {
            if (mTrace) System.out.println("equalPrimaryKeys");
            mInvocationTracks |= sEqualKeys;   // 0x40 0000
            return true;
        }

        public boolean equalProperties(Object obj) {
            if (mTrace) System.out.println("equalProperties");
            mInvocationTracks |= sEqualProperties;   // 0x80 0000
            return true;
        }

        public void copyVersionProperty(Storable storable) {
            if (mTrace) System.out.println("copyVersionProperty");
            mInvocationTracks |= sCopyVersionProperty;   // 0x100 0000
        }

        public void markPropertiesClean() {
            if (mTrace) System.out.println("markPropertiesClean");
            mInvocationTracks |= sMarkPropertiesClean;   // 0x400 0000
        }

        public void markAllPropertiesClean() {
            if (mTrace) System.out.println("markAllPropertiesClean");
            mInvocationTracks |= sMarkAllPropertiesClean;   // 0x800 0000
        }

        public void markPropertiesDirty() {
            if (mTrace) System.out.println("markPropertiesDirty");
            mInvocationTracks |= sMarkPropertiesDirty;   // 0x1000 0000
        }

        public void markAllPropertiesDirty() {
            if (mTrace) System.out.println("markAllPropertiesDirty");
            mInvocationTracks |= sMarkAllPropertiesDirty;   // 0x2000 0000
        }

        public Class storableType() {
            if (mTrace) System.out.println("storableType");
            mInvocationTracks |= sStorableType;   // 0x4000 0000
            return Storable.class;
        }

        public boolean isPropertyUninitialized(String name) {
            if (mTrace) System.out.println("isPropertyUninitialized");
            return false;
        }

        public boolean isPropertyDirty(String name) {
            if (mTrace) System.out.println("isPropertyDirty");
            return false;
        }

        public boolean isPropertyClean(String name) {
            if (mTrace) System.out.println("isPropertyClean");
            return false;
        }

        public boolean isPropertySupported(String name) {
            if (mTrace) System.out.println("isPropertySupported");
            return false;
        }

        public Repository getRootRepository() {
            return null;
        }

        public Trigger getInsertTrigger() {
            return null;
        }

        public Trigger getUpdateTrigger() {
            return null;
        }

        public Trigger getDeleteTrigger() {
            return null;
        }
    }

}