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.1KB

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