You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JetFormatTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.healthmarketscience.jackcess;
  2. import junit.framework.TestCase;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.channels.FileChannel;
  6. /**
  7. * @author Dan Rollo
  8. * Date: Mar 5, 2010
  9. * Time: 12:44:21 PM
  10. */
  11. public final class JetFormatTest extends TestCase {
  12. private static final File DIR_TEST_DATA = new File("test/data");
  13. /**
  14. * Defines known valid db test file base names.
  15. */
  16. public static enum Basename {
  17. BIG_INDEX("bigIndexTest"),
  18. COMP_INDEX("compIndexTest"),
  19. DEL_COL("delColTest"),
  20. DEL("delTest"),
  21. FIXED_NUMERIC("fixedNumericTest"),
  22. FIXED_TEXT("fixedTextTest"),
  23. INDEX_CURSOR("indexCursorTest"),
  24. INDEX("indexTest"),
  25. OVERFLOW("overflowTest"),
  26. QUERY("queryTest"),
  27. TEST("test"),
  28. TEST2("test2"),
  29. INDEX_CODES("testIndexCodes"),
  30. INDEX_PROPERTIES("testIndexProperties"),
  31. PROMOTION("testPromotion"),
  32. ;
  33. private final String basename;
  34. Basename(final String fileBasename) {
  35. basename = fileBasename;
  36. }
  37. }
  38. /** Defines currently supported db file formats. */
  39. final static Database.FileFormat[] SUPPORTED_FILEFORMATS = new Database.FileFormat[] {
  40. Database.FileFormat.V2000,
  41. Database.FileFormat.V2003,
  42. Database.FileFormat.V2007,
  43. // @todo Uncomment these elements to run test other formats
  44. };
  45. /**
  46. * Defines known valid test database files, and their jet format version.
  47. */
  48. public static final class TestDB {
  49. private final File dbFile;
  50. private final Database.FileFormat expectedFileFormat;
  51. private TestDB(final File databaseFile, final Database.FileFormat expectedDBFileFormat) {
  52. dbFile = databaseFile;
  53. expectedFileFormat = expectedDBFileFormat;
  54. }
  55. public final File getFile() { return dbFile; }
  56. public final Database.FileFormat getExpectedFileFormat() { return expectedFileFormat; }
  57. public final JetFormat getExpectedFormat() { return expectedFileFormat.getJetFormat(); }
  58. public final String toString() {
  59. return "dbFile: " + dbFile.getAbsolutePath()
  60. + "; expectedFileFormat: " + expectedFileFormat;
  61. }
  62. public static TestDB[] getSupportedForBasename(final Basename basename) {
  63. final TestDB[] supportedTestDBs = new TestDB[SUPPORTED_FILEFORMATS.length];
  64. int i = 0;
  65. for (final Database.FileFormat fileFormat: SUPPORTED_FILEFORMATS) {
  66. supportedTestDBs[i++] = new TestDB(
  67. getFileForBasename(basename, fileFormat),
  68. fileFormat);
  69. }
  70. return supportedTestDBs;
  71. }
  72. private static File getFileForBasename(Basename basename, Database.FileFormat fileFormat) {
  73. return new File(DIR_TEST_DATA,
  74. fileFormat.name() + "/" + basename.basename + fileFormat.name() +
  75. fileFormat.getFileExtension());
  76. }
  77. }
  78. static final TestDB UNSUPPORTED_TEST_V1997 = new TestDB(
  79. TestDB.getFileForBasename(Basename.TEST, Database.FileFormat.V1997), Database.FileFormat.V1997);
  80. static final TestDB[] SUPPORTED_DBS_TEST= TestDB.getSupportedForBasename(Basename.TEST);
  81. public void testGetFormat() throws Exception {
  82. try {
  83. JetFormat.getFormat(null);
  84. fail("npe");
  85. } catch (NullPointerException e) {
  86. assertNull(e.getMessage());
  87. }
  88. checkJetFormat(UNSUPPORTED_TEST_V1997);
  89. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  90. checkJetFormat(testDB);
  91. }
  92. }
  93. private static void checkJetFormat(final TestDB testDB)
  94. throws IOException {
  95. final FileChannel channel = Database.openChannel(testDB.dbFile, false);
  96. try {
  97. final JetFormat fmtActual = JetFormat.getFormat(channel);
  98. assertEquals("Unexpected JetFormat for dbFile: " + testDB.dbFile.getAbsolutePath(),
  99. testDB.expectedFileFormat.getJetFormat(), fmtActual);
  100. } finally {
  101. channel.close();
  102. }
  103. }
  104. }