aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/healthmarketscience/jackcess/IndexTest.java
blob: fbcd189769ee1a4ed9873f4c08dad603e261ceb9 (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
/*
Copyright (c) 2007 Health Market Science, Inc.

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.healthmarketscience.jackcess;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import static com.healthmarketscience.jackcess.Database.*;
import static com.healthmarketscience.jackcess.DatabaseTest.*;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.IndexCodesTest;
import com.healthmarketscience.jackcess.impl.IndexData;
import com.healthmarketscience.jackcess.impl.IndexImpl;
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
import com.healthmarketscience.jackcess.impl.RowIdImpl;
import com.healthmarketscience.jackcess.impl.TableImpl;
import junit.framework.TestCase;

/**
 * @author James Ahlborn
 */
public class IndexTest extends TestCase {

  public IndexTest(String name) {
    super(name);
  }

  @Override
  protected void setUp() {
    DatabaseTest.setTestAutoSync(false);
  }

  @Override
  protected void tearDown() {
    DatabaseTest.clearTestAutoSync();
  }

  public void testByteOrder() throws Exception {
    byte b1 = (byte)0x00;
    byte b2 = (byte)0x01;
    byte b3 = (byte)0x7F;
    byte b4 = (byte)0x80;
    byte b5 = (byte)0xFF;

    assertTrue(ByteUtil.asUnsignedByte(b1) < ByteUtil.asUnsignedByte(b2));
    assertTrue(ByteUtil.asUnsignedByte(b2) < ByteUtil.asUnsignedByte(b3));
    assertTrue(ByteUtil.asUnsignedByte(b3) < ByteUtil.asUnsignedByte(b4));
    assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5));
  }
  
  public void testByteCodeComparator() {
    byte[] b0 = null;
    byte[] b1 = new byte[]{(byte)0x00};
    byte[] b2 = new byte[]{(byte)0x00, (byte)0x00};
    byte[] b3 = new byte[]{(byte)0x00, (byte)0x01};
    byte[] b4 = new byte[]{(byte)0x01};
    byte[] b5 = new byte[]{(byte)0x80};
    byte[] b6 = new byte[]{(byte)0xFF};
    byte[] b7 = new byte[]{(byte)0xFF, (byte)0x00};
    byte[] b8 = new byte[]{(byte)0xFF, (byte)0x01};

    List<byte[]> expectedList = Arrays.<byte[]>asList(b0, b1, b2, b3, b4,
                                                      b5, b6, b7, b8);
    SortedSet<byte[]> sortedSet = new TreeSet<byte[]>(
        IndexData.BYTE_CODE_COMPARATOR);
    sortedSet.addAll(expectedList);
    assertEquals(expectedList, new ArrayList<byte[]>(sortedSet));
    
  }

  public void testPrimaryKey() throws Exception {
    for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
      Table table = open(testDB).getTable("Table1");
      Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
      Index pkIndex = null;
      for(Index index : table.getIndexes()) {
        foundPKs.put(index.getColumns().iterator().next().getName(),
                     index.isPrimaryKey());
        if(index.isPrimaryKey()) {
          pkIndex= index;
        
        }
      }
      Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
      expectedPKs.put("A", Boolean.TRUE);
      expectedPKs.put("B", Boolean.FALSE);
      assertEquals(expectedPKs, foundPKs);
      assertSame(pkIndex, table.getPrimaryKeyIndex());
    }
  }
  
  public void testLogicalIndexes() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
      Database mdb = open(testDB);

      TableImpl table = (TableImpl)mdb.getTable("Table1");
      for(IndexImpl idx : table.getIndexes()) {
        idx.initialize();
      }
      assertEquals(4, table.getIndexes().size());
      assertEquals(4, table.getLogicalIndexCount());
      checkIndexColumns(table,
                        "id", "id",
                        "PrimaryKey", "id",
                        "Table2Table1", "otherfk1",
                        "Table3Table1", "otherfk2");

      table = (TableImpl)mdb.getTable("Table2");
      for(IndexImpl idx : table.getIndexes()) {
        idx.initialize();
      }
      assertEquals(3, table.getIndexes().size());
      assertEquals(2, table.getIndexDatas().size());
      assertEquals(3, table.getLogicalIndexCount());
      checkIndexColumns(table,
                        "id", "id",
                        "PrimaryKey", "id",
                        ".rC", "id");

      IndexImpl pkIdx = table.getIndex("PrimaryKey");
      IndexImpl fkIdx = table.getIndex(".rC");
      assertNotSame(pkIdx, fkIdx);
      assertTrue(fkIdx.isForeignKey());
      assertSame(pkIdx.getIndexData(), fkIdx.getIndexData());
      IndexData indexData = pkIdx.getIndexData();
      assertEquals(Arrays.asList(pkIdx, fkIdx), indexData.getIndexes());
      assertSame(pkIdx, indexData.getPrimaryIndex());

      table = (TableImpl)mdb.getTable("Table3");
      for(IndexImpl idx : table.getIndexes()) {
        idx.initialize();
      }
      assertEquals(3, table.getIndexes().size());
      assertEquals(2, table.getIndexDatas().size());
      assertEquals(3, table.getLogicalIndexCount());
      checkIndexColumns(table,
                        "id", "id",
                        "PrimaryKey", "id",
                        ".rC", "id");

      pkIdx = table.getIndex("PrimaryKey");
      fkIdx = table.getIndex(".rC");
      assertNotSame(pkIdx, fkIdx);
      assertTrue(fkIdx.isForeignKey());
      assertSame(pkIdx.getIndexData(), fkIdx.getIndexData());
      indexData = pkIdx.getIndexData();
      assertEquals(Arrays.asList(pkIdx, fkIdx), indexData.getIndexes());
      assertSame(pkIdx, indexData.getPrimaryIndex());
    }
  }

  public void testComplexIndex() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) {
      // this file has an index with "compressed" entries and node pages
      Database db = open(testDB);
      TableImpl t = (TableImpl)db.getTable("Table1");
      IndexImpl index = t.getIndexes().get(0);
      assertFalse(index.isInitialized());
      assertEquals(512, countRows(t));
      assertEquals(512, index.getIndexData().getEntryCount());
      db.close();

      // copy to temp file and attempt to edit
      db = openCopy(testDB);
      t = (TableImpl)db.getTable("Table1");
      index = t.getIndexes().get(0);

      t.addRow(99, "abc", "def");
    }
  }

  public void testEntryDeletion() throws Exception {
    for (final TestDB testDB : SUPPORTED_DBS_TEST) {
      Table table = openCopy(testDB).getTable("Table1");

      for(int i = 0; i < 10; ++i) {
        table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i,
                     (6.7d / i), null, null, true);
      }
      table.reset();
      assertRowCount(12, table);

      for(Index index : table.getIndexes()) {
        assertEquals(12, ((IndexImpl)index).getIndexData().getEntryCount());
      }

      table.reset();
      table.getNextRow();
      table.getNextRow();
      table.getDefaultCursor().deleteCurrentRow();
      table.getNextRow();
      table.getDefaultCursor().deleteCurrentRow();
      table.getNextRow();
      table.getNextRow();
      table.getDefaultCursor().deleteCurrentRow();
      table.getNextRow();
      table.getNextRow();
      table.getNextRow();
      table.getDefaultCursor().deleteCurrentRow();

      table.reset();
      assertRowCount(8, table);

      for(Index index : table.getIndexes()) {
        assertEquals(8, ((IndexImpl)index).getIndexData().getEntryCount());
      }
    }
  }

  public void testIgnoreNulls() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) {
      Database db = openCopy(testDB);

      doTestIgnoreNulls(db, "TableIgnoreNulls1");
      doTestIgnoreNulls(db, "TableIgnoreNulls2");

      db.close();
    }
  }

  private void doTestIgnoreNulls(Database db, String tableName)
    throws Exception
  {
    Table orig = db.getTable(tableName);
    IndexImpl origI = (IndexImpl)orig.getIndex("DataIndex");
    Table temp = db.getTable(tableName + "_temp");
    IndexImpl tempI = (IndexImpl)temp.getIndex("DataIndex");

    // copy from orig table to temp table
    for(Map<String,Object> row : orig) {
      temp.addRow(orig.asRow(row));
    }

    assertEquals(origI.getIndexData().getEntryCount(), 
                 tempI.getIndexData().getEntryCount());

    Cursor origC = origI.newCursor().toCursor();
    Cursor tempC = tempI.newCursor().toCursor();

    while(true) {
      boolean origHasNext = origC.moveToNextRow();
      boolean tempHasNext = tempC.moveToNextRow();
      assertTrue(origHasNext == tempHasNext);
      if(!origHasNext) {
        break;
      }

      Map<String,Object> origRow = origC.getCurrentRow();
      Cursor.Position origCurPos = origC.getSavepoint().getCurrentPosition();
      Map<String,Object> tempRow = tempC.getCurrentRow();
      Cursor.Position tempCurPos = tempC.getSavepoint().getCurrentPosition();
      
      assertEquals(origRow, tempRow);
      assertEquals(IndexCodesTest.entryToString(origCurPos),
                   IndexCodesTest.entryToString(tempCurPos));
    }
  }

  public void testUnique() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) {
      Database db = openCopy(testDB);

      Table t = db.getTable("TableUnique1_temp");
      Index index = t.getIndex("DataIndex");

      doTestUnique(index, 1,
                   null, true,
                   "unique data", true,
                   null, true,
                   "more", false,
                   "stuff", false,
                   "unique data", false);

      t = db.getTable("TableUnique2_temp");
      index = t.getIndex("DataIndex");

      doTestUnique(index, 2,
                   null, null, true,
                   "unique data", 42, true,
                   "unique data", null, true,
                   null, null, true,
                   "some", 42, true,
                   "more unique data", 13, true,
                   null, -4242, true,
                   "another row", -3462, false,
                   null, 49, false,
                   "more", null, false,
                   "unique data", 42, false,
                   "unique data", null, false,
                   null, -4242, false);

      db.close();
    }
  }

  private void doTestUnique(Index index, int numValues,
                            Object... testData)
    throws Exception
  {
    for(int i = 0; i < testData.length; i += (numValues + 1)) {
      Object[] row = new Object[numValues + 1];
      row[0] = "testRow" + i;
      for(int j = 1; j < (numValues + 1); ++j) {
        row[j] = testData[i + j - 1];
      }
      boolean expectedSuccess = (Boolean)testData[i + numValues];

      IOException failure = null;
      try {
        ((IndexImpl)index).getIndexData().prepareAddRow(
            row, new RowIdImpl(400 + i, 0), null).commit();
      } catch(IOException e) {
        failure = e;
      }
      if(expectedSuccess) {
        assertNull(failure);
      } else {
        assertTrue(failure != null);
        assertTrue(failure.getMessage().contains("uniqueness"));
      }
    }
  }

  public void testUniqueEntryCount() throws Exception {
    for (final TestDB testDB : SUPPORTED_DBS_TEST) {
      Database db = openCopy(testDB);
      Table table = db.getTable("Table1");
      IndexImpl indA = (IndexImpl)table.getIndex("PrimaryKey");
      IndexImpl indB = (IndexImpl)table.getIndex("B");

      assertEquals(2, indA.getUniqueEntryCount());
      assertEquals(2, indB.getUniqueEntryCount());

      List<String> bElems = Arrays.asList("bar", null, "baz", "argle", null,
                                          "bazzle", "37", "bar", "bar", "BAZ");

      for(int i = 0; i < 10; ++i) {
        table.addRow("foo" + i, bElems.get(i), (byte)42 + i, (short)53 + i,
                     13 * i, (6.7d / i), null, null, true);
      }

      assertEquals(12, indA.getIndexData().getEntryCount());
      assertEquals(12, indB.getIndexData().getEntryCount());

      assertEquals(12, indA.getUniqueEntryCount());
      assertEquals(8, indB.getUniqueEntryCount());

      table = null;
      indA = null;
      indB = null;

      table = db.getTable("Table1");
      indA = (IndexImpl)table.getIndex("PrimaryKey");
      indB = (IndexImpl)table.getIndex("B");

      assertEquals(12, indA.getIndexData().getEntryCount());
      assertEquals(12, indB.getIndexData().getEntryCount());

      assertEquals(12, indA.getUniqueEntryCount());
      assertEquals(8, indB.getUniqueEntryCount());

      Cursor c = CursorBuilder.createCursor(table);
      assertTrue(c.moveToNextRow());

      final Row row = c.getCurrentRow();
      // Row order is arbitrary, so v2007 row order difference is valid
      if (testDB.getExpectedFileFormat().ordinal() >= 
          Database.FileFormat.V2007.ordinal()) {
        DatabaseTest.checkTestDBTable1RowA(testDB, table, row);
      } else {
        DatabaseTest.checkTestDBTable1RowABCDEFG(testDB, table, row);
      }
      c.deleteCurrentRow();

      assertEquals(11, indA.getIndexData().getEntryCount());
      assertEquals(11, indB.getIndexData().getEntryCount());

      assertEquals(12, indA.getUniqueEntryCount());
      assertEquals(8, indB.getUniqueEntryCount());

      db.close();
    }
  }
  
  public void testReplId() throws Exception
  {
    for (final TestDB testDB : SUPPORTED_DBS_TEST) {
      Database db = openCopy(testDB);
      Table table = db.getTable("Table4");

      for(int i = 0; i< 20; ++i) {
        table.addRow("row" + i, Column.AUTO_NUMBER);
      }

      assertEquals(20, table.getRowCount());

      db.close();
    }
  }

  public void testIndexCreation() throws Exception
  {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = create(fileFormat);

      Table t = new TableBuilder("TestTable")
        .addColumn(new ColumnBuilder("id", DataType.LONG))
        .addColumn(new ColumnBuilder("data", DataType.TEXT))
        .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
                  .addColumns("id").setPrimaryKey())
        .toTable(db);

      assertEquals(1, t.getIndexes().size());
      IndexImpl idx = (IndexImpl)t.getIndexes().get(0);
      
      assertEquals(IndexBuilder.PRIMARY_KEY_NAME, idx.getName());       
      assertEquals(1, idx.getColumns().size());
      assertEquals("id", idx.getColumns().get(0).getName());
      assertTrue(idx.getColumns().get(0).isAscending());
      assertTrue(idx.isPrimaryKey());
      assertTrue(idx.isUnique());
      assertFalse(idx.shouldIgnoreNulls());
      assertNull(idx.getReference());

      t.addRow(2, "row2");
      t.addRow(1, "row1");
      t.addRow(3, "row3");

      Cursor c = t.newCursor()
        .setIndexByName(IndexBuilder.PRIMARY_KEY_NAME).toCursor();

      for(int i = 1; i <= 3; ++i) {
        Map<String,Object> row = c.getNextRow();
        assertEquals(i, row.get("id"));
        assertEquals("row" + i, row.get("data"));
      }
      assertFalse(c.moveToNextRow());
    }    
  }
  
  public void testGetForeignKeyIndex() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
      Database db = open(testDB);
      Table t1 = db.getTable("Table1");
      Table t2 = db.getTable("Table2");
      Table t3 = db.getTable("Table3");

      IndexImpl t2t1 = (IndexImpl)t1.getIndex("Table2Table1");
      IndexImpl t3t1 = (IndexImpl)t1.getIndex("Table3Table1");


      assertTrue(t2t1.isForeignKey());
      assertNotNull(t2t1.getReference());
      assertFalse(t2t1.getReference().isPrimaryTable());
      assertFalse(t2t1.getReference().isCascadeUpdates());
      assertTrue(t2t1.getReference().isCascadeDeletes());
      doCheckForeignKeyIndex(t1, t2t1, t2);

      assertTrue(t3t1.isForeignKey());
      assertNotNull(t3t1.getReference());
      assertFalse(t3t1.getReference().isPrimaryTable());
      assertTrue(t3t1.getReference().isCascadeUpdates());
      assertFalse(t3t1.getReference().isCascadeDeletes());
      doCheckForeignKeyIndex(t1, t3t1, t3);
      
      Index t1pk = t1.getIndex(IndexBuilder.PRIMARY_KEY_NAME);
      assertNotNull(t1pk);
      assertNull(((IndexImpl)t1pk).getReference());
      assertNull(t1pk.getReferencedIndex());
    }    
  }

  public void testConstraintViolation() throws Exception
  {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = create(fileFormat);

      Table t = new TableBuilder("TestTable")
        .addColumn(new ColumnBuilder("id", DataType.LONG))
        .addColumn(new ColumnBuilder("data", DataType.TEXT))
        .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
                  .addColumns("id").setPrimaryKey())
        .addIndex(new IndexBuilder("data_ind")
                  .addColumns("data").setUnique())
        .toTable(db);

      for(int i = 0; i < 5; ++i) {
        t.addRow(i, "row" + i);
      }

      try {
        t.addRow(3, "badrow");
        fail("ConstraintViolationException should have been thrown");
      } catch(ConstraintViolationException ce) {
        // success
      }

      assertEquals(5, t.getRowCount());

      List<Row> expectedRows =
        createExpectedTable(
            createExpectedRow(
                "id", 0, "data", "row0"),
            createExpectedRow(
                "id", 1, "data", "row1"),
            createExpectedRow(
                "id", 2, "data", "row2"),
            createExpectedRow(
                "id", 3, "data", "row3"),
            createExpectedRow(
                "id", 4, "data", "row4"));

      assertTable(expectedRows, t);

      IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t);
      assertCursor(expectedRows, pkCursor);

      assertCursor(expectedRows, 
                   CursorBuilder.createCursor(t.getIndex("data_ind")));

      List<Object[]> batch = new ArrayList<Object[]>();
      batch.add(new Object[]{5, "row5"});
      batch.add(new Object[]{6, "row6"});
      batch.add(new Object[]{7, "row2"});
      batch.add(new Object[]{8, "row8"});

      try {
        t.addRows(batch);
        fail("BatchUpdateException should have been thrown");
      } catch(BatchUpdateException be) {
        // success
        assertTrue(be.getCause() instanceof ConstraintViolationException);
        assertEquals(2, be.getUpdateCount());
      }

      expectedRows = new ArrayList<Row>(expectedRows);
      expectedRows.add(createExpectedRow("id", 5, "data", "row5"));
      expectedRows.add(createExpectedRow("id", 6, "data", "row6"));

      assertTable(expectedRows, t);
      
      assertCursor(expectedRows, pkCursor);

      assertCursor(expectedRows, 
                   CursorBuilder.createCursor(t.getIndex("data_ind")));

      pkCursor.findFirstRowByEntry(4);
      Row row4 = pkCursor.getCurrentRow();

      row4.put("id", 3);

      try {
        t.updateRow(row4);
        fail("ConstraintViolationException should have been thrown");
      } catch(ConstraintViolationException ce) {
        // success
      }
      
      assertTable(expectedRows, t);
      
      assertCursor(expectedRows, pkCursor);

      assertCursor(expectedRows, 
                   CursorBuilder.createCursor(t.getIndex("data_ind")));

      db.close();
    }    
  }

  public void testAutoNumberRecover() throws Exception
  {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = create(fileFormat);

      Table t = new TableBuilder("TestTable")
        .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
        .addColumn(new ColumnBuilder("data", DataType.TEXT))
        .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
                  .addColumns("id").setPrimaryKey())
        .addIndex(new IndexBuilder("data_ind")
                  .addColumns("data").setUnique())
        .toTable(db);

      for(int i = 1; i < 3; ++i) {
        t.addRow(null, "row" + i);
      }

      try {
        t.addRow(null, "row1");
        fail("ConstraintViolationException should have been thrown");
      } catch(ConstraintViolationException ce) {
        // success
      }
      
      t.addRow(null, "row3");

      assertEquals(3, t.getRowCount());

      List<Row> expectedRows =
        createExpectedTable(
            createExpectedRow(
                "id", 1, "data", "row1"),
            createExpectedRow(
                "id", 2, "data", "row2"),
            createExpectedRow(
                "id", 3, "data", "row3"));

      assertTable(expectedRows, t);

      IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t);
      assertCursor(expectedRows, pkCursor);

      assertCursor(expectedRows, 
                   CursorBuilder.createCursor(t.getIndex("data_ind")));

      List<Object[]> batch = new ArrayList<Object[]>();
      batch.add(new Object[]{null, "row4"});
      batch.add(new Object[]{null, "row5"});
      batch.add(new Object[]{null, "row3"});

      try {
        t.addRows(batch);
        fail("BatchUpdateException should have been thrown");
      } catch(BatchUpdateException be) {
        // success
        assertTrue(be.getCause() instanceof ConstraintViolationException);
        assertEquals(2, be.getUpdateCount());
      }

      expectedRows = new ArrayList<Row>(expectedRows);
      expectedRows.add(createExpectedRow("id", 4, "data", "row4"));
      expectedRows.add(createExpectedRow("id", 5, "data", "row5"));

      assertTable(expectedRows, t);
      
      assertCursor(expectedRows, pkCursor);

      assertCursor(expectedRows, 
                   CursorBuilder.createCursor(t.getIndex("data_ind")));

      db.close();
    }
  }
  
  public void testBinaryIndex() throws Exception
  {
    for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BINARY_INDEX)) {
      Database db = open(testDB);

      Table table = db.getTable("Test");

      Index idx = table.getIndex("BinAscIdx");
      doTestBinaryIndex(idx, "BinAsc", false);

      idx = table.getIndex("BinDscIdx");
      doTestBinaryIndex(idx, "BinDsc", true);

      db.close();
    }
  }

  private static void doTestBinaryIndex(Index idx, String colName, boolean forward)
    throws Exception
  {
    IndexCursor ic = CursorBuilder.createCursor(idx);

    for(Row row : idx.getTable().getDefaultCursor().newIterable().setForward(forward)) {
      int id = row.getInt("ID");
      byte[] data = row.getBytes(colName);

      boolean found = false;
      for(Row idxRow : ic.newEntryIterable(data)) {
          
        assertTrue(Arrays.equals(data, idxRow.getBytes(colName)));
        if(id == idxRow.getInt("ID")) {
          found = true;
        }
      }

      assertTrue(found);
    }
  }

  private void doCheckForeignKeyIndex(Table ta, Index ia, Table tb)
    throws Exception
  {
    IndexImpl ib = (IndexImpl)ia.getReferencedIndex();
    assertNotNull(ib);
    assertSame(tb, ib.getTable());

    assertNotNull(ib.getReference());
    assertSame(ia, ib.getReferencedIndex());
    assertTrue(ib.getReference().isPrimaryTable());
  }

  private void checkIndexColumns(Table table, String... idxInfo)
    throws Exception
  {
    Map<String, String> expectedIndexes = new HashMap<String, String>();
    for(int i = 0; i < idxInfo.length; i+=2) {
      expectedIndexes.put(idxInfo[i], idxInfo[i+1]);
    }

    for(Index idx : table.getIndexes()) {
      String colName = expectedIndexes.get(idx.getName());
      assertEquals(1, idx.getColumns().size());
      assertEquals(colName, idx.getColumns().get(0).getName());
      if("PrimaryKey".equals(idx.getName())) {
        assertTrue(idx.isPrimaryKey());
      } else {
        assertFalse(idx.isPrimaryKey());
      }
    }
  }

}