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.

DatabaseTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (c) 2004 Health Market Science, Inc.
  2. package com.healthmarketscience.jackcess;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Map;
  10. import junit.framework.TestCase;
  11. /**
  12. * @author Tim McCune
  13. */
  14. public class DatabaseTest extends TestCase {
  15. public DatabaseTest(String name) throws Exception {
  16. super(name);
  17. }
  18. private Database open() throws Exception {
  19. return Database.open(new File("test/data/test.mdb"));
  20. }
  21. private Database create() throws Exception {
  22. File tmp = File.createTempFile("databaseTest", ".mdb");
  23. tmp.deleteOnExit();
  24. return Database.create(tmp);
  25. }
  26. public void testReadDeletedRows() throws Exception {
  27. Table table = Database.open(new File("test/data/delTest.mdb")).getTable("Table");
  28. int rows = 0;
  29. while (table.getNextRow() != null) {
  30. rows++;
  31. }
  32. assertEquals(2, rows);
  33. }
  34. public void testGetColumns() throws Exception {
  35. List columns = open().getTable("Table1").getColumns();
  36. assertEquals(9, columns.size());
  37. checkColumn(columns, 0, "A", DataType.TEXT);
  38. checkColumn(columns, 1, "B", DataType.TEXT);
  39. checkColumn(columns, 2, "C", DataType.BYTE);
  40. checkColumn(columns, 3, "D", DataType.INT);
  41. checkColumn(columns, 4, "E", DataType.LONG);
  42. checkColumn(columns, 5, "F", DataType.DOUBLE);
  43. checkColumn(columns, 6, "G", DataType.SHORT_DATE_TIME);
  44. checkColumn(columns, 7, "H", DataType.MONEY);
  45. checkColumn(columns, 8, "I", DataType.BOOLEAN);
  46. }
  47. private void checkColumn(List columns, int columnNumber, String name,
  48. DataType dataType)
  49. throws Exception {
  50. Column column = (Column) columns.get(columnNumber);
  51. assertEquals(name, column.getName());
  52. assertEquals(dataType, column.getType());
  53. }
  54. public void testGetNextRow() throws Exception {
  55. Database db = open();
  56. assertEquals(1, db.getTableNames().size());
  57. Table table = db.getTable("Table1");
  58. Map<String, Object> row = table.getNextRow();
  59. assertEquals("abcdefg", row.get("A"));
  60. assertEquals("hijklmnop", row.get("B"));
  61. assertEquals(new Byte((byte) 2), row.get("C"));
  62. assertEquals(new Short((short) 222), row.get("D"));
  63. assertEquals(new Integer(333333333), row.get("E"));
  64. assertEquals(new Double(444.555d), row.get("F"));
  65. Calendar cal = Calendar.getInstance();
  66. cal.setTime((Date) row.get("G"));
  67. assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH));
  68. assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
  69. assertEquals(1974, cal.get(Calendar.YEAR));
  70. assertEquals(Boolean.TRUE, row.get("I"));
  71. row = table.getNextRow();
  72. assertEquals("a", row.get("A"));
  73. assertEquals("b", row.get("B"));
  74. assertEquals(new Byte((byte) 0), row.get("C"));
  75. assertEquals(new Short((short) 0), row.get("D"));
  76. assertEquals(new Integer(0), row.get("E"));
  77. assertEquals(new Double(0d), row.get("F"));
  78. cal = Calendar.getInstance();
  79. cal.setTime((Date) row.get("G"));
  80. assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH));
  81. assertEquals(12, cal.get(Calendar.DAY_OF_MONTH));
  82. assertEquals(1981, cal.get(Calendar.YEAR));
  83. assertEquals(Boolean.FALSE, row.get("I"));
  84. }
  85. public void testCreate() throws Exception {
  86. Database db = create();
  87. assertEquals(0, db.getTableNames().size());
  88. }
  89. public void testWriteAndRead() throws Exception {
  90. Database db = create();
  91. createTestTable(db);
  92. Object[] row = createTestRow();
  93. row[3] = null;
  94. Table table = db.getTable("Test");
  95. int count = 1000;
  96. for (int i = 0; i < count; i++) {
  97. table.addRow(row);
  98. }
  99. for (int i = 0; i < count; i++) {
  100. Map<String, Object> readRow = table.getNextRow();
  101. assertEquals(row[0], readRow.get("A"));
  102. assertEquals(row[1], readRow.get("B"));
  103. assertEquals(row[2], readRow.get("C"));
  104. assertEquals(row[3], readRow.get("D"));
  105. assertEquals(row[4], readRow.get("E"));
  106. assertEquals(row[5], readRow.get("F"));
  107. assertEquals(row[6], readRow.get("G"));
  108. assertEquals(row[7], readRow.get("H"));
  109. }
  110. }
  111. public void testWriteAndReadInBatch() throws Exception {
  112. Database db = create();
  113. createTestTable(db);
  114. int count = 1000;
  115. List<Object[]> rows = new ArrayList<Object[]>(count);
  116. Object[] row = createTestRow();
  117. for (int i = 0; i < count; i++) {
  118. rows.add(row);
  119. }
  120. Table table = db.getTable("Test");
  121. table.addRows(rows);
  122. for (int i = 0; i < count; i++) {
  123. Map<String, Object> readRow = table.getNextRow();
  124. assertEquals(row[0], readRow.get("A"));
  125. assertEquals(row[1], readRow.get("B"));
  126. assertEquals(row[2], readRow.get("C"));
  127. assertEquals(row[3], readRow.get("D"));
  128. assertEquals(row[4], readRow.get("E"));
  129. assertEquals(row[5], readRow.get("F"));
  130. assertEquals(row[6], readRow.get("G"));
  131. assertEquals(row[7], readRow.get("H"));
  132. }
  133. }
  134. public void testDeleteCurrentRow() throws Exception {
  135. Database db = create();
  136. createTestTable(db);
  137. Object[] row = createTestRow();
  138. Table table = db.getTable("Test");
  139. for (int i = 0; i < 10; i++) {
  140. row[3] = i;
  141. table.addRow(row);
  142. }
  143. row[3] = 1974;
  144. assertEquals(10, countRows(table));
  145. table.reset();
  146. table.getNextRow();
  147. table.deleteCurrentRow();
  148. assertEquals(9, countRows(table));
  149. table.reset();
  150. table.getNextRow();
  151. table.deleteCurrentRow();
  152. assertEquals(8, countRows(table));
  153. table.reset();
  154. for (int i = 0; i < 8; i++) {
  155. table.getNextRow();
  156. }
  157. table.deleteCurrentRow();
  158. assertEquals(7, countRows(table));
  159. table.addRow(row);
  160. assertEquals(8, countRows(table));
  161. table.reset();
  162. for (int i = 0; i < 3; i++) {
  163. table.getNextRow();
  164. }
  165. table.deleteCurrentRow();
  166. assertEquals(7, countRows(table));
  167. table.reset();
  168. assertEquals(2, table.getNextRow().get("D"));
  169. }
  170. public void testReadMemo() throws Exception {
  171. Database db = Database.open(new File("test/data/test2.mdb"));
  172. Table table = db.getTable("MSP_PROJECTS");
  173. Map<String, Object> row = table.getNextRow();
  174. assertEquals("Jon Iles this is a a vawesrasoih aksdkl fas dlkjflkasjd flkjaslkdjflkajlksj dfl lkasjdf lkjaskldfj lkas dlk lkjsjdfkl; aslkdf lkasjkldjf lka skldf lka sdkjfl;kasjd falksjdfljaslkdjf laskjdfk jalskjd flkj aslkdjflkjkjasljdflkjas jf;lkasjd fjkas dasdf asd fasdf asdf asdmhf lksaiyudfoi jasodfj902384jsdf9 aw90se fisajldkfj lkasj dlkfslkd jflksjadf as", row.get("PROJ_PROP_AUTHOR"));
  175. assertEquals("T", row.get("PROJ_PROP_COMPANY"));
  176. assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME"));
  177. assertEquals("Project1", row.get("PROJ_PROP_TITLE"));
  178. }
  179. public void testWriteMemo() throws Exception {
  180. Database db = create();
  181. List<Column> columns = new ArrayList<Column>();
  182. Column col = new Column();
  183. col.setName("A");
  184. col.setType(DataType.TEXT);
  185. columns.add(col);
  186. col = new Column();
  187. col.setName("B");
  188. col.setType(DataType.MEMO);
  189. columns.add(col);
  190. db.createTable("test", columns);
  191. String testStr = "This is a test";
  192. Table table = db.getTable("Test");
  193. table.addRow(new Object[]{testStr, testStr});
  194. Map<String, Object> row = table.getNextRow();
  195. assertEquals(testStr, row.get("A"));
  196. assertEquals(testStr, row.get("B"));
  197. }
  198. public void testMissingFile() throws Exception {
  199. File bogusFile = new File("fooby-dooby.mdb");
  200. assertTrue(!bogusFile.exists());
  201. try {
  202. Database db = Database.open(bogusFile);
  203. fail("FileNotFoundException should have been thrown");
  204. } catch(FileNotFoundException e) {
  205. }
  206. assertTrue(!bogusFile.exists());
  207. }
  208. private int countRows(Table table) throws Exception {
  209. table.reset();
  210. int rtn = 0;
  211. while (table.getNextRow() != null) {
  212. rtn++;
  213. }
  214. return rtn;
  215. }
  216. private Object[] createTestRow() {
  217. return new Object[] {"Tim", "R", "McCune", 1234, (byte) 0xad, 555.66d,
  218. 777.88f, (short) 999, new Date()};
  219. }
  220. private void createTestTable(Database db) throws Exception {
  221. List<Column> columns = new ArrayList<Column>();
  222. Column col = new Column();
  223. col.setName("A");
  224. col.setType(DataType.TEXT);
  225. columns.add(col);
  226. col = new Column();
  227. col.setName("B");
  228. col.setType(DataType.TEXT);
  229. columns.add(col);
  230. col = new Column();
  231. col.setName("C");
  232. col.setType(DataType.TEXT);
  233. columns.add(col);
  234. col = new Column();
  235. col.setName("D");
  236. col.setType(DataType.LONG);
  237. columns.add(col);
  238. col = new Column();
  239. col.setName("E");
  240. col.setType(DataType.BYTE);
  241. columns.add(col);
  242. col = new Column();
  243. col.setName("F");
  244. col.setType(DataType.DOUBLE);
  245. columns.add(col);
  246. col = new Column();
  247. col.setName("G");
  248. col.setType(DataType.FLOAT);
  249. columns.add(col);
  250. col = new Column();
  251. col.setName("H");
  252. col.setType(DataType.INT);
  253. columns.add(col);
  254. col = new Column();
  255. col.setName("I");
  256. col.setType(DataType.SHORT_DATE_TIME);
  257. columns.add(col);
  258. db.createTable("test", columns);
  259. }
  260. }