aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/java/com/healthmarketscience/jackcess/JetFormatTest.java
blob: d2e326717befdb1354bd4c94faf4bf4563659ad4 (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
package com.healthmarketscience.jackcess;

import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import static com.healthmarketscience.jackcess.Database.*;

/**
 * @author Dan Rollo
 *         Date: Mar 5, 2010
 *         Time: 12:44:21 PM
 */
public class JetFormatTest extends TestCase {

  private static final File DIR_TEST_DATA = new File("test/data");

  /**
   * Defines known valid db test file base names.
   */
  public static enum Basename {

    BIG_INDEX("bigIndexTest"),
      COMP_INDEX("compIndexTest"),
      DEL_COL("delColTest"),
      DEL("delTest"),
      FIXED_NUMERIC("fixedNumericTest"),
      FIXED_TEXT("fixedTextTest"),
      INDEX_CURSOR("indexCursorTest"),
      INDEX("indexTest"),
      OVERFLOW("overflowTest"),
      QUERY("queryTest"),
      TEST("test"),
      TEST2("test2"),
      INDEX_CODES("testIndexCodes"),
      INDEX_PROPERTIES("testIndexProperties"),
      PROMOTION("testPromotion"),
      ;

    private final String _basename;

    Basename(String fileBasename) {
      _basename = fileBasename;
    }

    @Override
    public String toString() { return _basename; }
  }

  /** Defines currently supported db file formats. */
  final static FileFormat[] SUPPORTED_FILEFORMATS = 
    new FileFormat[] {
    FileFormat.V2000,
    FileFormat.V2003,
    FileFormat.V2007,
  };

  /**
   * Defines known valid test database files, and their jet format version.
   */
  public static final class TestDB {

    private final File dbFile;
    private final FileFormat expectedFileFormat;

    private TestDB(File databaseFile, 
                   FileFormat expectedDBFileFormat) {

      dbFile = databaseFile;
      expectedFileFormat = expectedDBFileFormat;
    }

    public final File getFile() { return dbFile; }

    public final FileFormat  getExpectedFileFormat() { 
      return expectedFileFormat; 
    }

    public final JetFormat getExpectedFormat() { 
      return expectedFileFormat.getJetFormat(); 
    }

    @Override
    public final String toString() {
      return "dbFile: " + dbFile.getAbsolutePath()
        + "; expectedFileFormat: " + expectedFileFormat;
    }

    public static List<TestDB> getSupportedForBasename(Basename basename) {

      String testFormatStr = System.getProperty("com.healthmarketscience.jackcess.testFormats");
      Set<FileFormat> testFormats = EnumSet.allOf(FileFormat.class);
      if((testFormatStr != null) && (testFormatStr.length() > 0)) {
        testFormats = EnumSet.noneOf(FileFormat.class);
        for(String tmp : testFormatStr.split(",")) {
          testFormats.add(FileFormat.valueOf(tmp.toUpperCase()));
        }
      }

      List<TestDB> supportedTestDBs = new ArrayList<TestDB>();
      for (FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
        if(!testFormats.contains(fileFormat)) {
          continue;
        }
        supportedTestDBs.add(new TestDB(
                                 getFileForBasename(basename, fileFormat),
                                 fileFormat));
      }
      return supportedTestDBs;
    }

    private static File getFileForBasename(
        Basename basename, FileFormat fileFormat) {

      return new File(DIR_TEST_DATA, 
                      fileFormat.name() + File.separator +
                      basename + fileFormat.name() + 
                      fileFormat.getFileExtension());
    }
  }

  private static final File UNSUPPORTED_TEST_V1997 = 
    new File(DIR_TEST_DATA, "V1997" + File.separator +
             Basename.TEST + "V1997.mdb");

  static final List<TestDB> SUPPORTED_DBS_TEST = 
    TestDB.getSupportedForBasename(Basename.TEST);


  public void testGetFormat() throws Exception {
    try {
      JetFormat.getFormat(null);
      fail("npe");
    } catch (NullPointerException e) {
      // success
    }

    checkUnsupportedJetFormat(UNSUPPORTED_TEST_V1997);

    for (final TestDB testDB : SUPPORTED_DBS_TEST) {
      checkJetFormat(testDB);
    }
  }

  private static void checkJetFormat(final TestDB testDB)
    throws IOException {

    final FileChannel channel = Database.openChannel(testDB.dbFile, false);
    try {

      JetFormat fmtActual = JetFormat.getFormat(channel);
      assertEquals("Unexpected JetFormat for dbFile: " + 
                   testDB.dbFile.getAbsolutePath(),
                   testDB.expectedFileFormat.getJetFormat(), fmtActual);

    } finally {
      channel.close();
    }
  }

  private static void checkUnsupportedJetFormat(File testDB)
    throws IOException {

    final FileChannel channel = Database.openChannel(testDB, false);
    try {
      JetFormat.getFormat(channel);
      fail("Unexpected JetFormat for dbFile: " + 
           testDB.getAbsolutePath());
    } catch(IOException ignored) {
      // success
    } finally {
      channel.close();
    }
  }

}