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

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