aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/healthmarketscience/jackcess/TestUtil.java
blob: fae1fa0733a8c66ee766d7923fafab832c4f1e91 (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
/*
Copyright (c) 2015 James Ahlborn

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.*;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.StreamSupport;

import com.healthmarketscience.jackcess.Database.FileFormat;
import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
import com.healthmarketscience.jackcess.impl.*;
import com.healthmarketscience.jackcess.impl.JetFormatTest.TestDB;
import com.healthmarketscience.jackcess.util.MemFileChannel;
import org.junit.Assert;

/**
 * Utilty code for the test cases.
 *
 * @author James Ahlborn
 */
@SuppressWarnings("deprecation")
public class TestUtil
{
  public static final TimeZone TEST_TZ =
    TimeZone.getTimeZone("America/New_York");

  private static final ThreadLocal<Boolean> _autoSync =
    new ThreadLocal<Boolean>();

  private TestUtil() {}

  static void setTestAutoSync(boolean autoSync) {
    _autoSync.set(autoSync);
  }

  static void clearTestAutoSync() {
    _autoSync.remove();
  }

  static boolean getTestAutoSync() {
    Boolean autoSync = _autoSync.get();
    return ((autoSync != null) ? autoSync : Database.DEFAULT_AUTO_SYNC);
  }

  public static Database open(FileFormat fileFormat, File file)
    throws Exception
  {
    return open(fileFormat, file, false);
  }

  public static Database open(FileFormat fileFormat, File file, boolean inMem)
    throws Exception
  {
    return open(fileFormat, file, inMem, null);
  }

  public static Database open(FileFormat fileFormat, File file, boolean inMem,
                              Charset charset)
    throws Exception
  {
    return openDB(fileFormat, file, inMem, charset, true);
  }

  public static Database open(TestDB testDB) throws Exception {
    return open(testDB.getExpectedFileFormat(), testDB.getFile(), false,
                testDB.getExpectedCharset());
  }

  public static Database openMem(TestDB testDB) throws Exception {
    return openDB(testDB.getExpectedFileFormat(), testDB.getFile(), true,
                    testDB.getExpectedCharset(), false);
  }

  public static Database create(FileFormat fileFormat) throws Exception {
    return create(fileFormat, false);
  }

  public static Database create(FileFormat fileFormat, boolean keep)
    throws Exception
  {
    return create(fileFormat, keep, true);
  }

  public static Database createMem(FileFormat fileFormat) throws Exception {
    return create(fileFormat);
  }

  public static Database createFile(FileFormat fileFormat) throws Exception {
    return create(fileFormat, false, false);
  }

  private static Database create(FileFormat fileFormat, boolean keep,
                                 boolean inMem)
    throws Exception
  {

    FileChannel channel = ((inMem && !keep) ? MemFileChannel.newChannel() :
                           null);

    if (fileFormat == FileFormat.GENERIC_JET4) {
      // while we don't support creating GENERIC_JET4 as a jackcess feature,
      // we do want to be able to test these types of dbs
      InputStream inStream = null;
      OutputStream outStream = null;
      try {
        inStream = TestUtil.class.getClassLoader()
          .getResourceAsStream("emptyJet4.mdb");
        File f = createTempFile(keep);
        if (channel != null) {
          JetFormatTest.transferDbFrom(channel, inStream);
        } else {
          ByteUtil.copy(inStream, outStream = new FileOutputStream(f));
          outStream.close();
        }
        return new DatabaseBuilder(f)
          .setAutoSync(getTestAutoSync()).setChannel(channel).open();
      } finally {
        ByteUtil.closeQuietly(inStream);
        ByteUtil.closeQuietly(outStream);
      }
    }

    return new DatabaseBuilder(createTempFile(keep)).setFileFormat(fileFormat)
        .setAutoSync(getTestAutoSync()).setChannel(channel).create();
  }


  public static Database openCopy(TestDB testDB) throws Exception {
    return openCopy(testDB, false);
  }

  public static Database openCopy(TestDB testDB, boolean keep)
    throws Exception
  {
    return openCopy(testDB.getExpectedFileFormat(), testDB.getFile(), keep);
  }

  public static Database openCopy(FileFormat fileFormat, File file)
    throws Exception
  {
    return openCopy(fileFormat, file, false);
  }

  public static Database openCopy(FileFormat fileFormat, File file,
                                  boolean keep)
    throws Exception
  {
    File tmp = createTempFile(keep);
    copyFile(file, tmp);
    return openDB(fileFormat, tmp, false, null, false);
  }

  private static Database openDB(
      FileFormat fileFormat, File file, boolean inMem, Charset charset,
      boolean readOnly)
    throws Exception
  {
    FileChannel channel = (inMem ? MemFileChannel.newChannel(
                               file, MemFileChannel.RW_CHANNEL_MODE)
                           : null);
    final Database db = new DatabaseBuilder(file).setReadOnly(readOnly)
      .setAutoSync(getTestAutoSync()).setChannel(channel)
      .setCharset(charset).open();
    if(fileFormat != null) {
      Assert.assertEquals(
          "Wrong JetFormat.",
          DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(),
          ((DatabaseImpl)db).getFormat());
      Assert.assertEquals(
          "Wrong FileFormat.", fileFormat, db.getFileFormat());
    }
    return db;
  }

  static Object[] createTestRow(String col1Val) {
    return new Object[] {col1Val, "R", "McCune", 1234, (byte) 0xad, 555.66d,
        777.88f, (short) 999, new Date()};
  }

  public static Object[] createTestRow() {
    return createTestRow("Tim");
  }

  static Map<String,Object> createTestRowMap(String col1Val) {
    return createExpectedRow("A", col1Val, "B", "R", "C", "McCune",
                             "D", 1234, "E", (byte) 0xad, "F", 555.66d,
                             "G", 777.88f, "H", (short) 999, "I", new Date());
  }

  public static void createTestTable(Database db) throws Exception {
    new TableBuilder("test")
      .addColumn(new ColumnBuilder("A", DataType.TEXT))
      .addColumn(new ColumnBuilder("B", DataType.TEXT))
      .addColumn(new ColumnBuilder("C", DataType.TEXT))
      .addColumn(new ColumnBuilder("D", DataType.LONG))
      .addColumn(new ColumnBuilder("E", DataType.BYTE))
      .addColumn(new ColumnBuilder("F", DataType.DOUBLE))
      .addColumn(new ColumnBuilder("G", DataType.FLOAT))
      .addColumn(new ColumnBuilder("H", DataType.INT))
      .addColumn(new ColumnBuilder("I", DataType.SHORT_DATE_TIME))
      .toTable(db);
  }

  public static String createString(int len) {
    return createString(len, 'a');
  }

  static String createNonAsciiString(int len) {
    return createString(len, '\u0CC0');
  }

  private static String createString(int len, char firstChar) {
    StringBuilder builder = new StringBuilder(len);
    for(int i = 0; i < len; ++i) {
      builder.append((char)(firstChar + (i % 26)));
    }
    return builder.toString();
  }

  static void assertRowCount(int expectedRowCount, Table table)
    throws Exception
  {
    Assert.assertEquals(expectedRowCount, countRows(table));
    Assert.assertEquals(expectedRowCount, table.getRowCount());
  }

  public static int countRows(Table table) throws Exception {
    Cursor cursor = CursorBuilder.createCursor(table);
    return (int) StreamSupport.stream(cursor.spliterator(), false).count();
  }

  public static void assertTable(
      List<? extends Map<String, Object>> expectedTable,
      Table table)
    throws IOException
  {
    assertCursor(expectedTable, CursorBuilder.createCursor(table));
  }

  public static void assertCursor(
      List<? extends Map<String, Object>> expectedTable,
      Cursor cursor)
  {
    List<Map<String, Object>> foundTable =
      new ArrayList<Map<String, Object>>();
    for(Map<String, Object> row : cursor) {
      foundTable.add(row);
    }
    Assert.assertEquals(expectedTable.size(), foundTable.size());
    for(int i = 0; i < expectedTable.size(); ++i) {
      Assert.assertEquals(expectedTable.get(i), foundTable.get(i));
    }
  }

  public static RowImpl createExpectedRow(Object... rowElements) {
    RowImpl row = new RowImpl((RowIdImpl)null);
    for(int i = 0; i < rowElements.length; i += 2) {
      row.put((String)rowElements[i],
              rowElements[i + 1]);
    }
    return row;
  }

  public static List<Row> createExpectedTable(Row... rows) {
    return Arrays.<Row>asList(rows);
  }

  public static void dumpDatabase(Database mdb) throws Exception {
    dumpDatabase(mdb, false);
  }

  public static void dumpDatabase(Database mdb, boolean systemTables)
    throws Exception
  {
    dumpDatabase(mdb, systemTables, new PrintWriter(System.out, true));
  }

  public static void dumpTable(Table table) throws Exception {
    dumpTable(table, new PrintWriter(System.out, true));
  }

  public static void dumpProperties(Table table) throws Exception {
    System.out.println("TABLE_PROPS: " + table.getName() + ": " +
                       table.getProperties());
    for(Column c : table.getColumns()) {
      System.out.println("COL_PROPS: " + c.getName() + ": " +
                         c.getProperties());
    }
  }

  static void dumpDatabase(Database mdb, boolean systemTables,
                           PrintWriter writer) throws Exception
  {
    writer.println("DATABASE:");
    for(Table table : mdb) {
      dumpTable(table, writer);
    }
    if(systemTables) {
      for(String sysTableName : mdb.getSystemTableNames()) {
        dumpTable(mdb.getSystemTable(sysTableName), writer);
      }
    }
  }

  static void dumpTable(Table table, PrintWriter writer) throws Exception {
    // make sure all indexes are read
    for(Index index : table.getIndexes()) {
      ((IndexImpl)index).initialize();
    }

    writer.println("TABLE: " + table.getName());
    List<String> colNames = new ArrayList<String>();
    for(Column col : table.getColumns()) {
      colNames.add(col.getName());
    }
    writer.println("COLUMNS: " + colNames);
    for(Map<String, Object> row : CursorBuilder.createCursor(table)) {
      writer.println(massageRow(row));
    }
  }

  private static Map<String,Object> massageRow(Map<String, Object> row)
    throws IOException
  {
      for(Map.Entry<String, Object> entry : row.entrySet()) {
        Object v = entry.getValue();
        if(v instanceof byte[]) {
          // make byte[] printable
          byte[] bv = (byte[])v;
          entry.setValue(ByteUtil.toHexString(ByteBuffer.wrap(bv), bv.length));
        } else if(v instanceof ComplexValueForeignKey) {
          // deref complex values
          String str = "ComplexValue(" + v + ")" +
            ((ComplexValueForeignKey)v).getValues();
          entry.setValue(str);
        }
      }

      return row;
  }

  static void dumpIndex(Index index) throws Exception {
    dumpIndex(index, new PrintWriter(System.out, true));
  }

  static void dumpIndex(Index index, PrintWriter writer) throws Exception {
    writer.println("INDEX: " + index);
    IndexData.EntryCursor ec = ((IndexImpl)index).cursor();
    IndexData.Entry lastE = ec.getLastEntry();
    IndexData.Entry e = null;
    while((e = ec.getNextEntry()) != lastE) {
      writer.println(e);
    }
  }

  static void assertSameDate(Date expected, Date found)
  {
    if(expected == found) {
      return;
    }
    if((expected == null) || (found == null)) {
      throw new AssertionError("Expected " + expected + ", found " + found);
    }
    long expTime = expected.getTime();
    long foundTime = found.getTime();
    // there are some rounding issues due to dates being stored as doubles,
    // but it results in a 1 millisecond difference, so i'm not going to worry
    // about it
    if((expTime != foundTime) && (Math.abs(expTime - foundTime) > 1)) {
      throw new AssertionError("Expected " + expTime + " (" + expected +
                               "), found " + foundTime + " (" + found + ")");
    }
  }

  static void assertSameDate(Date expected, LocalDateTime found)
  {
    if((expected == null) && (found == null)) {
      return;
    }
    if((expected == null) || (found == null)) {
      throw new AssertionError("Expected " + expected + ", found " + found);
    }

    LocalDateTime expectedLdt = LocalDateTime.ofInstant(
        Instant.ofEpochMilli(expected.getTime()),
        ZoneId.systemDefault());

    Assert.assertEquals(expectedLdt, found);
  }

  public static void copyFile(File srcFile, File dstFile)
    throws IOException
  {
    // FIXME should really be using commons io FileUtils here, but don't want
    // to add dep for one simple test method
    OutputStream ostream = new FileOutputStream(dstFile);
    InputStream istream = new FileInputStream(srcFile);
    try {
      copyStream(istream, ostream);
    } finally {
      ostream.close();
    }
  }

  static void copyStream(InputStream istream, OutputStream ostream)
    throws IOException
  {
    // FIXME should really be using commons io FileUtils here, but don't want
    // to add dep for one simple test method
    byte[] buf = new byte[1024];
    int numBytes = 0;
    while((numBytes = istream.read(buf)) >= 0) {
      ostream.write(buf, 0, numBytes);
    }
  }

  public static File createTempFile(boolean keep) throws Exception {
    File tmp = File.createTempFile("databaseTest", ".mdb");
    if(keep) {
      System.out.println("Created " + tmp);
    } else {
      tmp.deleteOnExit();
    }
    return tmp;
  }

  public static void clearTableCache(Database db) throws Exception
  {
    Field f = db.getClass().getDeclaredField("_tableCache");
    f.setAccessible(true);
    Object val = f.get(db);
    f = val.getClass().getDeclaredField("_tables");
    f.setAccessible(true);
    val = f.get(val);
    ((Map<?,?>)val).clear();
  }

  public static byte[] toByteArray(File file)
    throws IOException
  {
    return toByteArray(new FileInputStream(file), file.length());
  }

  public static byte[] toByteArray(InputStream in, long length)
    throws IOException
  {
    // FIXME should really be using commons io IOUtils here, but don't want
    // to add dep for one simple test method
    try {
      DataInputStream din = new DataInputStream(in);
      byte[] bytes = new byte[(int)length];
      din.readFully(bytes);
      return bytes;
    } finally {
      in.close();
    }
  }

  static void checkTestDBTable1RowABCDEFG(final TestDB testDB, final Table table, final Row row)
          throws IOException {
    Assert.assertEquals("testDB: " + testDB + "; table: " + table, "abcdefg", row.get("A"));
    Assert.assertEquals("hijklmnop", row.get("B"));
    Assert.assertEquals(new Byte((byte) 2), row.get("C"));
    Assert.assertEquals(new Short((short) 222), row.get("D"));
    Assert.assertEquals(new Integer(333333333), row.get("E"));
    Assert.assertEquals(new Double(444.555d), row.get("F"));
    final Calendar cal = Calendar.getInstance();
    cal.setTime(row.getDate("G"));
    Assert.assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH));
    Assert.assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
    Assert.assertEquals(1974, cal.get(Calendar.YEAR));
    Assert.assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(0, cal.get(Calendar.MINUTE));
    Assert.assertEquals(0, cal.get(Calendar.SECOND));
    Assert.assertEquals(0, cal.get(Calendar.MILLISECOND));
    Assert.assertEquals(Boolean.TRUE, row.get("I"));
  }

  static void checkTestDBTable1RowA(final TestDB testDB, final Table table, final Row row)
          throws IOException {
    Assert.assertEquals("testDB: " + testDB + "; table: " + table, "a", row.get("A"));
    Assert.assertEquals("b", row.get("B"));
    Assert.assertEquals(new Byte((byte) 0), row.get("C"));
    Assert.assertEquals(new Short((short) 0), row.get("D"));
    Assert.assertEquals(new Integer(0), row.get("E"));
    Assert.assertEquals(new Double(0d), row.get("F"));
    final Calendar cal = Calendar.getInstance();
    cal.setTime(row.getDate("G"));
    Assert.assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH));
    Assert.assertEquals(12, cal.get(Calendar.DAY_OF_MONTH));
    Assert.assertEquals(1981, cal.get(Calendar.YEAR));
    Assert.assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(0, cal.get(Calendar.MINUTE));
    Assert.assertEquals(0, cal.get(Calendar.SECOND));
    Assert.assertEquals(0, cal.get(Calendar.MILLISECOND));
    Assert.assertEquals(Boolean.FALSE, row.get("I"));
  }

}