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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.healthmarketscience.jackcess;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.channels.FileChannel;
  5. import java.util.ArrayList;
  6. import java.util.EnumSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import junit.framework.TestCase;
  10. import static com.healthmarketscience.jackcess.Database.*;
  11. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  12. /**
  13. * @author Dan Rollo
  14. * Date: Mar 5, 2010
  15. * Time: 12:44:21 PM
  16. */
  17. public class JetFormatTest extends TestCase {
  18. private static final File DIR_TEST_DATA = new File("test/data");
  19. /**
  20. * Defines known valid db test file base names.
  21. */
  22. public static enum Basename {
  23. BIG_INDEX("bigIndexTest"),
  24. COMP_INDEX("compIndexTest"),
  25. DEL_COL("delColTest"),
  26. DEL("delTest"),
  27. FIXED_NUMERIC("fixedNumericTest"),
  28. FIXED_TEXT("fixedTextTest"),
  29. INDEX_CURSOR("indexCursorTest"),
  30. INDEX("indexTest"),
  31. OVERFLOW("overflowTest"),
  32. QUERY("queryTest"),
  33. TEST("test"),
  34. TEST2("test2"),
  35. INDEX_CODES("testIndexCodes"),
  36. INDEX_PROPERTIES("testIndexProperties"),
  37. PROMOTION("testPromotion");
  38. private final String _basename;
  39. Basename(String fileBasename) {
  40. _basename = fileBasename;
  41. }
  42. @Override
  43. public String toString() { return _basename; }
  44. }
  45. /** Defines currently supported db file formats. (can be modified at
  46. runtime via the system property
  47. "com.healthmarketscience.jackcess.testFormats") */
  48. final static FileFormat[] SUPPORTED_FILEFORMATS;
  49. final static FileFormat[] SUPPORTED_FILEFORMATS_FOR_READ;
  50. static {
  51. String testFormatStr = System.getProperty("com.healthmarketscience.jackcess.testFormats");
  52. Set<FileFormat> testFormats = EnumSet.allOf(FileFormat.class);
  53. if((testFormatStr != null) && (testFormatStr.length() > 0)) {
  54. testFormats.clear();
  55. for(String tmp : testFormatStr.split(",")) {
  56. testFormats.add(FileFormat.valueOf(tmp.toUpperCase()));
  57. }
  58. }
  59. List<FileFormat> supported = new ArrayList<FileFormat>();
  60. List<FileFormat> supportedForRead = new ArrayList<FileFormat>();
  61. for(FileFormat ff : FileFormat.values()) {
  62. if(!testFormats.contains(ff)) {
  63. continue;
  64. }
  65. supportedForRead.add(ff);
  66. if(ff.getJetFormat().READ_ONLY) {
  67. continue;
  68. }
  69. supported.add(ff);
  70. }
  71. SUPPORTED_FILEFORMATS = supported.toArray(new FileFormat[0]);
  72. SUPPORTED_FILEFORMATS_FOR_READ =
  73. supportedForRead.toArray(new FileFormat[0]);
  74. }
  75. /**
  76. * Defines known valid test database files, and their jet format version.
  77. */
  78. public static final class TestDB {
  79. private final File dbFile;
  80. private final FileFormat expectedFileFormat;
  81. private TestDB(File databaseFile,
  82. FileFormat expectedDBFileFormat) {
  83. dbFile = databaseFile;
  84. expectedFileFormat = expectedDBFileFormat;
  85. }
  86. public final File getFile() { return dbFile; }
  87. public final FileFormat getExpectedFileFormat() {
  88. return expectedFileFormat;
  89. }
  90. public final JetFormat getExpectedFormat() {
  91. return expectedFileFormat.getJetFormat();
  92. }
  93. @Override
  94. public final String toString() {
  95. return "dbFile: " + dbFile.getAbsolutePath()
  96. + "; expectedFileFormat: " + expectedFileFormat;
  97. }
  98. public static List<TestDB> getSupportedForBasename(Basename basename) {
  99. return getSupportedForBasename(basename, false);
  100. }
  101. public static List<TestDB> getSupportedForBasename(Basename basename,
  102. boolean readOnly) {
  103. List<TestDB> supportedTestDBs = new ArrayList<TestDB>();
  104. for (FileFormat fileFormat :
  105. (readOnly ? SUPPORTED_FILEFORMATS_FOR_READ :
  106. SUPPORTED_FILEFORMATS)) {
  107. File testFile = getFileForBasename(basename, fileFormat);
  108. if(!testFile.exists()) {
  109. continue;
  110. }
  111. // verify that the db is the file format expected
  112. try {
  113. // System.out.println("FOO checking " + testFile);
  114. Database db = Database.open(testFile, true);
  115. FileFormat dbFileFormat = db.getFileFormat();
  116. db.close();
  117. if(dbFileFormat != fileFormat) {
  118. throw new IllegalStateException("Expected " + fileFormat +
  119. " was " + dbFileFormat);
  120. }
  121. } catch(Exception e) {
  122. throw new RuntimeException(e);
  123. }
  124. supportedTestDBs.add(new TestDB(testFile, fileFormat));
  125. }
  126. return supportedTestDBs;
  127. }
  128. private static File getFileForBasename(
  129. Basename basename, FileFormat fileFormat) {
  130. return new File(DIR_TEST_DATA,
  131. fileFormat.name() + File.separator +
  132. basename + fileFormat.name() +
  133. fileFormat.getFileExtension());
  134. }
  135. }
  136. static final List<TestDB> SUPPORTED_DBS_TEST =
  137. TestDB.getSupportedForBasename(Basename.TEST);
  138. static final List<TestDB> SUPPORTED_DBS_TEST_FOR_READ =
  139. TestDB.getSupportedForBasename(Basename.TEST, true);
  140. public void testGetFormat() throws Exception {
  141. try {
  142. JetFormat.getFormat(null);
  143. fail("npe");
  144. } catch (NullPointerException e) {
  145. // success
  146. }
  147. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  148. final FileChannel channel = Database.openChannel(testDB.dbFile, false);
  149. try {
  150. JetFormat fmtActual = JetFormat.getFormat(channel);
  151. assertEquals("Unexpected JetFormat for dbFile: " +
  152. testDB.dbFile.getAbsolutePath(),
  153. testDB.expectedFileFormat.getJetFormat(), fmtActual);
  154. } finally {
  155. channel.close();
  156. }
  157. }
  158. }
  159. public void testReadOnlyFormat() throws Exception {
  160. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  161. Database db = null;
  162. IOException failure = null;
  163. try {
  164. db = openCopy(testDB);
  165. } catch(IOException e) {
  166. failure = e;
  167. } finally {
  168. if(db != null) {
  169. db.close();
  170. }
  171. }
  172. if(!testDB.getExpectedFormat().READ_ONLY) {
  173. assertNull(failure);
  174. } else {
  175. assertTrue(failure.getMessage().contains("does not support writing"));
  176. }
  177. }
  178. }
  179. public void testFileFormat() throws Exception {
  180. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  181. Database db = null;
  182. try {
  183. db = open(testDB);
  184. assertEquals(testDB.getExpectedFileFormat(), db.getFileFormat());
  185. } finally {
  186. if(db != null) {
  187. db.close();
  188. }
  189. }
  190. }
  191. }
  192. }