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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.math.BigDecimal;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.time.ZoneId;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Calendar;
  24. import java.util.Date;
  25. import java.util.HashSet;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.TimeZone;
  31. import java.util.TreeSet;
  32. import java.util.UUID;
  33. import java.util.stream.Collectors;
  34. import static com.healthmarketscience.jackcess.Database.*;
  35. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  36. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  37. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  38. import com.healthmarketscience.jackcess.impl.RowIdImpl;
  39. import com.healthmarketscience.jackcess.impl.RowImpl;
  40. import com.healthmarketscience.jackcess.impl.TableImpl;
  41. import com.healthmarketscience.jackcess.util.LinkResolver;
  42. import com.healthmarketscience.jackcess.util.RowFilterTest;
  43. import junit.framework.TestCase;
  44. import static com.healthmarketscience.jackcess.TestUtil.*;
  45. import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
  46. /**
  47. * @author Tim McCune
  48. */
  49. @SuppressWarnings("deprecation")
  50. public class DatabaseTest extends TestCase
  51. {
  52. public DatabaseTest(String name) throws Exception {
  53. super(name);
  54. }
  55. public void testInvalidTableDefs() throws Exception {
  56. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  57. Database db = create(fileFormat);
  58. try {
  59. newTable("test").toTable(db);
  60. fail("created table with no columns?");
  61. } catch(IllegalArgumentException e) {
  62. // success
  63. }
  64. try {
  65. newTable("test")
  66. .addColumn(newColumn("A", DataType.TEXT))
  67. .addColumn(newColumn("a", DataType.MEMO))
  68. .toTable(db);
  69. fail("created table with duplicate column names?");
  70. } catch(IllegalArgumentException e) {
  71. // success
  72. }
  73. try {
  74. newTable("test")
  75. .addColumn(newColumn("A", DataType.TEXT)
  76. .setLengthInUnits(352))
  77. .toTable(db);
  78. fail("created table with invalid column length?");
  79. } catch(IllegalArgumentException e) {
  80. // success
  81. }
  82. try {
  83. newTable("test")
  84. .addColumn(newColumn("A_" + createString(70), DataType.TEXT))
  85. .toTable(db);
  86. fail("created table with too long column name?");
  87. } catch(IllegalArgumentException e) {
  88. // success
  89. }
  90. newTable("test")
  91. .addColumn(newColumn("A", DataType.TEXT))
  92. .toTable(db);
  93. try {
  94. newTable("Test")
  95. .addColumn(newColumn("A", DataType.TEXT))
  96. .toTable(db);
  97. fail("create duplicate tables?");
  98. } catch(IllegalArgumentException e) {
  99. // success
  100. }
  101. db.close();
  102. }
  103. }
  104. public void testReadDeletedRows() throws Exception {
  105. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL, true)) {
  106. Table table = open(testDB).getTable("Table");
  107. int rows = 0;
  108. while (table.getNextRow() != null) {
  109. rows++;
  110. }
  111. assertEquals(2, rows);
  112. table.getDatabase().close();
  113. }
  114. }
  115. public void testGetColumns() throws Exception {
  116. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  117. List<? extends Column> columns = open(testDB).getTable("Table1").getColumns();
  118. assertEquals(9, columns.size());
  119. checkColumn(columns, 0, "A", DataType.TEXT);
  120. checkColumn(columns, 1, "B", DataType.TEXT);
  121. checkColumn(columns, 2, "C", DataType.BYTE);
  122. checkColumn(columns, 3, "D", DataType.INT);
  123. checkColumn(columns, 4, "E", DataType.LONG);
  124. checkColumn(columns, 5, "F", DataType.DOUBLE);
  125. checkColumn(columns, 6, "G", DataType.SHORT_DATE_TIME);
  126. checkColumn(columns, 7, "H", DataType.MONEY);
  127. checkColumn(columns, 8, "I", DataType.BOOLEAN);
  128. }
  129. }
  130. private static void checkColumn(
  131. List<? extends Column> columns, int columnNumber, String name,
  132. DataType dataType)
  133. throws Exception
  134. {
  135. Column column = columns.get(columnNumber);
  136. assertEquals(name, column.getName());
  137. assertEquals(dataType, column.getType());
  138. }
  139. public void testGetNextRow() throws Exception {
  140. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  141. final Database db = open(testDB);
  142. db.setDateTimeType(DateTimeType.DATE);
  143. assertEquals(4, db.getTableNames().size());
  144. final Table table = db.getTable("Table1");
  145. Row row1 = table.getNextRow();
  146. Row row2 = table.getNextRow();
  147. if(!"abcdefg".equals(row1.get("A"))) {
  148. Row tmpRow = row1;
  149. row1 = row2;
  150. row2 = tmpRow;
  151. }
  152. checkTestDBTable1RowABCDEFG(testDB, table, row1);
  153. checkTestDBTable1RowA(testDB, table, row2);
  154. db.close();
  155. }
  156. }
  157. public void testCreate() throws Exception {
  158. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  159. Database db = create(fileFormat);
  160. assertEquals(0, db.getTableNames().size());
  161. db.close();
  162. }
  163. }
  164. public void testDeleteCurrentRow() throws Exception {
  165. // make sure correct row is deleted
  166. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  167. Database db = createMem(fileFormat);
  168. createTestTable(db);
  169. Map<String,Object> row1 = createTestRowMap("Tim1");
  170. Map<String,Object> row2 = createTestRowMap("Tim2");
  171. Map<String,Object> row3 = createTestRowMap("Tim3");
  172. Table table = db.getTable("Test");
  173. @SuppressWarnings("unchecked")
  174. List<Map<String,Object>> rows = Arrays.asList(row1, row2, row3);
  175. table.addRowsFromMaps(rows);
  176. assertRowCount(3, table);
  177. table.reset();
  178. table.getNextRow();
  179. table.getNextRow();
  180. table.getDefaultCursor().deleteCurrentRow();
  181. table.reset();
  182. Map<String, Object> outRow = table.getNextRow();
  183. assertEquals("Tim1", outRow.get("A"));
  184. outRow = table.getNextRow();
  185. assertEquals("Tim3", outRow.get("A"));
  186. assertRowCount(2, table);
  187. db.close();
  188. // test multi row delete/add
  189. db = createMem(fileFormat);
  190. createTestTable(db);
  191. Object[] row = createTestRow();
  192. table = db.getTable("Test");
  193. for (int i = 0; i < 10; i++) {
  194. row[3] = i;
  195. table.addRow(row);
  196. }
  197. row[3] = 1974;
  198. assertRowCount(10, table);
  199. table.reset();
  200. table.getNextRow();
  201. table.getDefaultCursor().deleteCurrentRow();
  202. assertRowCount(9, table);
  203. table.reset();
  204. table.getNextRow();
  205. table.getDefaultCursor().deleteCurrentRow();
  206. assertRowCount(8, table);
  207. table.reset();
  208. for (int i = 0; i < 8; i++) {
  209. table.getNextRow();
  210. }
  211. table.getDefaultCursor().deleteCurrentRow();
  212. assertRowCount(7, table);
  213. table.addRow(row);
  214. assertRowCount(8, table);
  215. table.reset();
  216. for (int i = 0; i < 3; i++) {
  217. table.getNextRow();
  218. }
  219. table.getDefaultCursor().deleteCurrentRow();
  220. assertRowCount(7, table);
  221. table.reset();
  222. assertEquals(2, table.getNextRow().get("D"));
  223. db.close();
  224. }
  225. }
  226. public void testDeleteRow() throws Exception {
  227. // make sure correct row is deleted
  228. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  229. Database db = createMem(fileFormat);
  230. createTestTable(db);
  231. Table table = db.getTable("Test");
  232. for(int i = 0; i < 10; ++i) {
  233. table.addRowFromMap(createTestRowMap("Tim" + i));
  234. }
  235. assertRowCount(10, table);
  236. table.reset();
  237. List<Row> rows = RowFilterTest.toList(table);
  238. Row r1 = rows.remove(7);
  239. Row r2 = rows.remove(3);
  240. assertEquals(8, rows.size());
  241. assertSame(r2, table.deleteRow(r2));
  242. assertSame(r1, table.deleteRow(r1));
  243. assertTable(rows, table);
  244. table.deleteRow(r2);
  245. table.deleteRow(r1);
  246. assertTable(rows, table);
  247. }
  248. }
  249. public void testMissingFile() throws Exception {
  250. File bogusFile = new File("fooby-dooby.mdb");
  251. assertTrue(!bogusFile.exists());
  252. try {
  253. newDatabase(bogusFile).setReadOnly(true).
  254. setAutoSync(getTestAutoSync()).open();
  255. fail("FileNotFoundException should have been thrown");
  256. } catch(FileNotFoundException e) {
  257. }
  258. assertTrue(!bogusFile.exists());
  259. }
  260. public void testReadWithDeletedCols() throws Exception {
  261. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL_COL, true)) {
  262. Table table = open(testDB).getTable("Table1");
  263. Map<String, Object> expectedRow0 = new LinkedHashMap<String, Object>();
  264. expectedRow0.put("id", 0);
  265. expectedRow0.put("id2", 2);
  266. expectedRow0.put("data", "foo");
  267. expectedRow0.put("data2", "foo2");
  268. Map<String, Object> expectedRow1 = new LinkedHashMap<String, Object>();
  269. expectedRow1.put("id", 3);
  270. expectedRow1.put("id2", 5);
  271. expectedRow1.put("data", "bar");
  272. expectedRow1.put("data2", "bar2");
  273. int rowNum = 0;
  274. Map<String, Object> row = null;
  275. while ((row = table.getNextRow()) != null) {
  276. if(rowNum == 0) {
  277. assertEquals(expectedRow0, row);
  278. } else if(rowNum == 1) {
  279. assertEquals(expectedRow1, row);
  280. } else if(rowNum >= 2) {
  281. fail("should only have 2 rows");
  282. }
  283. rowNum++;
  284. }
  285. table.getDatabase().close();
  286. }
  287. }
  288. public void testCurrency() throws Exception {
  289. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  290. Database db = create(fileFormat);
  291. Table table = newTable("test")
  292. .addColumn(newColumn("A", DataType.MONEY))
  293. .toTable(db);
  294. table.addRow(new BigDecimal("-2341234.03450"));
  295. table.addRow(37L);
  296. table.addRow("10000.45");
  297. table.reset();
  298. List<Object> foundValues = new ArrayList<Object>();
  299. Map<String, Object> row = null;
  300. while((row = table.getNextRow()) != null) {
  301. foundValues.add(row.get("A"));
  302. }
  303. assertEquals(Arrays.asList(
  304. new BigDecimal("-2341234.0345"),
  305. new BigDecimal("37.0000"),
  306. new BigDecimal("10000.4500")),
  307. foundValues);
  308. try {
  309. table.addRow(new BigDecimal("342523234145343543.3453"));
  310. fail("IOException should have been thrown");
  311. } catch(IOException e) {
  312. // ignored
  313. }
  314. db.close();
  315. }
  316. }
  317. public void testGUID() throws Exception
  318. {
  319. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  320. Database db = create(fileFormat);
  321. Table table = newTable("test")
  322. .addColumn(newColumn("A", DataType.GUID))
  323. .toTable(db);
  324. table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}");
  325. table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}");
  326. table.addRow("{11111111-1111-1111-1111-111111111111}");
  327. table.addRow(" {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} ");
  328. table.addRow(UUID.fromString("32a59f01-1234-3e29-4aaf-4523453cd2e6"));
  329. table.reset();
  330. List<Object> foundValues = new ArrayList<Object>();
  331. Map<String, Object> row = null;
  332. while((row = table.getNextRow()) != null) {
  333. foundValues.add(row.get("A"));
  334. }
  335. assertEquals(Arrays.asList(
  336. "{32A59F01-AA34-3E29-453F-4523453CD2E6}",
  337. "{32A59F01-AA34-3E29-453F-4523453CD2E6}",
  338. "{11111111-1111-1111-1111-111111111111}",
  339. "{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}",
  340. "{32A59F01-1234-3E29-4AAF-4523453CD2E6}"),
  341. foundValues);
  342. try {
  343. table.addRow("3245234");
  344. fail("IOException should have been thrown");
  345. } catch(IOException e) {
  346. // ignored
  347. }
  348. db.close();
  349. }
  350. }
  351. public void testNumeric() throws Exception
  352. {
  353. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  354. Database db = create(fileFormat);
  355. ColumnBuilder col = newColumn("A", DataType.NUMERIC)
  356. .setScale(4).setPrecision(8).toColumn();
  357. assertTrue(col.getType().isVariableLength());
  358. Table table = newTable("test")
  359. .addColumn(col)
  360. .addColumn(newColumn("B", DataType.NUMERIC)
  361. .setScale(8).setPrecision(28))
  362. .toTable(db);
  363. table.addRow(new BigDecimal("-1234.03450"),
  364. new BigDecimal("23923434453436.36234219"));
  365. table.addRow(37L, 37L);
  366. table.addRow("1000.45", "-3452345321000");
  367. table.reset();
  368. List<Object> foundSmallValues = new ArrayList<Object>();
  369. List<Object> foundBigValues = new ArrayList<Object>();
  370. Map<String, Object> row = null;
  371. while((row = table.getNextRow()) != null) {
  372. foundSmallValues.add(row.get("A"));
  373. foundBigValues.add(row.get("B"));
  374. }
  375. assertEquals(Arrays.asList(
  376. new BigDecimal("-1234.0345"),
  377. new BigDecimal("37.0000"),
  378. new BigDecimal("1000.4500")),
  379. foundSmallValues);
  380. assertEquals(Arrays.asList(
  381. new BigDecimal("23923434453436.36234219"),
  382. new BigDecimal("37.00000000"),
  383. new BigDecimal("-3452345321000.00000000")),
  384. foundBigValues);
  385. try {
  386. table.addRow(new BigDecimal("3245234.234"),
  387. new BigDecimal("3245234.234"));
  388. fail("IOException should have been thrown");
  389. } catch(IOException e) {
  390. // ignored
  391. }
  392. db.close();
  393. }
  394. }
  395. public void testFixedNumeric() throws Exception
  396. {
  397. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_NUMERIC)) {
  398. Database db = openCopy(testDB);
  399. Table t = db.getTable("test");
  400. boolean first = true;
  401. for(Column col : t.getColumns()) {
  402. if(first) {
  403. assertTrue(col.isVariableLength());
  404. assertEquals(DataType.MEMO, col.getType());
  405. first = false;
  406. } else {
  407. assertFalse(col.isVariableLength());
  408. assertEquals(DataType.NUMERIC, col.getType());
  409. }
  410. }
  411. Map<String, Object> row = t.getNextRow();
  412. assertEquals("some data", row.get("col1"));
  413. assertEquals(new BigDecimal("1"), row.get("col2"));
  414. assertEquals(new BigDecimal("0"), row.get("col3"));
  415. assertEquals(new BigDecimal("0"), row.get("col4"));
  416. assertEquals(new BigDecimal("4"), row.get("col5"));
  417. assertEquals(new BigDecimal("-1"), row.get("col6"));
  418. assertEquals(new BigDecimal("1"), row.get("col7"));
  419. Object[] tmpRow = new Object[]{
  420. "foo", new BigDecimal("1"), new BigDecimal(3), new BigDecimal("13"),
  421. new BigDecimal("-17"), new BigDecimal("0"), new BigDecimal("8734")};
  422. t.addRow(tmpRow);
  423. t.reset();
  424. t.getNextRow();
  425. row = t.getNextRow();
  426. assertEquals(tmpRow[0], row.get("col1"));
  427. assertEquals(tmpRow[1], row.get("col2"));
  428. assertEquals(tmpRow[2], row.get("col3"));
  429. assertEquals(tmpRow[3], row.get("col4"));
  430. assertEquals(tmpRow[4], row.get("col5"));
  431. assertEquals(tmpRow[5], row.get("col6"));
  432. assertEquals(tmpRow[6], row.get("col7"));
  433. db.close();
  434. }
  435. }
  436. public void testMultiPageTableDef() throws Exception
  437. {
  438. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  439. List<? extends Column> columns = open(testDB).getTable("Table2").getColumns();
  440. assertEquals(89, columns.size());
  441. }
  442. }
  443. public void testOverflow() throws Exception
  444. {
  445. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OVERFLOW, true)) {
  446. Database mdb = open(testDB);
  447. Table table = mdb.getTable("Table1");
  448. // 7 rows, 3 and 5 are overflow
  449. table.getNextRow();
  450. table.getNextRow();
  451. Map<String, Object> row = table.getNextRow();
  452. assertEquals(Arrays.<Object>asList(
  453. null, "row3col3", null, null, null, null, null,
  454. "row3col9", null),
  455. new ArrayList<Object>(row.values()));
  456. table.getNextRow();
  457. row = table.getNextRow();
  458. assertEquals(Arrays.<Object>asList(
  459. null, "row5col2", null, null, null, null, null, null,
  460. null),
  461. new ArrayList<Object>(row.values()));
  462. table.reset();
  463. assertRowCount(7, table);
  464. mdb.close();
  465. }
  466. }
  467. public void testUsageMapPromotion() throws Exception {
  468. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.PROMOTION)) {
  469. Database db = openMem(testDB);
  470. Table t = db.getTable("jobDB1");
  471. assertTrue(((TableImpl)t).getOwnedPagesCursor().getUsageMap().toString()
  472. .startsWith("InlineHandler"));
  473. String lval = createNonAsciiString(255); // "--255 chars long text--";
  474. ((DatabaseImpl)db).getPageChannel().startWrite();
  475. try {
  476. for(int i = 0; i < 1000; ++i) {
  477. t.addRow(i, 13, 57, lval, lval, lval, lval, lval, lval, 47.0d);
  478. }
  479. } finally {
  480. ((DatabaseImpl)db).getPageChannel().finishWrite();
  481. }
  482. Set<Integer> ids = t.stream()
  483. .map(r -> r.getInt("ID"))
  484. .collect(Collectors.toSet());
  485. assertEquals(1000, ids.size());
  486. assertTrue(((TableImpl)t).getOwnedPagesCursor().getUsageMap().toString()
  487. .startsWith("ReferenceHandler"));
  488. db.close();
  489. }
  490. }
  491. public void testLargeTableDef() throws Exception {
  492. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  493. Database db = create(fileFormat);
  494. final int numColumns = 90;
  495. List<ColumnBuilder> columns = new ArrayList<ColumnBuilder>();
  496. List<String> colNames = new ArrayList<String>();
  497. for(int i = 0; i < numColumns; ++i) {
  498. String colName = "MyColumnName" + i;
  499. colNames.add(colName);
  500. columns.add(newColumn(colName, DataType.TEXT).toColumn());
  501. }
  502. Table t = newTable("test")
  503. .addColumns(columns)
  504. .toTable(db);
  505. List<String> row = new ArrayList<String>();
  506. Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>();
  507. for(int i = 0; i < numColumns; ++i) {
  508. String value = "" + i + " some row data";
  509. row.add(value);
  510. expectedRowData.put(colNames.get(i), value);
  511. }
  512. t.addRow(row.toArray());
  513. t.reset();
  514. assertEquals(expectedRowData, t.getNextRow());
  515. db.close();
  516. }
  517. }
  518. public void testWriteAndReadDate() throws Exception {
  519. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  520. Database db = createMem(fileFormat);
  521. db.setDateTimeType(DateTimeType.DATE);
  522. Table table = newTable("test")
  523. .addColumn(newColumn("name", DataType.TEXT))
  524. .addColumn(newColumn("date", DataType.SHORT_DATE_TIME))
  525. .toTable(db);
  526. // since jackcess does not really store millis, shave them off before
  527. // storing the current date/time
  528. long curTimeNoMillis = (System.currentTimeMillis() / 1000L);
  529. curTimeNoMillis *= 1000L;
  530. DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
  531. List<Date> dates =
  532. new ArrayList<Date>(
  533. Arrays.asList(
  534. df.parse("19801231 00:00:00"),
  535. df.parse("19930513 14:43:27"),
  536. null,
  537. df.parse("20210102 02:37:00"),
  538. new Date(curTimeNoMillis)));
  539. Calendar c = Calendar.getInstance();
  540. for(int year = 1801; year < 2050; year +=3) {
  541. for(int month = 0; month <= 12; ++month) {
  542. for(int day = 1; day < 29; day += 3) {
  543. c.clear();
  544. c.set(Calendar.YEAR, year);
  545. c.set(Calendar.MONTH, month);
  546. c.set(Calendar.DAY_OF_MONTH, day);
  547. dates.add(c.getTime());
  548. }
  549. }
  550. }
  551. ((DatabaseImpl)db).getPageChannel().startWrite();
  552. try {
  553. for(Date d : dates) {
  554. table.addRow("row " + d, d);
  555. }
  556. } finally {
  557. ((DatabaseImpl)db).getPageChannel().finishWrite();
  558. }
  559. List<Date> foundDates = table.stream()
  560. .map(r -> r.getDate("date"))
  561. .collect(Collectors.toList());
  562. assertEquals(dates.size(), foundDates.size());
  563. for(int i = 0; i < dates.size(); ++i) {
  564. Date expected = dates.get(i);
  565. Date found = foundDates.get(i);
  566. assertSameDate(expected, found);
  567. }
  568. db.close();
  569. }
  570. }
  571. public void testAncientDates() throws Exception
  572. {
  573. TimeZone tz = TimeZone.getTimeZone("America/New_York");
  574. SimpleDateFormat sdf = DatabaseBuilder.createDateFormat("yyyy-MM-dd");
  575. sdf.getCalendar().setTimeZone(tz);
  576. List<String> dates = Arrays.asList("1582-10-15", "1582-10-14",
  577. "1492-01-10", "1392-01-10");
  578. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  579. Database db = createMem(fileFormat);
  580. db.setDateTimeType(DateTimeType.DATE);
  581. db.setTimeZone(tz);
  582. Table table = newTable("test")
  583. .addColumn(newColumn("name", DataType.TEXT))
  584. .addColumn(newColumn("date", DataType.SHORT_DATE_TIME))
  585. .toTable(db);
  586. for(String dateStr : dates) {
  587. Date d = sdf.parse(dateStr);
  588. table.addRow("row " + dateStr, d);
  589. }
  590. List<String> foundDates = table.stream()
  591. .map(r -> sdf.format(r.getDate("date")))
  592. .collect(Collectors.toList());
  593. assertEquals(dates, foundDates);
  594. db.close();
  595. }
  596. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OLD_DATES)) {
  597. Database db = openCopy(testDB);
  598. db.setDateTimeType(DateTimeType.DATE);
  599. Table t = db.getTable("Table1");
  600. List<String> foundDates = new ArrayList<String>();
  601. for(Row row : t) {
  602. foundDates.add(sdf.format(row.getDate("DateField")));
  603. }
  604. assertEquals(dates, foundDates);
  605. db.close();
  606. }
  607. }
  608. public void testSystemTable() throws Exception
  609. {
  610. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  611. Database db = create(fileFormat);
  612. Set<String> sysTables = new TreeSet<String>(
  613. String.CASE_INSENSITIVE_ORDER);
  614. sysTables.addAll(
  615. Arrays.asList("MSysObjects", "MSysQueries", "MSysACES",
  616. "MSysRelationships"));
  617. if (fileFormat == FileFormat.GENERIC_JET4) {
  618. assertNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects"));
  619. } else if (fileFormat.ordinal() < FileFormat.V2003.ordinal()) {
  620. assertNotNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects"));
  621. sysTables.add("MSysAccessObjects");
  622. } else {
  623. // v2003+ template files have no "MSysAccessObjects" table
  624. assertNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects"));
  625. sysTables.addAll(
  626. Arrays.asList("MSysNavPaneGroupCategories",
  627. "MSysNavPaneGroups", "MSysNavPaneGroupToObjects",
  628. "MSysNavPaneObjectIDs", "MSysAccessStorage"));
  629. if(fileFormat.ordinal() >= FileFormat.V2007.ordinal()) {
  630. sysTables.addAll(
  631. Arrays.asList(
  632. "MSysComplexColumns", "MSysComplexType_Attachment",
  633. "MSysComplexType_Decimal", "MSysComplexType_GUID",
  634. "MSysComplexType_IEEEDouble", "MSysComplexType_IEEESingle",
  635. "MSysComplexType_Long", "MSysComplexType_Short",
  636. "MSysComplexType_Text", "MSysComplexType_UnsignedByte"));
  637. }
  638. if(fileFormat.ordinal() >= FileFormat.V2010.ordinal()) {
  639. sysTables.add("f_12D7448B56564D8AAE333BCC9B3718E5_Data");
  640. sysTables.add("MSysResources");
  641. }
  642. if(fileFormat.ordinal() >= FileFormat.V2019.ordinal()) {
  643. sysTables.remove("f_12D7448B56564D8AAE333BCC9B3718E5_Data");
  644. sysTables.add("f_8FA5340F56044616AE380F64A2FEC135_Data");
  645. sysTables.add("MSysWSDPCacheComplexColumnMapping");
  646. sysTables.add("MSysWSDPChangeTokenMapping");
  647. sysTables.add("MSysWSDPRelationshipMapping");
  648. }
  649. }
  650. assertEquals(sysTables, db.getSystemTableNames());
  651. assertNotNull(db.getSystemTable("MSysObjects"));
  652. assertNotNull(db.getSystemTable("MSysQueries"));
  653. assertNotNull(db.getSystemTable("MSysACES"));
  654. assertNotNull(db.getSystemTable("MSysRelationships"));
  655. assertNull(db.getSystemTable("MSysBogus"));
  656. TableMetaData tmd = db.getTableMetaData("MSysObjects");
  657. assertEquals("MSysObjects", tmd.getName());
  658. assertFalse(tmd.isLinked());
  659. assertTrue(tmd.isSystem());
  660. db.close();
  661. }
  662. }
  663. public void testFixedText() throws Exception
  664. {
  665. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_TEXT)) {
  666. Database db = openCopy(testDB);
  667. Table t = db.getTable("users");
  668. Column c = t.getColumn("c_flag_");
  669. assertEquals(DataType.TEXT, c.getType());
  670. assertEquals(false, c.isVariableLength());
  671. assertEquals(2, c.getLength());
  672. Map<String,Object> row = t.getNextRow();
  673. assertEquals("N", row.get("c_flag_"));
  674. t.addRow(3, "testFixedText", "boo", "foo", "bob", 3, 5, 9, "Y",
  675. new Date());
  676. t.getNextRow();
  677. row = t.getNextRow();
  678. assertEquals("testFixedText", row.get("c_user_login"));
  679. assertEquals("Y", row.get("c_flag_"));
  680. db.close();
  681. }
  682. }
  683. public void testDbSortOrder() throws Exception {
  684. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  685. Database db = open(testDB);
  686. assertEquals(((DatabaseImpl)db).getFormat().DEFAULT_SORT_ORDER,
  687. ((DatabaseImpl)db).getDefaultSortOrder());
  688. db.close();
  689. }
  690. }
  691. public void testUnsupportedColumns() throws Exception {
  692. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) {
  693. Database db = open(testDB);
  694. Table t = db.getTable("Test");
  695. Column varCol = t.getColumn("UnknownVar");
  696. assertEquals(DataType.UNSUPPORTED_VARLEN, varCol.getType());
  697. Column fixCol = t.getColumn("UnknownFix");
  698. assertEquals(DataType.UNSUPPORTED_FIXEDLEN, fixCol.getType());
  699. List<String> varVals = Arrays.asList(
  700. "RawData[(10) FF FE 73 6F 6D 65 64 61 74 61]",
  701. "RawData[(12) FF FE 6F 74 68 65 72 20 64 61 74 61]",
  702. null);
  703. List<String> fixVals = Arrays.asList("RawData[(4) 37 00 00 00]",
  704. "RawData[(4) F3 FF FF FF]",
  705. "RawData[(4) 02 00 00 00]");
  706. int idx = 0;
  707. for(Map<String,Object> row : t) {
  708. checkRawValue(varVals.get(idx), varCol.getRowValue(row));
  709. checkRawValue(fixVals.get(idx), fixCol.getRowValue(row));
  710. ++idx;
  711. }
  712. db.close();
  713. }
  714. }
  715. public void testLinkedTables() throws Exception {
  716. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.LINKED)) {
  717. Database db = openCopy(testDB);
  718. try {
  719. db.getTable("Table2");
  720. fail("FileNotFoundException should have been thrown");
  721. } catch(FileNotFoundException e) {
  722. // success
  723. }
  724. TableMetaData tmd = db.getTableMetaData("Table2");
  725. assertEquals("Table2", tmd.getName());
  726. assertTrue(tmd.isLinked());
  727. assertFalse(tmd.isSystem());
  728. assertEquals("Table1", tmd.getLinkedTableName());
  729. assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName());
  730. tmd = db.getTableMetaData("FooTable");
  731. assertNull(tmd);
  732. assertTrue(db.getLinkedDatabases().isEmpty());
  733. final String linkeeDbName = "Z:\\jackcess_test\\linkeeTest.accdb";
  734. final File linkeeFile = new File("src/test/data/linkeeTest.accdb");
  735. db.setLinkResolver(new LinkResolver() {
  736. public Database resolveLinkedDatabase(Database linkerdb, String dbName)
  737. throws IOException {
  738. assertEquals(linkeeDbName, dbName);
  739. return DatabaseBuilder.open(linkeeFile);
  740. }
  741. });
  742. Table t2 = db.getTable("Table2");
  743. assertEquals(1, db.getLinkedDatabases().size());
  744. Database linkeeDb = db.getLinkedDatabases().get(linkeeDbName);
  745. assertNotNull(linkeeDb);
  746. assertEquals(linkeeFile, linkeeDb.getFile());
  747. assertEquals("linkeeTest.accdb", ((DatabaseImpl)linkeeDb).getName());
  748. List<? extends Map<String, Object>> expectedRows =
  749. createExpectedTable(
  750. createExpectedRow(
  751. "ID", 1,
  752. "Field1", "bar"));
  753. assertTable(expectedRows, t2);
  754. db.createLinkedTable("FooTable", linkeeDbName, "Table2");
  755. tmd = db.getTableMetaData("FooTable");
  756. assertEquals("FooTable", tmd.getName());
  757. assertTrue(tmd.isLinked());
  758. assertFalse(tmd.isSystem());
  759. assertEquals("Table2", tmd.getLinkedTableName());
  760. assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName());
  761. Table t3 = db.getTable("FooTable");
  762. assertEquals(1, db.getLinkedDatabases().size());
  763. expectedRows =
  764. createExpectedTable(
  765. createExpectedRow(
  766. "ID", 1,
  767. "Field1", "buzz"));
  768. assertTable(expectedRows, t3);
  769. tmd = db.getTableMetaData("Table1");
  770. assertEquals("Table1", tmd.getName());
  771. assertFalse(tmd.isLinked());
  772. assertFalse(tmd.isSystem());
  773. assertNull(tmd.getLinkedTableName());
  774. assertNull(tmd.getLinkedDbName());
  775. Table t1 = tmd.open(db);
  776. assertFalse(db.isLinkedTable(null));
  777. assertTrue(db.isLinkedTable(t2));
  778. assertTrue(db.isLinkedTable(t3));
  779. assertFalse(db.isLinkedTable(t1));
  780. List<Table> tables = getTables(db.newIterable());
  781. assertEquals(3, tables.size());
  782. assertTrue(tables.contains(t1));
  783. assertTrue(tables.contains(t2));
  784. assertTrue(tables.contains(t3));
  785. assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
  786. tables = getTables(db.newIterable().setIncludeNormalTables(false));
  787. assertEquals(2, tables.size());
  788. assertFalse(tables.contains(t1));
  789. assertTrue(tables.contains(t2));
  790. assertTrue(tables.contains(t3));
  791. assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
  792. tables = getTables(db.newIterable().withLocalUserTablesOnly());
  793. assertEquals(1, tables.size());
  794. assertTrue(tables.contains(t1));
  795. assertFalse(tables.contains(t2));
  796. assertFalse(tables.contains(t3));
  797. assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
  798. tables = getTables(db.newIterable().withSystemTablesOnly());
  799. assertTrue(tables.size() > 5);
  800. assertFalse(tables.contains(t1));
  801. assertFalse(tables.contains(t2));
  802. assertFalse(tables.contains(t3));
  803. assertTrue(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
  804. db.close();
  805. }
  806. }
  807. private static List<Table> getTables(Iterable<Table> tableIter)
  808. {
  809. List<Table> tableList = new ArrayList<Table>();
  810. for(Table t : tableIter) {
  811. tableList.add(t);
  812. }
  813. return tableList;
  814. }
  815. public void testTimeZone() throws Exception
  816. {
  817. TimeZone tz = TimeZone.getTimeZone("America/New_York");
  818. doTestTimeZone(tz);
  819. tz = TimeZone.getTimeZone("Australia/Sydney");
  820. doTestTimeZone(tz);
  821. }
  822. private static void doTestTimeZone(final TimeZone tz) throws Exception
  823. {
  824. ColumnImpl col = new ColumnImpl(null, null, DataType.SHORT_DATE_TIME, 0, 0, 0) {
  825. @Override
  826. public TimeZone getTimeZone() { return tz; }
  827. @Override
  828. public ZoneId getZoneId() { return null; }
  829. @Override
  830. public ColumnImpl.DateTimeFactory getDateTimeFactory() {
  831. return getDateTimeFactory(DateTimeType.DATE);
  832. }
  833. };
  834. SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
  835. df.setTimeZone(tz);
  836. long startDate = df.parse("2012.01.01").getTime();
  837. long endDate = df.parse("2013.01.01").getTime();
  838. Calendar curCal = Calendar.getInstance(tz);
  839. curCal.setTimeInMillis(startDate);
  840. SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
  841. sdf.setTimeZone(tz);
  842. while(curCal.getTimeInMillis() < endDate) {
  843. Date curDate = curCal.getTime();
  844. Date newDate = new Date(col.fromDateDouble(col.toDateDouble(curDate)));
  845. if(curDate.getTime() != newDate.getTime()) {
  846. assertEquals(sdf.format(curDate), sdf.format(newDate));
  847. }
  848. curCal.add(Calendar.MINUTE, 30);
  849. }
  850. }
  851. public void testToString()
  852. {
  853. RowImpl row = new RowImpl(new RowIdImpl(1, 1));
  854. row.put("id", 37);
  855. row.put("data", null);
  856. assertEquals("Row[1:1][{id=37,data=<null>}]", row.toString());
  857. }
  858. public void testIterateTableNames() throws Exception {
  859. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  860. final Database db = open(testDB);
  861. Set<String> names = new HashSet<>();
  862. int sysCount = 0;
  863. for(TableMetaData tmd : db.newTableMetaDataIterable()) {
  864. if(tmd.isSystem()) {
  865. ++sysCount;
  866. continue;
  867. }
  868. assertFalse(tmd.isLinked());
  869. assertNull(tmd.getLinkedTableName());
  870. assertNull(tmd.getLinkedDbName());
  871. names.add(tmd.getName());
  872. }
  873. assertTrue(sysCount > 4);
  874. assertEquals(new HashSet<>(Arrays.asList("Table1", "Table2", "Table3",
  875. "Table4")),
  876. names);
  877. }
  878. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.LINKED)) {
  879. final Database db = open(testDB);
  880. Set<String> names = new HashSet<>();
  881. for(TableMetaData tmd : db.newTableMetaDataIterable()) {
  882. if(tmd.isSystem()) {
  883. continue;
  884. }
  885. if("Table1".equals(tmd.getName())) {
  886. assertFalse(tmd.isLinked());
  887. assertNull(tmd.getLinkedTableName());
  888. assertNull(tmd.getLinkedDbName());
  889. } else {
  890. assertTrue(tmd.isLinked());
  891. assertEquals("Table1", tmd.getLinkedTableName());
  892. assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName());
  893. }
  894. names.add(tmd.getName());
  895. }
  896. assertEquals(new HashSet<>(Arrays.asList("Table1", "Table2")),
  897. names);
  898. }
  899. }
  900. public void testTableDates() throws Exception {
  901. for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
  902. Table table = open(testDB).getTable("Table1");
  903. String expectedCreateDate = null;
  904. String expectedUpdateDate = null;
  905. if(testDB.getExpectedFileFormat() == FileFormat.V1997) {
  906. expectedCreateDate = "2010-03-05T14:48:26.420";
  907. expectedUpdateDate = "2010-03-05T14:48:26.607";
  908. } else {
  909. expectedCreateDate = "2004-05-28T17:51:48.701";
  910. expectedUpdateDate = "2006-07-24T09:56:19.701";
  911. }
  912. System.out.println("FOO " + testDB.getExpectedFileFormat() + " " +
  913. table.getCreatedDate() + " " +
  914. table.getUpdatedDate());
  915. assertEquals(expectedCreateDate, table.getCreatedDate().toString());
  916. assertEquals(expectedUpdateDate, table.getUpdatedDate().toString());
  917. }
  918. }
  919. private static void checkRawValue(String expected, Object val)
  920. {
  921. if(expected != null) {
  922. assertTrue(ColumnImpl.isRawData(val));
  923. assertEquals(expected, val.toString());
  924. } else {
  925. assertNull(val);
  926. }
  927. }
  928. }