diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2012-11-20 04:54:19 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2012-11-20 04:54:19 +0000 |
commit | 792d46d8ea9c3b8ba46c44f5c8748ebacdac70e2 (patch) | |
tree | f8aa6fb1b7310e18b180f162046f28fc361639d2 /test/src/java/com/healthmarketscience/jackcess | |
parent | 7af9991637bfe1072b900a5f53fc2714101a7ae4 (diff) | |
download | jackcess-792d46d8ea9c3b8ba46c44f5c8748ebacdac70e2.tar.gz jackcess-792d46d8ea9c3b8ba46c44f5c8748ebacdac70e2.zip |
initial support for optionally enforcing foreign-key constraints (fixes feature request #22)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@655 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'test/src/java/com/healthmarketscience/jackcess')
-rw-r--r-- | test/src/java/com/healthmarketscience/jackcess/FKEnforcerTest.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/test/src/java/com/healthmarketscience/jackcess/FKEnforcerTest.java b/test/src/java/com/healthmarketscience/jackcess/FKEnforcerTest.java new file mode 100644 index 0000000..9dd0c88 --- /dev/null +++ b/test/src/java/com/healthmarketscience/jackcess/FKEnforcerTest.java @@ -0,0 +1,138 @@ +/* +Copyright (c) 2012 James Ahlborn + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +USA +*/ + +package com.healthmarketscience.jackcess; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static com.healthmarketscience.jackcess.DatabaseTest.*; +import static com.healthmarketscience.jackcess.JetFormatTest.*; +import junit.framework.TestCase; + +/** + * + * @author James Ahlborn + */ +public class FKEnforcerTest extends TestCase +{ + + public FKEnforcerTest(String name) throws Exception { + super(name); + } + + public void testNoEnforceForeignKeys() throws Exception { + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { + + Database db = openCopy(testDB); + Table t1 = db.getTable("Table1"); + Table t2 = db.getTable("Table2"); + Table t3 = db.getTable("Table3"); + + t1.addRow(20, 0, 20, "some data", 20); + + Cursor c = Cursor.createCursor(t2); + c.moveToNextRow(); + c.updateCurrentRow(30, "foo30"); + + c = Cursor.createCursor(t3); + c.moveToNextRow(); + c.deleteCurrentRow(); + + db.close(); + } + + } + + public void testEnforceForeignKeys() throws Exception { + for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { + + Database db = openCopy(testDB); + db.setEnforceForeignKeys(true); + 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 = Cursor.createCursor(t2); + c.moveToNextRow(); + c.updateCurrentRow(30, "foo30"); + fail("IOException should have been thrown"); + } catch(IOException ignored) { + // success + assertTrue(ignored.getMessage().contains("Table2[id]")); + } + + try { + Cursor c = Cursor.createCursor(t3); + c.moveToNextRow(); + c.deleteCurrentRow(); + fail("IOException should have been thrown"); + } catch(IOException ignored) { + // success + assertTrue(ignored.getMessage().contains("Table3[id]")); + } + + Cursor c = Cursor.createCursor(t3); + Column col = t3.getColumn("id"); + for(Map<String,Object> row : c) { + int id = (Integer)row.get("id"); + id += 20; + c.setCurrentRowValue(col, id); + } + + List<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)); + + assertTable(expectedRows, t1); + + c = Cursor.createCursor(t2); + for(Iterator<?> iter = c.iterator(); iter.hasNext(); ) { + iter.next(); + iter.remove(); + } + + assertEquals(0, t1.getRowCount()); + + db.close(); + } + + } + + private static Map<String,Object> createT1Row( + int id1, int fk1, int fk2, String data, int fk3) + { + return createExpectedRow("id", id1, "otherfk1", fk1, "otherfk2", fk2, + "data", data, "otherfk3", fk3); + } +} |