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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (c) 2004 Health Market Science, Inc.
  2. package com.healthmarketscience.jackcess;
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.Map;
  9. import junit.framework.TestCase;
  10. /**
  11. * @author Tim McCune
  12. */
  13. public class DatabaseTest extends TestCase {
  14. public DatabaseTest(String name) throws Exception {
  15. super(name);
  16. }
  17. private Database open() throws Exception {
  18. return Database.open(new File("test/data/test.mdb"));
  19. }
  20. private Database create() throws Exception {
  21. File tmp = File.createTempFile("databaseTest", ".mdb");
  22. tmp.deleteOnExit();
  23. return Database.create(tmp);
  24. }
  25. public void testReadDeletedRows() throws Exception {
  26. Table table = Database.open(new File("test/data/delTest.mdb")).getTable("Table");
  27. int rows = 0;
  28. while (table.getNextRow() != null) {
  29. rows++;
  30. }
  31. assertEquals(2, rows);
  32. }
  33. public void testGetColumns() throws Exception {
  34. List columns = open().getTable("Table1").getColumns();
  35. assertEquals(9, columns.size());
  36. checkColumn(columns, 0, "A", DataType.TEXT);
  37. checkColumn(columns, 1, "B", DataType.TEXT);
  38. checkColumn(columns, 2, "C", DataType.BYTE);
  39. checkColumn(columns, 3, "D", DataType.INT);
  40. checkColumn(columns, 4, "E", DataType.LONG);
  41. checkColumn(columns, 5, "F", DataType.DOUBLE);
  42. checkColumn(columns, 6, "G", DataType.SHORT_DATE_TIME);
  43. checkColumn(columns, 7, "H", DataType.MONEY);
  44. checkColumn(columns, 8, "I", DataType.BOOLEAN);
  45. }
  46. private void checkColumn(List columns, int columnNumber, String name,
  47. DataType dataType)
  48. throws Exception {
  49. Column column = (Column) columns.get(columnNumber);
  50. assertEquals(name, column.getName());
  51. assertEquals(dataType, column.getType());
  52. }
  53. public void testGetNextRow() throws Exception {
  54. Database db = open();
  55. assertEquals(1, db.getTableNames().size());
  56. Table table = db.getTable("Table1");
  57. Map row = table.getNextRow();
  58. assertEquals("abcdefg", row.get("A"));
  59. assertEquals("hijklmnop", row.get("B"));
  60. assertEquals(new Byte((byte) 2), row.get("C"));
  61. assertEquals(new Short((short) 222), row.get("D"));
  62. assertEquals(new Integer(333333333), row.get("E"));
  63. assertEquals(new Double(444.555d), row.get("F"));
  64. Calendar cal = Calendar.getInstance();
  65. cal.setTime((Date) row.get("G"));
  66. assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH));
  67. assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
  68. assertEquals(1974, cal.get(Calendar.YEAR));
  69. assertEquals(Boolean.TRUE, row.get("I"));
  70. row = table.getNextRow();
  71. assertEquals("a", row.get("A"));
  72. assertEquals("b", row.get("B"));
  73. assertEquals(new Byte((byte) 0), row.get("C"));
  74. assertEquals(new Short((short) 0), row.get("D"));
  75. assertEquals(new Integer(0), row.get("E"));
  76. assertEquals(new Double(0d), row.get("F"));
  77. cal = Calendar.getInstance();
  78. cal.setTime((Date) row.get("G"));
  79. assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH));
  80. assertEquals(12, cal.get(Calendar.DAY_OF_MONTH));
  81. assertEquals(1981, cal.get(Calendar.YEAR));
  82. assertEquals(Boolean.FALSE, row.get("I"));
  83. }
  84. public void testCreate() throws Exception {
  85. Database db = create();
  86. assertEquals(0, db.getTableNames().size());
  87. }
  88. public void testWriteAndRead() throws Exception {
  89. Database db = create();
  90. createTestTable(db);
  91. Object[] row = createTestRow();
  92. row[3] = null;
  93. Table table = db.getTable("Test");
  94. int count = 1000;
  95. for (int i = 0; i < count; i++) {
  96. table.addRow(row);
  97. }
  98. for (int i = 0; i < count; i++) {
  99. Map readRow = table.getNextRow();
  100. assertEquals(row[0], readRow.get("A"));
  101. assertEquals(row[1], readRow.get("B"));
  102. assertEquals(row[2], readRow.get("C"));
  103. assertEquals(row[3], readRow.get("D"));
  104. assertEquals(row[4], readRow.get("E"));
  105. assertEquals(row[5], readRow.get("F"));
  106. assertEquals(row[6], readRow.get("G"));
  107. assertEquals(row[7], readRow.get("H"));
  108. }
  109. }
  110. public void testWriteAndReadInBatch() throws Exception {
  111. Database db = create();
  112. createTestTable(db);
  113. int count = 1000;
  114. List<Object[]> rows = new ArrayList<Object[]>(count);
  115. Object[] row = createTestRow();
  116. for (int i = 0; i < count; i++) {
  117. rows.add(row);
  118. }
  119. Table table = db.getTable("Test");
  120. table.addRows(rows);
  121. for (int i = 0; i < count; i++) {
  122. Map readRow = table.getNextRow();
  123. assertEquals(row[0], readRow.get("A"));
  124. assertEquals(row[1], readRow.get("B"));
  125. assertEquals(row[2], readRow.get("C"));
  126. assertEquals(row[3], readRow.get("D"));
  127. assertEquals(row[4], readRow.get("E"));
  128. assertEquals(row[5], readRow.get("F"));
  129. assertEquals(row[6], readRow.get("G"));
  130. assertEquals(row[7], readRow.get("H"));
  131. }
  132. }
  133. public void testDeleteCurrentRow() throws Exception {
  134. Database db = create();
  135. createTestTable(db);
  136. Object[] row = createTestRow();
  137. Table table = db.getTable("Test");
  138. for (int i = 0; i < 10; i++) {
  139. row[3] = i;
  140. table.addRow(row);
  141. }
  142. row[3] = 1974;
  143. assertEquals(10, countRows(table));
  144. table.reset();
  145. table.getNextRow();
  146. table.deleteCurrentRow();
  147. assertEquals(9, countRows(table));
  148. table.reset();
  149. table.getNextRow();
  150. table.deleteCurrentRow();
  151. assertEquals(8, countRows(table));
  152. table.reset();
  153. for (int i = 0; i < 8; i++) {
  154. table.getNextRow();
  155. }
  156. table.deleteCurrentRow();
  157. assertEquals(7, countRows(table));
  158. table.addRow(row);
  159. assertEquals(8, countRows(table));
  160. table.reset();
  161. for (int i = 0; i < 3; i++) {
  162. table.getNextRow();
  163. }
  164. table.deleteCurrentRow();
  165. assertEquals(7, countRows(table));
  166. table.reset();
  167. assertEquals(2, table.getNextRow().get("D"));
  168. }
  169. private int countRows(Table table) throws Exception {
  170. table.reset();
  171. int rtn = 0;
  172. while (table.getNextRow() != null) {
  173. rtn++;
  174. }
  175. return rtn;
  176. }
  177. private Object[] createTestRow() {
  178. return new Object[] {"Tim", "R", "McCune", 1234, (byte) 0xad, 555.66d,
  179. 777.88f, (short) 999, new Date()};
  180. }
  181. private void createTestTable(Database db) throws Exception {
  182. List<Column> columns = new ArrayList<Column>();
  183. Column col = new Column();
  184. col.setName("A");
  185. col.setType(DataType.TEXT);
  186. columns.add(col);
  187. col = new Column();
  188. col.setName("B");
  189. col.setType(DataType.TEXT);
  190. columns.add(col);
  191. col = new Column();
  192. col.setName("C");
  193. col.setType(DataType.TEXT);
  194. columns.add(col);
  195. col = new Column();
  196. col.setName("D");
  197. col.setType(DataType.LONG);
  198. columns.add(col);
  199. col = new Column();
  200. col.setName("E");
  201. col.setType(DataType.BYTE);
  202. columns.add(col);
  203. col = new Column();
  204. col.setName("F");
  205. col.setType(DataType.DOUBLE);
  206. columns.add(col);
  207. col = new Column();
  208. col.setName("G");
  209. col.setType(DataType.FLOAT);
  210. columns.add(col);
  211. col = new Column();
  212. col.setName("H");
  213. col.setType(DataType.INT);
  214. columns.add(col);
  215. col = new Column();
  216. col.setName("I");
  217. col.setType(DataType.SHORT_DATE_TIME);
  218. columns.add(col);
  219. db.createTable("test", columns);
  220. }
  221. }