Skip to content

Commit

Permalink
Temp directory and file management
Browse files Browse the repository at this point in the history
- Relocate methods that create temp files from TestUtil to AbstractBaseTest
- Clarify and reduce number of open/create database signatures
- Write all temp test files in subdir test-jackcess-<user> of system temp directory
- Use meaningful names in temp files (test method name, datestamp) while ensuring uniqueness
- Reduce number of static imports on TestUtil
- Move TestUtil.TEST_TZ to AbstractBaseTest
  • Loading branch information
spannm committed Mar 17, 2024
1 parent 385fa64 commit 4125132
Show file tree
Hide file tree
Showing 39 changed files with 444 additions and 412 deletions.
15 changes: 8 additions & 7 deletions src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public class DatabaseImpl implements Database, DateTimeContext {
/**
* the maximum size of any of the included "empty db" resources
*/
private static final long MAX_EMPTYDB_SIZE = 440000L;
private static final long MAX_SIZE_EMPTY_DB = 440000L;

/**
* this object is a "system" object
Expand Down Expand Up @@ -606,7 +606,7 @@ public static DatabaseImpl create(FileFormat fileFormat, Path mdbFile, FileChann
boolean success = false;
try {
channel.truncate(0);
transferDbFrom(channel, getResourceAsStream(details.getEmptyFilePath()));
transferDatabase(getResourceAsStream(details.getEmptyFilePath()), channel);
channel.force(true);
DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync, fileFormat, charset, timeZone, null, false, false);
success = true;
Expand Down Expand Up @@ -2031,19 +2031,20 @@ public static DateTimeType getDefaultDateTimeType() {
}

/**
* Copies the given db InputStream to the given channel using the most efficient means possible.
* Copies the given database input stream {@code from} to the given channel {@code to}
* using the most efficient means possible.
*/
public static void transferDbFrom(FileChannel channel, InputStream in) throws IOException {
ReadableByteChannel readChannel = Channels.newChannel(in);
public static void transferDatabase(InputStream from, FileChannel to) throws IOException {
ReadableByteChannel readChannel = Channels.newChannel(from);
if (!BROKEN_NIO) {
// sane implementation
channel.transferFrom(readChannel, 0, MAX_EMPTYDB_SIZE);
to.transferFrom(readChannel, 0, MAX_SIZE_EMPTY_DB);
} else {
// do things the hard way for broken vms
ByteBuffer bb = ByteBuffer.allocate(8096);
while (readChannel.read(bb) >= 0) {
bb.flip();
channel.write(bb);
to.write(bb);
bb.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void close() throws IOException {
static FileChannel initDbChannel(FileChannel channel, FileFormat format)
throws IOException {
FileFormatDetails details = getFileFormatDetails(format);
transferDbFrom(channel, getResourceAsStream(details.getEmptyFilePath()));
transferDatabase(getResourceAsStream(details.getEmptyFilePath()), channel);
return channel;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public interface LinkResolver {
LinkResolver DEFAULT = (linkerDb, linkeeFileName) -> {
// if linker is read-only, open linkee read-only
boolean readOnly = linkerDb instanceof DatabaseImpl && ((DatabaseImpl) linkerDb).isReadOnly();
return new DatabaseBuilder(new File(linkeeFileName))
return new DatabaseBuilder()
.withFile(new File(linkeeFileName))
.withReadOnly(readOnly).open();
};

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/github/spannm/jackcess/BigIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import static io.github.spannm.jackcess.test.Basename.BIG_INDEX;
import static io.github.spannm.jackcess.test.Basename.COMP_INDEX;
import static io.github.spannm.jackcess.test.TestUtil.countRows;

import io.github.spannm.jackcess.impl.IndexImpl;
import io.github.spannm.jackcess.impl.TableImpl;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.source.TestDbReadOnlySource;
import io.github.spannm.jackcess.test.source.TestDbSource;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -45,7 +45,7 @@ void testComplexIndex(TestDb testDb) throws Exception {
TableImpl t = (TableImpl) db.getTable("Table1");
IndexImpl index = t.getIndex("CD_AGENTE");
assertFalse(index.isInitialized());
assertEquals(512, countRows(t));
assertEquals(512, TestUtil.countRows(t));
assertEquals(512, index.getIndexData().getEntryCount());
}
}
Expand All @@ -58,7 +58,7 @@ void testBigIndex(TestDb testDb) throws Exception {
TableImpl t = (TableImpl) db.getTable("Table1");
IndexImpl i = t.getIndex("col1");
assertFalse(i.isInitialized());
assertEquals(0, countRows(t));
assertEquals(0, TestUtil.countRows(t));
assertEquals(0, i.getIndexData().getEntryCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

import static io.github.spannm.jackcess.test.Basename.COMPLEX_DATA;
import static io.github.spannm.jackcess.test.Basename.UNSUPPORTED_FIELDS;
import static io.github.spannm.jackcess.test.TestUtil.TEST_TZ;
import static io.github.spannm.jackcess.test.TestUtil.assertSameDate;

import io.github.spannm.jackcess.complex.*;
import io.github.spannm.jackcess.impl.ByteUtil;
import io.github.spannm.jackcess.impl.ColumnImpl;
import io.github.spannm.jackcess.impl.PageChannel;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.source.TestDbSource;
import org.junit.jupiter.params.ParameterizedTest;

Expand Down Expand Up @@ -338,7 +337,7 @@ private static void checkVersions(
Date modDate = (Date) versionInfos[i + 1];
Version v = versions.get(i / 2);
assertEquals(value, v.getValue());
assertSameDate(modDate, v.getModifiedDate());
TestUtil.assertSameDate(modDate, v.getModifiedDate());
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/io/github/spannm/jackcess/CursorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ private static List<Map<String, Object>> createTestTableData(int startIdx, int e
return expectedRows;
}

private static Database createTestTable(FileFormat fileFormat) throws Exception {
Database db = createMem(fileFormat);
private Database createTestTable(FileFormat fileFormat) throws Exception {
Database db = createDbMem(fileFormat);

Table table = DatabaseBuilder.newTable("test")
.addColumn(DatabaseBuilder.newColumn("id", DataType.LONG))
Expand Down Expand Up @@ -120,8 +120,8 @@ private static List<Map<String, Object>> createDupeTestTableData() {
return expectedRows;
}

private static Database createDupeTestTable(FileFormat _fileFormat) throws Exception {
Database db = createMem(_fileFormat);
private Database createDupTestTable(FileFormat _fileFormat) throws Exception {
Database db = createDbMem(_fileFormat);

Table table = DatabaseBuilder.newTable("test")
.addColumn(DatabaseBuilder.newColumn("id", DataType.LONG))
Expand Down Expand Up @@ -693,7 +693,7 @@ void testLiveDeletionIndexSubRange(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testFindAllIndex(FileFormat fileFormat) throws Exception {
try (Database testDb = createDupeTestTable(fileFormat)) {
try (Database testDb = createDupTestTable(fileFormat)) {
Table table = testDb.getTable("test");
Cursor cursor = CursorBuilder.createCursor(table);

Expand Down Expand Up @@ -1115,7 +1115,7 @@ private static void doTestFindByRowId(Cursor cursor, Row row, int id) throws Exc
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testIterationEarlyExit(FileFormat fileFormat) throws Exception {
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
Table table = DatabaseBuilder.newTable("test")
.addColumn(DatabaseBuilder.newColumn("id", DataType.LONG))
.addColumn(DatabaseBuilder.newColumn("value", DataType.TEXT))
Expand Down Expand Up @@ -1163,7 +1163,7 @@ void testIterationEarlyExit(FileFormat fileFormat) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testPartialIndexFind(FileFormat fileFormat) throws Exception {
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
TableImpl t = (TableImpl) DatabaseBuilder.newTable("Test").addColumn(DatabaseBuilder.newColumn("id", DataType.LONG)).addColumn(DatabaseBuilder.newColumn("data1", DataType.TEXT))
.addColumn(DatabaseBuilder.newColumn("num2", DataType.LONG)).addColumn(DatabaseBuilder.newColumn("key3", DataType.TEXT)).addColumn(DatabaseBuilder.newColumn("value", DataType.TEXT))
.addIndex(DatabaseBuilder.newIndex("idx3").withColumns("data1", "num2", "key3")).toTable(db);
Expand Down Expand Up @@ -1215,7 +1215,7 @@ void testPartialIndexFind(FileFormat fileFormat) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testPartialIndexLookup(FileFormat fileFormat) throws Exception {
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
TableImpl t = (TableImpl) DatabaseBuilder.newTable("Test")
.addColumn(DatabaseBuilder.newColumn("id", DataType.LONG))
.addColumn(DatabaseBuilder.newColumn("data1", DataType.TEXT))
Expand Down
27 changes: 14 additions & 13 deletions src/test/java/io/github/spannm/jackcess/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DatabaseTest extends AbstractBaseTest {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testInvalidTableDefs(FileFormat fileFormat) throws Exception {
try (Database db = create(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
try {
DatabaseBuilder.newTable("test").toTable(db);
fail("created table with no columns?");
Expand Down Expand Up @@ -163,7 +163,7 @@ void testGetNextRow(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testCreate(FileFormat fileFormat) throws Exception {
try (Database db = create(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
assertEquals(0, db.getTableNames().size());
}
}
Expand All @@ -172,7 +172,7 @@ void testCreate(FileFormat fileFormat) throws Exception {
@FileFormatSource
void testDeleteCurrentRow(FileFormat fileFormat) throws Exception {
// make sure correct row is deleted
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
createTestTable(db);
Map<String, Object> row1 = createTestRowMap("Tim1");
Map<String, Object> row2 = createTestRowMap("Tim2");
Expand All @@ -196,7 +196,7 @@ void testDeleteCurrentRow(FileFormat fileFormat) throws Exception {
assertRowCount(2, table);
}

try (Database db = createMem(fileFormat)) { // test multi row delete/add
try (Database db = createDbMem(fileFormat)) { // test multi row delete/add
createTestTable(db);
Object[] row = createTestRow();
Table table = db.getTable("Test");
Expand Down Expand Up @@ -238,7 +238,7 @@ void testDeleteCurrentRow(FileFormat fileFormat) throws Exception {
void testDeleteRow(FileFormat fileFormat) throws Exception {
// make sure correct row is deleted
try (
Database db = createMem(fileFormat)) {
Database db = createDbMem(fileFormat)) {
createTestTable(db);
Table table = db.getTable("Test");
for (int i = 0; i < 10; i++) {
Expand Down Expand Up @@ -313,7 +313,7 @@ void testReadWithDeletedCols(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testCurrency(FileFormat fileFormat) throws Exception {
try (Database db = create(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
Table table = DatabaseBuilder.newTable("test")
.addColumn(DatabaseBuilder.newColumn("A", DataType.MONEY))
.toTable(db);
Expand Down Expand Up @@ -343,7 +343,7 @@ void testCurrency(FileFormat fileFormat) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testGUID(FileFormat fileFormat) throws Exception {
Database db = create(fileFormat);
Database db = createDbMem(fileFormat);

Table table = DatabaseBuilder.newTable("test")
.addColumn(DatabaseBuilder.newColumn("A", DataType.GUID))
Expand Down Expand Up @@ -377,7 +377,7 @@ void testGUID(FileFormat fileFormat) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testNumeric(FileFormat fileFormat) throws Exception {
Database db = create(fileFormat);
Database db = createDbMem(fileFormat);

ColumnBuilder col = DatabaseBuilder.newColumn("A", DataType.NUMERIC)
.withScale(4).withPrecision(8).toColumn();
Expand Down Expand Up @@ -531,7 +531,7 @@ void testUsageMapPromotion(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testLargeTableDef(FileFormat fileFormat) throws Exception {
Database db = create(fileFormat);
Database db = createDbMem(fileFormat);

final int numColumns = 90;

Expand Down Expand Up @@ -564,7 +564,7 @@ void testLargeTableDef(FileFormat fileFormat) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testWriteAndReadDate(FileFormat fileFormat) throws Exception {
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
db.setDateTimeType(DateTimeType.DATE);

Table table = DatabaseBuilder.newTable("test")
Expand Down Expand Up @@ -627,7 +627,7 @@ void testAncientDatesWrite(FileFormat fileFormat) throws Exception {

List<String> dates = List.of("1582-10-15", "1582-10-14", "1492-01-10", "1392-01-10");

Database db = createMem(fileFormat);
Database db = createDbMem(fileFormat);
db.setDateTimeType(DateTimeType.DATE);

Table table = DatabaseBuilder.newTable("test")
Expand Down Expand Up @@ -678,7 +678,7 @@ void testAncientDatesRead(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testSystemTable(FileFormat fileFormat) throws Exception {
Database db = create(fileFormat);
Database db = createDbMem(fileFormat);

Set<String> sysTables = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
sysTables.addAll(List.of("MSysObjects", "MSysQueries", "MSysACES", "MSysRelationships"));
Expand Down Expand Up @@ -918,7 +918,8 @@ void testTableDates(TestDb testDb) throws Exception {
@ParameterizedTest(name = "[{index}] {0}")
@TestDbSource(COMMON1)
void testBrokenIndex(TestDb testDb) throws Exception {
try (Database db = new DatabaseBuilder(testDb.getFile())
try (Database db = new DatabaseBuilder()
.withFile(testDb.getFile())
.withReadOnly(true).withIgnoreBrokenSystemCatalogIndex(true).open()) {
Table test = db.getTable("Table1");
assertNotNull(test);
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/io/github/spannm/jackcess/LinkedTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import static io.github.spannm.jackcess.test.Basename.LINKED;
import static io.github.spannm.jackcess.test.Basename.LINKED_ODBC;
import static io.github.spannm.jackcess.test.TestUtil.*;

import io.github.spannm.jackcess.impl.DatabaseImpl;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.source.TestDbSource;
import org.junit.jupiter.params.ParameterizedTest;

Expand Down Expand Up @@ -74,12 +74,12 @@ void testLinkedTables(TestDb testDb) throws Exception {
assertEquals("linkeeTest.accdb", ((DatabaseImpl) linkeeDb).getName());

List<? extends Map<String, Object>> expectedRows =
createExpectedTable(
createExpectedRow(
TestUtil.createExpectedTable(
TestUtil.createExpectedRow(
"ID", 1,
"Field1", "bar"));

assertTable(expectedRows, t2);
TestUtil.assertTable(expectedRows, t2);

db.createLinkedTable("FooTable", linkeeDbName, "Table2");

Expand All @@ -95,12 +95,12 @@ void testLinkedTables(TestDb testDb) throws Exception {
assertEquals(1, db.getLinkedDatabases().size());

expectedRows =
createExpectedTable(
createExpectedRow(
TestUtil.createExpectedTable(
TestUtil.createExpectedRow(
"ID", 1,
"Field1", "buzz"));

assertTable(expectedRows, t3);
TestUtil.assertTable(expectedRows, t3);

tmd = db.getTableMetaData("Table1");
assertEquals("Table1", tmd.getName());
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/io/github/spannm/jackcess/LocalDateTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import static io.github.spannm.jackcess.DatabaseBuilder.newColumn;
import static io.github.spannm.jackcess.DatabaseBuilder.newTable;
import static io.github.spannm.jackcess.test.Basename.OLD_DATES;
import static io.github.spannm.jackcess.test.TestUtil.assertSameDate;
import static io.github.spannm.jackcess.test.TestUtil.createMem;

import io.github.spannm.jackcess.Database.FileFormat;
import io.github.spannm.jackcess.impl.ColumnImpl;
import io.github.spannm.jackcess.impl.DatabaseImpl;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.source.FileFormatSource;
import io.github.spannm.jackcess.test.source.TestDbSource;
import org.junit.jupiter.api.Test;
Expand All @@ -46,7 +45,7 @@ class LocalDateTimeTest extends AbstractBaseTest {
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
void testWriteAndReadLocalDate(FileFormat fileFormat) throws Exception {
try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);

Table table = newTable("test")
Expand Down Expand Up @@ -98,7 +97,7 @@ void testWriteAndReadLocalDate(FileFormat fileFormat) throws Exception {
for (int i = 0; i < dates.size(); i++) {
Date expected = dates.get(i);
LocalDateTime found = foundDates.get(i);
assertSameDate(expected, found);
TestUtil.assertSameDate(expected, found);
}
}
}
Expand All @@ -109,7 +108,7 @@ void testAncientLocalDates1(FileFormat fileFormat) throws Exception {
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
List<String> dates = List.of("1582-10-15", "1582-10-14", "1492-01-10", "1392-01-10");

try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
db.setZoneId(ZoneId.of("America/New_York"));
db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);

Expand Down Expand Up @@ -209,7 +208,7 @@ public ColumnImpl.DateTimeFactory getDateTimeFactory() {
void testWriteAndReadTemporals(FileFormat fileFormat) throws Exception {
ZoneId zoneId = ZoneId.of("America/New_York");

try (Database db = createMem(fileFormat)) {
try (Database db = createDbMem(fileFormat)) {
db.setZoneId(zoneId);
db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);

Expand Down
Loading

0 comments on commit 4125132

Please sign in to comment.