/target/ | |||||
nb-configuration.xml |
<name>Jackcess</name> | <name>Jackcess</name> | ||||
<description>A pure Java library for reading from and writing to MS Access databases.</description> | <description>A pure Java library for reading from and writing to MS Access databases.</description> | ||||
<version>4.0.6-SNAPSHOT</version> | <version>4.0.6-SNAPSHOT</version> | ||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<junit.version>5.10.0</junit.version> | |||||
</properties> | |||||
<url>https://jackcess.sourceforge.io</url> | <url>https://jackcess.sourceforge.io</url> | ||||
<inceptionYear>2005</inceptionYear> | <inceptionYear>2005</inceptionYear> | ||||
<developers> | <developers> | ||||
<version>1.2</version> | <version>1.2</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||
<groupId>junit</groupId> | |||||
<artifactId>junit</artifactId> | |||||
<version>4.13.1</version> | |||||
<groupId>org.junit.jupiter</groupId> | |||||
<artifactId>junit-jupiter-api</artifactId> | |||||
<version>${junit.version}</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.junit.jupiter</groupId> | |||||
<artifactId>junit-jupiter-params</artifactId> | |||||
<version>${junit.version}</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.junit.jupiter</groupId> | |||||
<artifactId>junit-jupiter-engine</artifactId> | |||||
<version>${junit.version}</version> | |||||
<scope>test</scope> | <scope>test</scope> | ||||
</dependency> | </dependency> | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Random; | import java.util.Random; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import com.healthmarketscience.jackcess.impl.IndexImpl; | import com.healthmarketscience.jackcess.impl.IndexImpl; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author james | * @author james | ||||
*/ | */ | ||||
public class BigIndexTest extends TestCase { | |||||
public BigIndexTest(String name) { | |||||
super(name); | |||||
} | |||||
public class BigIndexTest | |||||
{ | |||||
@Test | |||||
public void testComplexIndex() throws Exception | public void testComplexIndex() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX, true)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testBigIndex() throws Exception | public void testBigIndex() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) { | ||||
if(val == null) { | if(val == null) { | ||||
val = firstValue; | val = firstValue; | ||||
} | } | ||||
assertTrue("" + prevValue + " <= " + val + " " + rowCount, | |||||
prevValue.compareTo(val) <= 0); | |||||
assertTrue(prevValue.compareTo(val) <= 0, "" + prevValue + " <= " + val + " " + rowCount); | |||||
if(firstTwo.size() < 2) { | if(firstTwo.size() < 2) { | ||||
firstTwo.add(origVal); | firstTwo.add(origVal); | ||||
} | } |
import com.healthmarketscience.jackcess.impl.ByteUtil; | import com.healthmarketscience.jackcess.impl.ByteUtil; | ||||
import com.healthmarketscience.jackcess.impl.ColumnImpl; | import com.healthmarketscience.jackcess.impl.ColumnImpl; | ||||
import com.healthmarketscience.jackcess.impl.PageChannel; | import com.healthmarketscience.jackcess.impl.PageChannel; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
@SuppressWarnings("deprecation") | @SuppressWarnings("deprecation") | ||||
public class ComplexColumnTest extends TestCase | |||||
public class ComplexColumnTest | |||||
{ | { | ||||
public ComplexColumnTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testVersions() throws Exception | public void testVersions() throws Exception | ||||
{ | { | ||||
for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | ||||
"row3-memo-revised", new Date(1315876953077L), | "row3-memo-revised", new Date(1315876953077L), | ||||
"row3-memo", new Date(1315876879126L)); | "row3-memo", new Date(1315876879126L)); | ||||
try { | |||||
v.update(); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException expected) { | |||||
// success | |||||
} | |||||
assertThrows(UnsupportedOperationException.class, () -> v.update()); | |||||
checkVersions(3, row3ValFk, "new-value", | checkVersions(3, row3ValFk, "new-value", | ||||
"new-value", upTime, | "new-value", upTime, | ||||
"row3-memo-revised", new Date(1315876953077L), | "row3-memo-revised", new Date(1315876953077L), | ||||
"row3-memo", new Date(1315876879126L)); | "row3-memo", new Date(1315876879126L)); | ||||
try { | |||||
v.delete(); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException expected) { | |||||
// success | |||||
} | |||||
assertThrows(UnsupportedOperationException.class, () -> v.delete()); | |||||
checkVersions(3, row3ValFk, "new-value", | checkVersions(3, row3ValFk, "new-value", | ||||
"new-value", upTime, | "new-value", upTime, | ||||
"row3-memo-revised", new Date(1315876953077L), | "row3-memo-revised", new Date(1315876953077L), | ||||
"row3-memo", new Date(1315876879126L)); | "row3-memo", new Date(1315876879126L)); | ||||
try { | |||||
v.getComplexValueForeignKey().deleteAllValues(); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException expected) { | |||||
// success | |||||
} | |||||
assertThrows(UnsupportedOperationException.class, | |||||
() -> v.getComplexValueForeignKey().deleteAllValues()); | |||||
checkVersions(3, row3ValFk, "new-value", | checkVersions(3, row3ValFk, "new-value", | ||||
"new-value", upTime, | "new-value", upTime, | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAttachments() throws Exception | public void testAttachments() throws Exception | ||||
{ | { | ||||
for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testMultiValues() throws Exception | public void testMultiValues() throws Exception | ||||
{ | { | ||||
for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUnsupported() throws Exception | public void testUnsupported() throws Exception | ||||
{ | { | ||||
for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) { | for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) { |
package com.healthmarketscience.jackcess; | package com.healthmarketscience.jackcess; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.IndexImpl; | import com.healthmarketscience.jackcess.impl.IndexImpl; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class CursorBuilderTest extends TestCase { | |||||
public CursorBuilderTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
public class CursorBuilderTest | |||||
{ | |||||
private static void assertCursor( | private static void assertCursor( | ||||
Cursor expected, Cursor found) | Cursor expected, Cursor found) | ||||
{ | { | ||||
found.getSavepoint().getCurrentPosition()); | found.getSavepoint().getCurrentPosition()); | ||||
} | } | ||||
@Test | |||||
public void test() throws Exception | public void test() throws Exception | ||||
{ | { | ||||
for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) { | ||||
.toCursor(); | .toCursor(); | ||||
assertCursor(expected, found); | assertCursor(expected, found); | ||||
try { | |||||
table.newCursor() | |||||
.setIndexByName("foo"); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> table.newCursor().setIndexByName("foo")); | |||||
expected = CursorBuilder.createCursor(idx); | expected = CursorBuilder.createCursor(idx); | ||||
found = table.newCursor() | found = table.newCursor() | ||||
.toCursor(); | .toCursor(); | ||||
assertCursor(expected, found); | assertCursor(expected, found); | ||||
try { | |||||
table.newCursor() | |||||
.setIndexByColumns(table.getColumn("value")); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
try { | |||||
table.newCursor() | |||||
.setIndexByColumns(table.getColumn("id"), table.getColumn("value")); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> table.newCursor().setIndexByColumns(table.getColumn("value"))); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> table.newCursor().setIndexByColumns(table.getColumn("id"), table.getColumn("value"))); | |||||
expected = CursorBuilder.createCursor(table); | expected = CursorBuilder.createCursor(table); | ||||
expected.beforeFirst(); | expected.beforeFirst(); |
import com.healthmarketscience.jackcess.util.ColumnMatcher; | import com.healthmarketscience.jackcess.util.ColumnMatcher; | ||||
import com.healthmarketscience.jackcess.util.RowFilterTest; | import com.healthmarketscience.jackcess.util.RowFilterTest; | ||||
import com.healthmarketscience.jackcess.util.SimpleColumnMatcher; | import com.healthmarketscience.jackcess.util.SimpleColumnMatcher; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import org.junit.jupiter.api.AfterEach; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.BeforeEach; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class CursorTest extends TestCase { | |||||
public class CursorTest | |||||
{ | |||||
static final List<TestDB> INDEX_CURSOR_DBS = | static final List<TestDB> INDEX_CURSOR_DBS = | ||||
TestDB.getSupportedForBasename(Basename.INDEX_CURSOR); | TestDB.getSupportedForBasename(Basename.INDEX_CURSOR); | ||||
public CursorTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Override | |||||
@BeforeEach | |||||
protected void setUp() { | protected void setUp() { | ||||
TestUtil.setTestAutoSync(false); | TestUtil.setTestAutoSync(false); | ||||
} | } | ||||
@Override | |||||
@AfterEach | |||||
protected void tearDown() { | protected void tearDown() { | ||||
TestUtil.clearTestAutoSync(); | TestUtil.clearTestAutoSync(); | ||||
} | } | ||||
.toCursor(); | .toCursor(); | ||||
} | } | ||||
@Test | |||||
public void testRowId() throws Exception { | public void testRowId() throws Exception { | ||||
// test special cases | // test special cases | ||||
RowIdImpl rowId1 = new RowIdImpl(1, 2); | RowIdImpl rowId1 = new RowIdImpl(1, 2); | ||||
sortedRowIds); | sortedRowIds); | ||||
} | } | ||||
@Test | |||||
public void testSimple() throws Exception { | public void testSimple() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
assertEquals(expectedRows, foundRows); | assertEquals(expectedRows, foundRows); | ||||
} | } | ||||
@Test | |||||
public void testMove() throws Exception { | public void testMove() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
assertEquals(expectedRow, cursor.getCurrentRow()); | assertEquals(expectedRow, cursor.getCurrentRow()); | ||||
} | } | ||||
@Test | |||||
public void testMoveNoReset() throws Exception { | public void testMoveNoReset() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
assertEquals(expectedRows, foundRows); | assertEquals(expectedRows, foundRows); | ||||
} | } | ||||
@Test | |||||
public void testSearch() throws Exception { | public void testSearch() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReverse() throws Exception { | public void testReverse() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
assertEquals(expectedRows, foundRows); | assertEquals(expectedRows, foundRows); | ||||
} | } | ||||
@Test | |||||
public void testLiveAddition() throws Exception { | public void testLiveAddition() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
} | } | ||||
@Test | |||||
public void testLiveDeletion() throws Exception { | public void testLiveDeletion() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
assertTrue(cursor4.isCurrentRowDeleted()); | assertTrue(cursor4.isCurrentRowDeleted()); | ||||
} | } | ||||
@Test | |||||
public void testSimpleIndex() throws Exception { | public void testSimpleIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testMoveIndex() throws Exception { | public void testMoveIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReverseIndex() throws Exception { | public void testReverseIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testSearchIndex() throws Exception { | public void testSearchIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testLiveAdditionIndex() throws Exception { | public void testLiveAdditionIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testLiveDeletionIndex() throws Exception { | public void testLiveDeletionIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testSimpleIndexSubRange() throws Exception { | public void testSimpleIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testMoveIndexSubRange() throws Exception { | public void testMoveIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testSearchIndexSubRange() throws Exception { | public void testSearchIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReverseIndexSubRange() throws Exception { | public void testReverseIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testLiveAdditionIndexSubRange() throws Exception { | public void testLiveAdditionIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testLiveDeletionIndexSubRange() throws Exception { | public void testLiveDeletionIndexSubRange() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
for(int i = 0; i < 2; ++i) { | for(int i = 0; i < 2; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFindAllIndex() throws Exception { | public void testFindAllIndex() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createDupeTestTable(fileFormat); | Database db = createDupeTestTable(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFindAll() throws Exception { | public void testFindAll() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createDupeTestTable(indexCursorDB); | Database db = createDupeTestTable(indexCursorDB); | ||||
assertTrue(rows.isEmpty()); | assertTrue(rows.isEmpty()); | ||||
} | } | ||||
@Test | |||||
public void testId() throws Exception | public void testId() throws Exception | ||||
{ | { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
tCursor.restoreSavepoint(tSave); | tCursor.restoreSavepoint(tSave); | ||||
iCursor.restoreSavepoint(iSave); | iCursor.restoreSavepoint(iSave); | ||||
try { | |||||
tCursor.restoreSavepoint(iSave); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, () -> tCursor.restoreSavepoint(iSave)); | |||||
try { | |||||
iCursor.restoreSavepoint(tSave); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, () -> iCursor.restoreSavepoint(tSave)); | |||||
Cursor tCursor2 = CursorBuilder.createCursor(table); | Cursor tCursor2 = CursorBuilder.createCursor(table); | ||||
Cursor iCursor2 = CursorBuilder.createCursor(idx); | Cursor iCursor2 = CursorBuilder.createCursor(idx); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testColumnMatcher() throws Exception { | public void testColumnMatcher() throws Exception { | ||||
.setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE))); | .setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE))); | ||||
} | } | ||||
@Test | |||||
public void testIndexCursor() throws Exception | public void testIndexCursor() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testIndexCursorDelete() throws Exception | public void testIndexCursorDelete() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | ||||
iter.hasNext(); ) { | iter.hasNext(); ) { | ||||
expectedData.add(iter.next().getString("data")); | expectedData.add(iter.next().getString("data")); | ||||
iter.remove(); | iter.remove(); | ||||
try { | |||||
iter.remove(); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> iter.remove()); | |||||
if(!iter.hasNext()) { | |||||
try { | |||||
iter.next(); | |||||
fail("NoSuchElementException should have been thrown"); | |||||
} catch(NoSuchElementException e) { | |||||
// success | |||||
} | |||||
if (!iter.hasNext()) { | |||||
assertThrows(NoSuchElementException.class, () -> iter.next()); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testCursorDelete() throws Exception | public void testCursorDelete() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | ||||
if(row.get("otherfk1").equals(1)) { | if(row.get("otherfk1").equals(1)) { | ||||
expectedData.add(row.getString("data")); | expectedData.add(row.getString("data")); | ||||
iter.remove(); | iter.remove(); | ||||
try { | |||||
iter.remove(); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> iter.remove()); | |||||
} | } | ||||
if(!iter.hasNext()) { | |||||
try { | |||||
iter.next(); | |||||
fail("NoSuchElementException should have been thrown"); | |||||
} catch(NoSuchElementException e) { | |||||
// success | |||||
} | |||||
if (!iter.hasNext()) { | |||||
assertThrows(NoSuchElementException.class, () -> iter.next()); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFindByRowId() throws Exception { | public void testFindByRowId() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestTable(fileFormat); | Database db = createTestTable(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFindByRowIdIndex() throws Exception { | public void testFindByRowIdIndex() throws Exception { | ||||
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) { | ||||
Database db = createTestIndexTable(indexCursorDB); | Database db = createTestIndexTable(indexCursorDB); | ||||
assertEquals(id - 1, cursor.getCurrentRow().get("id")); | assertEquals(id - 1, cursor.getCurrentRow().get("id")); | ||||
} | } | ||||
@Test | |||||
public void testIterationEarlyExit() throws Exception { | public void testIterationEarlyExit() throws Exception { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
IndexCursor cursor = CursorBuilder.createCursor( | IndexCursor cursor = CursorBuilder.createCursor( | ||||
table.getIndex("value_idx")); | table.getIndex("value_idx")); | ||||
try { | |||||
cursor.newIterable() | |||||
.addMatchPattern("value", "val-9") | |||||
.addMatchPattern("memo", "anything") | |||||
.iterator().hasNext(); | |||||
fail("RuntimeIOException should have been thrown"); | |||||
} catch(RuntimeIOException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(RuntimeIOException.class, () -> cursor.newIterable() | |||||
.addMatchPattern("value", "val-9") | |||||
.addMatchPattern("memo", "anything") | |||||
.iterator().hasNext()); | |||||
List<Row> rows = new ArrayList<Row>(); | List<Row> rows = new ArrayList<Row>(); | ||||
for (Row row : cursor.newIterable() | for (Row row : cursor.newIterable() | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testPartialIndexFind() throws Exception | public void testPartialIndexFind() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testPartialIndexLookup() throws Exception | public void testPartialIndexLookup() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
doFindFirstByEntry(c, null, "C", 4, "K3"); | doFindFirstByEntry(c, null, "C", 4, "K3"); | ||||
} | } | ||||
try { | |||||
if(colCount > 2) { | |||||
assertThrows(IllegalArgumentException.class, () -> | |||||
{ | |||||
if (colCount > 2) { | |||||
c.findFirstRowByEntry("C", 4, "K1", 14); | c.findFirstRowByEntry("C", 4, "K1", 14); | ||||
} else if(colCount > 1) { | } else if(colCount > 1) { | ||||
c.findFirstRowByEntry("C", 4, "K1"); | c.findFirstRowByEntry("C", 4, "K1"); | ||||
} else { | } else { | ||||
c.findFirstRowByEntry("C", 4); | c.findFirstRowByEntry("C", 4); | ||||
} | } | ||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException expected) { | |||||
// scucess | |||||
} | |||||
}); | |||||
doFindByEntryRange(c, 11, 20, "B"); | doFindByEntryRange(c, 11, 20, "B"); | ||||
doFindByEntry(c, new int[]{}, "Z"); | doFindByEntry(c, new int[]{}, "Z"); |
import com.healthmarketscience.jackcess.impl.RowImpl; | import com.healthmarketscience.jackcess.impl.RowImpl; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import com.healthmarketscience.jackcess.util.RowFilterTest; | import com.healthmarketscience.jackcess.util.RowFilterTest; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author Tim McCune | * @author Tim McCune | ||||
*/ | */ | ||||
@SuppressWarnings("deprecation") | @SuppressWarnings("deprecation") | ||||
public class DatabaseTest extends TestCase | |||||
public class DatabaseTest | |||||
{ | { | ||||
public DatabaseTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
public void testInvalidTableDefs() throws Exception { | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
try { | |||||
newTable("test").toTable(db); | |||||
fail("created table with no columns?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
try { | |||||
newTable("test") | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.addColumn(newColumn("a", DataType.MEMO)) | |||||
.toTable(db); | |||||
fail("created table with duplicate column names?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
try { | |||||
newTable("test") | |||||
.addColumn(newColumn("A", DataType.TEXT) | |||||
.setLengthInUnits(352)) | |||||
.toTable(db); | |||||
fail("created table with invalid column length?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
@Test | |||||
public void testInvalidTableDefs() throws Exception | |||||
{ | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newTable("test").toTable(db), | |||||
"created table with no columns?"); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newTable("test") | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.addColumn(newColumn("a", DataType.MEMO)) | |||||
.toTable(db), | |||||
"created table with duplicate column names?"); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newTable("test") | |||||
.addColumn(newColumn("A", DataType.TEXT) | |||||
.setLengthInUnits(352)) | |||||
.toTable(db), | |||||
"created table with invalid column length?"); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newTable("test") | |||||
.addColumn(newColumn("A_" + createString(70), DataType.TEXT)) | |||||
.toTable(db), | |||||
"created table with too long column name?"); | |||||
try { | |||||
newTable("test") | newTable("test") | ||||
.addColumn(newColumn("A_" + createString(70), DataType.TEXT)) | |||||
.toTable(db); | |||||
fail("created table with too long column name?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
newTable("test") | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.toTable(db); | |||||
try { | |||||
newTable("Test") | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.toTable(db); | |||||
fail("create duplicate tables?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.toTable(db); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newTable("Test") | |||||
.addColumn(newColumn("A", DataType.TEXT)) | |||||
.toTable(db), | |||||
"create duplicate tables?"); | |||||
} | } | ||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReadDeletedRows() throws Exception { | public void testReadDeletedRows() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL, true)) { | ||||
Table table = open(testDB).getTable("Table"); | Table table = open(testDB).getTable("Table"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testGetColumns() throws Exception { | public void testGetColumns() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
assertEquals(dataType, column.getType()); | assertEquals(dataType, column.getType()); | ||||
} | } | ||||
@Test | |||||
public void testGetNextRow() throws Exception { | public void testGetNextRow() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
final Database db = open(testDB); | final Database db = open(testDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testCreate() throws Exception { | public void testCreate() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testDeleteCurrentRow() throws Exception { | public void testDeleteCurrentRow() throws Exception { | ||||
// make sure correct row is deleted | // make sure correct row is deleted | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testDeleteRow() throws Exception { | public void testDeleteRow() throws Exception { | ||||
// make sure correct row is deleted | // make sure correct row is deleted | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testMissingFile() throws Exception { | public void testMissingFile() throws Exception { | ||||
File bogusFile = new File("fooby-dooby.mdb"); | File bogusFile = new File("fooby-dooby.mdb"); | ||||
assertTrue(!bogusFile.exists()); | assertTrue(!bogusFile.exists()); | ||||
try { | |||||
newDatabase(bogusFile).setReadOnly(true). | |||||
setAutoSync(getTestAutoSync()).open(); | |||||
fail("FileNotFoundException should have been thrown"); | |||||
} catch(FileNotFoundException e) { | |||||
} | |||||
assertThrows(FileNotFoundException.class, | |||||
() -> newDatabase(bogusFile).setReadOnly(true).setAutoSync(getTestAutoSync()).open()); | |||||
assertTrue(!bogusFile.exists()); | assertTrue(!bogusFile.exists()); | ||||
} | } | ||||
@Test | |||||
public void testReadWithDeletedCols() throws Exception { | public void testReadWithDeletedCols() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL_COL, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.DEL_COL, true)) { | ||||
Table table = open(testDB).getTable("Table1"); | Table table = open(testDB).getTable("Table1"); | ||||
} | } | ||||
} | } | ||||
public void testCurrency() throws Exception { | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("A", DataType.MONEY)) | |||||
.toTable(db); | |||||
@Test | |||||
public void testCurrency() throws Exception | |||||
{ | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (Database db = create(fileFormat)) | |||||
{ | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("A", DataType.MONEY)) | |||||
.toTable(db); | |||||
table.addRow(new BigDecimal("-2341234.03450")); | |||||
table.addRow(37L); | |||||
table.addRow("10000.45"); | |||||
table.reset(); | |||||
List<Object> foundValues = new ArrayList<Object>(); | |||||
Map<String, Object> row = null; | |||||
while ((row = table.getNextRow()) != null) | |||||
{ | |||||
foundValues.add(row.get("A")); | |||||
} | |||||
table.addRow(new BigDecimal("-2341234.03450")); | |||||
table.addRow(37L); | |||||
table.addRow("10000.45"); | |||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("-2341234.0345"), | |||||
new BigDecimal("37.0000"), | |||||
new BigDecimal("10000.4500")), | |||||
foundValues); | |||||
table.reset(); | |||||
assertThrows(IOException.class, | |||||
() -> table.addRow(new BigDecimal("342523234145343543.3453"))); | |||||
List<Object> foundValues = new ArrayList<Object>(); | |||||
Map<String, Object> row = null; | |||||
while((row = table.getNextRow()) != null) { | |||||
foundValues.add(row.get("A")); | |||||
} | } | ||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("-2341234.0345"), | |||||
new BigDecimal("37.0000"), | |||||
new BigDecimal("10000.4500")), | |||||
foundValues); | |||||
try { | |||||
table.addRow(new BigDecimal("342523234145343543.3453")); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// ignored | |||||
} | |||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testGUID() throws Exception | public void testGUID() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("A", DataType.GUID)) | |||||
.toTable(db); | |||||
table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}"); | |||||
table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}"); | |||||
table.addRow("{11111111-1111-1111-1111-111111111111}"); | |||||
table.addRow(" {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} "); | |||||
table.addRow(UUID.fromString("32a59f01-1234-3e29-4aaf-4523453cd2e6")); | |||||
table.reset(); | |||||
List<Object> foundValues = new ArrayList<Object>(); | |||||
Map<String, Object> row = null; | |||||
while((row = table.getNextRow()) != null) { | |||||
foundValues.add(row.get("A")); | |||||
} | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("A", DataType.GUID)) | |||||
.toTable(db); | |||||
table.addRow("{32A59F01-AA34-3E29-453F-4523453CD2E6}"); | |||||
table.addRow("{32a59f01-aa34-3e29-453f-4523453cd2e6}"); | |||||
table.addRow("{11111111-1111-1111-1111-111111111111}"); | |||||
table.addRow(" {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} "); | |||||
table.addRow(UUID.fromString("32a59f01-1234-3e29-4aaf-4523453cd2e6")); | |||||
table.reset(); | |||||
List<Object> foundValues = new ArrayList<Object>(); | |||||
Map<String, Object> row = null; | |||||
while ((row = table.getNextRow()) != null) | |||||
{ | |||||
foundValues.add(row.get("A")); | |||||
} | |||||
assertEquals(Arrays.asList( | |||||
"{32A59F01-AA34-3E29-453F-4523453CD2E6}", | |||||
"{32A59F01-AA34-3E29-453F-4523453CD2E6}", | |||||
"{11111111-1111-1111-1111-111111111111}", | |||||
"{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}", | |||||
"{32A59F01-1234-3E29-4AAF-4523453CD2E6}"), | |||||
foundValues); | |||||
assertEquals(Arrays.asList( | |||||
"{32A59F01-AA34-3E29-453F-4523453CD2E6}", | |||||
"{32A59F01-AA34-3E29-453F-4523453CD2E6}", | |||||
"{11111111-1111-1111-1111-111111111111}", | |||||
"{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}", | |||||
"{32A59F01-1234-3E29-4AAF-4523453CD2E6}"), | |||||
foundValues); | |||||
try { | |||||
table.addRow("3245234"); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// ignored | |||||
assertThrows(IOException.class, () -> table.addRow("3245234")); | |||||
} | } | ||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testNumeric() throws Exception | public void testNumeric() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
ColumnBuilder col = newColumn("A", DataType.NUMERIC) | |||||
.setScale(4).setPrecision(8).toColumn(); | |||||
assertTrue(col.getType().isVariableLength()); | |||||
Table table = newTable("test") | |||||
.addColumn(col) | |||||
.addColumn(newColumn("B", DataType.NUMERIC) | |||||
.setScale(8).setPrecision(28)) | |||||
.toTable(db); | |||||
table.addRow(new BigDecimal("-1234.03450"), | |||||
new BigDecimal("23923434453436.36234219")); | |||||
table.addRow(37L, 37L); | |||||
table.addRow("1000.45", "-3452345321000"); | |||||
table.reset(); | |||||
List<Object> foundSmallValues = new ArrayList<Object>(); | |||||
List<Object> foundBigValues = new ArrayList<Object>(); | |||||
Map<String, Object> row = null; | |||||
while((row = table.getNextRow()) != null) { | |||||
foundSmallValues.add(row.get("A")); | |||||
foundBigValues.add(row.get("B")); | |||||
} | |||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("-1234.0345"), | |||||
new BigDecimal("37.0000"), | |||||
new BigDecimal("1000.4500")), | |||||
foundSmallValues); | |||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("23923434453436.36234219"), | |||||
new BigDecimal("37.00000000"), | |||||
new BigDecimal("-3452345321000.00000000")), | |||||
foundBigValues); | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
ColumnBuilder col = newColumn("A", DataType.NUMERIC) | |||||
.setScale(4).setPrecision(8).toColumn(); | |||||
assertTrue(col.getType().isVariableLength()); | |||||
Table table = newTable("test") | |||||
.addColumn(col) | |||||
.addColumn(newColumn("B", DataType.NUMERIC) | |||||
.setScale(8).setPrecision(28)) | |||||
.toTable(db); | |||||
table.addRow(new BigDecimal("-1234.03450"), | |||||
new BigDecimal("23923434453436.36234219")); | |||||
table.addRow(37L, 37L); | |||||
table.addRow("1000.45", "-3452345321000"); | |||||
table.reset(); | |||||
List<Object> foundSmallValues = new ArrayList<>(); | |||||
List<Object> foundBigValues = new ArrayList<>(); | |||||
Map<String, Object> row = null; | |||||
while ((row = table.getNextRow()) != null) | |||||
{ | |||||
foundSmallValues.add(row.get("A")); | |||||
foundBigValues.add(row.get("B")); | |||||
} | |||||
try { | |||||
table.addRow(new BigDecimal("3245234.234"), | |||||
new BigDecimal("3245234.234")); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// ignored | |||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("-1234.0345"), | |||||
new BigDecimal("37.0000"), | |||||
new BigDecimal("1000.4500")), | |||||
foundSmallValues); | |||||
assertEquals(Arrays.asList( | |||||
new BigDecimal("23923434453436.36234219"), | |||||
new BigDecimal("37.00000000"), | |||||
new BigDecimal("-3452345321000.00000000")), | |||||
foundBigValues); | |||||
assertThrows(IOException.class, | |||||
() -> table.addRow(new BigDecimal("3245234.234"), | |||||
new BigDecimal("3245234.234"))); | |||||
} | } | ||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFixedNumeric() throws Exception | public void testFixedNumeric() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_NUMERIC)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_NUMERIC)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testMultiPageTableDef() throws Exception | public void testMultiPageTableDef() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testOverflow() throws Exception | public void testOverflow() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OVERFLOW, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OVERFLOW, true)) { | ||||
} | } | ||||
@Test | |||||
public void testUsageMapPromotion() throws Exception { | public void testUsageMapPromotion() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.PROMOTION)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.PROMOTION)) { | ||||
Database db = openMem(testDB); | Database db = openMem(testDB); | ||||
} | } | ||||
@Test | |||||
public void testLargeTableDef() throws Exception { | public void testLargeTableDef() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteAndReadDate() throws Exception { | public void testWriteAndReadDate() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = createMem(fileFormat); | Database db = createMem(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAncientDates() throws Exception | public void testAncientDates() throws Exception | ||||
{ | { | ||||
TimeZone tz = TimeZone.getTimeZone("America/New_York"); | TimeZone tz = TimeZone.getTimeZone("America/New_York"); | ||||
} | } | ||||
@Test | |||||
public void testSystemTable() throws Exception | public void testSystemTable() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
"MSysRelationships")); | "MSysRelationships")); | ||||
if (fileFormat == FileFormat.GENERIC_JET4) { | if (fileFormat == FileFormat.GENERIC_JET4) { | ||||
assertNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects")); | |||||
assertNull(db.getSystemTable("MSysAccessObjects"), "file format: " + fileFormat); | |||||
} else if (fileFormat.ordinal() < FileFormat.V2003.ordinal()) { | } else if (fileFormat.ordinal() < FileFormat.V2003.ordinal()) { | ||||
assertNotNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects")); | |||||
assertNotNull(db.getSystemTable("MSysAccessObjects"), "file format: " + fileFormat); | |||||
sysTables.add("MSysAccessObjects"); | sysTables.add("MSysAccessObjects"); | ||||
} else { | } else { | ||||
// v2003+ template files have no "MSysAccessObjects" table | // v2003+ template files have no "MSysAccessObjects" table | ||||
assertNull("file format: " + fileFormat, db.getSystemTable("MSysAccessObjects")); | |||||
assertNull(db.getSystemTable("MSysAccessObjects"), "file format: " + fileFormat); | |||||
sysTables.addAll( | sysTables.addAll( | ||||
Arrays.asList("MSysNavPaneGroupCategories", | Arrays.asList("MSysNavPaneGroupCategories", | ||||
"MSysNavPaneGroups", "MSysNavPaneGroupToObjects", | "MSysNavPaneGroups", "MSysNavPaneGroupToObjects", | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFixedText() throws Exception | public void testFixedText() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_TEXT)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.FIXED_TEXT)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testDbSortOrder() throws Exception { | public void testDbSortOrder() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUnsupportedColumns() throws Exception { | public void testUnsupportedColumns() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.UNSUPPORTED)) { | ||||
return tableList; | return tableList; | ||||
} | } | ||||
@Test | |||||
public void testTimeZone() throws Exception | public void testTimeZone() throws Exception | ||||
{ | { | ||||
TimeZone tz = TimeZone.getTimeZone("America/New_York"); | TimeZone tz = TimeZone.getTimeZone("America/New_York"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testToString() | public void testToString() | ||||
{ | { | ||||
RowImpl row = new RowImpl(new RowIdImpl(1, 1)); | RowImpl row = new RowImpl(new RowIdImpl(1, 1)); | ||||
assertEquals("Row[1:1][{id=37,data=<null>}]", row.toString()); | assertEquals("Row[1:1][{id=37,data=<null>}]", row.toString()); | ||||
} | } | ||||
@Test | |||||
public void testIterateTableNames() throws Exception { | public void testIterateTableNames() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
final Database db = open(testDB); | final Database db = open(testDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testTableDates() throws Exception { | public void testTableDates() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
Table table = open(testDB).getTable("Table1"); | Table table = open(testDB).getTable("Table1"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testBrokenIndex() throws Exception { | public void testBrokenIndex() throws Exception { | ||||
TestDB testDb = TestDB.getSupportedForBasename(Basename.TEST).get(0); | TestDB testDb = TestDB.getSupportedForBasename(Basename.TEST).get(0); | ||||
try (Database db = new DatabaseBuilder(testDb.getFile()) | try (Database db = new DatabaseBuilder(testDb.getFile()) |
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.RowIdImpl; | import com.healthmarketscience.jackcess.impl.RowIdImpl; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import org.junit.jupiter.api.AfterEach; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.BeforeEach; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class IndexTest extends TestCase { | |||||
public IndexTest(String name) { | |||||
super(name); | |||||
} | |||||
@Override | |||||
public class IndexTest | |||||
{ | |||||
@BeforeEach | |||||
protected void setUp() { | protected void setUp() { | ||||
TestUtil.setTestAutoSync(false); | TestUtil.setTestAutoSync(false); | ||||
} | } | ||||
@Override | |||||
@AfterEach | |||||
protected void tearDown() { | protected void tearDown() { | ||||
TestUtil.clearTestAutoSync(); | TestUtil.clearTestAutoSync(); | ||||
} | } | ||||
@Test | |||||
public void testByteOrder() throws Exception { | public void testByteOrder() throws Exception { | ||||
byte b1 = (byte)0x00; | byte b1 = (byte)0x00; | ||||
byte b2 = (byte)0x01; | byte b2 = (byte)0x01; | ||||
assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5)); | assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5)); | ||||
} | } | ||||
@Test | |||||
public void testByteCodeComparator() { | public void testByteCodeComparator() { | ||||
byte[] b0 = null; | byte[] b0 = null; | ||||
byte[] b1 = new byte[]{(byte)0x00}; | byte[] b1 = new byte[]{(byte)0x00}; | ||||
} | } | ||||
@Test | |||||
public void testPrimaryKey() throws Exception { | public void testPrimaryKey() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
Table table = open(testDB).getTable("Table1"); | Table table = open(testDB).getTable("Table1"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testLogicalIndexes() throws Exception | public void testLogicalIndexes() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testComplexIndex() throws Exception | public void testComplexIndex() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testEntryDeletion() throws Exception { | public void testEntryDeletion() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST) { | for (final TestDB testDB : SUPPORTED_DBS_TEST) { | ||||
Table table = openCopy(testDB).getTable("Table1"); | Table table = openCopy(testDB).getTable("Table1"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testIgnoreNulls() throws Exception | public void testIgnoreNulls() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUnique() throws Exception | public void testUnique() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUniqueEntryCount() throws Exception { | public void testUniqueEntryCount() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST) { | for (final TestDB testDB : SUPPORTED_DBS_TEST) { | ||||
Database db = openCopy(testDB); | Database db = openCopy(testDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReplId() throws Exception | public void testReplId() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST) { | for (final TestDB testDB : SUPPORTED_DBS_TEST) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testIndexCreation() throws Exception | public void testIndexCreation() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testIndexCreationSharedData() throws Exception | public void testIndexCreationSharedData() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testGetForeignKeyIndex() throws Exception | public void testGetForeignKeyIndex() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testConstraintViolation() throws Exception | public void testConstraintViolation() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table t = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG)) | |||||
.addColumn(newColumn("data", DataType.TEXT)) | |||||
.setPrimaryKey("id") | |||||
.addIndex(newIndex("data_ind") | |||||
.addColumns("data").setUnique()) | |||||
.toTable(db); | |||||
for(int i = 0; i < 5; ++i) { | |||||
t.addRow(i, "row" + i); | |||||
} | |||||
try { | |||||
t.addRow(3, "badrow"); | |||||
fail("ConstraintViolationException should have been thrown"); | |||||
} catch(ConstraintViolationException ce) { | |||||
// success | |||||
} | |||||
assertEquals(5, t.getRowCount()); | |||||
List<Row> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 0, "data", "row0"), | |||||
createExpectedRow( | |||||
"id", 1, "data", "row1"), | |||||
createExpectedRow( | |||||
"id", 2, "data", "row2"), | |||||
createExpectedRow( | |||||
"id", 3, "data", "row3"), | |||||
createExpectedRow( | |||||
"id", 4, "data", "row4")); | |||||
assertTable(expectedRows, t); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
List<Object[]> batch = new ArrayList<Object[]>(); | |||||
batch.add(new Object[]{5, "row5"}); | |||||
batch.add(new Object[]{6, "row6"}); | |||||
batch.add(new Object[]{7, "row2"}); | |||||
batch.add(new Object[]{8, "row8"}); | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
Table t = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG)) | |||||
.addColumn(newColumn("data", DataType.TEXT)) | |||||
.setPrimaryKey("id") | |||||
.addIndex(newIndex("data_ind") | |||||
.addColumns("data").setUnique()) | |||||
.toTable(db); | |||||
for (int i = 0; i < 5; ++i) | |||||
{ | |||||
t.addRow(i, "row" + i); | |||||
} | |||||
try { | |||||
t.addRows(batch); | |||||
fail("BatchUpdateException should have been thrown"); | |||||
} catch(BatchUpdateException be) { | |||||
// success | |||||
assertThrows(ConstraintViolationException.class, () -> t.addRow(3, "badrow")); | |||||
assertEquals(5, t.getRowCount()); | |||||
List<Row> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 0, "data", "row0"), | |||||
createExpectedRow( | |||||
"id", 1, "data", "row1"), | |||||
createExpectedRow( | |||||
"id", 2, "data", "row2"), | |||||
createExpectedRow( | |||||
"id", 3, "data", "row3"), | |||||
createExpectedRow( | |||||
"id", 4, "data", "row4")); | |||||
assertTable(expectedRows, t); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
List<Object[]> batch = new ArrayList<Object[]>(); | |||||
batch.add(new Object[] | |||||
{ | |||||
5, "row5" | |||||
}); | |||||
batch.add(new Object[] | |||||
{ | |||||
6, "row6" | |||||
}); | |||||
batch.add(new Object[] | |||||
{ | |||||
7, "row2" | |||||
}); | |||||
batch.add(new Object[] | |||||
{ | |||||
8, "row8" | |||||
}); | |||||
final BatchUpdateException be = assertThrows(BatchUpdateException.class, | |||||
() -> t.addRows(batch)); | |||||
assertTrue(be.getCause() instanceof ConstraintViolationException); | assertTrue(be.getCause() instanceof ConstraintViolationException); | ||||
assertEquals(2, be.getUpdateCount()); | assertEquals(2, be.getUpdateCount()); | ||||
} | |||||
expectedRows = new ArrayList<Row>(expectedRows); | |||||
expectedRows.add(createExpectedRow("id", 5, "data", "row5")); | |||||
expectedRows.add(createExpectedRow("id", 6, "data", "row6")); | |||||
assertTable(expectedRows, t); | |||||
expectedRows = new ArrayList<Row>(expectedRows); | |||||
expectedRows.add(createExpectedRow("id", 5, "data", "row5")); | |||||
expectedRows.add(createExpectedRow("id", 6, "data", "row6")); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertTable(expectedRows, t); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
assertCursor(expectedRows, pkCursor); | |||||
pkCursor.findFirstRowByEntry(4); | |||||
Row row4 = pkCursor.getCurrentRow(); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
row4.put("id", 3); | |||||
pkCursor.findFirstRowByEntry(4); | |||||
Row row4 = pkCursor.getCurrentRow(); | |||||
try { | |||||
t.updateRow(row4); | |||||
fail("ConstraintViolationException should have been thrown"); | |||||
} catch(ConstraintViolationException ce) { | |||||
// success | |||||
} | |||||
row4.put("id", 3); | |||||
assertTable(expectedRows, t); | |||||
assertThrows(ConstraintViolationException.class, () -> t.updateRow(row4)); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertTable(expectedRows, t); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
assertCursor(expectedRows, pkCursor); | |||||
db.close(); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
} | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAutoNumberRecover() throws Exception | public void testAutoNumberRecover() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table t = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data", DataType.TEXT)) | |||||
.setPrimaryKey("id") | |||||
.addIndex(newIndex("data_ind") | |||||
.addColumns("data").setUnique()) | |||||
.toTable(db); | |||||
for(int i = 1; i < 3; ++i) { | |||||
t.addRow(null, "row" + i); | |||||
} | |||||
try { | |||||
t.addRow(null, "row1"); | |||||
fail("ConstraintViolationException should have been thrown"); | |||||
} catch(ConstraintViolationException ce) { | |||||
// success | |||||
} | |||||
t.addRow(null, "row3"); | |||||
assertEquals(3, t.getRowCount()); | |||||
List<Row> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, "data", "row1"), | |||||
createExpectedRow( | |||||
"id", 2, "data", "row2"), | |||||
createExpectedRow( | |||||
"id", 3, "data", "row3")); | |||||
assertTable(expectedRows, t); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
List<Object[]> batch = new ArrayList<Object[]>(); | |||||
batch.add(new Object[]{null, "row4"}); | |||||
batch.add(new Object[]{null, "row5"}); | |||||
batch.add(new Object[]{null, "row3"}); | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
Table t = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data", DataType.TEXT)) | |||||
.setPrimaryKey("id") | |||||
.addIndex(newIndex("data_ind") | |||||
.addColumns("data").setUnique()) | |||||
.toTable(db); | |||||
for (int i = 1; i < 3; ++i) | |||||
{ | |||||
t.addRow(null, "row" + i); | |||||
} | |||||
try { | |||||
t.addRows(batch); | |||||
fail("BatchUpdateException should have been thrown"); | |||||
} catch(BatchUpdateException be) { | |||||
// success | |||||
assertThrows(ConstraintViolationException.class, () -> t.addRow(null, "row1")); | |||||
t.addRow(null, "row3"); | |||||
assertEquals(3, t.getRowCount()); | |||||
List<Row> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, "data", "row1"), | |||||
createExpectedRow( | |||||
"id", 2, "data", "row2"), | |||||
createExpectedRow( | |||||
"id", 3, "data", "row3")); | |||||
assertTable(expectedRows, t); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
List<Object[]> batch = new ArrayList<Object[]>(); | |||||
batch.add(new Object[] | |||||
{ | |||||
null, "row4" | |||||
}); | |||||
batch.add(new Object[] | |||||
{ | |||||
null, "row5" | |||||
}); | |||||
batch.add(new Object[] | |||||
{ | |||||
null, "row3" | |||||
}); | |||||
BatchUpdateException be = assertThrows(BatchUpdateException.class, | |||||
() -> t.addRows(batch)); | |||||
assertTrue(be.getCause() instanceof ConstraintViolationException); | assertTrue(be.getCause() instanceof ConstraintViolationException); | ||||
assertEquals(2, be.getUpdateCount()); | assertEquals(2, be.getUpdateCount()); | ||||
} | |||||
expectedRows = new ArrayList<Row>(expectedRows); | |||||
expectedRows.add(createExpectedRow("id", 4, "data", "row4")); | |||||
expectedRows.add(createExpectedRow("id", 5, "data", "row5")); | |||||
expectedRows = new ArrayList<Row>(expectedRows); | |||||
expectedRows.add(createExpectedRow("id", 4, "data", "row4")); | |||||
expectedRows.add(createExpectedRow("id", 5, "data", "row5")); | |||||
assertTable(expectedRows, t); | |||||
assertTable(expectedRows, t); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, pkCursor); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
assertCursor(expectedRows, | |||||
CursorBuilder.createCursor(t.getIndex("data_ind"))); | |||||
db.close(); | |||||
} | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testBinaryIndex() throws Exception | public void testBinaryIndex() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BINARY_INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BINARY_INDEX)) { |
import java.io.File; | import java.io.File; | ||||
import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
import java.io.IOException; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import com.healthmarketscience.jackcess.impl.DatabaseImpl; | import com.healthmarketscience.jackcess.impl.DatabaseImpl; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.util.LinkResolver; | |||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class LinkedTableTest extends TestCase | |||||
public class LinkedTableTest | |||||
{ | { | ||||
public void testLinkedTables() throws Exception { | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.LINKED)) { | |||||
Database db = openCopy(testDB); | |||||
try { | |||||
db.getTable("Table2"); | |||||
fail("FileNotFoundException should have been thrown"); | |||||
} catch(FileNotFoundException e) { | |||||
// success | |||||
} | |||||
TableMetaData tmd = db.getTableMetaData("Table2"); | |||||
assertEquals("Table2", tmd.getName()); | |||||
assertTrue(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertEquals("Table1", tmd.getLinkedTableName()); | |||||
assertNull(tmd.getConnectionName()); | |||||
assertEquals(TableMetaData.Type.LINKED, tmd.getType()); | |||||
assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName()); | |||||
assertNull(tmd.getTableDefinition(db)); | |||||
tmd = db.getTableMetaData("FooTable"); | |||||
assertNull(tmd); | |||||
assertTrue(db.getLinkedDatabases().isEmpty()); | |||||
final String linkeeDbName = "Z:\\jackcess_test\\linkeeTest.accdb"; | |||||
final File linkeeFile = new File("src/test/data/linkeeTest.accdb"); | |||||
db.setLinkResolver(new LinkResolver() { | |||||
@Override | |||||
public Database resolveLinkedDatabase(Database linkerdb, String dbName) | |||||
throws IOException { | |||||
@Test | |||||
public void testLinkedTables() throws Exception | |||||
{ | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.LINKED)) | |||||
{ | |||||
try (final Database db = openCopy(testDB)) | |||||
{ | |||||
assertThrows(FileNotFoundException.class, () -> db.getTable("Table2")); | |||||
TableMetaData tmd = db.getTableMetaData("Table2"); | |||||
assertEquals("Table2", tmd.getName()); | |||||
assertTrue(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertEquals("Table1", tmd.getLinkedTableName()); | |||||
assertNull(tmd.getConnectionName()); | |||||
assertEquals(TableMetaData.Type.LINKED, tmd.getType()); | |||||
assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName()); | |||||
assertNull(tmd.getTableDefinition(db)); | |||||
tmd = db.getTableMetaData("FooTable"); | |||||
assertNull(tmd); | |||||
assertTrue(db.getLinkedDatabases().isEmpty()); | |||||
final String linkeeDbName = "Z:\\jackcess_test\\linkeeTest.accdb"; | |||||
final File linkeeFile = new File("src/test/data/linkeeTest.accdb"); | |||||
db.setLinkResolver((Database linkerdb, String dbName) -> | |||||
{ | |||||
assertEquals(linkeeDbName, dbName); | assertEquals(linkeeDbName, dbName); | ||||
return DatabaseBuilder.open(linkeeFile); | return DatabaseBuilder.open(linkeeFile); | ||||
} | |||||
}); | |||||
Table t2 = db.getTable("Table2"); | |||||
assertEquals(1, db.getLinkedDatabases().size()); | |||||
Database linkeeDb = db.getLinkedDatabases().get(linkeeDbName); | |||||
assertNotNull(linkeeDb); | |||||
assertEquals(linkeeFile, linkeeDb.getFile()); | |||||
assertEquals("linkeeTest.accdb", ((DatabaseImpl)linkeeDb).getName()); | |||||
List<? extends Map<String, Object>> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"ID", 1, | |||||
"Field1", "bar")); | |||||
assertTable(expectedRows, t2); | |||||
db.createLinkedTable("FooTable", linkeeDbName, "Table2"); | |||||
tmd = db.getTableMetaData("FooTable"); | |||||
assertEquals("FooTable", tmd.getName()); | |||||
assertTrue(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertEquals("Table2", tmd.getLinkedTableName()); | |||||
assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName()); | |||||
Table t3 = db.getTable("FooTable"); | |||||
assertEquals(1, db.getLinkedDatabases().size()); | |||||
expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"ID", 1, | |||||
"Field1", "buzz")); | |||||
assertTable(expectedRows, t3); | |||||
tmd = db.getTableMetaData("Table1"); | |||||
assertEquals("Table1", tmd.getName()); | |||||
assertFalse(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertNull(tmd.getLinkedTableName()); | |||||
assertNull(tmd.getLinkedDbName()); | |||||
Table t1 = tmd.open(db); | |||||
assertFalse(db.isLinkedTable(null)); | |||||
assertTrue(db.isLinkedTable(t2)); | |||||
assertTrue(db.isLinkedTable(t3)); | |||||
assertFalse(db.isLinkedTable(t1)); | |||||
List<Table> tables = DatabaseTest.getTables(db.newIterable()); | |||||
assertEquals(3, tables.size()); | |||||
assertTrue(tables.contains(t1)); | |||||
assertTrue(tables.contains(t2)); | |||||
assertTrue(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().setIncludeNormalTables(false)); | |||||
assertEquals(2, tables.size()); | |||||
assertFalse(tables.contains(t1)); | |||||
assertTrue(tables.contains(t2)); | |||||
assertTrue(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().withLocalUserTablesOnly()); | |||||
assertEquals(1, tables.size()); | |||||
assertTrue(tables.contains(t1)); | |||||
assertFalse(tables.contains(t2)); | |||||
assertFalse(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().withSystemTablesOnly()); | |||||
assertTrue(tables.size() > 5); | |||||
assertFalse(tables.contains(t1)); | |||||
assertFalse(tables.contains(t2)); | |||||
assertFalse(tables.contains(t3)); | |||||
assertTrue(tables.contains(((DatabaseImpl)db).getSystemCatalog())); | |||||
db.close(); | |||||
}); | |||||
Table t2 = db.getTable("Table2"); | |||||
assertEquals(1, db.getLinkedDatabases().size()); | |||||
Database linkeeDb = db.getLinkedDatabases().get(linkeeDbName); | |||||
assertNotNull(linkeeDb); | |||||
assertEquals(linkeeFile, linkeeDb.getFile()); | |||||
assertEquals("linkeeTest.accdb", ((DatabaseImpl) linkeeDb).getName()); | |||||
List<? extends Map<String, Object>> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"ID", 1, | |||||
"Field1", "bar")); | |||||
assertTable(expectedRows, t2); | |||||
db.createLinkedTable("FooTable", linkeeDbName, "Table2"); | |||||
tmd = db.getTableMetaData("FooTable"); | |||||
assertEquals("FooTable", tmd.getName()); | |||||
assertTrue(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertEquals("Table2", tmd.getLinkedTableName()); | |||||
assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName()); | |||||
Table t3 = db.getTable("FooTable"); | |||||
assertEquals(1, db.getLinkedDatabases().size()); | |||||
expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"ID", 1, | |||||
"Field1", "buzz")); | |||||
assertTable(expectedRows, t3); | |||||
tmd = db.getTableMetaData("Table1"); | |||||
assertEquals("Table1", tmd.getName()); | |||||
assertFalse(tmd.isLinked()); | |||||
assertFalse(tmd.isSystem()); | |||||
assertNull(tmd.getLinkedTableName()); | |||||
assertNull(tmd.getLinkedDbName()); | |||||
Table t1 = tmd.open(db); | |||||
assertFalse(db.isLinkedTable(null)); | |||||
assertTrue(db.isLinkedTable(t2)); | |||||
assertTrue(db.isLinkedTable(t3)); | |||||
assertFalse(db.isLinkedTable(t1)); | |||||
List<Table> tables = DatabaseTest.getTables(db.newIterable()); | |||||
assertEquals(3, tables.size()); | |||||
assertTrue(tables.contains(t1)); | |||||
assertTrue(tables.contains(t2)); | |||||
assertTrue(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl) db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().setIncludeNormalTables(false)); | |||||
assertEquals(2, tables.size()); | |||||
assertFalse(tables.contains(t1)); | |||||
assertTrue(tables.contains(t2)); | |||||
assertTrue(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl) db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().withLocalUserTablesOnly()); | |||||
assertEquals(1, tables.size()); | |||||
assertTrue(tables.contains(t1)); | |||||
assertFalse(tables.contains(t2)); | |||||
assertFalse(tables.contains(t3)); | |||||
assertFalse(tables.contains(((DatabaseImpl) db).getSystemCatalog())); | |||||
tables = DatabaseTest.getTables(db.newIterable().withSystemTablesOnly()); | |||||
assertTrue(tables.size() > 5); | |||||
assertFalse(tables.contains(t1)); | |||||
assertFalse(tables.contains(t2)); | |||||
assertFalse(tables.contains(t3)); | |||||
assertTrue(tables.contains(((DatabaseImpl) db).getSystemCatalog())); | |||||
} | |||||
} | } | ||||
} | } | ||||
public void testOdbcLinkedTables() throws Exception { | |||||
for (final TestDB testDB : | |||||
TestDB.getSupportedForBasename(Basename.LINKED_ODBC)) { | |||||
Database db = openCopy(testDB); | |||||
@Test | |||||
public void testOdbcLinkedTables() throws Exception | |||||
{ | |||||
for (final TestDB testDB | |||||
: TestDB.getSupportedForBasename(Basename.LINKED_ODBC)) | |||||
{ | |||||
try (final Database db = openCopy(testDB)) | |||||
{ | |||||
TableMetaData tmd = db.getTableMetaData("Ordrar"); | |||||
assertEquals(TableMetaData.Type.LINKED_ODBC, tmd.getType()); | |||||
assertEquals("dbo.Ordrar", tmd.getLinkedTableName()); | |||||
assertNull(tmd.getLinkedDbName()); | |||||
assertEquals("DSN=Magnapinna;Description=Safexit;UID=safexit;PWD=DummyPassword;APP=Microsoft Office;DATABASE=safexit", tmd.getConnectionName()); | |||||
assertFalse(tmd.toString().contains("DummyPassword")); | |||||
TableMetaData tmd = db.getTableMetaData("Ordrar"); | |||||
assertEquals(TableMetaData.Type.LINKED_ODBC, tmd.getType()); | |||||
assertEquals("dbo.Ordrar", tmd.getLinkedTableName()); | |||||
assertNull(tmd.getLinkedDbName()); | |||||
assertEquals("DSN=Magnapinna;Description=Safexit;UID=safexit;PWD=DummyPassword;APP=Microsoft Office;DATABASE=safexit", tmd.getConnectionName()); | |||||
assertFalse(tmd.toString().contains("DummyPassword")); | |||||
TableDefinition t = tmd.getTableDefinition(db); | |||||
TableDefinition t = tmd.getTableDefinition(db); | |||||
List<? extends Column> cols = t.getColumns(); | |||||
assertEquals(20, cols.size()); | |||||
List<? extends Column> cols = t.getColumns(); | |||||
assertEquals(20, cols.size()); | |||||
List<? extends Index> idxs = t.getIndexes(); | |||||
assertEquals(5, idxs.size()); | |||||
List<? extends Index> idxs = t.getIndexes(); | |||||
assertEquals(5, idxs.size()); | |||||
Table tbl = db.getTable("Ordrar"); | |||||
Table tbl = db.getTable("Ordrar"); | |||||
assertThrows(UnsupportedOperationException.class, () -> tbl.iterator()); | |||||
try { | |||||
tbl.iterator(); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException expected) { | |||||
// expected | |||||
} | |||||
assertThrows(UnsupportedOperationException.class, () -> tbl.addRow(1L, "bar")); | |||||
try { | |||||
tbl.addRow(1L,"bar"); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException expected) { | |||||
// expected | |||||
} | } | ||||
db.close(); | |||||
} | } | ||||
} | } | ||||
import com.healthmarketscience.jackcess.impl.ColumnImpl; | import com.healthmarketscience.jackcess.impl.ColumnImpl; | ||||
import com.healthmarketscience.jackcess.impl.DatabaseImpl; | import com.healthmarketscience.jackcess.impl.DatabaseImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static com.healthmarketscience.jackcess.Database.*; | import static com.healthmarketscience.jackcess.Database.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class LocalDateTimeTest extends TestCase | |||||
public class LocalDateTimeTest | |||||
{ | { | ||||
public LocalDateTimeTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testWriteAndReadLocalDate() throws Exception { | public void testWriteAndReadLocalDate() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = createMem(fileFormat); | Database db = createMem(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAncientLocalDates() throws Exception | public void testAncientLocalDates() throws Exception | ||||
{ | { | ||||
ZoneId zoneId = ZoneId.of("America/New_York"); | ZoneId zoneId = ZoneId.of("America/New_York"); | ||||
} | } | ||||
@Test | |||||
public void testZoneId() throws Exception | public void testZoneId() throws Exception | ||||
{ | { | ||||
ZoneId zoneId = ZoneId.of("America/New_York"); | ZoneId zoneId = ZoneId.of("America/New_York"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteAndReadTemporals() throws Exception { | public void testWriteAndReadTemporals() throws Exception { | ||||
ZoneId zoneId = ZoneId.of("America/New_York"); | ZoneId zoneId = ZoneId.of("America/New_York"); | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { |
import java.util.UUID; | import java.util.UUID; | ||||
import static com.healthmarketscience.jackcess.Database.*; | import static com.healthmarketscience.jackcess.Database.*; | ||||
import com.healthmarketscience.jackcess.InvalidValueException; | |||||
import com.healthmarketscience.jackcess.impl.DatabaseImpl; | import com.healthmarketscience.jackcess.impl.DatabaseImpl; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.PropertyMapImpl; | import com.healthmarketscience.jackcess.impl.PropertyMapImpl; | ||||
import com.healthmarketscience.jackcess.impl.PropertyMaps; | import com.healthmarketscience.jackcess.impl.PropertyMaps; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class PropertiesTest extends TestCase | |||||
public class PropertiesTest | |||||
{ | { | ||||
public PropertiesTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testPropertyMaps() throws Exception | public void testPropertyMaps() throws Exception | ||||
{ | { | ||||
PropertyMaps maps = new PropertyMaps(10, null, null, null); | PropertyMaps maps = new PropertyMaps(10, null, null, null); | ||||
colMap.get("buzz")), props); | colMap.get("buzz")), props); | ||||
} | } | ||||
@Test | |||||
public void testInferTypes() throws Exception | public void testInferTypes() throws Exception | ||||
{ | { | ||||
PropertyMaps maps = new PropertyMaps(10, null, null, null); | PropertyMaps maps = new PropertyMaps(10, null, null, null); | ||||
defMap.put("intprop", 37).getType()); | defMap.put("intprop", 37).getType()); | ||||
} | } | ||||
@Test | |||||
public void testReadProperties() throws Exception | public void testReadProperties() throws Exception | ||||
{ | { | ||||
byte[] nameMapBytes = null; | byte[] nameMapBytes = null; | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testParseProperties() throws Exception | public void testParseProperties() throws Exception | ||||
{ | { | ||||
for(FileFormat ff : SUPPORTED_FILEFORMATS_FOR_READ) { | for(FileFormat ff : SUPPORTED_FILEFORMATS_FOR_READ) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteProperties() throws Exception | public void testWriteProperties() throws Exception | ||||
{ | { | ||||
for(TestDB testDb : SUPPORTED_DBS_TEST) { | for(TestDB testDb : SUPPORTED_DBS_TEST) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testModifyProperties() throws Exception | public void testModifyProperties() throws Exception | ||||
{ | { | ||||
for(TestDB testDb : SUPPORTED_DBS_TEST) { | for(TestDB testDb : SUPPORTED_DBS_TEST) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testCreateDbProperties() throws Exception | public void testCreateDbProperties() throws Exception | ||||
{ | { | ||||
for(FileFormat ff : SUPPORTED_FILEFORMATS) { | for(FileFormat ff : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testEnforceProperties() throws Exception | public void testEnforceProperties() throws Exception | ||||
{ | { | ||||
for(final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
final Table testReq = newTable("testReq") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.toTable(db); | |||||
Table t = newTable("testReq") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.toTable(db); | |||||
testReq.addRow(Column.AUTO_NUMBER, "v1"); | |||||
t.addRow(Column.AUTO_NUMBER, "v1"); | |||||
assertThrows(InvalidValueException.class, () -> testReq.addRow(Column.AUTO_NUMBER, null)); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, null); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException expected) { | |||||
// success | |||||
} | |||||
testReq.addRow(Column.AUTO_NUMBER, ""); | |||||
t.addRow(Column.AUTO_NUMBER, ""); | |||||
List<? extends Map<String, Object>> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"value", "v1"), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"value", "")); | |||||
assertTable(expectedRows, testReq); | |||||
List<? extends Map<String, Object>> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"value", "v1"), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"value", "")); | |||||
assertTable(expectedRows, t); | |||||
final Table testNz = newTable("testNz") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT) | |||||
.putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false)) | |||||
.toTable(db); | |||||
testNz.addRow(Column.AUTO_NUMBER, "v1"); | |||||
t = newTable("testNz") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT) | |||||
.putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false)) | |||||
.toTable(db); | |||||
assertThrows(InvalidValueException.class, () -> testNz.addRow(Column.AUTO_NUMBER, "")); | |||||
t.addRow(Column.AUTO_NUMBER, "v1"); | |||||
testNz.addRow(Column.AUTO_NUMBER, null); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, ""); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException expected) { | |||||
// success | |||||
} | |||||
expectedRows = createExpectedTable(createExpectedRow("id", 1, "value", "v1"), | |||||
createExpectedRow("id", 2, "value", null)); | |||||
assertTable(expectedRows, testNz); | |||||
t.addRow(Column.AUTO_NUMBER, null); | |||||
final Table testReqNz = newTable("testReqNz") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT)) | |||||
.toTable(db); | |||||
expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"value", "v1"), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"value", null)); | |||||
assertTable(expectedRows, t); | |||||
Column col = testReqNz.getColumn("value"); | |||||
PropertyMap props = col.getProperties(); | |||||
props.put(PropertyMap.REQUIRED_PROP, true); | |||||
props.put(PropertyMap.ALLOW_ZERO_LEN_PROP, false); | |||||
props.save(); | |||||
testReqNz.addRow(Column.AUTO_NUMBER, "v1"); | |||||
t = newTable("testReqNz") | |||||
.addColumn(newColumn("id", DataType.LONG) | |||||
.setAutoNumber(true) | |||||
.putProperty(PropertyMap.REQUIRED_PROP, true)) | |||||
.addColumn(newColumn("value", DataType.TEXT)) | |||||
.toTable(db); | |||||
assertThrows(InvalidValueException.class, () -> testReqNz.addRow(Column.AUTO_NUMBER, "")); | |||||
Column col = t.getColumn("value"); | |||||
PropertyMap props = col.getProperties(); | |||||
props.put(PropertyMap.REQUIRED_PROP, true); | |||||
props.put(PropertyMap.ALLOW_ZERO_LEN_PROP, false); | |||||
props.save(); | |||||
assertThrows(InvalidValueException.class, () -> testReqNz.addRow(Column.AUTO_NUMBER, null)); | |||||
t.addRow(Column.AUTO_NUMBER, "v1"); | |||||
testReqNz.addRow(Column.AUTO_NUMBER, "v2"); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, ""); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException expected) { | |||||
// success | |||||
} | |||||
expectedRows = createExpectedTable(createExpectedRow("id", 1, "value", "v1"), | |||||
createExpectedRow("id", 2, "value", "v2")); | |||||
assertTable(expectedRows, testReqNz); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, null); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException expected) { | |||||
// success | |||||
} | } | ||||
t.addRow(Column.AUTO_NUMBER, "v2"); | |||||
expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"value", "v1"), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"value", "v2")); | |||||
assertTable(expectedRows, t); | |||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testEnumValues() throws Exception | public void testEnumValues() throws Exception | ||||
{ | { | ||||
PropertyMaps maps = new PropertyMaps(10, null, null, null); | PropertyMaps maps = new PropertyMaps(10, null, null, null); |
import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions; | import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions; | ||||
import com.healthmarketscience.jackcess.impl.expr.FunctionSupport; | import com.healthmarketscience.jackcess.impl.expr.FunctionSupport; | ||||
import com.healthmarketscience.jackcess.impl.expr.ValueSupport; | import com.healthmarketscience.jackcess.impl.expr.ValueSupport; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.Database.*; | import static com.healthmarketscience.jackcess.Database.*; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class PropertyExpressionTest extends TestCase | |||||
public class PropertyExpressionTest | |||||
{ | { | ||||
public PropertyExpressionTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testDefaultValue() throws Exception | public void testDefaultValue() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testCalculatedValue() throws Exception | public void testCalculatedValue() throws Exception | ||||
{ | { | ||||
Database db = create(FileFormat.V2016); | Database db = create(FileFormat.V2016); | ||||
db.close(); | db.close(); | ||||
} | } | ||||
@Test | |||||
public void testColumnValidator() throws Exception | public void testColumnValidator() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
db.setEvaluateExpressions(true); | |||||
Table t = newTable("test") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data1", DataType.LONG) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
">37")) | |||||
.addColumn(newColumn("data2", DataType.LONG) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
"between 7 and 10") | |||||
.putProperty(PropertyMap.VALIDATION_TEXT_PROP, | |||||
"You failed")) | |||||
.toTable(db); | |||||
t.addRow(Column.AUTO_NUMBER, 42, 8); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, 42, 20); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException ive) { | |||||
// success | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (Database db = create(fileFormat)) | |||||
{ | |||||
db.setEvaluateExpressions(true); | |||||
Table t = newTable("test") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data1", DataType.LONG) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
">37")) | |||||
.addColumn(newColumn("data2", DataType.LONG) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
"between 7 and 10") | |||||
.putProperty(PropertyMap.VALIDATION_TEXT_PROP, | |||||
"You failed")) | |||||
.toTable(db); | |||||
t.addRow(Column.AUTO_NUMBER, 42, 8); | |||||
InvalidValueException ive = assertThrows(InvalidValueException.class, | |||||
() -> t.addRow(Column.AUTO_NUMBER, 42, 20)); | |||||
assertTrue(ive.getMessage().contains("You failed")); | assertTrue(ive.getMessage().contains("You failed")); | ||||
} | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, 3, 8); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException ive) { | |||||
// success | |||||
ive = assertThrows(InvalidValueException.class, () -> t.addRow(Column.AUTO_NUMBER, 3, 8)); | |||||
assertFalse(ive.getMessage().contains("You failed")); | assertFalse(ive.getMessage().contains("You failed")); | ||||
} | |||||
t.addRow(Column.AUTO_NUMBER, 54, 9); | |||||
t.addRow(Column.AUTO_NUMBER, 54, 9); | |||||
setProp(t, "data1", PropertyMap.VALIDATION_RULE_PROP, null); | |||||
setProp(t, "data2", PropertyMap.VALIDATION_RULE_PROP, "<100"); | |||||
setProp(t, "data2", PropertyMap.VALIDATION_TEXT_PROP, "Too big"); | |||||
setProp(t, "data1", PropertyMap.VALIDATION_RULE_PROP, null); | |||||
setProp(t, "data2", PropertyMap.VALIDATION_RULE_PROP, "<100"); | |||||
setProp(t, "data2", PropertyMap.VALIDATION_TEXT_PROP, "Too big"); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, 42, 200); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException ive) { | |||||
// success | |||||
ive = assertThrows(InvalidValueException.class, () -> t.addRow(Column.AUTO_NUMBER, 42, 200)); | |||||
assertTrue(ive.getMessage().contains("Too big")); | assertTrue(ive.getMessage().contains("Too big")); | ||||
} | |||||
t.addRow(Column.AUTO_NUMBER, 1, 9); | |||||
List<Row> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"data1", 42, | |||||
"data2", 8), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"data1", 54, | |||||
"data2", 9), | |||||
createExpectedRow( | |||||
"id", 3, | |||||
"data1", 1, | |||||
"data2", 9)); | |||||
assertTable(expectedRows, t); | |||||
db.close(); | |||||
t.addRow(Column.AUTO_NUMBER, 1, 9); | |||||
List<Row> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"data1", 42, | |||||
"data2", 8), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"data1", 54, | |||||
"data2", 9), | |||||
createExpectedRow( | |||||
"id", 3, | |||||
"data1", 1, | |||||
"data2", 9)); | |||||
assertTable(expectedRows, t); | |||||
} | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testRowValidator() throws Exception | public void testRowValidator() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
db.setEvaluateExpressions(true); | |||||
Table t = newTable("test") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data1", DataType.LONG)) | |||||
.addColumn(newColumn("data2", DataType.LONG)) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
"([data1] > 10) and ([data2] < 100)") | |||||
.putProperty(PropertyMap.VALIDATION_TEXT_PROP, | |||||
"You failed") | |||||
.toTable(db); | |||||
t.addRow(Column.AUTO_NUMBER, 42, 8); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, 1, 20); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException ive) { | |||||
// success | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
db.setEvaluateExpressions(true); | |||||
Table t = newTable("test") | |||||
.addColumn(newColumn("id", DataType.LONG).setAutoNumber(true)) | |||||
.addColumn(newColumn("data1", DataType.LONG)) | |||||
.addColumn(newColumn("data2", DataType.LONG)) | |||||
.putProperty(PropertyMap.VALIDATION_RULE_PROP, | |||||
"([data1] > 10) and ([data2] < 100)") | |||||
.putProperty(PropertyMap.VALIDATION_TEXT_PROP, | |||||
"You failed") | |||||
.toTable(db); | |||||
t.addRow(Column.AUTO_NUMBER, 42, 8); | |||||
InvalidValueException ive = assertThrows(InvalidValueException.class, | |||||
() -> t.addRow(Column.AUTO_NUMBER, 1, 20)); | |||||
assertTrue(ive.getMessage().contains("You failed")); | assertTrue(ive.getMessage().contains("You failed")); | ||||
} | |||||
t.addRow(Column.AUTO_NUMBER, 54, 9); | |||||
t.addRow(Column.AUTO_NUMBER, 54, 9); | |||||
setTableProp(t, PropertyMap.VALIDATION_RULE_PROP, "[data2]<100"); | |||||
setTableProp(t, PropertyMap.VALIDATION_TEXT_PROP, "Too big"); | |||||
setTableProp(t, PropertyMap.VALIDATION_RULE_PROP, "[data2]<100"); | |||||
setTableProp(t, PropertyMap.VALIDATION_TEXT_PROP, "Too big"); | |||||
try { | |||||
t.addRow(Column.AUTO_NUMBER, 42, 200); | |||||
fail("InvalidValueException should have been thrown"); | |||||
} catch(InvalidValueException ive) { | |||||
// success | |||||
ive = assertThrows(InvalidValueException.class, () -> t.addRow(Column.AUTO_NUMBER, 42, 200)); | |||||
assertTrue(ive.getMessage().contains("Too big")); | assertTrue(ive.getMessage().contains("Too big")); | ||||
} | |||||
t.addRow(Column.AUTO_NUMBER, 1, 9); | |||||
t.addRow(Column.AUTO_NUMBER, 1, 9); | |||||
List<Row> expectedRows | |||||
= createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"data1", 42, | |||||
"data2", 8), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"data1", 54, | |||||
"data2", 9), | |||||
createExpectedRow( | |||||
"id", 3, | |||||
"data1", 1, | |||||
"data2", 9)); | |||||
assertTable(expectedRows, t); | |||||
List<Row> expectedRows = | |||||
createExpectedTable( | |||||
createExpectedRow( | |||||
"id", 1, | |||||
"data1", 42, | |||||
"data2", 8), | |||||
createExpectedRow( | |||||
"id", 2, | |||||
"data1", 54, | |||||
"data2", 9), | |||||
createExpectedRow( | |||||
"id", 3, | |||||
"data1", 1, | |||||
"data2", 9)); | |||||
assertTable(expectedRows, t); | |||||
db.close(); | |||||
} | |||||
} | } | ||||
} | } | ||||
public static void testCustomEvalConfig() throws Exception | |||||
@Test | |||||
public void testCustomEvalConfig() throws Exception | |||||
{ | { | ||||
TemporalConfig tempConf = new TemporalConfig("[uuuu/]M/d", | TemporalConfig tempConf = new TemporalConfig("[uuuu/]M/d", | ||||
"uuuu-MMM-d", | "uuuu-MMM-d", |
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.RelationshipImpl; | import com.healthmarketscience.jackcess.impl.RelationshipImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class RelationshipTest extends TestCase { | |||||
public class RelationshipTest | |||||
{ | |||||
private static final Comparator<Relationship> REL_COMP = new Comparator<Relationship>() { | private static final Comparator<Relationship> REL_COMP = new Comparator<Relationship>() { | ||||
public int compare(Relationship r1, Relationship r2) { | public int compare(Relationship r1, Relationship r2) { | ||||
return String.CASE_INSENSITIVE_ORDER.compare(r1.getName(), r2.getName()); | return String.CASE_INSENSITIVE_ORDER.compare(r1.getName(), r2.getName()); | ||||
} | } | ||||
}; | }; | ||||
public RelationshipTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testTwoTables() throws Exception { | public void testTwoTables() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
Database db = open(testDB); | Database db = open(testDB); | ||||
assertTrue(rel.cascadeUpdates()); | assertTrue(rel.cascadeUpdates()); | ||||
assertSameRelationships(rels, db.getRelationships(t3, t1), true); | assertSameRelationships(rels, db.getRelationships(t3, t1), true); | ||||
try { | |||||
db.getRelationships(t1, t1); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, () -> db.getRelationships(t1, t1)); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testOneTable() throws Exception { | public void testOneTable() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
Database db = open(testDB); | Database db = open(testDB); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testNoTables() throws Exception { | public void testNoTables() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) { | ||||
Database db = open(testDB); | Database db = open(testDB); |
import com.healthmarketscience.jackcess.impl.JetFormat; | import com.healthmarketscience.jackcess.impl.JetFormat; | ||||
import com.healthmarketscience.jackcess.impl.PageChannel; | import com.healthmarketscience.jackcess.impl.PageChannel; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author Tim McCune | * @author Tim McCune | ||||
*/ | */ | ||||
public class TableTest extends TestCase { | |||||
public class TableTest | |||||
{ | |||||
private final PageChannel _pageChannel = new PageChannel(true) {}; | private final PageChannel _pageChannel = new PageChannel(true) {}; | ||||
private List<ColumnImpl> _columns = new ArrayList<ColumnImpl>(); | private List<ColumnImpl> _columns = new ArrayList<ColumnImpl>(); | ||||
private TestTable _testTable; | private TestTable _testTable; | ||||
private int _varLenIdx; | private int _varLenIdx; | ||||
private int _fixedOffset; | private int _fixedOffset; | ||||
public TableTest(String name) { | |||||
super(name); | |||||
} | |||||
private void reset() { | private void reset() { | ||||
_testTable = null; | _testTable = null; | ||||
_columns = new ArrayList<ColumnImpl>(); | _columns = new ArrayList<ColumnImpl>(); | ||||
_fixedOffset = 0; | _fixedOffset = 0; | ||||
} | } | ||||
@Test | |||||
public void testCreateRow() throws Exception { | public void testCreateRow() throws Exception { | ||||
reset(); | reset(); | ||||
newTestColumn(DataType.INT, false); | newTestColumn(DataType.INT, false); | ||||
assertEquals((byte) 7, buffer.get(30)); | assertEquals((byte) 7, buffer.get(30)); | ||||
} | } | ||||
@Test | |||||
public void testUnicodeCompression() throws Exception { | public void testUnicodeCompression() throws Exception { | ||||
reset(); | reset(); | ||||
newTestColumn(DataType.TEXT, false); | newTestColumn(DataType.TEXT, false); |
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.impl.DatabaseImpl; | import com.healthmarketscience.jackcess.impl.DatabaseImpl; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class TableUpdaterTest extends TestCase | |||||
public class TableUpdaterTest | |||||
{ | { | ||||
public TableUpdaterTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testTableUpdating() throws Exception { | public void testTableUpdating() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testTableUpdatingOneToOne() throws Exception { | public void testTableUpdatingOneToOne() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testTableUpdatingNoEnforce() throws Exception { | public void testTableUpdatingNoEnforce() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testTableUpdatingNamedRelationship() throws Exception { | public void testTableUpdatingNamedRelationship() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
private void doTestUpdating(Database db, boolean oneToOne, boolean enforce, String relationshipName) | private void doTestUpdating(Database db, boolean oneToOne, boolean enforce, String relationshipName) | ||||
throws Exception | throws Exception | ||||
{ | { | ||||
Table t1 = newTable("TestTable") | |||||
final Table t1 = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG)) | .addColumn(newColumn("id", DataType.LONG)) | ||||
.toTable(db); | .toTable(db); | ||||
Table t2 = newTable("TestTable2") | |||||
final Table t2 = newTable("TestTable2") | |||||
.addColumn(newColumn("id2", DataType.LONG)) | .addColumn(newColumn("id2", DataType.LONG)) | ||||
.toTable(db); | .toTable(db); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testInvalidUpdate() throws Exception | public void testInvalidUpdate() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table t1 = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG)) | |||||
.toTable(db); | |||||
try { | |||||
newColumn("ID", DataType.TEXT) | |||||
.addToTable(t1); | |||||
fail("created table with no columns?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
Table t1 = newTable("TestTable") | |||||
.addColumn(newColumn("id", DataType.LONG)) | |||||
.toTable(db); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newColumn("ID", DataType.TEXT).addToTable(t1), | |||||
"created table with no columns?"); | |||||
Table t2 = newTable("TestTable2") | |||||
.addColumn(newColumn("id2", DataType.LONG)) | |||||
.toTable(db); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newRelationship(t1, t2).toRelationship(db), | |||||
"created rel with no columns?"); | |||||
assertThrows(IllegalArgumentException.class, | |||||
() -> newRelationship("TestTable", "TestTable2").addColumns("id", "id") .toRelationship(db), | |||||
"created rel with wrong columns?"); | |||||
} | } | ||||
Table t2 = newTable("TestTable2") | |||||
.addColumn(newColumn("id2", DataType.LONG)) | |||||
.toTable(db); | |||||
try { | |||||
newRelationship(t1, t2) | |||||
.toRelationship(db); | |||||
fail("created rel with no columns?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
try { | |||||
newRelationship("TestTable", "TestTable2") | |||||
.addColumns("id", "id") | |||||
.toRelationship(db); | |||||
fail("created rel with wrong columns?"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
db.close(); | |||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUpdateLargeTableDef() throws Exception | public void testUpdateLargeTableDef() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { |
import com.healthmarketscience.jackcess.impl.RowIdImpl; | import com.healthmarketscience.jackcess.impl.RowIdImpl; | ||||
import com.healthmarketscience.jackcess.impl.RowImpl; | import com.healthmarketscience.jackcess.impl.RowImpl; | ||||
import com.healthmarketscience.jackcess.util.MemFileChannel; | import com.healthmarketscience.jackcess.util.MemFileChannel; | ||||
import org.junit.Assert; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
/** | /** | ||||
* Utilty code for the test cases. | * Utilty code for the test cases. | ||||
.setAutoSync(getTestAutoSync()).setChannel(channel) | .setAutoSync(getTestAutoSync()).setChannel(channel) | ||||
.setCharset(charset).open(); | .setCharset(charset).open(); | ||||
if(fileFormat != null) { | if(fileFormat != null) { | ||||
Assert.assertEquals( | |||||
"Wrong JetFormat.", | |||||
DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(), | |||||
((DatabaseImpl)db).getFormat()); | |||||
Assert.assertEquals( | |||||
"Wrong FileFormat.", fileFormat, db.getFileFormat()); | |||||
assertEquals(DatabaseImpl.getFileFormatDetails(fileFormat).getFormat(), | |||||
((DatabaseImpl)db).getFormat(), | |||||
"Wrong JetFormat."); | |||||
assertEquals(fileFormat, db.getFileFormat(), "Wrong FileFormat."); | |||||
} | } | ||||
return db; | return db; | ||||
} | } | ||||
static void assertRowCount(int expectedRowCount, Table table) | static void assertRowCount(int expectedRowCount, Table table) | ||||
throws Exception | throws Exception | ||||
{ | { | ||||
Assert.assertEquals(expectedRowCount, countRows(table)); | |||||
Assert.assertEquals(expectedRowCount, table.getRowCount()); | |||||
assertEquals(expectedRowCount, countRows(table)); | |||||
assertEquals(expectedRowCount, table.getRowCount()); | |||||
} | } | ||||
public static int countRows(Table table) throws Exception { | public static int countRows(Table table) throws Exception { | ||||
for(Map<String, Object> row : cursor) { | for(Map<String, Object> row : cursor) { | ||||
foundTable.add(row); | foundTable.add(row); | ||||
} | } | ||||
Assert.assertEquals(expectedTable.size(), foundTable.size()); | |||||
assertEquals(expectedTable.size(), foundTable.size()); | |||||
for(int i = 0; i < expectedTable.size(); ++i) { | for(int i = 0; i < expectedTable.size(); ++i) { | ||||
Assert.assertEquals(expectedTable.get(i), foundTable.get(i)); | |||||
assertEquals(expectedTable.get(i), foundTable.get(i)); | |||||
} | } | ||||
} | } | ||||
Instant.ofEpochMilli(expected.getTime()), | Instant.ofEpochMilli(expected.getTime()), | ||||
ZoneId.systemDefault()); | ZoneId.systemDefault()); | ||||
Assert.assertEquals(expectedLdt, found); | |||||
assertEquals(expectedLdt, found); | |||||
} | } | ||||
public static void copyFile(File srcFile, File dstFile) | public static void copyFile(File srcFile, File dstFile) | ||||
static void checkTestDBTable1RowABCDEFG(final TestDB testDB, final Table table, final Row row) | static void checkTestDBTable1RowABCDEFG(final TestDB testDB, final Table table, final Row row) | ||||
throws IOException { | throws IOException { | ||||
Assert.assertEquals("testDB: " + testDB + "; table: " + table, "abcdefg", row.get("A")); | |||||
Assert.assertEquals("hijklmnop", row.get("B")); | |||||
Assert.assertEquals(new Byte((byte) 2), row.get("C")); | |||||
Assert.assertEquals(new Short((short) 222), row.get("D")); | |||||
Assert.assertEquals(new Integer(333333333), row.get("E")); | |||||
Assert.assertEquals(new Double(444.555d), row.get("F")); | |||||
assertEquals("abcdefg", row.get("A"), "testDB: " + testDB + "; table: " + table); | |||||
assertEquals("hijklmnop", row.get("B")); | |||||
assertEquals(new Byte((byte) 2), row.get("C")); | |||||
assertEquals(new Short((short) 222), row.get("D")); | |||||
assertEquals(new Integer(333333333), row.get("E")); | |||||
assertEquals(new Double(444.555d), row.get("F")); | |||||
final Calendar cal = Calendar.getInstance(); | final Calendar cal = Calendar.getInstance(); | ||||
cal.setTime(row.getDate("G")); | cal.setTime(row.getDate("G")); | ||||
Assert.assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH)); | |||||
Assert.assertEquals(21, cal.get(Calendar.DAY_OF_MONTH)); | |||||
Assert.assertEquals(1974, cal.get(Calendar.YEAR)); | |||||
Assert.assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); | |||||
Assert.assertEquals(0, cal.get(Calendar.MINUTE)); | |||||
Assert.assertEquals(0, cal.get(Calendar.SECOND)); | |||||
Assert.assertEquals(0, cal.get(Calendar.MILLISECOND)); | |||||
Assert.assertEquals(Boolean.TRUE, row.get("I")); | |||||
assertEquals(Calendar.SEPTEMBER, cal.get(Calendar.MONTH)); | |||||
assertEquals(21, cal.get(Calendar.DAY_OF_MONTH)); | |||||
assertEquals(1974, cal.get(Calendar.YEAR)); | |||||
assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); | |||||
assertEquals(0, cal.get(Calendar.MINUTE)); | |||||
assertEquals(0, cal.get(Calendar.SECOND)); | |||||
assertEquals(0, cal.get(Calendar.MILLISECOND)); | |||||
assertEquals(Boolean.TRUE, row.get("I")); | |||||
} | } | ||||
static void checkTestDBTable1RowA(final TestDB testDB, final Table table, final Row row) | static void checkTestDBTable1RowA(final TestDB testDB, final Table table, final Row row) | ||||
throws IOException { | throws IOException { | ||||
Assert.assertEquals("testDB: " + testDB + "; table: " + table, "a", row.get("A")); | |||||
Assert.assertEquals("b", row.get("B")); | |||||
Assert.assertEquals(new Byte((byte) 0), row.get("C")); | |||||
Assert.assertEquals(new Short((short) 0), row.get("D")); | |||||
Assert.assertEquals(new Integer(0), row.get("E")); | |||||
Assert.assertEquals(new Double(0d), row.get("F")); | |||||
assertEquals("a", row.get("A"), "testDB: " + testDB + "; table: " + table); | |||||
assertEquals("b", row.get("B")); | |||||
assertEquals(new Byte((byte) 0), row.get("C")); | |||||
assertEquals(new Short((short) 0), row.get("D")); | |||||
assertEquals(new Integer(0), row.get("E")); | |||||
assertEquals(new Double(0d), row.get("F")); | |||||
final Calendar cal = Calendar.getInstance(); | final Calendar cal = Calendar.getInstance(); | ||||
cal.setTime(row.getDate("G")); | cal.setTime(row.getDate("G")); | ||||
Assert.assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH)); | |||||
Assert.assertEquals(12, cal.get(Calendar.DAY_OF_MONTH)); | |||||
Assert.assertEquals(1981, cal.get(Calendar.YEAR)); | |||||
Assert.assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); | |||||
Assert.assertEquals(0, cal.get(Calendar.MINUTE)); | |||||
Assert.assertEquals(0, cal.get(Calendar.SECOND)); | |||||
Assert.assertEquals(0, cal.get(Calendar.MILLISECOND)); | |||||
Assert.assertEquals(Boolean.FALSE, row.get("I")); | |||||
assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH)); | |||||
assertEquals(12, cal.get(Calendar.DAY_OF_MONTH)); | |||||
assertEquals(1981, cal.get(Calendar.YEAR)); | |||||
assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); | |||||
assertEquals(0, cal.get(Calendar.MINUTE)); | |||||
assertEquals(0, cal.get(Calendar.SECOND)); | |||||
assertEquals(0, cal.get(Calendar.MILLISECOND)); | |||||
assertEquals(Boolean.FALSE, row.get("I")); | |||||
} | } | ||||
} | } |
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; | import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class AutoNumberTest extends TestCase | |||||
public class AutoNumberTest | |||||
{ | { | ||||
public AutoNumberTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testAutoNumber() throws Exception | public void testAutoNumber() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAutoNumberPK() throws Exception | public void testAutoNumberPK() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST) { | for (final TestDB testDB : SUPPORTED_DBS_TEST) { | ||||
assertTable(expectedRows, table); | assertTable(expectedRows, table); | ||||
} | } | ||||
@Test | |||||
public void testAutoNumberGuid() throws Exception | public void testAutoNumberGuid() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testInsertLongAutoNumber() throws Exception | public void testInsertLongAutoNumber() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testInsertLongAutoNumberPK() throws Exception | public void testInsertLongAutoNumberPK() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
assertEquals(13, ((TableImpl)table).getLastLongAutoNumber()); | assertEquals(13, ((TableImpl)table).getLastLongAutoNumber()); | ||||
try { | |||||
table.addRow("not a number", "nope"); | |||||
fail("NumberFormatException should have been thrown"); | |||||
} catch(NumberFormatException e) { | |||||
// success | |||||
} | |||||
assertThrows(NumberFormatException.class, () -> table.addRow("not a number", "nope")); | |||||
assertEquals(13, ((TableImpl)table).getLastLongAutoNumber()); | assertEquals(13, ((TableImpl)table).getLastLongAutoNumber()); | ||||
} | } | ||||
@Test | |||||
public void testInsertComplexAutoNumber() throws Exception | public void testInsertComplexAutoNumber() throws Exception | ||||
{ | { | ||||
for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { | ||||
assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
try { | |||||
t1.addRow("nope", "not a number"); | |||||
fail("NumberFormatException should have been thrown"); | |||||
} catch(NumberFormatException e) { | |||||
// success | |||||
} | |||||
assertThrows(NumberFormatException.class, () -> t1.addRow("nope", "not a number")); | |||||
assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
try { | |||||
t1.addRow("uh-uh", -10); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> t1.addRow("uh-uh", -10)); | |||||
assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(13, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
try { | |||||
t1.addRow("wut", 6, null, null, 40, 42); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> t1.addRow("wut", 6, null, null, 40, 42)); | |||||
row = t1.addRow("morerows"); | row = t1.addRow("morerows"); | ||||
checkAllComplexAutoNums(14, row); | checkAllComplexAutoNums(14, row); | ||||
assertEquals(14, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(14, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
Row row13 = CursorBuilder.findRow( | |||||
t1, Collections.singletonMap("id", "row13")); | |||||
final Row row13 = CursorBuilder.findRow(t1, Collections.singletonMap("id", "row13")); | |||||
row13.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", "45"); | row13.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", "45"); | ||||
row13.put("multi-value-data", "45"); | row13.put("multi-value-data", "45"); | ||||
row13.put("attach-data", "45"); | row13.put("attach-data", "45"); | ||||
row13 = t1.updateRow(row13); | |||||
checkAllComplexAutoNums(45, row13); | |||||
final Row row14 = t1.updateRow(row13); | |||||
checkAllComplexAutoNums(45, row14); | |||||
assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
row13.put("attach-data", -1); | |||||
row14.put("attach-data", -1); | |||||
try { | |||||
t1.updateRow(row13); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> t1.updateRow(row14)); | |||||
assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
row13.put("attach-data", 55); | |||||
row14.put("attach-data", 55); | |||||
try { | |||||
t1.updateRow(row13); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> t1.updateRow(row14)); | |||||
assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
row13.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", 55); | |||||
row13.put("multi-value-data", 55); | |||||
row14.put("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880", 55); | |||||
row14.put("multi-value-data", 55); | |||||
db.setAllowAutoNumberInsert(null); | db.setAllowAutoNumberInsert(null); | ||||
row13 = t1.updateRow(row13); | |||||
checkAllComplexAutoNums(45, row13); | |||||
final Row row15 = t1.updateRow(row14); | |||||
checkAllComplexAutoNums(45, row15); | |||||
assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | assertEquals(45, ((TableImpl)t1).getLastComplexTypeAutoNumber()); | ||||
private static void checkAllComplexAutoNums(int expected, Row row) | private static void checkAllComplexAutoNums(int expected, Row row) | ||||
{ | { | ||||
assertEquals(expected, ((Number)row.get("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880")).intValue()); | |||||
assertEquals(expected, ((Number)row.get("multi-value-data")).intValue()); | |||||
assertEquals(expected, ((Number)row.get("attach-data")).intValue()); | |||||
assertEquals(expected, ((Number)row.get("VersionHistory_F5F8918F-0A3F-4DA9-AE71-184EE5012880")).intValue()); | |||||
assertEquals(expected, ((Number)row.get("multi-value-data")).intValue()); | |||||
assertEquals(expected, ((Number)row.get("attach-data")).intValue()); | |||||
} | } | ||||
@Test | |||||
public void testInsertGuidAutoNumber() throws Exception | public void testInsertGuidAutoNumber() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = createMem(fileFormat); | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = createMem(fileFormat)) | |||||
{ | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("a", DataType.GUID) | |||||
.setAutoNumber(true)) | |||||
.addColumn(newColumn("b", DataType.TEXT)) | |||||
.toTable(db); | |||||
Table table = newTable("test") | |||||
.addColumn(newColumn("a", DataType.GUID) | |||||
.setAutoNumber(true)) | |||||
.addColumn(newColumn("b", DataType.TEXT)) | |||||
.toTable(db); | |||||
db.setAllowAutoNumberInsert(true); | |||||
table.setAllowAutoNumberInsert(false); | |||||
assertFalse(table.isAllowAutoNumberInsert()); | |||||
db.setAllowAutoNumberInsert(true); | |||||
table.setAllowAutoNumberInsert(false); | |||||
assertFalse(table.isAllowAutoNumberInsert()); | |||||
Object[] row = {null, "row1"}; | |||||
assertSame(row, table.addRow(row)); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow(13, "row2"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow("flubber", "row3"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
Object[] row = | |||||
{ | |||||
null, "row1" | |||||
}; | |||||
assertSame(row, table.addRow(row)); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow(13, "row2"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow("flubber", "row3"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
Object[] smallRow = {Column.AUTO_NUMBER}; | |||||
row = table.addRow(smallRow); | |||||
assertNotSame(row, smallRow); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
Object[] smallRow = | |||||
{ | |||||
Column.AUTO_NUMBER | |||||
}; | |||||
row = table.addRow(smallRow); | |||||
assertNotSame(row, smallRow); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
table.setAllowAutoNumberInsert(null); | |||||
assertTrue(table.isAllowAutoNumberInsert()); | |||||
table.setAllowAutoNumberInsert(null); | |||||
assertTrue(table.isAllowAutoNumberInsert()); | |||||
Row row2 = CursorBuilder.findRow( | |||||
table, Collections.singletonMap("b", "row2")); | |||||
assertEquals("row2", row2.getString("b")); | |||||
Row row2 = CursorBuilder.findRow( | |||||
table, Collections.singletonMap("b", "row2")); | |||||
assertEquals("row2", row2.getString("b")); | |||||
String row2Guid = row2.getString("a"); | |||||
table.deleteRow(row2); | |||||
String row2Guid = row2.getString("a"); | |||||
table.deleteRow(row2); | |||||
row = table.addRow(Column.AUTO_NUMBER, "row4"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow(Column.AUTO_NUMBER, "row4"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow(row2Guid, "row2-redux"); | |||||
assertEquals(row2Guid, row[0]); | |||||
row = table.addRow(row2Guid, "row2-redux"); | |||||
assertEquals(row2Guid, row[0]); | |||||
row2 = CursorBuilder.findRow( | |||||
table, Collections.singletonMap("a", row2Guid)); | |||||
assertEquals("row2-redux", row2.getString("b")); | |||||
row2 = CursorBuilder.findRow( | |||||
table, Collections.singletonMap("a", row2Guid)); | |||||
assertEquals("row2-redux", row2.getString("b")); | |||||
try { | |||||
table.addRow("not a guid", "nope"); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> table.addRow("not a guid", "nope")); | |||||
row = table.addRow(Column.AUTO_NUMBER, "row5"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row = table.addRow(Column.AUTO_NUMBER, "row5"); | |||||
assertTrue(ColumnImpl.isGUIDValue(row[0])); | |||||
row2Guid = UUID.randomUUID().toString(); | |||||
row2.put("a", row2Guid); | |||||
row2Guid = UUID.randomUUID().toString(); | |||||
row2.put("a", row2Guid); | |||||
row2 = table.updateRow(row2); | |||||
assertEquals(row2Guid, row2.get("a")); | |||||
final Row row3 = table.updateRow(row2); | |||||
assertEquals(row2Guid, row3.get("a")); | |||||
row2.put("a", "not a guid"); | |||||
row3.put("a", "not a guid"); | |||||
try { | |||||
table.updateRow(row2); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertThrows(IOException.class, () -> table.updateRow(row3)); | |||||
table.setAllowAutoNumberInsert(false); | |||||
table.setAllowAutoNumberInsert(false); | |||||
row2 = table.updateRow(row2); | |||||
assertTrue(ColumnImpl.isGUIDValue(row2.get("a"))); | |||||
assertFalse(row2Guid.equals(row2.get("a"))); | |||||
final Row row4 = table.updateRow(row3); | |||||
assertTrue(ColumnImpl.isGUIDValue(row4.get("a"))); | |||||
assertFalse(row2Guid.equals(row4.get("a"))); | |||||
db.close(); | |||||
} | |||||
} | } | ||||
} | } | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.Comparator; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import com.healthmarketscience.jackcess.DataType; | import com.healthmarketscience.jackcess.DataType; | ||||
import com.healthmarketscience.jackcess.Database; | import com.healthmarketscience.jackcess.Database; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | import static com.healthmarketscience.jackcess.DatabaseBuilder.*; | ||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class BigIntTest extends TestCase | |||||
public class BigIntTest | |||||
{ | { | ||||
public BigIntTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testBigInt() throws Exception { | public void testBigInt() throws Exception { | ||||
for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) { |
import com.healthmarketscience.jackcess.Row; | import com.healthmarketscience.jackcess.Row; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class CalcFieldTest extends TestCase | |||||
public class CalcFieldTest | |||||
{ | { | ||||
public CalcFieldTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testCreateCalcField() throws Exception { | public void testCreateCalcField() throws Exception { | ||||
ColumnBuilder cb = new ColumnBuilder("calc_data", DataType.TEXT) | ColumnBuilder cb = new ColumnBuilder("calc_data", DataType.TEXT) | ||||
.setCalculatedInfo("[id] & \"_\" & [data]"); | .setCalculatedInfo("[id] & \"_\" & [data]"); | ||||
try { | |||||
cb.validate(JetFormat.VERSION_12); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
assertTrue(e.getMessage().contains("")); | |||||
} | |||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, | |||||
() -> cb.validate(JetFormat.VERSION_12)); | |||||
assertTrue(e.getMessage().contains("")); | |||||
cb.validate(JetFormat.VERSION_14); | cb.validate(JetFormat.VERSION_14); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReadCalcFields() throws Exception { | public void testReadCalcFields() throws Exception { | ||||
for(TestDB testDB : TestDB.getSupportedForBasename(Basename.CALC_FIELD)) { | for(TestDB testDB : TestDB.getSupportedForBasename(Basename.CALC_FIELD)) { |
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import com.healthmarketscience.jackcess.TestUtil; | import com.healthmarketscience.jackcess.TestUtil; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class CodecHandlerTest extends TestCase | |||||
public class CodecHandlerTest | |||||
{ | { | ||||
private static final CodecProvider SIMPLE_PROVIDER = new CodecProvider() { | private static final CodecProvider SIMPLE_PROVIDER = new CodecProvider() { | ||||
public CodecHandler createHandler(PageChannel channel, Charset charset) | public CodecHandler createHandler(PageChannel channel, Charset charset) | ||||
}; | }; | ||||
public CodecHandlerTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testCodecHandler() throws Exception | public void testCodecHandler() throws Exception | ||||
{ | { | ||||
doTestCodecHandler(true); | doTestCodecHandler(true); |
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import com.healthmarketscience.jackcess.util.RowFilterTest; | import com.healthmarketscience.jackcess.util.RowFilterTest; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class DatabaseReadWriteTest extends TestCase | |||||
public class DatabaseReadWriteTest | |||||
{ | { | ||||
public DatabaseReadWriteTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testWriteAndRead() throws Exception { | public void testWriteAndRead() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteAndReadInMem() throws Exception { | public void testWriteAndReadInMem() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = createMem(fileFormat); | Database db = createMem(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteAndReadInBatch() throws Exception { | public void testWriteAndReadInBatch() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = createMem(fileFormat); | Database db = createMem(fileFormat); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testUpdateRow() throws Exception | public void testUpdateRow() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testDateMath() | public void testDateMath() | ||||
{ | { | ||||
long now = System.currentTimeMillis(); | long now = System.currentTimeMillis(); |
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import org.junit.Assert; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ExtendedDateTest extends TestCase | |||||
public class ExtendedDateTest | |||||
{ | { | ||||
public ExtendedDateTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testReadExtendedDate() throws Exception { | public void testReadExtendedDate() throws Exception { | ||||
DateTimeFormatter dtfNoTime = DateTimeFormatter.ofPattern("M/d/yyy"); | DateTimeFormatter dtfNoTime = DateTimeFormatter.ofPattern("M/d/yyy"); | ||||
String str1 = dtfNoTime.format(ldt); | String str1 = dtfNoTime.format(ldt); | ||||
String str2 = dtfFull.format(ldt); | String str2 = dtfFull.format(ldt); | ||||
Assert.assertTrue(str1.equals(str) || str2.equals(str)); | |||||
assertTrue(str1.equals(str) || str2.equals(str)); | |||||
} else { | } else { | ||||
Assert.assertNull(str); | |||||
assertNull(str); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testWriteExtendedDate() throws Exception { | public void testWriteExtendedDate() throws Exception { | ||||
for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) { |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
See the License for the specific language governing permissions and | See the License for the specific language governing permissions and | ||||
limitations under the License. | limitations under the License. | ||||
*/ | |||||
*/ | |||||
package com.healthmarketscience.jackcess.impl; | package com.healthmarketscience.jackcess.impl; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import com.healthmarketscience.jackcess.Row; | import com.healthmarketscience.jackcess.Row; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class FKEnforcerTest extends TestCase | |||||
public class FKEnforcerTest | |||||
{ | { | ||||
public FKEnforcerTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
public void testNoEnforceForeignKeys() throws Exception { | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | |||||
@Test | |||||
public void testNoEnforceForeignKeys() throws Exception | |||||
{ | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) | |||||
{ | |||||
Database db = openCopy(testDB); | Database db = openCopy(testDB); | ||||
db.setEnforceForeignKeys(false); | db.setEnforceForeignKeys(false); | ||||
} | } | ||||
public void testEnforceForeignKeys() throws Exception { | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | |||||
Database db = openCopy(testDB); | |||||
db.setEvaluateExpressions(false); | |||||
Table t1 = db.getTable("Table1"); | |||||
Table t2 = db.getTable("Table2"); | |||||
Table t3 = db.getTable("Table3"); | |||||
try { | |||||
t1.addRow(20, 0, 20, "some data", 20); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException ignored) { | |||||
// success | |||||
assertTrue(ignored.getMessage().contains("Table1[otherfk2]")); | |||||
} | |||||
try { | |||||
Cursor c = CursorBuilder.createCursor(t2); | |||||
c.moveToNextRow(); | |||||
c.updateCurrentRow(30, "foo30"); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException ignored) { | |||||
// success | |||||
assertTrue(ignored.getMessage().contains("Table2[id]")); | |||||
} | |||||
@Test | |||||
public void testEnforceForeignKeys() throws Exception | |||||
{ | |||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) | |||||
{ | |||||
try (final Database db = openCopy(testDB)) | |||||
{ | |||||
db.setEvaluateExpressions(false); | |||||
Table t1 = db.getTable("Table1"); | |||||
Table t2 = db.getTable("Table2"); | |||||
Table t3 = db.getTable("Table3"); | |||||
IOException ioe = assertThrows(IOException.class, () -> t1.addRow(20, 0, 20, "some data", 20)); | |||||
assertTrue(ioe.getMessage().contains("Table1[otherfk2]")); | |||||
ioe = assertThrows(IOException.class, | |||||
() -> | |||||
{ | |||||
Cursor c = CursorBuilder.createCursor(t2); | |||||
c.moveToNextRow(); | |||||
c.updateCurrentRow(30, "foo30"); | |||||
}); | |||||
assertTrue(ioe.getMessage().contains("Table2[id]")); | |||||
ioe = assertThrows(IOException.class, | |||||
() -> | |||||
{ | |||||
Cursor c = CursorBuilder.createCursor(t3); | |||||
c.moveToNextRow(); | |||||
c.deleteCurrentRow(); | |||||
fail("IOException should have been thrown"); | |||||
}); | |||||
assertTrue(ioe.getMessage().contains("Table3[id]")); | |||||
t1.addRow(21, null, null, "null fks", null); | |||||
try { | |||||
Cursor c = CursorBuilder.createCursor(t3); | Cursor c = CursorBuilder.createCursor(t3); | ||||
c.moveToNextRow(); | |||||
c.deleteCurrentRow(); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException ignored) { | |||||
// success | |||||
assertTrue(ignored.getMessage().contains("Table3[id]")); | |||||
} | |||||
Column col = t3.getColumn("id"); | |||||
for (Row row : c) | |||||
{ | |||||
int id = row.getInt("id"); | |||||
id += 20; | |||||
c.setCurrentRowValue(col, id); | |||||
} | |||||
List<? extends Map<String, Object>> expectedRows | |||||
= createExpectedTable( | |||||
createT1Row(0, 0, 30, "baz0", 0), | |||||
createT1Row(1, 1, 31, "baz11", 0), | |||||
createT1Row(2, 1, 31, "baz11-2", 0), | |||||
createT1Row(3, 2, 33, "baz13", 0), | |||||
createT1Row(21, null, null, "null fks", null)); | |||||
assertTable(expectedRows, t1); | |||||
c = CursorBuilder.createCursor(t2); | |||||
for (Iterator<?> iter = c.iterator(); iter.hasNext();) | |||||
{ | |||||
iter.next(); | |||||
iter.remove(); | |||||
} | |||||
assertEquals(1, t1.getRowCount()); | |||||
t1.addRow(21, null, null, "null fks", null); | |||||
Cursor c = CursorBuilder.createCursor(t3); | |||||
Column col = t3.getColumn("id"); | |||||
for(Row row : c) { | |||||
int id = row.getInt("id"); | |||||
id += 20; | |||||
c.setCurrentRowValue(col, id); | |||||
} | } | ||||
List<? extends Map<String, Object>> expectedRows = | |||||
createExpectedTable( | |||||
createT1Row(0, 0, 30, "baz0", 0), | |||||
createT1Row(1, 1, 31, "baz11", 0), | |||||
createT1Row(2, 1, 31, "baz11-2", 0), | |||||
createT1Row(3, 2, 33, "baz13", 0), | |||||
createT1Row(21, null, null, "null fks", null)); | |||||
assertTable(expectedRows, t1); | |||||
c = CursorBuilder.createCursor(t2); | |||||
for(Iterator<?> iter = c.iterator(); iter.hasNext(); ) { | |||||
iter.next(); | |||||
iter.remove(); | |||||
} | |||||
assertEquals(1, t1.getRowCount()); | |||||
db.close(); | |||||
} | } | ||||
} | } | ||||
private static Row createT1Row( | private static Row createT1Row( | ||||
int id1, Integer fk1, Integer fk2, String data, Integer fk3) | |||||
int id1, Integer fk1, Integer fk2, String data, Integer fk3) | |||||
{ | { | ||||
return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2, | return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2, | ||||
"data", data, "otherfk3", fk3); | "data", data, "otherfk3", fk3); |
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class IndexCodesTest extends TestCase { | |||||
private static final Map<Character,String> SPECIAL_CHARS = | |||||
new HashMap<Character,String>(); | |||||
public class IndexCodesTest | |||||
{ | |||||
private static final Map<Character,String> SPECIAL_CHARS = new HashMap<>(); | |||||
static { | static { | ||||
SPECIAL_CHARS.put('\b', "\\b"); | SPECIAL_CHARS.put('\b', "\\b"); | ||||
SPECIAL_CHARS.put('\t', "\\t"); | SPECIAL_CHARS.put('\t', "\\t"); | ||||
SPECIAL_CHARS.put('\\', "\\\\"); | SPECIAL_CHARS.put('\\', "\\\\"); | ||||
} | } | ||||
public IndexCodesTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testIndexCodes() throws Exception | public void testIndexCodes() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_CODES, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_CODES, true)) { | ||||
// | // | ||||
////// | ////// | ||||
@Test | |||||
public void testNothing() throws Exception { | public void testNothing() throws Exception { | ||||
// keep this so build doesn't fail if other tests are disabled | // keep this so build doesn't fail if other tests are disabled | ||||
} | } |
import static com.healthmarketscience.jackcess.Database.*; | import static com.healthmarketscience.jackcess.Database.*; | ||||
import com.healthmarketscience.jackcess.DatabaseBuilder; | import com.healthmarketscience.jackcess.DatabaseBuilder; | ||||
import com.healthmarketscience.jackcess.PropertyMap; | import com.healthmarketscience.jackcess.PropertyMap; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* Date: Mar 5, 2010 | * Date: Mar 5, 2010 | ||||
* Time: 12:44:21 PM | * Time: 12:44:21 PM | ||||
*/ | */ | ||||
public class JetFormatTest extends TestCase { | |||||
public class JetFormatTest | |||||
{ | |||||
public static final File DIR_TEST_DATA = new File("src/test/data"); | public static final File DIR_TEST_DATA = new File("src/test/data"); | ||||
/** | /** | ||||
TestDB.getSupportedForBasename(Basename.TEST, true); | TestDB.getSupportedForBasename(Basename.TEST, true); | ||||
public void testGetFormat() throws Exception { | |||||
try { | |||||
JetFormat.getFormat(null); | |||||
fail("npe"); | |||||
} catch (NullPointerException e) { | |||||
// success | |||||
} | |||||
@Test | |||||
public void testGetFormat() throws Exception | |||||
{ | |||||
assertThrows(NullPointerException.class, () -> JetFormat.getFormat(null)); | |||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | |||||
final FileChannel channel = DatabaseImpl.openChannel( | |||||
testDB.dbFile.toPath(), false, false); | |||||
try { | |||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) | |||||
{ | |||||
final FileChannel channel = DatabaseImpl.openChannel(testDB.dbFile.toPath(), false, false); | |||||
try | |||||
{ | |||||
JetFormat fmtActual = JetFormat.getFormat(channel); | JetFormat fmtActual = JetFormat.getFormat(channel); | ||||
assertEquals("Unexpected JetFormat for dbFile: " + | |||||
testDB.dbFile.getAbsolutePath(), | |||||
testDB.getExpectedFormat(), fmtActual); | |||||
} finally { | |||||
assertEquals(testDB.getExpectedFormat(), | |||||
fmtActual, | |||||
"Unexpected JetFormat for dbFile: " + testDB.dbFile.getAbsolutePath()); | |||||
} | |||||
finally | |||||
{ | |||||
channel.close(); | channel.close(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReadOnlyFormat() throws Exception { | public void testReadOnlyFormat() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testFileFormat() throws Exception { | public void testFileFormat() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testSqlTypes() throws Exception { | public void testSqlTypes() throws Exception { | ||||
JetFormat v2000 = JetFormat.VERSION_4; | JetFormat v2000 = JetFormat.VERSION_4; |
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class LongValueTest extends TestCase | |||||
public class LongValueTest | |||||
{ | { | ||||
public LongValueTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testReadLongValue() throws Exception { | public void testReadLongValue() throws Exception { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.TEST2, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.TEST2, true)) { |
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class TopoSorterTest extends TestCase | |||||
public class TopoSorterTest | |||||
{ | { | ||||
public TopoSorterTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testTopoSort() throws Exception | public void testTopoSort() throws Exception | ||||
{ | { | ||||
doTopoTest(Arrays.asList("A", "B", "C"), | doTopoTest(Arrays.asList("A", "B", "C"), | ||||
"B", "C", | "B", "C", | ||||
"A", "B"); | "A", "B"); | ||||
try { | |||||
doTopoTest(Arrays.asList("B", "A", "C"), | |||||
Arrays.asList("C", "B", "A"), | |||||
"B", "C", | |||||
"A", "B", | |||||
"C", "A"); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException expected) { | |||||
// success | |||||
assertTrue(expected.getMessage().startsWith("Cycle")); | |||||
} | |||||
IllegalStateException expected = assertThrows(IllegalStateException.class, | |||||
() -> doTopoTest(Arrays.asList("B", "A", "C"), | |||||
Arrays.asList("C", "B", "A"), | |||||
"B", "C", | |||||
"A", "B", | |||||
"C", "A")); | |||||
assertTrue(expected.getMessage().startsWith("Cycle")); | |||||
try { | |||||
doTopoTest(Arrays.asList("B", "A", "C"), | |||||
Arrays.asList("C", "B", "A"), | |||||
"B", "D"); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException expected) { | |||||
// success | |||||
assertTrue(expected.getMessage().startsWith("Unknown descendent")); | |||||
} | |||||
expected = assertThrows(IllegalStateException.class, | |||||
() -> doTopoTest(Arrays.asList("B", "A", "C"), | |||||
Arrays.asList("C", "B", "A"), | |||||
"B", "D")); | |||||
assertTrue(expected.getMessage().startsWith("Unknown descendent")); | |||||
doTopoTest(Arrays.asList("B", "D", "A", "C"), | doTopoTest(Arrays.asList("B", "D", "A", "C"), | ||||
Arrays.asList("D", "A", "B", "C"), | Arrays.asList("D", "A", "B", "C"), | ||||
List<String> expected, | List<String> expected, | ||||
String... descs) { | String... descs) { | ||||
List<String> values = new ArrayList<String>(); | |||||
List<String> values = new ArrayList<>(); | |||||
values.addAll(original); | values.addAll(original); | ||||
TestTopoSorter tsorter = new TestTopoSorter(values, false); | TestTopoSorter tsorter = new TestTopoSorter(values, false); |
import com.healthmarketscience.jackcess.DatabaseBuilder; | import com.healthmarketscience.jackcess.DatabaseBuilder; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author Dan Rollo | * @author Dan Rollo | ||||
* Date: Mar 5, 2010 | * Date: Mar 5, 2010 | ||||
* Time: 2:21:22 PM | * Time: 2:21:22 PM | ||||
*/ | */ | ||||
public final class UsageMapTest extends TestCase { | |||||
public final class UsageMapTest | |||||
{ | |||||
@Test | |||||
public void testRead() throws Exception { | public void testRead() throws Exception { | ||||
for (final TestDB testDB : SUPPORTED_DBS_TEST) { | for (final TestDB testDB : SUPPORTED_DBS_TEST) { | ||||
final int expectedFirstPage; | final int expectedFirstPage; | ||||
PageChannel.PAGE_GLOBAL_USAGE_MAP, | PageChannel.PAGE_GLOBAL_USAGE_MAP, | ||||
PageChannel.ROW_GLOBAL_USAGE_MAP, | PageChannel.ROW_GLOBAL_USAGE_MAP, | ||||
true); | true); | ||||
assertEquals("Unexpected FirstPageNumber.", expectedFirstPage, | |||||
usageMap.getFirstPageNumber()); | |||||
assertEquals("Unexpected LastPageNumber.", expectedLastPage, | |||||
usageMap.getLastPageNumber()); | |||||
assertEquals(expectedFirstPage, usageMap.getFirstPageNumber(), "Unexpected FirstPageNumber."); | |||||
assertEquals(expectedLastPage, usageMap.getLastPageNumber(), "Unexpected LastPageNumber."); | |||||
} | } | ||||
@Test | |||||
public void testGobalReferenceUsageMap() throws Exception | public void testGobalReferenceUsageMap() throws Exception | ||||
{ | { | ||||
Database db = openCopy( | Database db = openCopy( |
import java.util.Calendar; | import java.util.Calendar; | ||||
import com.healthmarketscience.jackcess.expr.EvalException; | import com.healthmarketscience.jackcess.expr.EvalException; | ||||
import junit.framework.AssertionFailedError; | |||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.eval; | import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.eval; | ||||
import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.toBD; | import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.toBD; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
import org.opentest4j.AssertionFailedError; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class DefaultFunctionsTest extends TestCase | |||||
public class DefaultFunctionsTest | |||||
{ | { | ||||
public DefaultFunctionsTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testFuncs() throws Exception | public void testFuncs() throws Exception | ||||
{ | { | ||||
assertEval("foo", "=IIf(10 > 1, \"foo\", \"bar\")"); | assertEval("foo", "=IIf(10 > 1, \"foo\", \"bar\")"); | ||||
assertEval("-42", "=Str$(-42)"); | assertEval("-42", "=Str$(-42)"); | ||||
assertNull(eval("=Str(Null)")); | assertNull(eval("=Str(Null)")); | ||||
try { | |||||
eval("=Str$(Null)"); | |||||
fail("EvalException should have been thrown"); | |||||
} catch(EvalException expected) { | |||||
// success | |||||
} | |||||
assertThrows(EvalException.class, () -> eval("=Str$(Null)")); | |||||
assertEval(-1, "=CBool(\"1\")"); | assertEval(-1, "=CBool(\"1\")"); | ||||
assertEval(13, "=CByte(\"13\")"); | assertEval(13, "=CByte(\"13\")"); | ||||
assertEval("bar", "=Switch(False,'foo', True, 'bar', True, 'blah')"); | assertEval("bar", "=Switch(False,'foo', True, 'bar', True, 'blah')"); | ||||
assertEval("blah", "=Switch(False,'foo', False, 'bar', True, 'blah')"); | assertEval("blah", "=Switch(False,'foo', False, 'bar', True, 'blah')"); | ||||
try { | |||||
eval("=StrReverse('blah', 1)"); | |||||
fail("EvalException should have been thrown"); | |||||
} catch(EvalException e) { | |||||
assertTrue(e.getMessage().contains("Invalid function call")); | |||||
} | |||||
EvalException e = assertThrows(EvalException.class, () -> eval("=StrReverse('blah', 1)")); | |||||
assertTrue(e.getMessage().contains("Invalid function call")); | |||||
try { | |||||
eval("=StrReverse()"); | |||||
fail("EvalException should have been thrown"); | |||||
} catch(EvalException e) { | |||||
assertTrue(e.getMessage().contains("Invalid function call")); | |||||
} | |||||
e = assertThrows(EvalException.class, () -> eval("=StrReverse()")); | |||||
assertTrue(e.getMessage().contains("Invalid function call")); | |||||
assertEval(1615198d, "=Val(' 1615 198th Street N.E.')"); | assertEval(1615198d, "=Val(' 1615 198th Street N.E.')"); | ||||
assertEval(-1d, "=Val(' &HFFFFwhatever')"); | assertEval(-1d, "=Val(' &HFFFFwhatever')"); | ||||
assertEval("13:37", "=FormatDateTime(#1/1/1973 1:37:25 PM#,4)"); | assertEval("13:37", "=FormatDateTime(#1/1/1973 1:37:25 PM#,4)"); | ||||
} | } | ||||
@Test | |||||
public void testFormat() throws Exception | public void testFormat() throws Exception | ||||
{ | { | ||||
assertEval("12345.6789", "=Format(12345.6789, 'General Number')"); | assertEval("12345.6789", "=Format(12345.6789, 'General Number')"); | ||||
assertEval("19:00", "=Format(#01/02/2003 7:00:00 PM#, 'Short Time')"); | assertEval("19:00", "=Format(#01/02/2003 7:00:00 PM#, 'Short Time')"); | ||||
} | } | ||||
@Test | |||||
public void testCustomFormat() throws Exception | public void testCustomFormat() throws Exception | ||||
{ | { | ||||
assertEval("07:00 a", "=Format(#01/10/2003 7:00:00 AM#, 'hh:nn a/p')"); | assertEval("07:00 a", "=Format(#01/10/2003 7:00:00 AM#, 'hh:nn a/p')"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testNumberFuncs() throws Exception | public void testNumberFuncs() throws Exception | ||||
{ | { | ||||
assertEval(1, "=Abs(1)"); | assertEval(1, "=Abs(1)"); | ||||
assertEval(-4, "=Round(-4, 2)"); | assertEval(-4, "=Round(-4, 2)"); | ||||
} | } | ||||
@Test | |||||
public void testDateFuncs() throws Exception | public void testDateFuncs() throws Exception | ||||
{ | { | ||||
assertEval("1/2/2003", "=CStr(DateValue(#01/02/2003 7:00:00 AM#))"); | assertEval("1/2/2003", "=CStr(DateValue(#01/02/2003 7:00:00 AM#))"); | ||||
assertEval(-83421497, "=DateDiff('s',#11/3/2018 2:15:30 PM#,#3/13/2016 1:37:13 AM#)"); | assertEval(-83421497, "=DateDiff('s',#11/3/2018 2:15:30 PM#,#3/13/2016 1:37:13 AM#)"); | ||||
} | } | ||||
@Test | |||||
public void testFinancialFuncs() throws Exception | public void testFinancialFuncs() throws Exception | ||||
{ | { | ||||
assertEval("-9.57859403981306", "=CStr(NPer(0.12/12,-100,-1000))"); | assertEval("-9.57859403981306", "=CStr(NPer(0.12/12,-100,-1000))"); |
import com.healthmarketscience.jackcess.expr.TemporalConfig; | import com.healthmarketscience.jackcess.expr.TemporalConfig; | ||||
import com.healthmarketscience.jackcess.expr.Value; | import com.healthmarketscience.jackcess.expr.Value; | ||||
import com.healthmarketscience.jackcess.impl.BaseEvalContext; | import com.healthmarketscience.jackcess.impl.BaseEvalContext; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ExpressionatorTest extends TestCase | |||||
public class ExpressionatorTest | |||||
{ | { | ||||
private static final double[] DBLS = { | private static final double[] DBLS = { | ||||
-10.3d,-9.0d,-8.234d,-7.11111d,-6.99999d,-5.5d,-4.0d,-3.4159265d,-2.84d, | -10.3d,-9.0d,-8.234d,-7.11111d,-6.99999d,-5.5d,-4.0d,-3.4159265d,-2.84d, | ||||
private static final int TRUE_NUM = -1; | private static final int TRUE_NUM = -1; | ||||
private static final int FALSE_NUM = 0; | private static final int FALSE_NUM = 0; | ||||
public ExpressionatorTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testParseSimpleExprs() throws Exception | public void testParseSimpleExprs() throws Exception | ||||
{ | { | ||||
validateExpr("\"A\"", "<ELiteralValue>{\"A\"}"); | validateExpr("\"A\"", "<ELiteralValue>{\"A\"}"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testOrderOfOperations() throws Exception | public void testOrderOfOperations() throws Exception | ||||
{ | { | ||||
validateExpr("\"A\" Eqv \"B\"", | validateExpr("\"A\" Eqv \"B\"", | ||||
} | } | ||||
@Test | |||||
public void testSimpleMathExpressions() throws Exception | public void testSimpleMathExpressions() throws Exception | ||||
{ | { | ||||
for(int i = -10; i <= 10; ++i) { | for(int i = -10; i <= 10; ++i) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testComparison() throws Exception | public void testComparison() throws Exception | ||||
{ | { | ||||
assertEquals(TRUE_NUM, eval("='blah'<'fuzz'")); | assertEquals(TRUE_NUM, eval("='blah'<'fuzz'")); | ||||
assertEquals(TRUE_NUM, eval("=Not(True Eqv False)")); | assertEquals(TRUE_NUM, eval("=Not(True Eqv False)")); | ||||
} | } | ||||
@Test | |||||
public void testDateArith() throws Exception | public void testDateArith() throws Exception | ||||
{ | { | ||||
assertEquals(LocalDateTime.of(2003,1,2,7,0), eval("=#01/02/2003# + #7:00:00 AM#")); | assertEquals(LocalDateTime.of(2003,1,2,7,0), eval("=#01/02/2003# + #7:00:00 AM#")); | ||||
assertEquals("1/2/2003 1:10:00 PM", eval("=CStr(#01/02/2003# + #13:10:00#)")); | assertEquals("1/2/2003 1:10:00 PM", eval("=CStr(#01/02/2003# + #13:10:00#)")); | ||||
} | } | ||||
@Test | |||||
public void testNull() throws Exception | public void testNull() throws Exception | ||||
{ | { | ||||
assertNull(eval("=37 + Null")); | assertNull(eval("=37 + Null")); | ||||
assertNull(eval("=Null In (23, Null, 45)")); | assertNull(eval("=Null In (23, Null, 45)")); | ||||
} | } | ||||
@Test | |||||
public void testTrickyMathExpressions() throws Exception | public void testTrickyMathExpressions() throws Exception | ||||
{ | { | ||||
assertEquals(37, eval("=30+7")); | assertEquals(37, eval("=30+7")); | ||||
assertEquals(toBD(-101d), eval("=-10E-1-10e+1")); | assertEquals(toBD(-101d), eval("=-10E-1-10e+1")); | ||||
} | } | ||||
@Test | |||||
public void testTypeCoercion() throws Exception | public void testTypeCoercion() throws Exception | ||||
{ | { | ||||
assertEquals("foobar", eval("=\"foo\" + \"bar\"")); | assertEquals("foobar", eval("=\"foo\" + \"bar\"")); | ||||
assertEquals(128208, eval("=#1/1/2017# * 3")); | assertEquals(128208, eval("=#1/1/2017# * 3")); | ||||
} | } | ||||
@Test | |||||
public void testLikeExpression() throws Exception | public void testLikeExpression() throws Exception | ||||
{ | { | ||||
validateExpr("Like \"[abc]*\"", "<ELikeOp>{<EThisValue>{<THIS_COL>} Like \"[abc]*\"([abc].*)}", | validateExpr("Like \"[abc]*\"", "<ELikeOp>{<EThisValue>{<THIS_COL>} Like \"[abc]*\"([abc].*)}", | ||||
assertFalse(evalCondition("Like \"[abc*\"", "")); | assertFalse(evalCondition("Like \"[abc*\"", "")); | ||||
} | } | ||||
@Test | |||||
public void testLiteralDefaultValue() throws Exception | public void testLiteralDefaultValue() throws Exception | ||||
{ | { | ||||
assertEquals("-28 blah ", eval("=CDbl(9)-37 & \" blah \"", | assertEquals("-28 blah ", eval("=CDbl(9)-37 & \" blah \"", | ||||
assertEquals(-28d, eval("CDbl(9)-37", Value.Type.DOUBLE)); | assertEquals(-28d, eval("CDbl(9)-37", Value.Type.DOUBLE)); | ||||
} | } | ||||
@Test | |||||
public void testParseSomeExprs() throws Exception | public void testParseSomeExprs() throws Exception | ||||
{ | { | ||||
BufferedReader br = new BufferedReader(new FileReader("src/test/resources/test_exprs.txt")); | BufferedReader br = new BufferedReader(new FileReader("src/test/resources/test_exprs.txt")); | ||||
br.close(); | br.close(); | ||||
} | } | ||||
@Test | |||||
public void testInvalidExpressions() throws Exception | public void testInvalidExpressions() throws Exception | ||||
{ | { | ||||
doTestEvalFail("", "empty"); | doTestEvalFail("", "empty"); | ||||
doTestEvalFail("=(3 + 5) Rnd()", "multiple expressions"); | doTestEvalFail("=(3 + 5) Rnd()", "multiple expressions"); | ||||
} | } | ||||
private static void doTestEvalFail(String exprStr, String msgStr) { | |||||
try { | |||||
eval(exprStr); | |||||
fail("ParseException should have been thrown"); | |||||
} catch(ParseException pe) { | |||||
// success | |||||
assertTrue(pe.getMessage().contains(msgStr)); | |||||
} | |||||
private static void doTestEvalFail(String exprStr, String msgStr) | |||||
{ | |||||
final ParseException pe = assertThrows(ParseException.class, () -> eval(exprStr)); | |||||
assertTrue(pe.getMessage().contains(msgStr)); | |||||
} | } | ||||
private static void validateExpr(String exprStr, String debugStr) { | private static void validateExpr(String exprStr, String debugStr) { | ||||
return expr.eval(tc); | return expr.eval(tc); | ||||
} | } | ||||
private static void evalFail( | |||||
String exprStr, Class<? extends Exception> failure) | |||||
private static void evalFail(String exprStr, Class<? extends Exception> failure) | |||||
{ | { | ||||
TestContext tc = new TestContext(); | |||||
Expression expr = Expressionator.parse( | |||||
Expressionator.Type.DEFAULT_VALUE, exprStr, null, tc); | |||||
try { | |||||
expr.eval(tc); | |||||
fail(failure + " should have been thrown"); | |||||
} catch(Exception e) { | |||||
assertTrue(failure.isInstance(e)); | |||||
} | |||||
final TestContext tc = new TestContext(); | |||||
Expression expr = Expressionator.parse(Expressionator.Type.DEFAULT_VALUE, exprStr, null, tc); | |||||
final Exception e = assertThrows(Exception.class, () -> expr.eval(tc)); | |||||
assertTrue(failure.isInstance(e)); | |||||
} | } | ||||
private static Boolean evalCondition(String exprStr, String thisVal) { | private static Boolean evalCondition(String exprStr, String thisVal) { |
import java.math.BigDecimal; | import java.math.BigDecimal; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class NumberFormatterTest extends TestCase | |||||
public class NumberFormatterTest | |||||
{ | { | ||||
public NumberFormatterTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testDoubleFormat() throws Exception | public void testDoubleFormat() throws Exception | ||||
{ | { | ||||
assertEquals("894984737284944", NumberFormatter.format(894984737284944d)); | assertEquals("894984737284944", NumberFormatter.format(894984737284944d)); | ||||
assertEquals("1.#QNAN", NumberFormatter.format(Double.NaN)); | assertEquals("1.#QNAN", NumberFormatter.format(Double.NaN)); | ||||
} | } | ||||
@Test | |||||
public void testFloatFormat() throws Exception | public void testFloatFormat() throws Exception | ||||
{ | { | ||||
assertEquals("8949847", NumberFormatter.format(8949847f)); | assertEquals("8949847", NumberFormatter.format(8949847f)); | ||||
assertEquals("1.#QNAN", NumberFormatter.format(Float.NaN)); | assertEquals("1.#QNAN", NumberFormatter.format(Float.NaN)); | ||||
} | } | ||||
@Test | |||||
public void testDecimalFormat() throws Exception | public void testDecimalFormat() throws Exception | ||||
{ | { | ||||
assertEquals("9874539485972.2342342234234", NumberFormatter.format(new BigDecimal("9874539485972.2342342234234"))); | assertEquals("9874539485972.2342342234234", NumberFormatter.format(new BigDecimal("9874539485972.2342342234234"))); |
import com.healthmarketscience.jackcess.TestUtil; | import com.healthmarketscience.jackcess.TestUtil; | ||||
import com.healthmarketscience.jackcess.impl.query.QueryImpl; | import com.healthmarketscience.jackcess.impl.query.QueryImpl; | ||||
import com.healthmarketscience.jackcess.impl.query.QueryImpl.Row; | import com.healthmarketscience.jackcess.impl.query.QueryImpl.Row; | ||||
import junit.framework.TestCase; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*; | import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class QueryTest extends TestCase | |||||
public class QueryTest | |||||
{ | { | ||||
public QueryTest(String name) throws Exception { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testUnionQuery() throws Exception | public void testUnionQuery() throws Exception | ||||
{ | { | ||||
String expr1 = "Select * from Table1"; | String expr1 = "Select * from Table1"; | ||||
removeRows(query, TABLE_ATTRIBUTE); | removeRows(query, TABLE_ATTRIBUTE); | ||||
try { | |||||
query.toSQLString(); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> query.toSQLString()); | |||||
} | } | ||||
@Test | |||||
public void testPassthroughQuery() throws Exception | public void testPassthroughQuery() throws Exception | ||||
{ | { | ||||
String expr = "Select * from Table1"; | String expr = "Select * from Table1"; | ||||
assertEquals(constr, query.getConnectionString()); | assertEquals(constr, query.getConnectionString()); | ||||
} | } | ||||
@Test | |||||
public void testDataDefinitionQuery() throws Exception | public void testDataDefinitionQuery() throws Exception | ||||
{ | { | ||||
String expr = "Drop table Table1"; | String expr = "Drop table Table1"; | ||||
assertEquals(expr, query.toSQLString()); | assertEquals(expr, query.toSQLString()); | ||||
} | } | ||||
@Test | |||||
public void testUpdateQuery() throws Exception | public void testUpdateQuery() throws Exception | ||||
{ | { | ||||
UpdateQuery query = (UpdateQuery)newQuery( | UpdateQuery query = (UpdateQuery)newQuery( | ||||
query.toSQLString()); | query.toSQLString()); | ||||
} | } | ||||
@Test | |||||
public void testSelectQuery() throws Exception | public void testSelectQuery() throws Exception | ||||
{ | { | ||||
SelectQuery query = (SelectQuery)newQuery( | SelectQuery query = (SelectQuery)newQuery( | ||||
doTestOrderings(query); | doTestOrderings(query); | ||||
} | } | ||||
@Test | |||||
public void testBadQueries() throws Exception | public void testBadQueries() throws Exception | ||||
{ | { | ||||
List<Row> rowList = new ArrayList<Row>(); | List<Row> rowList = new ArrayList<Row>(); | ||||
rowList.add(newRow(TYPE_ATTRIBUTE, null, -1, null, null)); | rowList.add(newRow(TYPE_ATTRIBUTE, null, -1, null, null)); | ||||
QueryImpl query = QueryImpl.create(-1, "TestQuery", rowList, 13); | QueryImpl query = QueryImpl.create(-1, "TestQuery", rowList, 13); | ||||
try { | |||||
query.toSQLString(); | |||||
fail("UnsupportedOperationException should have been thrown"); | |||||
} catch(UnsupportedOperationException e) { | |||||
// success | |||||
} | |||||
addRows(query, newRow(TYPE_ATTRIBUTE, null, -1, null, null)); | |||||
assertThrows(UnsupportedOperationException.class, () -> query.toSQLString()); | |||||
try { | |||||
query.getTypeRow(); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
addRows(query, newRow(TYPE_ATTRIBUTE, null, -1, null, null)); | |||||
try { | |||||
new QueryImpl("TestQuery", rowList, 13, Query.Type.UNION.getObjectFlag(), | |||||
Query.Type.UNION) { | |||||
@Override protected void toSQLString(StringBuilder builder) { | |||||
throw new UnsupportedOperationException(); | |||||
}}; | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> query.getTypeRow()); | |||||
assertThrows(IllegalStateException.class, | |||||
() -> new QueryImpl("TestQuery", rowList, 13, Query.Type.UNION.getObjectFlag(), | |||||
Query.Type.UNION) | |||||
{ | |||||
@Override | |||||
protected void toSQLString(StringBuilder builder) | |||||
{ | |||||
throw new UnsupportedOperationException(); | |||||
} | |||||
}); | |||||
} | } | ||||
@Test | |||||
public void testReadQueries() throws Exception | public void testReadQueries() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.QUERY, true)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.QUERY, true)) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testAppendQuery() throws Exception | public void testAppendQuery() throws Exception | ||||
{ | { | ||||
AppendQuery query = (AppendQuery)newQuery( | AppendQuery query = (AppendQuery)newQuery( | ||||
query.toSQLString()); | query.toSQLString()); | ||||
addRows(query, newRow(PARAMETER_ATTRIBUTE, null, -1, "BadVal", null)); | addRows(query, newRow(PARAMETER_ATTRIBUTE, null, -1, "BadVal", null)); | ||||
try { | |||||
query.toSQLString(); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> query.toSQLString()); | |||||
removeRows(query, PARAMETER_ATTRIBUTE); | removeRows(query, PARAMETER_ATTRIBUTE); | ||||
} | } | ||||
addRows(query, newRow(JOIN_ATTRIBUTE, "(Table1.id = Table3Val.id)", 5, "Table1", "Table3Val")); | addRows(query, newRow(JOIN_ATTRIBUTE, "(Table1.id = Table3Val.id)", 5, "Table1", "Table3Val")); | ||||
try { | |||||
query.toSQLString(); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> query.toSQLString()); | |||||
removeLastRows(query, 1); | removeLastRows(query, 1); | ||||
query.toSQLString(); | query.toSQLString(); | ||||
query.toSQLString()); | query.toSQLString()); | ||||
} | } | ||||
@Test | |||||
public void testComplexJoins() throws Exception | public void testComplexJoins() throws Exception | ||||
{ | { | ||||
SelectQuery query = (SelectQuery)newQuery( | SelectQuery query = (SelectQuery)newQuery( | ||||
addJoinRows(query, 1, 2, 1, | addJoinRows(query, 1, 2, 1, | ||||
2, 1, 2); | 2, 1, 2); | ||||
try { | |||||
query.toSQLString(); | |||||
fail("IllegalStateException should have been thrown"); | |||||
} catch(IllegalStateException e) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalStateException.class, () -> query.toSQLString()); | |||||
addJoinRows(query, 1, 2, 1, | addJoinRows(query, 1, 2, 1, | ||||
3, 4, 1, | 3, 4, 1, |
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | |||||
import com.healthmarketscience.jackcess.Column; | import com.healthmarketscience.jackcess.Column; | ||||
import com.healthmarketscience.jackcess.ColumnBuilder; | import com.healthmarketscience.jackcess.ColumnBuilder; | ||||
import com.healthmarketscience.jackcess.CursorBuilder; | |||||
import com.healthmarketscience.jackcess.DataType; | import com.healthmarketscience.jackcess.DataType; | ||||
import com.healthmarketscience.jackcess.Database; | import com.healthmarketscience.jackcess.Database; | ||||
import com.healthmarketscience.jackcess.Database.FileFormat; | import com.healthmarketscience.jackcess.Database.FileFormat; | ||||
import com.healthmarketscience.jackcess.IndexCursor; | |||||
import com.healthmarketscience.jackcess.PropertyMap; | import com.healthmarketscience.jackcess.PropertyMap; | ||||
import com.healthmarketscience.jackcess.Row; | import com.healthmarketscience.jackcess.Row; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ColumnFormatterTest extends TestCase | |||||
public class ColumnFormatterTest | |||||
{ | { | ||||
@Test | |||||
public void testFormat() throws Exception { | public void testFormat() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { |
import com.healthmarketscience.jackcess.Row; | import com.healthmarketscience.jackcess.Row; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ColumnValidatorTest extends TestCase | |||||
public class ColumnValidatorTest | |||||
{ | { | ||||
public ColumnValidatorTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testValidate() throws Exception { | public void testValidate() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = create(fileFormat); | Database db = create(fileFormat); | ||||
db.setColumnValidatorFactory(fact); | db.setColumnValidatorFactory(fact); | ||||
table = db.getTable("Test"); | |||||
final Table test = db.getTable("Test"); | |||||
for(Column col : table.getColumns()) { | |||||
for(Column col : test.getColumns()) { | |||||
ColumnValidator cur = col.getColumnValidator(); | ColumnValidator cur = col.getColumnValidator(); | ||||
assertNotNull(cur); | assertNotNull(cur); | ||||
if("num".equals(col.getName())) { | if("num".equals(col.getName())) { | ||||
} | } | ||||
} | } | ||||
Column idCol = table.getColumn("id"); | |||||
Column dataCol = table.getColumn("data"); | |||||
Column numCol = table.getColumn("num"); | |||||
Column idCol = test.getColumn("id"); | |||||
Column dataCol = test.getColumn("data"); | |||||
Column numCol = test.getColumn("num"); | |||||
assertThrows(IllegalArgumentException.class, () -> idCol.setColumnValidator(cv)); | |||||
try { | |||||
idCol.setColumnValidator(cv); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
// success | |||||
} | |||||
assertSame(SimpleColumnValidator.INSTANCE, idCol.getColumnValidator()); | assertSame(SimpleColumnValidator.INSTANCE, idCol.getColumnValidator()); | ||||
try { | |||||
table.addRow(Column.AUTO_NUMBER, "row4", -3); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
} | |||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, | |||||
() -> test.addRow(Column.AUTO_NUMBER, "row4", -3)); | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
table.addRow(Column.AUTO_NUMBER, "row4", 4); | |||||
test.addRow(Column.AUTO_NUMBER, "row4", 4); | |||||
List<? extends Map<String, Object>> expectedRows = | List<? extends Map<String, Object>> expectedRows = | ||||
createExpectedTable( | createExpectedTable( | ||||
createExpectedRow("id", 3, "data", "row3", "num", 1), | createExpectedRow("id", 3, "data", "row3", "num", 1), | ||||
createExpectedRow("id", 4, "data", "row4", "num", 4)); | createExpectedRow("id", 4, "data", "row4", "num", 4)); | ||||
assertTable(expectedRows, table); | |||||
assertTable(expectedRows, test); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(table); | |||||
IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(test); | |||||
assertNotNull(pkCursor.findRowByEntry(1)); | assertNotNull(pkCursor.findRowByEntry(1)); | ||||
pkCursor.setCurrentRowValue(dataCol, "row1_mod"); | pkCursor.setCurrentRowValue(dataCol, "row1_mod"); | ||||
assertEquals(createExpectedRow("id", 1, "data", "row1_mod", "num", -1), | assertEquals(createExpectedRow("id", 1, "data", "row1_mod", "num", -1), | ||||
pkCursor.getCurrentRow()); | pkCursor.getCurrentRow()); | ||||
try { | |||||
pkCursor.setCurrentRowValue(numCol, -2); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
} | |||||
e = assertThrows(IllegalArgumentException.class, | |||||
() -> pkCursor.setCurrentRowValue(numCol, -2)); | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
assertEquals(createExpectedRow("id", 1, "data", "row1_mod", "num", -1), | assertEquals(createExpectedRow("id", 1, "data", "row1_mod", "num", -1), | ||||
pkCursor.getCurrentRow()); | pkCursor.getCurrentRow()); | ||||
Row row3 = CursorBuilder.findRowByPrimaryKey(table, 3); | |||||
Row row3 = CursorBuilder.findRowByPrimaryKey(test, 3); | |||||
row3.put("num", -2); | row3.put("num", -2); | ||||
try { | |||||
table.updateRow(row3); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException e) { | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
} | |||||
e = assertThrows(IllegalArgumentException.class, () -> test.updateRow(row3)); | |||||
assertEquals("not gonna happen", e.getMessage()); | |||||
assertEquals(createExpectedRow("id", 3, "data", "row3", "num", 1), | assertEquals(createExpectedRow("id", 3, "data", "row3", "num", 1), | ||||
CursorBuilder.findRowByPrimaryKey(table, 3)); | |||||
CursorBuilder.findRowByPrimaryKey(test, 3)); | |||||
final ColumnValidator cv2 = (col, v1) -> { | final ColumnValidator cv2 = (col, v1) -> { | ||||
Number num = (Number)v1; | Number num = (Number)v1; | ||||
numCol.setColumnValidator(cv2); | numCol.setColumnValidator(cv2); | ||||
table.addRow(Column.AUTO_NUMBER, "row5", -5); | |||||
test.addRow(Column.AUTO_NUMBER, "row5", -5); | |||||
expectedRows = | expectedRows = | ||||
createExpectedTable( | createExpectedTable( | ||||
createExpectedRow("id", 4, "data", "row4", "num", 4), | createExpectedRow("id", 4, "data", "row4", "num", 4), | ||||
createExpectedRow("id", 5, "data", "row5", "num", 0)); | createExpectedRow("id", 5, "data", "row5", "num", 0)); | ||||
assertTable(expectedRows, table); | |||||
assertTable(expectedRows, test); | |||||
assertNotNull(pkCursor.findRowByEntry(3)); | assertNotNull(pkCursor.findRowByEntry(3)); | ||||
pkCursor.setCurrentRowValue(numCol, -10); | pkCursor.setCurrentRowValue(numCol, -10); |
import com.healthmarketscience.jackcess.Database.FileFormat; | import com.healthmarketscience.jackcess.Database.FileFormat; | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class CustomLinkResolverTest extends TestCase | |||||
public class CustomLinkResolverTest | |||||
{ | { | ||||
@Test | |||||
public void testCustomLinkResolver() throws Exception | |||||
{ | |||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
db.setLinkResolver(new TestLinkResolver()); | |||||
public CustomLinkResolverTest(String name) { | |||||
super(name); | |||||
} | |||||
db.createLinkedTable("Table1", "testFile1.txt", "Table1"); | |||||
db.createLinkedTable("Table2", "testFile2.txt", "OtherTable2"); | |||||
db.createLinkedTable("Table3", "missingFile3.txt", "MissingTable3"); | |||||
db.createLinkedTable("Table4", "testFile2.txt", "MissingTable4"); | |||||
public void testCustomLinkResolver() throws Exception { | |||||
for(final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
db.setLinkResolver(new TestLinkResolver()); | |||||
db.createLinkedTable("Table1", "testFile1.txt", "Table1"); | |||||
db.createLinkedTable("Table2", "testFile2.txt", "OtherTable2"); | |||||
db.createLinkedTable("Table3", "missingFile3.txt", "MissingTable3"); | |||||
db.createLinkedTable("Table4", "testFile2.txt", "MissingTable4"); | |||||
Table t1 = db.getTable("Table1"); | |||||
assertNotNull(t1); | |||||
assertNotSame(db, t1.getDatabase()); | |||||
assertTable(createExpectedTable(createExpectedRow("id", 0, | |||||
"data1", "row0"), | |||||
createExpectedRow("id", 1, | |||||
"data1", "row1"), | |||||
createExpectedRow("id", 2, | |||||
"data1", "row2")), | |||||
t1); | |||||
Table t2 = db.getTable("Table2"); | |||||
assertNotNull(t2); | |||||
assertNotSame(db, t2.getDatabase()); | |||||
assertTable(createExpectedTable(createExpectedRow("id", 3, | |||||
"data2", "row3"), | |||||
createExpectedRow("id", 4, | |||||
"data2", "row4"), | |||||
createExpectedRow("id", 5, | |||||
"data2", "row5")), | |||||
t2); | |||||
assertNull(db.getTable("Table4")); | |||||
try { | |||||
db.getTable("Table3"); | |||||
fail("FileNotFoundException should have been thrown"); | |||||
} catch(FileNotFoundException e) { | |||||
// success | |||||
} | |||||
Table t1 = db.getTable("Table1"); | |||||
assertNotNull(t1); | |||||
assertNotSame(db, t1.getDatabase()); | |||||
assertTable(createExpectedTable(createExpectedRow("id", 0, "data1", "row0"), | |||||
createExpectedRow("id", 1, "data1", "row1"), | |||||
createExpectedRow("id", 2, "data1", "row2")), | |||||
t1); | |||||
db.close(); | |||||
Table t2 = db.getTable("Table2"); | |||||
assertNotNull(t2); | |||||
assertNotSame(db, t2.getDatabase()); | |||||
assertTable(createExpectedTable(createExpectedRow("id", 3, "data2", "row3"), | |||||
createExpectedRow("id", 4, "data2", "row4"), | |||||
createExpectedRow("id", 5, "data2", "row5")), | |||||
t2); | |||||
assertNull(db.getTable("Table4")); | |||||
assertThrows(FileNotFoundException.class, () -> db.getTable("Table3")); | |||||
} | |||||
} | } | ||||
} | } | ||||
import com.healthmarketscience.jackcess.impl.ColumnImpl; | import com.healthmarketscience.jackcess.impl.ColumnImpl; | ||||
import com.healthmarketscience.jackcess.impl.JetFormatTest; | import com.healthmarketscience.jackcess.impl.JetFormatTest; | ||||
import com.healthmarketscience.jackcess.impl.TableImpl; | import com.healthmarketscience.jackcess.impl.TableImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ErrorHandlerTest extends TestCase | |||||
public class ErrorHandlerTest | |||||
{ | { | ||||
public ErrorHandlerTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testErrorHandler() throws Exception | public void testErrorHandler() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | |||||
Database db = create(fileFormat); | |||||
Table table = | |||||
new TableBuilder("test") | |||||
.addColumn(new ColumnBuilder("col", DataType.TEXT)) | |||||
.addColumn(new ColumnBuilder("val", DataType.LONG)) | |||||
.toTable(db); | |||||
table.addRow("row1", 1); | |||||
table.addRow("row2", 2); | |||||
table.addRow("row3", 3); | |||||
assertTable(createExpectedTable( | |||||
createExpectedRow("col", "row1", | |||||
"val", 1), | |||||
createExpectedRow("col", "row2", | |||||
"val", 2), | |||||
createExpectedRow("col", "row3", | |||||
"val", 3)), | |||||
table); | |||||
replaceColumn(table, "val"); | |||||
table.reset(); | |||||
try { | |||||
table.getNextRow(); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) | |||||
{ | |||||
try (final Database db = create(fileFormat)) | |||||
{ | |||||
final Table table | |||||
= new TableBuilder("test") | |||||
.addColumn(new ColumnBuilder("col", DataType.TEXT)) | |||||
.addColumn(new ColumnBuilder("val", DataType.LONG)) | |||||
.toTable(db); | |||||
table.reset(); | |||||
table.setErrorHandler(new ReplacementErrorHandler()); | |||||
assertTable(createExpectedTable( | |||||
createExpectedRow("col", "row1", | |||||
"val", null), | |||||
createExpectedRow("col", "row2", | |||||
"val", null), | |||||
createExpectedRow("col", "row3", | |||||
"val", null)), | |||||
table); | |||||
Cursor c1 = CursorBuilder.createCursor(table); | |||||
Cursor c2 = CursorBuilder.createCursor(table); | |||||
Cursor c3 = CursorBuilder.createCursor(table); | |||||
c2.setErrorHandler(new DebugErrorHandler("#error")); | |||||
c3.setErrorHandler(ErrorHandler.DEFAULT); | |||||
assertCursor(createExpectedTable( | |||||
createExpectedRow("col", "row1", | |||||
"val", null), | |||||
createExpectedRow("col", "row2", | |||||
"val", null), | |||||
createExpectedRow("col", "row3", | |||||
"val", null)), | |||||
c1); | |||||
assertCursor(createExpectedTable( | |||||
createExpectedRow("col", "row1", | |||||
"val", "#error"), | |||||
createExpectedRow("col", "row2", | |||||
"val", "#error"), | |||||
createExpectedRow("col", "row3", | |||||
"val", "#error")), | |||||
c2); | |||||
try { | |||||
c3.getNextRow(); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
table.addRow("row1", 1); | |||||
table.addRow("row2", 2); | |||||
table.addRow("row3", 3); | |||||
table.setErrorHandler(null); | |||||
c1.setErrorHandler(null); | |||||
c1.reset(); | |||||
try { | |||||
c1.getNextRow(); | |||||
fail("IOException should have been thrown"); | |||||
} catch(IOException e) { | |||||
// success | |||||
} | |||||
assertTable(createExpectedTable(createExpectedRow("col", "row1", "val", 1), | |||||
createExpectedRow("col", "row2", "val", 2), | |||||
createExpectedRow("col", "row3", "val", 3)), | |||||
table); | |||||
replaceColumn(table, "val"); | |||||
table.reset(); | |||||
assertThrows(IOException.class, () -> table.getNextRow()); | |||||
table.reset(); | |||||
table.setErrorHandler(new ReplacementErrorHandler()); | |||||
db.close(); | |||||
assertTable(createExpectedTable(createExpectedRow("col", "row1", "val", null), | |||||
createExpectedRow("col", "row2", "val", null), | |||||
createExpectedRow("col", "row3", "val", null)), | |||||
table); | |||||
Cursor c1 = CursorBuilder.createCursor(table); | |||||
Cursor c2 = CursorBuilder.createCursor(table); | |||||
Cursor c3 = CursorBuilder.createCursor(table); | |||||
c2.setErrorHandler(new DebugErrorHandler("#error")); | |||||
c3.setErrorHandler(ErrorHandler.DEFAULT); | |||||
assertCursor(createExpectedTable(createExpectedRow("col", "row1", "val", null), | |||||
createExpectedRow("col", "row2", "val", null), | |||||
createExpectedRow("col", "row3", "val", null)), | |||||
c1); | |||||
assertCursor(createExpectedTable(createExpectedRow("col", "row1", "val", "#error"), | |||||
createExpectedRow("col", "row2", "val", "#error"), | |||||
createExpectedRow("col", "row3", "val", "#error")), | |||||
c2); | |||||
assertThrows(IOException.class, () -> c3.getNextRow()); | |||||
table.setErrorHandler(null); | |||||
c1.setErrorHandler(null); | |||||
c1.reset(); | |||||
assertThrows(IOException.class, () -> c1.getNextRow()); | |||||
} | |||||
} | } | ||||
} | } | ||||
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import com.healthmarketscience.jackcess.impl.JetFormatTest; | import com.healthmarketscience.jackcess.impl.JetFormatTest; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class ExportTest extends TestCase | |||||
public class ExportTest | |||||
{ | { | ||||
private static final String NL = System.lineSeparator(); | private static final String NL = System.lineSeparator(); | ||||
public ExportTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testExportToFile() throws Exception | public void testExportToFile() throws Exception | ||||
{ | { | ||||
DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); | DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); |
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import com.healthmarketscience.jackcess.impl.JetFormatTest; | import com.healthmarketscience.jackcess.impl.JetFormatTest; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author Rob Di Marco | * @author Rob Di Marco | ||||
*/ | */ | ||||
public class ImportTest extends TestCase | |||||
public class ImportTest | |||||
{ | { | ||||
public ImportTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testImportFromFile() throws Exception | public void testImportFromFile() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testImportFromFileWithOnlyHeaders() throws Exception | public void testImportFromFileWithOnlyHeaders() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testCopySqlHeaders() throws Exception | public void testCopySqlHeaders() throws Exception | ||||
{ | { | ||||
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { |
import com.healthmarketscience.jackcess.Table; | import com.healthmarketscience.jackcess.Table; | ||||
import com.healthmarketscience.jackcess.impl.RowImpl; | import com.healthmarketscience.jackcess.impl.RowImpl; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class JoinerTest extends TestCase { | |||||
public JoinerTest(String name) { | |||||
super(name); | |||||
} | |||||
public class JoinerTest | |||||
{ | |||||
@Test | |||||
public void testJoiner() throws Exception | public void testJoiner() throws Exception | ||||
{ | { | ||||
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { | for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { |
import java.nio.channels.NonWritableChannelException; | import java.nio.channels.NonWritableChannelException; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import junit.framework.TestCase; | |||||
import com.healthmarketscience.jackcess.TestUtil; | import com.healthmarketscience.jackcess.TestUtil; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class MemFileChannelTest extends TestCase | |||||
public class MemFileChannelTest | |||||
{ | { | ||||
public MemFileChannelTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testReadOnlyChannel() throws Exception | public void testReadOnlyChannel() throws Exception | ||||
{ | { | ||||
File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb"); | File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb"); | ||||
MemFileChannel ch = MemFileChannel.newChannel(testFile, "r"); | |||||
final MemFileChannel ch = MemFileChannel.newChannel(testFile, "r"); | |||||
assertEquals(testFile.length(), ch.size()); | assertEquals(testFile.length(), ch.size()); | ||||
assertEquals(0L, ch.position()); | assertEquals(0L, ch.position()); | ||||
try { | |||||
ByteBuffer bb = ByteBuffer.allocate(1024); | |||||
ch.write(bb); | |||||
fail("NonWritableChannelException should have been thrown"); | |||||
} catch(NonWritableChannelException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(NonWritableChannelException.class, () -> ch.write(ByteBuffer.allocate(1024))); | |||||
try { | |||||
ch.truncate(0L); | |||||
fail("NonWritableChannelException should have been thrown"); | |||||
} catch(NonWritableChannelException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(NonWritableChannelException.class, () -> ch.truncate(0L)); | |||||
try { | |||||
ch.transferFrom(null, 0L, 10L); | |||||
fail("NonWritableChannelException should have been thrown"); | |||||
} catch(NonWritableChannelException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(NonWritableChannelException.class, () -> ch.transferFrom(null, 0L, 10L)); | |||||
assertEquals(testFile.length(), ch.size()); | assertEquals(testFile.length(), ch.size()); | ||||
assertEquals(0L, ch.position()); | assertEquals(0L, ch.position()); | ||||
ch.close(); | ch.close(); | ||||
} | } | ||||
@Test | |||||
public void testChannel() throws Exception | public void testChannel() throws Exception | ||||
{ | { | ||||
ByteBuffer bb = ByteBuffer.allocate(1024); | ByteBuffer bb = ByteBuffer.allocate(1024); | ||||
MemFileChannel ch = MemFileChannel.newChannel(); | |||||
final MemFileChannel ch = MemFileChannel.newChannel(); | |||||
assertTrue(ch.isOpen()); | assertTrue(ch.isOpen()); | ||||
assertEquals(0L, ch.size()); | assertEquals(0L, ch.size()); | ||||
assertEquals(0L, ch.position()); | assertEquals(0L, ch.position()); | ||||
assertFalse(ch.isOpen()); | assertFalse(ch.isOpen()); | ||||
File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb"); | File testFile = new File("src/test/data/V1997/compIndexTestV1997.mdb"); | ||||
ch = MemFileChannel.newChannel(testFile, "r"); | |||||
assertEquals(testFile.length(), ch.size()); | |||||
assertEquals(0L, ch.position()); | |||||
final MemFileChannel ch1 = MemFileChannel.newChannel(testFile, "r"); | |||||
assertEquals(testFile.length(), ch1.size()); | |||||
assertEquals(0L, ch1.position()); | |||||
try { | |||||
ch.position(-1); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, () -> ch1.position(-1)); | |||||
MemFileChannel ch2 = MemFileChannel.newChannel(); | |||||
ch.transferTo(ch2); | |||||
final MemFileChannel ch2 = MemFileChannel.newChannel(); | |||||
ch1.transferTo(ch2); | |||||
ch2.force(true); | ch2.force(true); | ||||
assertEquals(testFile.length(), ch2.size()); | assertEquals(testFile.length(), ch2.size()); | ||||
assertEquals(testFile.length(), ch2.position()); | assertEquals(testFile.length(), ch2.position()); | ||||
try { | |||||
ch2.truncate(-1L); | |||||
fail("IllegalArgumentException should have been thrown"); | |||||
} catch(IllegalArgumentException ignored) { | |||||
// success | |||||
} | |||||
assertThrows(IllegalArgumentException.class, () -> ch2.truncate(-1L)); | |||||
long trucSize = ch2.size()/3; | long trucSize = ch2.size()/3; | ||||
ch2.truncate(trucSize); | ch2.truncate(trucSize); | ||||
assertEquals(trucSize, ch2.size()); | assertEquals(trucSize, ch2.size()); | ||||
assertEquals(trucSize, ch2.position()); | assertEquals(trucSize, ch2.position()); | ||||
ch2.position(0L); | ch2.position(0L); | ||||
copy(ch, ch2, bb); | |||||
copy(ch1, ch2, bb); | |||||
File tmpFile = File.createTempFile("chtest_", ".dat"); | File tmpFile = File.createTempFile("chtest_", ".dat"); | ||||
tmpFile.deleteOnExit(); | tmpFile.deleteOnExit(); |
import com.healthmarketscience.jackcess.complex.Attachment; | import com.healthmarketscience.jackcess.complex.Attachment; | ||||
import com.healthmarketscience.jackcess.impl.ByteUtil; | import com.healthmarketscience.jackcess.impl.ByteUtil; | ||||
import com.healthmarketscience.jackcess.impl.CompoundOleUtil; | import com.healthmarketscience.jackcess.impl.CompoundOleUtil; | ||||
import junit.framework.TestCase; | |||||
import org.apache.poi.poifs.filesystem.DocumentEntry; | import org.apache.poi.poifs.filesystem.DocumentEntry; | ||||
import org.apache.poi.poifs.filesystem.DocumentInputStream; | import org.apache.poi.poifs.filesystem.DocumentInputStream; | ||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class OleBlobTest extends TestCase | |||||
public class OleBlobTest | |||||
{ | { | ||||
public OleBlobTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testCreateBlob() throws Exception | public void testCreateBlob() throws Exception | ||||
{ | { | ||||
File sampleFile = new File("src/test/data/sample-input.tab"); | File sampleFile = new File("src/test/data/sample-input.tab"); | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testReadBlob() throws Exception | public void testReadBlob() throws Exception | ||||
{ | { | ||||
for(TestDB testDb : TestDB.getSupportedForBasename(Basename.BLOB, true)) { | for(TestDB testDb : TestDB.getSupportedForBasename(Basename.BLOB, true)) { |
import com.healthmarketscience.jackcess.TableBuilder; | import com.healthmarketscience.jackcess.TableBuilder; | ||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; | ||||
import junit.framework.TestCase; | |||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* | * | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class PatternColumnPredicateTest extends TestCase | |||||
public class PatternColumnPredicateTest | |||||
{ | { | ||||
public PatternColumnPredicateTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
public void testRegexPredicate() throws Exception { | public void testRegexPredicate() throws Exception { | ||||
for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { | ||||
Database db = createTestDb(fileFormat); | Database db = createTestDb(fileFormat); |
import com.healthmarketscience.jackcess.DataType; | import com.healthmarketscience.jackcess.DataType; | ||||
import com.healthmarketscience.jackcess.Row; | import com.healthmarketscience.jackcess.Row; | ||||
import com.healthmarketscience.jackcess.impl.ColumnImpl; | import com.healthmarketscience.jackcess.impl.ColumnImpl; | ||||
import junit.framework.TestCase; | |||||
import static com.healthmarketscience.jackcess.TestUtil.*; | import static com.healthmarketscience.jackcess.TestUtil.*; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* @author James Ahlborn | * @author James Ahlborn | ||||
*/ | */ | ||||
public class RowFilterTest extends TestCase | |||||
public class RowFilterTest | |||||
{ | { | ||||
private static final String ID_COL = "id"; | private static final String ID_COL = "id"; | ||||
private static final String COL1 = "col1"; | private static final String COL1 = "col1"; | ||||
private static final String COL2 = "col2"; | private static final String COL2 = "col2"; | ||||
private static final String COL3 = "col3"; | private static final String COL3 = "col3"; | ||||
public RowFilterTest(String name) { | |||||
super(name); | |||||
} | |||||
@Test | |||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
public void testFilter() throws Exception | public void testFilter() throws Exception | ||||
{ | { |