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.

DatabaseReadWriteTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. Copyright (c) 2015 James Ahlborn
  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.impl;
  14. import java.time.Instant;
  15. import java.time.LocalDateTime;
  16. import java.time.ZoneOffset;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import com.healthmarketscience.jackcess.Column;
  21. import com.healthmarketscience.jackcess.ColumnBuilder;
  22. import com.healthmarketscience.jackcess.Cursor;
  23. import com.healthmarketscience.jackcess.CursorBuilder;
  24. import com.healthmarketscience.jackcess.DataType;
  25. import com.healthmarketscience.jackcess.Database;
  26. import static com.healthmarketscience.jackcess.Database.*;
  27. import com.healthmarketscience.jackcess.Row;
  28. import com.healthmarketscience.jackcess.Table;
  29. import com.healthmarketscience.jackcess.TableBuilder;
  30. import static com.healthmarketscience.jackcess.TestUtil.*;
  31. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  32. import com.healthmarketscience.jackcess.util.RowFilterTest;
  33. import junit.framework.TestCase;
  34. /**
  35. *
  36. * @author James Ahlborn
  37. */
  38. public class DatabaseReadWriteTest extends TestCase
  39. {
  40. public DatabaseReadWriteTest(String name) throws Exception {
  41. super(name);
  42. }
  43. public void testWriteAndRead() throws Exception {
  44. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  45. Database db = create(fileFormat);
  46. doTestWriteAndRead(db);
  47. db.close();
  48. }
  49. }
  50. public void testWriteAndReadInMem() throws Exception {
  51. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  52. Database db = createMem(fileFormat);
  53. doTestWriteAndRead(db);
  54. db.close();
  55. }
  56. }
  57. private static void doTestWriteAndRead(Database db) throws Exception {
  58. createTestTable(db);
  59. Object[] row = createTestRow();
  60. row[3] = null;
  61. Table table = db.getTable("Test");
  62. int count = 1000;
  63. ((DatabaseImpl)db).getPageChannel().startWrite();
  64. try {
  65. for (int i = 0; i < count; i++) {
  66. table.addRow(row);
  67. }
  68. } finally {
  69. ((DatabaseImpl)db).getPageChannel().finishWrite();
  70. }
  71. for (int i = 0; i < count; i++) {
  72. Map<String, Object> readRow = table.getNextRow();
  73. assertEquals(row[0], readRow.get("A"));
  74. assertEquals(row[1], readRow.get("B"));
  75. assertEquals(row[2], readRow.get("C"));
  76. assertEquals(row[3], readRow.get("D"));
  77. assertEquals(row[4], readRow.get("E"));
  78. assertEquals(row[5], readRow.get("F"));
  79. assertEquals(row[6], readRow.get("G"));
  80. assertEquals(row[7], readRow.get("H"));
  81. }
  82. }
  83. public void testWriteAndReadInBatch() throws Exception {
  84. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  85. Database db = createMem(fileFormat);
  86. createTestTable(db);
  87. int count = 1000;
  88. List<Object[]> rows = new ArrayList<Object[]>(count);
  89. Object[] row = createTestRow();
  90. for (int i = 0; i < count; i++) {
  91. rows.add(row);
  92. }
  93. Table table = db.getTable("Test");
  94. table.addRows(rows);
  95. for (int i = 0; i < count; i++) {
  96. Map<String, Object> readRow = table.getNextRow();
  97. assertEquals(row[0], readRow.get("A"));
  98. assertEquals(row[1], readRow.get("B"));
  99. assertEquals(row[2], readRow.get("C"));
  100. assertEquals(row[3], readRow.get("D"));
  101. assertEquals(row[4], readRow.get("E"));
  102. assertEquals(row[5], readRow.get("F"));
  103. assertEquals(row[6], readRow.get("G"));
  104. assertEquals(row[7], readRow.get("H"));
  105. }
  106. db.close();
  107. }
  108. }
  109. public void testUpdateRow() throws Exception
  110. {
  111. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  112. Database db = createMem(fileFormat);
  113. Table t = new TableBuilder("test")
  114. .addColumn(new ColumnBuilder("name", DataType.TEXT))
  115. .addColumn(new ColumnBuilder("id", DataType.LONG)
  116. .setAutoNumber(true))
  117. .addColumn(new ColumnBuilder("data", DataType.TEXT)
  118. .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH))
  119. .toTable(db);
  120. for(int i = 0; i < 10; ++i) {
  121. t.addRow("row" + i, Column.AUTO_NUMBER, "initial data");
  122. }
  123. Cursor c = CursorBuilder.createCursor(t);
  124. c.reset();
  125. c.moveNextRows(2);
  126. Map<String,Object> row = c.getCurrentRow();
  127. assertEquals(createExpectedRow("name", "row1",
  128. "id", 2,
  129. "data", "initial data"),
  130. row);
  131. Map<String,Object> newRow = createExpectedRow(
  132. "name", Column.KEEP_VALUE,
  133. "id", Column.AUTO_NUMBER,
  134. "data", "new data");
  135. assertSame(newRow, c.updateCurrentRowFromMap(newRow));
  136. assertEquals(createExpectedRow("name", "row1",
  137. "id", 2,
  138. "data", "new data"),
  139. newRow);
  140. c.moveNextRows(3);
  141. row = c.getCurrentRow();
  142. assertEquals(createExpectedRow("name", "row4",
  143. "id", 5,
  144. "data", "initial data"),
  145. row);
  146. c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data");
  147. c.reset();
  148. c.moveNextRows(2);
  149. row = c.getCurrentRow();
  150. assertEquals(createExpectedRow("name", "row1",
  151. "id", 2,
  152. "data", "new data"),
  153. row);
  154. c.moveNextRows(3);
  155. row = c.getCurrentRow();
  156. assertEquals(createExpectedRow("name", "row4",
  157. "id", 5,
  158. "data", "a larger amount of new data"),
  159. row);
  160. t.reset();
  161. String str = createString(100);
  162. for(int i = 10; i < 50; ++i) {
  163. t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str);
  164. }
  165. c.reset();
  166. c.moveNextRows(9);
  167. row = c.getCurrentRow();
  168. assertEquals(createExpectedRow("name", "row8",
  169. "id", 9,
  170. "data", "initial data"),
  171. row);
  172. String newText = "updated big data_" + createString(200);
  173. c.setCurrentRowValue(t.getColumn("data"), newText);
  174. c.reset();
  175. c.moveNextRows(9);
  176. row = c.getCurrentRow();
  177. assertEquals(createExpectedRow("name", "row8",
  178. "id", 9,
  179. "data", newText),
  180. row);
  181. List<Row> rows = RowFilterTest.toList(t);
  182. assertEquals(50, rows.size());
  183. for(Row r : rows) {
  184. r.put("data", "final data " + r.get("id"));
  185. }
  186. for(Row r : rows) {
  187. assertSame(r, t.updateRow(r));
  188. }
  189. t.reset();
  190. for(Row r : t) {
  191. assertEquals("final data " + r.get("id"), r.get("data"));
  192. }
  193. db.close();
  194. }
  195. }
  196. public void testDateMath()
  197. {
  198. long now = System.currentTimeMillis();
  199. // test around current time
  200. doTestDateMath(now);
  201. // test around the unix epoch
  202. doTestDateMath(0L);
  203. // test around the access epoch
  204. doTestDateMath(-ColumnImpl.MILLIS_BETWEEN_EPOCH_AND_1900);
  205. }
  206. private static void doTestDateMath(long testTime)
  207. {
  208. final long timeRange = 100000000L;
  209. final long timeStep = 37L;
  210. for(long time = testTime - timeRange; time < testTime + timeRange;
  211. time += timeStep) {
  212. double accTime = ColumnImpl.toLocalDateDouble(time);
  213. long newTime = ColumnImpl.fromLocalDateDouble(accTime);
  214. assertEquals(time, newTime);
  215. Instant inst = Instant.ofEpochMilli(time);
  216. LocalDateTime ldt = LocalDateTime.ofInstant(inst, ZoneOffset.UTC);
  217. accTime = ColumnImpl.toDateDouble(ldt);
  218. LocalDateTime newLdt = ColumnImpl.ldtFromLocalDateDouble(accTime);
  219. assertEquals(ldt, newLdt);
  220. }
  221. }
  222. }