aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/healthmarketscience/jackcess/impl/DatabaseReadWriteTest.java
blob: 104b266ed916a8b940d72f1ed9fda39ee5a7a8ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright (c) 2015 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import static com.healthmarketscience.jackcess.Database.*;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import static com.healthmarketscience.jackcess.TestUtil.*;
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
import com.healthmarketscience.jackcess.util.RowFilterTest;
import junit.framework.TestCase;

/**
 *
 * @author James Ahlborn
 */
public class DatabaseReadWriteTest extends TestCase
{

  public DatabaseReadWriteTest(String name) throws Exception {
    super(name);
  }

  public void testWriteAndRead() throws Exception {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = create(fileFormat);
      doTestWriteAndRead(db);
      db.close();
    }
  }

  public void testWriteAndReadInMem() throws Exception {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = createMem(fileFormat);
      doTestWriteAndRead(db);
      db.close();
    }
  }

  private static void doTestWriteAndRead(Database db) throws Exception {
      createTestTable(db);
      Object[] row = createTestRow();
      row[3] = null;
      Table table = db.getTable("Test");
      int count = 1000;
      ((DatabaseImpl)db).getPageChannel().startWrite();
      try {
        for (int i = 0; i < count; i++) {
          table.addRow(row);
        }
      } finally {
        ((DatabaseImpl)db).getPageChannel().finishWrite();
      }
      for (int i = 0; i < count; i++) {
        Map<String, Object> readRow = table.getNextRow();
        assertEquals(row[0], readRow.get("A"));
        assertEquals(row[1], readRow.get("B"));
        assertEquals(row[2], readRow.get("C"));
        assertEquals(row[3], readRow.get("D"));
        assertEquals(row[4], readRow.get("E"));
        assertEquals(row[5], readRow.get("F"));
        assertEquals(row[6], readRow.get("G"));
        assertEquals(row[7], readRow.get("H"));
      }
  }

  public void testWriteAndReadInBatch() throws Exception {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = createMem(fileFormat);
      createTestTable(db);
      int count = 1000;
      List<Object[]> rows = new ArrayList<Object[]>(count);
      Object[] row = createTestRow();
      for (int i = 0; i < count; i++) {
        rows.add(row);
      }
      Table table = db.getTable("Test");
      table.addRows(rows);
      for (int i = 0; i < count; i++) {
        Map<String, Object> readRow = table.getNextRow();
        assertEquals(row[0], readRow.get("A"));
        assertEquals(row[1], readRow.get("B"));
        assertEquals(row[2], readRow.get("C"));
        assertEquals(row[3], readRow.get("D"));
        assertEquals(row[4], readRow.get("E"));
        assertEquals(row[5], readRow.get("F"));
        assertEquals(row[6], readRow.get("G"));
        assertEquals(row[7], readRow.get("H"));
      }

      db.close();
    }
  }

  public void testUpdateRow() throws Exception
  {
    for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
      Database db = createMem(fileFormat);

      Table t = new TableBuilder("test")
        .addColumn(new ColumnBuilder("name", DataType.TEXT))
        .addColumn(new ColumnBuilder("id", DataType.LONG)
                   .setAutoNumber(true))
        .addColumn(new ColumnBuilder("data", DataType.TEXT)
                   .setLength(JetFormat.TEXT_FIELD_MAX_LENGTH))
        .toTable(db);

      for(int i = 0; i < 10; ++i) {
        t.addRow("row" + i, Column.AUTO_NUMBER, "initial data");
      }

      Cursor c = CursorBuilder.createCursor(t);
      c.reset();
      c.moveNextRows(2);
      Map<String,Object> row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row1",
                                     "id", 2,
                                     "data", "initial data"),
                   row);

      Map<String,Object> newRow = createExpectedRow(
          "name", Column.KEEP_VALUE,
          "id", Column.AUTO_NUMBER,
          "data", "new data");
      assertSame(newRow, c.updateCurrentRowFromMap(newRow));
      assertEquals(createExpectedRow("name", "row1",
                                     "id", 2,
                                     "data", "new data"),
                   newRow);

      c.moveNextRows(3);
      row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row4",
                                     "id", 5,
                                     "data", "initial data"),
                   row);

      c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data");

      c.reset();
      c.moveNextRows(2);
      row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row1",
                                     "id", 2,
                                     "data", "new data"),
                   row);

      c.moveNextRows(3);
      row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row4",
                                     "id", 5,
                                     "data", "a larger amount of new data"),
                   row);

      t.reset();

      String str = createString(100);
      for(int i = 10; i < 50; ++i) {
        t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str);
      }

      c.reset();
      c.moveNextRows(9);
      row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row8",
                                     "id", 9,
                                     "data", "initial data"),
                   row);

      String newText = "updated big data_" + createString(200);

      c.setCurrentRowValue(t.getColumn("data"), newText);

      c.reset();
      c.moveNextRows(9);
      row = c.getCurrentRow();

      assertEquals(createExpectedRow("name", "row8",
                                     "id", 9,
                                     "data", newText),
                   row);

      List<Row> rows = RowFilterTest.toList(t);
      assertEquals(50, rows.size());

      for(Row r : rows) {
        r.put("data", "final data " + r.get("id"));
      }

      for(Row r : rows) {
        assertSame(r, t.updateRow(r));
      }

      t.reset();

      for(Row r : t) {
        assertEquals("final data " + r.get("id"), r.get("data"));
      }

      db.close();
    }
  }

  public void testDateMath()
  {
    long now = System.currentTimeMillis();

    // test around current time
    doTestDateMath(now);

    // test around the unix epoch
    doTestDateMath(0L);

    // test around the access epoch
    doTestDateMath(-ColumnImpl.MILLIS_BETWEEN_EPOCH_AND_1900);
  }

  private static void doTestDateMath(long testTime)
  {
    final long timeRange = 100000000L;
    final long timeStep = 37L;

    for(long time = testTime - timeRange; time < testTime + timeRange;
        time += timeStep) {
      double accTime = ColumnImpl.toLocalDateDouble(time);
      long newTime = ColumnImpl.fromLocalDateDouble(accTime);
      assertEquals(time, newTime);

      Instant inst = Instant.ofEpochMilli(time);
      LocalDateTime ldt = LocalDateTime.ofInstant(inst, ZoneOffset.UTC);

      accTime = ColumnImpl.toDateDouble(ldt);
      LocalDateTime newLdt = ColumnImpl.ldtFromLocalDateDouble(accTime);
      assertEquals(ldt, newLdt);
    }
  }
}