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

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