diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2022-08-26 21:02:28 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2022-08-26 21:02:28 +0000 |
commit | b5aa0575e557d975a062fb7eb34f1ecb5b6a5a29 (patch) | |
tree | 97e9f362b8701f0462ccb2b35fcc37a7550eaa02 /src/test/java/com/healthmarketscience | |
parent | f1403cdbb47ce8cb1839cd3700ab00c8c8a416bc (diff) | |
download | jackcess-b5aa0575e557d975a062fb7eb34f1ecb5b6a5a29.tar.gz jackcess-b5aa0575e557d975a062fb7eb34f1ecb5b6a5a29.zip |
Add support for linked odbc tables. TableMetaData provides access to connection string and local TableDefinition if available. Fixes #45
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1379 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/test/java/com/healthmarketscience')
3 files changed, 202 insertions, 123 deletions
diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 5f5a488..3b90545 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -38,17 +38,16 @@ import java.util.UUID; import java.util.stream.Collectors; import static com.healthmarketscience.jackcess.Database.*; +import static com.healthmarketscience.jackcess.DatabaseBuilder.*; +import static com.healthmarketscience.jackcess.TestUtil.*; import com.healthmarketscience.jackcess.impl.ColumnImpl; import com.healthmarketscience.jackcess.impl.DatabaseImpl; import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; import com.healthmarketscience.jackcess.impl.RowIdImpl; import com.healthmarketscience.jackcess.impl.RowImpl; import com.healthmarketscience.jackcess.impl.TableImpl; -import com.healthmarketscience.jackcess.util.LinkResolver; import com.healthmarketscience.jackcess.util.RowFilterTest; import junit.framework.TestCase; -import static com.healthmarketscience.jackcess.TestUtil.*; -import static com.healthmarketscience.jackcess.DatabaseBuilder.*; /** @@ -865,123 +864,7 @@ public class DatabaseTest extends TestCase } } - 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()); - assertEquals("Z:\\jackcess_test\\linkeeTest.accdb", tmd.getLinkedDbName()); - - 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() { - public Database resolveLinkedDatabase(Database linkerdb, String dbName) - throws IOException { - assertEquals(linkeeDbName, dbName); - 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 = 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 = 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 = 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 = 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(); - } - } - - private static List<Table> getTables(Iterable<Table> tableIter) + static List<Table> getTables(Iterable<Table> tableIter) { List<Table> tableList = new ArrayList<Table>(); for(Table t : tableIter) { @@ -1102,9 +985,6 @@ public class DatabaseTest extends TestCase expectedCreateDate = "2004-05-28T17:51:48.701"; expectedUpdateDate = "2006-07-24T09:56:19.701"; } - System.out.println("FOO " + testDB.getExpectedFileFormat() + " " + - table.getCreatedDate() + " " + - table.getUpdatedDate()); assertEquals(expectedCreateDate, table.getCreatedDate().toString()); assertEquals(expectedUpdateDate, table.getUpdatedDate().toString()); } diff --git a/src/test/java/com/healthmarketscience/jackcess/LinkedTableTest.java b/src/test/java/com/healthmarketscience/jackcess/LinkedTableTest.java new file mode 100644 index 0000000..5763285 --- /dev/null +++ b/src/test/java/com/healthmarketscience/jackcess/LinkedTableTest.java @@ -0,0 +1,198 @@ +/* +Copyright (c) 2016 James Ahlborn + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.healthmarketscience.jackcess; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static com.healthmarketscience.jackcess.TestUtil.*; +import com.healthmarketscience.jackcess.impl.DatabaseImpl; +import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; +import com.healthmarketscience.jackcess.util.LinkResolver; +import junit.framework.TestCase; + +/** + * + * @author James Ahlborn + */ +public class LinkedTableTest extends TestCase +{ + + 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 { + assertEquals(linkeeDbName, dbName); + 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(); + } + } + + public void testOdbcLinkedTables() throws Exception { + for (final TestDB testDB : + TestDB.getSupportedForBasename(Basename.LINKED_ODBC)) { + 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")); + + TableDefinition t = tmd.getTableDefinition(db); + + List<? extends Column> cols = t.getColumns(); + assertEquals(20, cols.size()); + + List<? extends Index> idxs = t.getIndexes(); + assertEquals(5, idxs.size()); + + Table tbl = db.getTable("Ordrar"); + + try { + tbl.iterator(); + fail("UnsupportedOperationException should have been thrown"); + } catch(UnsupportedOperationException expected) { + // expected + } + + try { + tbl.addRow(1L,"bar"); + fail("UnsupportedOperationException should have been thrown"); + } catch(UnsupportedOperationException expected) { + // expected + } + + db.close(); + } + } + +} diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java index 6f01bca..0f6c889 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java @@ -53,6 +53,7 @@ public class JetFormatTest extends TestCase { COMPLEX("complexDataTest"), UNSUPPORTED("unsupportedFieldsTest"), LINKED("linkerTest"), + LINKED_ODBC("odbcLinkerTest"), BLOB("testOle"), CALC_FIELD("calcFieldTest"), BINARY_INDEX("binIdxTest"), |