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

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