From 6d3704b7bad4732a289c0be68e8f18a1fdf8abac Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 15 Oct 2012 12:17:02 -0400 Subject: [PATCH] Added drop reference table test (fails on H2) Unfortunately, it looks like H2 1.3.168 has a bug which allows you to drop a table even though there are active constraints referencing it. HSQL, Derby, MySQL, and PostgreSQL all throw a constraint violation exception, as expected, but H2 does not. --- src/com/iciql/IciqlException.java | 13 +++++++++++++ tests/com/iciql/test/ForeignKeyTest.java | 13 ++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/com/iciql/IciqlException.java b/src/com/iciql/IciqlException.java index 320604d..38c8fac 100644 --- a/src/com/iciql/IciqlException.java +++ b/src/com/iciql/IciqlException.java @@ -29,6 +29,7 @@ public class IciqlException extends RuntimeException { public static final int CODE_DUPLICATE_KEY = 2; public static final int CODE_OBJECT_NOT_FOUND = 3; public static final int CODE_OBJECT_ALREADY_EXISTS = 4; + public static final int CODE_CONSTRAINT_VIOLATION = 5; private static final String TOKEN_UNMAPPED_FIELD = "\\? (=|\\>|\\<|\\<\\>|!=|\\>=|\\<=|LIKE|BETWEEN) \\?"; @@ -97,6 +98,9 @@ public class IciqlException extends RuntimeException { if ("23000".equals(state)) { // MySQL duplicate primary key on insert iciqlCode = CODE_DUPLICATE_KEY; + if (s.getErrorCode() == 1217) { + iciqlCode = CODE_CONSTRAINT_VIOLATION; + } } else if ("23505".equals(state)) { // Derby duplicate primary key on insert iciqlCode = CODE_DUPLICATE_KEY; @@ -139,6 +143,15 @@ public class IciqlException extends RuntimeException { } else if ("42504".equals(state)) { // HSQL index already exists iciqlCode = CODE_OBJECT_ALREADY_EXISTS; + } else if ("2BP01".equals(state)) { + // PostgreSQL constraint violation + iciqlCode = CODE_CONSTRAINT_VIOLATION; + } else if ("42533".equals(state)) { + // HSQL constraint violation + iciqlCode = CODE_CONSTRAINT_VIOLATION; + } else if ("X0Y25".equals(state)) { + // Derby constraint violation + iciqlCode = CODE_CONSTRAINT_VIOLATION; } } } diff --git a/tests/com/iciql/test/ForeignKeyTest.java b/tests/com/iciql/test/ForeignKeyTest.java index 3e0314c..d789408 100644 --- a/tests/com/iciql/test/ForeignKeyTest.java +++ b/tests/com/iciql/test/ForeignKeyTest.java @@ -17,12 +17,14 @@ package com.iciql.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.iciql.Db; +import com.iciql.IciqlException; import com.iciql.test.models.CategoryAnnotationOnly; import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey; @@ -62,6 +64,15 @@ public class ForeignKeyTest { assertEquals(count1, count2 + 2L); } - + + @Test + public void testForeignKeyDropReferenceTable() { + try { + db.dropTable(CategoryAnnotationOnly.class); + assertTrue("Should not be able to drop reference table!", false); + } catch (IciqlException e) { + assertEquals(e.getMessage(), IciqlException.CODE_CONSTRAINT_VIOLATION, e.getIciqlCode()); + } + } } -- 2.39.5