From 8fdd98bd2a2bcfda6c7cd408180adad5009b9740 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Thu, 8 Sep 2016 01:43:36 +0000 Subject: [PATCH] expand unit tests git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1029 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/TableUpdaterTest.java | 178 +++++++++++------- 1 file changed, 115 insertions(+), 63 deletions(-) diff --git a/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java b/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java index 01e2576..160f71c 100644 --- a/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java @@ -20,6 +20,7 @@ import java.util.Arrays; import com.healthmarketscience.jackcess.Database.FileFormat; import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; +import com.healthmarketscience.jackcess.impl.DatabaseImpl; import com.healthmarketscience.jackcess.impl.TableImpl; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.TestUtil.*; @@ -39,49 +40,96 @@ public class TableUpdaterTest extends TestCase for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); - Table t1 = new TableBuilder("TestTable") - .addColumn(new ColumnBuilder("id", DataType.LONG)) - .toTable(db); - - Table t2 = new TableBuilder("TestTable2") - .addColumn(new ColumnBuilder("id2", DataType.LONG)) - .toTable(db); + doTestUpdating(db, false, true); + + db.close(); + } + } + + public void testTableUpdatingOneToOne() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + doTestUpdating(db, true, true); + // FIXME, add one-to-one, add no enforce rel + + db.close(); + } + } + + public void testTableUpdatingNoEnforce() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + doTestUpdating(db, false, false); + + db.close(); + } + } + private void doTestUpdating(Database db, boolean oneToOne, boolean enforce) + throws Exception + { + Table t1 = new TableBuilder("TestTable") + .addColumn(new ColumnBuilder("id", DataType.LONG)) + .toTable(db); + + Table t2 = new TableBuilder("TestTable2") + .addColumn(new ColumnBuilder("id2", DataType.LONG)) + .toTable(db); + + int t1idxs = 1; + new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME) + .addColumns("id").setPrimaryKey() + .addToTable(t1); + new ColumnBuilder("data", DataType.TEXT) + .addToTable(t1); + new ColumnBuilder("bigdata", DataType.MEMO) + .addToTable(t1); + + new ColumnBuilder("data2", DataType.TEXT) + .addToTable(t2); + new ColumnBuilder("bigdata2", DataType.MEMO) + .addToTable(t2); + + int t2idxs = 0; + if(oneToOne) { + ++t2idxs; new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME) - .addColumns("id").setPrimaryKey() - .addToTable(t1); - new ColumnBuilder("data", DataType.TEXT) - .addToTable(t1); - new ColumnBuilder("bigdata", DataType.MEMO) - .addToTable(t1); - - new ColumnBuilder("data2", DataType.TEXT) - .addToTable(t2); - new ColumnBuilder("bigdata2", DataType.MEMO) + .addColumns("id2").setPrimaryKey() .addToTable(t2); - - Relationship rel = new RelationshipBuilder("TestTable", "TestTable2") - .addColumns("id", "id2") - .setReferentialIntegrity() - .setCascadeDeletes() - .toRelationship(db); - - assertEquals("TestTableTestTable2", rel.getName()); - assertSame(t1, rel.getFromTable()); - assertEquals(Arrays.asList(t1.getColumn("id")), rel.getFromColumns()); - assertSame(t2, rel.getToTable()); - assertEquals(Arrays.asList(t2.getColumn("id2")), rel.getToColumns()); - assertFalse(rel.isOneToOne()); - assertTrue(rel.hasReferentialIntegrity()); - assertTrue(rel.cascadeDeletes()); - assertFalse(rel.cascadeUpdates()); - assertEquals(Relationship.JoinType.INNER, rel.getJoinType()); - - assertEquals(2, t1.getIndexes().size()); - assertEquals(1, ((TableImpl)t1).getIndexDatas().size()); - - assertEquals(1, t2.getIndexes().size()); - assertEquals(1, ((TableImpl)t2).getIndexDatas().size()); + } + + RelationshipBuilder rb = new RelationshipBuilder("TestTable", "TestTable2") + .addColumns("id", "id2"); + if(enforce) { + ++t1idxs; + ++t2idxs; + rb.setReferentialIntegrity() + .setCascadeDeletes(); + } + + Relationship rel = rb.toRelationship(db); + + assertEquals("TestTableTestTable2", rel.getName()); + assertSame(t1, rel.getFromTable()); + assertEquals(Arrays.asList(t1.getColumn("id")), rel.getFromColumns()); + assertSame(t2, rel.getToTable()); + assertEquals(Arrays.asList(t2.getColumn("id2")), rel.getToColumns()); + assertEquals(oneToOne, rel.isOneToOne()); + assertEquals(enforce, rel.hasReferentialIntegrity()); + assertEquals(enforce, rel.cascadeDeletes()); + assertFalse(rel.cascadeUpdates()); + assertEquals(Relationship.JoinType.INNER, rel.getJoinType()); + + assertEquals(t1idxs, t1.getIndexes().size()); + assertEquals(1, ((TableImpl)t1).getIndexDatas().size()); + + assertEquals(t2idxs, t2.getIndexes().size()); + assertEquals((t2idxs > 0 ? 1 : 0), ((TableImpl)t2).getIndexDatas().size()); + + ((DatabaseImpl)db).getPageChannel().startWrite(); + try { for(int i = 0; i < 10; ++i) { t1.addRow(i, "row" + i, "row-data" + i); @@ -91,35 +139,39 @@ public class TableUpdaterTest extends TestCase t2.addRow(i, "row2_" + i, "row-data2_" + i); } - try { - t2.addRow(13, "row13", "row-data13"); - fail("ConstraintViolationException should have been thrown"); - } catch(ConstraintViolationException cv) { - // success - } + } finally { + ((DatabaseImpl)db).getPageChannel().finishWrite(); + } - Row r1 = CursorBuilder.findRowByPrimaryKey(t1, 5); - t1.deleteRow(r1); + try { + t2.addRow(10, "row10", "row-data10"); + if(enforce) { + fail("ConstraintViolationException should have been thrown"); + } + } catch(ConstraintViolationException cv) { + // success + if(!enforce) { throw cv; } + } + + Row r1 = CursorBuilder.findRowByPrimaryKey(t1, 5); + t1.deleteRow(r1); - int id = 0; - for(Row r : t1) { - assertEquals(id, r.get("id")); + int id = 0; + for(Row r : t1) { + assertEquals(id, r.get("id")); + ++id; + if(id == 5) { ++id; - if(id == 5) { - ++id; - } } + } - id = 0; - for(Row r : t2) { - assertEquals(id, r.get("id2")); + id = 0; + for(Row r : t2) { + assertEquals(id, r.get("id2")); + ++id; + if(enforce && (id == 5)) { ++id; - if(id == 5) { - ++id; - } } - - db.close(); - } + } } } -- 2.39.5