diff --git a/src/test/java/net/ucanaccess/console/MainTest.java b/src/test/java/net/ucanaccess/console/MainTest.java index b708cf0e..2bfefb2a 100644 --- a/src/test/java/net/ucanaccess/console/MainTest.java +++ b/src/test/java/net/ucanaccess/console/MainTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.console; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.test.util.AbstractBaseTest; import org.junit.jupiter.api.Test; @@ -12,54 +14,54 @@ class MainTest extends AbstractBaseTest { void testTokenize() throws IOException { // normal space as token delimiter List tokens = Main.tokenize("export -t License License.csv"); - assertListEquals(tokens, "export", "-t", "License", "License.csv"); + assertThat(tokens).containsExactly("export", "-t", "License", "License.csv"); // tab (\t), line-feed (\n) and carriage-return (\r) as token delimiter tokens = Main.tokenize("export\t-t\nLicense\rLicense.csv"); - assertListEquals(tokens, "export", "-t", "License", "License.csv"); + assertThat(tokens).containsExactly("export", "-t", "License", "License.csv"); // backslash escapes are not interpreted outside of quote tokens = Main.tokenize("export -t License c:\\temp\\License.csv"); - assertListEquals(tokens, "export", "-t", "License", "c:\\temp\\License.csv"); + assertThat(tokens).containsExactly("export", "-t", "License", "c:\\temp\\License.csv"); // backslash escapes are interpreted inside quote, so we have to double escape tokens = Main.tokenize("export -t License 'c:\\\\temp\\\\License.csv"); - assertListEquals(tokens, "export", "-t", "License", "c:\\temp\\License.csv"); + assertThat(tokens).containsExactly("export", "-t", "License", "c:\\temp\\License.csv"); // octal escapes are not interpreted outside of quote tokens = Main.tokenize("export -t License c:\\101"); - assertListEquals(tokens, "export", "-t", "License", "c:\\101"); + assertThat(tokens).containsExactly("export", "-t", "License", "c:\\101"); // octal escapes are interpreted inside a quote tokens = Main.tokenize("export -t License 'c:\\101'"); - assertListEquals(tokens, "export", "-t", "License", "c:A"); + assertThat(tokens).containsExactly("export", "-t", "License", "c:A"); // embedded space inside double quote tokens = Main.tokenize("export \"License and Address.csv\""); - assertListEquals(tokens, "export", "License and Address.csv"); + assertThat(tokens).containsExactly("export", "License and Address.csv"); // embedded space inside single quote tokens = Main.tokenize("export 'License and Address.csv'"); - assertListEquals(tokens, "export", "License and Address.csv"); + assertThat(tokens).containsExactly("export", "License and Address.csv"); // single quote and embedded space inside double quote tokens = Main.tokenize("export \"License 'and' Address.csv\""); - assertListEquals(tokens, "export", "License 'and' Address.csv"); + assertThat(tokens).containsExactly("export", "License 'and' Address.csv"); // double quote and embedded space inside single quote tokens = Main.tokenize("export 'License \"and\" Address.csv'"); - assertListEquals(tokens, "export", "License \"and\" Address.csv"); + assertThat(tokens).containsExactly("export", "License \"and\" Address.csv"); // backslash escaped double quote inside double quote tokens = Main.tokenize("export \"\\\"License and Address.csv\\\"\""); - assertListEquals(tokens, "export", "\"License and Address.csv\""); + assertThat(tokens).containsExactly("export", "\"License and Address.csv\""); // non-breaking-white-space U+00A0 is treated like a normal character tokens = Main.tokenize("export License\u00A0and\u00A0Address.csv"); - assertListEquals(tokens, "export", "License\u00A0and\u00A0Address.csv"); + assertThat(tokens).containsExactly("export", "License\u00A0and\u00A0Address.csv"); // any unicode > U+00FF is treated like a normal character, U+202F is NARROW NO-BREAK SPACE tokens = Main.tokenize("export License\u202Fand\u202FAddress.csv"); - assertListEquals(tokens, "export", "License\u202Fand\u202FAddress.csv"); + assertThat(tokens).containsExactly("export", "License\u202Fand\u202FAddress.csv"); } } diff --git a/src/test/java/net/ucanaccess/test/integration/AccessLikeTest.java b/src/test/java/net/ucanaccess/test/integration/AccessLikeTest.java index 5e1489c7..a71104ab 100644 --- a/src/test/java/net/ucanaccess/test/integration/AccessLikeTest.java +++ b/src/test/java/net/ucanaccess/test/integration/AccessLikeTest.java @@ -75,7 +75,7 @@ void testLikeExternal(AccessVersion _accessVersion) throws SQLException, IOExcep @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.util.AccessVersion#getDefaultAccessVersion()") - void testNotLikeExternal(AccessVersion _accessVersion) throws SQLException, IOException { + void testNotLikeExternal(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); String tableName = "Tx21"; diff --git a/src/test/java/net/ucanaccess/test/integration/AlterTableTest.java b/src/test/java/net/ucanaccess/test/integration/AlterTableTest.java index f7799e2f..94cce2b1 100644 --- a/src/test/java/net/ucanaccess/test/integration/AlterTableTest.java +++ b/src/test/java/net/ucanaccess/test/integration/AlterTableTest.java @@ -1,5 +1,8 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import com.healthmarketscience.jackcess.*; import com.healthmarketscience.jackcess.Index.Column; import net.ucanaccess.jdbc.UcanaccessSQLException; @@ -15,8 +18,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; +import java.util.Map; class AlterTableTest extends UcanaccessBaseTest { @@ -28,9 +30,9 @@ protected String getAccessPath() { @Override protected void init(AccessVersion _accessVersion) throws SQLException { super.init(_accessVersion); - executeStatements("CREATE TABLE AAAn (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))", - "CREATE TABLE [AAA n] (baaaa TEXT(3), A INTEGER, C TEXT(4), b YESNO, " - + "d DATETIME DEFAULT NOW(), e NUMERIC(8,3), [f f] TEXT ) "); + executeStatements( + "CREATE TABLE AAAn (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))", + "CREATE TABLE [AAA n] (baaaa TEXT(3), A INTEGER, C TEXT(4), b YESNO, d DATETIME DEFAULT NOW(), e NUMERIC(8,3), [f f] TEXT)"); } @ParameterizedTest(name = "[{index}] {0}") @@ -40,13 +42,9 @@ void testRename(AccessVersion _accessVersion) throws SQLException, IOException { try (Statement st = ucanaccess.createStatement()) { st.execute("ALTER TABLE [??###] RENAME TO [1GIà GIà]"); - boolean b = false; - try { - st.execute("ALTER TABLE T4 RENAME TO [1GIà GIà]"); - } catch (SQLException e) { - b = true; - } - assertTrue(b); + assertThatThrownBy(() -> st.execute("ALTER TABLE T4 RENAME TO [1GIà GIà]")) + .isInstanceOf(SQLException.class) + .hasMessageContaining("object name already exists"); checkQuery("SELECT * FROM [1GIà GIà]"); dumpQueryResult("SELECT * FROM [1GIà GIà]"); getLogger().debug("After having renamed a few tables ..."); @@ -58,321 +56,243 @@ void testRename(AccessVersion _accessVersion) throws SQLException, IOException { @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) void testAddColumn(AccessVersion _accessVersion) throws SQLException, IOException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute("ALTER TABLE AAAn RENAME TO [GIà GIà]"); - st.execute("INSERT INTO [GIà GIà] (baaaa) values('chi')"); - checkQuery("SELECT * FROM [GIà GIà] ORDER BY c"); - dumpQueryResult("SELECT * FROM [GIà GIà] ORDER BY c"); - st.execute("ALTER TABLE [GIà GIà] RENAME TO [22 alterTableTest123]"); - checkQuery("SELECT * FROM [22 alterTableTest123] ORDER BY c"); - dumpQueryResult("SELECT * FROM [22 alterTableTest123] ORDER BY c"); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci] TEXT(100) NOT NULL DEFAULT 'PIPPO' "); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [健康] decimal (23,5) "); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [£健康] numeric (23,6) default 13.031955 not null"); - boolean b = false; - try { - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN defaultwwwdefault numeric (23,6) not null"); - } catch (UcanaccessSQLException e) { - b = true; - System.err.println(e.getMessage()); + + try (Statement st = ucanaccess.createStatement()) { + st.execute("ALTER TABLE AAAn RENAME TO [GIà GIà]"); + st.execute("INSERT INTO [GIà GIà] (baaaa) values('chi')"); + checkQuery("SELECT * FROM [GIà GIà] ORDER BY c"); + dumpQueryResult("SELECT * FROM [GIà GIà] ORDER BY c"); + st.execute("ALTER TABLE [GIà GIà] RENAME TO [22 alterTableTest123]"); + checkQuery("SELECT * FROM [22 alterTableTest123] ORDER BY c"); + dumpQueryResult("SELECT * FROM [22 alterTableTest123] ORDER BY c"); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci] TEXT(100) NOT NULL DEFAULT 'PIPPO'"); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [健康] decimal (23,5) "); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [£健康] numeric (23,6) default 13.031955 NOT NULL"); + assertThatThrownBy(() -> st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN defaultwwwdefault numeric (23,6) NOT NULL")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("When adding a new column"); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci1] DATETIME NOT NULL DEFAULT NOW()"); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci2] YESNO"); + st.execute("INSERT INTO [22 alterTableTest123] (baaaa) VALUES('cha')"); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN Memo Memo "); + st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN ole OLE "); + dumpQueryResult("SELECT * FROM [22 alterTableTest123] ORDER BY c"); + checkQuery("SELECT * FROM [22 alterTableTest123] ORDER BY c"); + + dumpQueryResult("SELECT * FROM Sample"); + checkQuery("SELECT * FROM Sample"); + st.executeUpdate("UPDATE sample SET Description='wRRRw'"); + dumpQueryResult("SELECT * FROM Sample"); + checkQuery("SELECT * FROM Sample"); + st.execute("ALTER TABLE Sample ADD COLUMN dt DATETIME DEFAULT NOW() "); + dumpQueryResult("SELECT * FROM Sample"); + checkQuery("SELECT * FROM Sample"); + st.execute("Update sample set Description='ww'"); + dumpQueryResult("SELECT * FROM Sample"); + checkQuery("SELECT * FROM Sample"); + + dumpQueryResult("SELECT * FROM UCA_METADATA.Columns"); + + createFK(); + + st.execute("ALTER TABLE Sample ADD COLUMN website HYPERLINK"); + ResultSet rs = ucanaccess.getMetaData().getColumns(null, null, "Sample", "website"); + rs.next(); + assertEquals("HYPERLINK", rs.getString("ORIGINAL_TYPE")); } - assertTrue(b); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci1] DATETIME NOT NULL DEFAULT now() "); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN [ci ci2] YESNO "); - st.execute("INSERT INTO [22 alterTableTest123] (baaaa) values('cha')"); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN Memo Memo "); - st.execute("ALTER TABLE [22 alterTableTest123] ADD COLUMN ole OLE "); - dumpQueryResult("SELECT * FROM [22 alterTableTest123] ORDER BY c"); - checkQuery("SELECT * FROM [22 alterTableTest123] ORDER BY c"); - - dumpQueryResult("SELECT * FROM Sample"); - checkQuery("SELECT * FROM Sample"); - st.executeUpdate("Update sample set Description='wRRRw'"); - dumpQueryResult("SELECT * FROM Sample"); - checkQuery("SELECT * FROM Sample"); - st.execute("ALTER TABLE Sample ADD COLUMN dt datetime default now() "); - dumpQueryResult("SELECT * FROM Sample"); - checkQuery("SELECT * FROM Sample"); - st.execute("Update sample set Description='ww'"); - dumpQueryResult("SELECT * FROM Sample"); - checkQuery("SELECT * FROM Sample"); - - dumpQueryResult("SELECT * FROM UCA_METADATA.Columns"); - - createFK(); - - st.execute("ALTER TABLE Sample ADD COLUMN website HYPERLINK"); - ResultSet rs = ucanaccess.getMetaData().getColumns(null, null, "Sample", "website"); - rs.next(); - assertEquals("HYPERLINK", rs.getString("ORIGINAL_TYPE")); - st.close(); } @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) void testCreateIndex(AccessVersion _accessVersion) throws SQLException, IOException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute("CREATE unique INDEX [èèè 23] on [AAA n] (a ASC,c ASC )"); - boolean b = false; - try { - st.execute("INSERT INT0 [AAA n] (a,C ) values (24,'su')"); - st.execute("INSERT INT0 [AAA n] (a,C ) values (24,'su')"); - } catch (Exception e) { - b = true; - } - assertTrue(b); - Database db = ucanaccess.getDbIO(); - Table tb = db.getTable("AAA n"); - - boolean found = false; - for (Index idx : tb.getIndexes()) { - if ("èèè 23".equals(idx.getName()) && idx.isUnique()) { - found = true; - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertTrue(ar.contains("A")); - assertTrue(ar.contains("C")); - } - } - assertTrue(found); - found = false; - st.execute("CREATE INDEX [健 康] on [AAA n] (c DESC )"); - for (Index idx : tb.getIndexes()) { - if ("健 康".equals(idx.getName()) && !idx.isUnique()) { - found = true; - assertEquals("C", idx.getColumns().get(0).getName()); - - } - } - st.execute("CREATE INDEX [%健 康] on [AAA n] (b,d,e )"); - for (Index idx : tb.getIndexes()) { - if ("%健 康".equals(idx.getName()) && !idx.isUnique()) { - found = true; - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertEquals(3, ar.size()); - assertTrue(ar.contains("b")); - assertTrue(ar.contains("d")); - assertTrue(ar.contains("e")); - - } - } - - st.execute("CREATE INDEX ciao on Sample (description)"); - for (Index idx : tb.getIndexes()) { - if ("ciao".equals(idx.getName()) && !idx.isUnique()) { - found = true; - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertEquals(1, ar.size()); - assertTrue(ar.contains("field")); - - } + try (Statement st = ucanaccess.createStatement()) { + String tbl = "AAA n"; + st.execute("CREATE UNIQUE INDEX [èèè 23] on [" + tbl + "] (a ASC, c ASC)"); + st.execute("INSERT INTO [" + tbl + "] (a, C) VALUES (24, 'su')"); + assertThatThrownBy(() -> st.execute("INSERT INTO [" + tbl + "] (a, C) VALUES (24, 'su')")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("unique constraint or index violation"); + Database db = ucanaccess.getDbIO(); + db.getTable(tbl).getIndexes().stream().filter(Index::isUnique).filter(idx -> "èèè 23".equals(idx.getName())).findFirst() + .ifPresentOrElse(idx -> assertThat(idx.getColumns().stream().map(Column::getName)).contains("A", "C"), () -> fail("Unique index not found")); + + st.execute("CREATE INDEX [健 康] on [" + tbl + "] (c DESC)"); + db.getTable(tbl).getIndexes().stream().filter(idx -> !idx.isUnique()).filter(idx -> "健 康".equals(idx.getName())).findFirst() + .ifPresentOrElse(idx -> assertEquals("C", idx.getColumns().get(0).getName()), () -> fail("Index not found")); + + st.execute("CREATE INDEX [%健 康] on [" + tbl + "] (b, d, e)"); + db.getTable(tbl).getIndexes().stream().filter(idx -> !idx.isUnique()).filter(idx -> "%健 康".equals(idx.getName())).findFirst() + .ifPresentOrElse(idx -> assertThat(idx.getColumns().stream().map(Column::getName)) + .containsExactly("b", "d", "e"), () -> fail("Index not found")); + + st.execute("CREATE INDEX ciao on Sample (Description)"); + db.getTable("Sample").getIndexes().stream().filter(idx -> !idx.isUnique()).filter(idx -> "ciao".equals(idx.getName())).findFirst() + .ifPresentOrElse(idx -> assertThat(idx.getColumns().stream().map(Column::getName)) + .containsExactly("Description"), () -> fail("Index not found")); } - - assertTrue(found); - st.close(); } @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) - void testCreatePK(AccessVersion _accessVersion) throws SQLException, IOException { + void testCreatePk(AccessVersion _accessVersion) throws SQLException, IOException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - - st.execute("ALTER TABLE [AAA n] add Primary key (baaaa,a)"); - Database db = ucanaccess.getDbIO(); - Table tb = db.getTable("AAA n"); - Index idx = tb.getPrimaryKeyIndex(); - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertTrue(ar.contains("A")); - assertTrue(ar.contains("baaaa")); - - st.execute("ALTER TABLE Sample add Primary key (RegionId)"); - tb = db.getTable("Sample"); - idx = tb.getPrimaryKeyIndex(); - ar.clear(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertTrue(ar.contains("RegionId")); - st.close(); + try (Statement st = ucanaccess.createStatement()) { + String tbl = "AAA n"; + st.execute("ALTER TABLE [" + tbl + "] ADD PRIMARY KEY (baaaa, a)"); + Database db = ucanaccess.getDbIO(); + assertThat(db.getTable(tbl).getPrimaryKeyIndex().getColumns().stream().map(Column::getName)) + .containsExactly("baaaa", "A"); + + st.execute("ALTER TABLE Sample ADD PRIMARY KEY (RegionId)"); + assertThat(db.getTable("Sample").getPrimaryKeyIndex().getColumns().stream().map(Column::getName)) + .containsExactly("RegionId"); + } } - private void createFK() throws SQLException, IOException { - Statement st = null; - st = ucanaccess.createStatement(); - - // test case: constraint name specified - st.execute( - "ALTER TABLE [AAA n] add constraint [pippo1] foreign key (c) references [22 alterTableTest123] (baaaa) ON delete cascade"); - Database db = ucanaccess.getDbIO(); - Table tb = db.getTable("AAA n"); - Table tbr = db.getTable("22 alterTableTest123"); - Index idx = tb.getForeignKeyIndex(tbr); - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); - } - assertTrue(ar.contains("C")); - // - // also verify that the Relationship name was actually used in the Access database ... - tb = db.getSystemTable("MSysRelationships"); - IndexCursor crsr = CursorBuilder.createCursor(tb.getIndex("szRelationship")); - Row r = crsr.findRowByEntry("pippo1"); - assertNotNull(r); - // ... and the right name was used in the HSQLDB database - Connection hsqldbConn = ucanaccess.getHSQLDBConnection(); - Statement hsqldbStmt = hsqldbConn.createStatement(); - ResultSet hsqldbRs = - hsqldbStmt.executeQuery("SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS " - + "WHERE CONSTRAINT_TYPE='FOREIGN KEY' AND TABLE_NAME='AAA N'"); - hsqldbRs.next(); - assertEquals("AAA N_PIPPO1", hsqldbRs.getString(1)); - // - // now test dropping the foreign key - // first try with Hibernate mode inactive - HibernateSupport.setActive(false); - try { + void createFK() throws SQLException, IOException { + try (Statement st = ucanaccess.createStatement()) { + // test case: constraint name specified + st.execute("ALTER TABLE [AAA n] ADD CONSTRAINT [pippo1] FOREIGN KEY (c) REFERENCES [22 alterTableTest123] (baaaa) ON DELETE CASCADE"); + Database db = ucanaccess.getDbIO(); + Table tb = db.getTable("AAA n"); + Table tbr = db.getTable("22 alterTableTest123"); + assertThat(tb.getForeignKeyIndex(tbr).getColumns().stream().map(Column::getName)) + .containsExactly("C"); + + // also verify that the Relationship name was actually used in the Access database ... + tb = db.getSystemTable("MSysRelationships"); + IndexCursor crsr = CursorBuilder.createCursor(tb.getIndex("szRelationship")); + Row r = crsr.findRowByEntry("pippo1"); + assertNotNull(r); + // ... and the right name was used in the HSQLDB database + Connection hsqldbConn = ucanaccess.getHSQLDBConnection(); + Statement hsqldbStmt = hsqldbConn.createStatement(); + ResultSet hsqldbRs = + hsqldbStmt.executeQuery("SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS " + + "WHERE CONSTRAINT_TYPE='FOREIGN KEY' AND TABLE_NAME='AAA N'"); + hsqldbRs.next(); + assertEquals("AAA N_PIPPO1", hsqldbRs.getString(1)); + + // now test dropping the foreign key + // first try with Hibernate mode inactive + HibernateSupport.setActive(false); + assertThatThrownBy(() -> st.execute("ALTER TABLE [AAA n] DROP CONSTRAINT [pippo1]")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("DROP CONSTRAINT is only supported for Hibernate hbm2ddl"); + // now try again with Hibernate mode active + HibernateSupport.setActive(true); st.execute("ALTER TABLE [AAA n] DROP CONSTRAINT [pippo1]"); - fail("UcanaccessSQLException should have been thrown"); - } catch (UcanaccessSQLException ignored) {} - // now try again with Hibernate mode active - HibernateSupport.setActive(true); - st.execute("ALTER TABLE [AAA n] DROP CONSTRAINT [pippo1]"); - // and verify that it actually got dropped - try { - st.execute("ALTER TABLE [AAA n] DROP CONSTRAINT [pippo1]"); // again - fail("UcanaccessSQLException should have been thrown"); - } catch (UcanaccessSQLException ignored) {} - HibernateSupport.setActive(null); - - // test case: constraint name not specified - st.execute("ALTER TABLE Son add foreign key (integer, txt) references Father(id,txt) ON delete cascade"); - st.close(); + // and verify that it actually got dropped + assertThatThrownBy(() -> st.execute("ALTER TABLE [AAA n] DROP CONSTRAINT [pippo1]")) // again + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("object not found"); + HibernateSupport.setActive(null); + + // test case: constraint name not specified + st.execute("ALTER TABLE Son ADD FOREIGN KEY (integer, txt) REFERENCES Father(id, txt) ON DELETE CASCADE"); + } } @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) void testDoubleRelationship(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); + // repro code from https://stackoverflow.com/q/49160150/2144390 - Statement statement = ucanaccess.createStatement(); - // - String tableToBeReferenced = "PersonsTable"; - String tableWithTheReferences = "RelationShipsTable"; - - statement.execute("CREATE TABLE " + tableToBeReferenced + " (ID autoincrement NOT NULL PRIMARY KEY, " - + "Name VARCHAR(255) " - + ")"); - - statement.execute("CREATE TABLE " + tableWithTheReferences + " (ID LONG NOT NULL PRIMARY KEY, " - + "RelationShip VARCHAR(255) NOT NULL DEFAULT 'FRIENDS', " - + "Person1Id LONG NOT NULL, " - + "Person2Id LONG NOT NULL)"); - - // reference #1 - statement.execute("ALTER TABLE " + tableWithTheReferences - + " ADD CONSTRAINT FOREIGN_KEY_1 FOREIGN KEY (Person1Id) REFERENCES " - + tableToBeReferenced + "(ID) ON DELETE CASCADE"); - - // reference #2 - statement.execute("ALTER TABLE " + tableWithTheReferences - + " ADD CONSTRAINT FOREIGN_KEY_2 FOREIGN KEY (Person2Id) REFERENCES " - + tableToBeReferenced + "(ID) ON DELETE CASCADE"); + try (Statement st = ucanaccess.createStatement()) { + String tableToBeReferenced = "PersonsTable"; + String tableWithTheReferences = "RelationShipsTable"; + + st.execute("CREATE TABLE " + tableToBeReferenced + " (ID AUTOINCREMENT NOT NULL PRIMARY KEY, " + + "Name VARCHAR(255))"); + + st.execute("CREATE TABLE " + tableWithTheReferences + " (ID LONG NOT NULL PRIMARY KEY, " + + "RelationShip VARCHAR(255) NOT NULL DEFAULT 'FRIENDS', " + + "Person1Id LONG NOT NULL, " + + "Person2Id LONG NOT NULL)"); + + // reference #1 + st.execute("ALTER TABLE " + tableWithTheReferences + + " ADD CONSTRAINT FOREIGN_KEY_1 FOREIGN KEY (Person1Id) REFERENCES " + + tableToBeReferenced + "(ID) ON DELETE CASCADE"); + + // reference #2 + st.execute("ALTER TABLE " + tableWithTheReferences + + " ADD CONSTRAINT FOREIGN_KEY_2 FOREIGN KEY (Person2Id) REFERENCES " + + tableToBeReferenced + "(ID) ON DELETE CASCADE"); } } @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) void testMiscellaneous(AccessVersion _accessVersion) throws SQLException, IOException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute("ALTER TABLE tx add constraint pk primary key ([i d]) "); - st.execute("ALTER TABLE tx add column [my best friend] long "); - st.execute("ALTER TABLE tx add column [my worst friend] single "); - st.execute("ALTER TABLE tx add column [Is Pippo] TEXT(100) "); - st.execute("ALTER TABLE tx add column [Is not Pippo]TEXT default \"what's this?\""); - - st.execute("create TABLE tx1 (n1 long, [n 2] text)"); - st.execute("ALTER TABLE tx1 add primary key (n1, [n 2])"); - st.execute( - "ALTER TABLE tx add foreign key ([my best friend],[Is Pippo])references tx1(n1, [n 2])ON delete cascade"); - st.execute("INSERT INTO tx1 values(1,\"ciao\")"); - st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) values(1,2,\"ciao\")"); - checkQuery("SELECT COUNT(*) FROM tx", 1); - st.execute("DELETE FROM tx1"); - checkQuery("SELECT COUNT(*) FROM tx"); - checkQuery("SELECT COUNT(*) FROM tx", 0); - st.execute("DROP TABLE tx "); - st.execute("DROP TABLE tx1 "); - - st.execute( - "CREATE TABLE tx (id counter primary key, [my best friend]long , [my worst friend] single,[Is Pippo] TEXT(100) ,[Is not Pippo]TEXT default \"what's this?\" )"); - st.execute("create TABLE tx1 (n1 long, [n 2] text)"); - st.execute("ALTER TABLE tx1 add primary key (n1, [n 2])"); - st.execute( - "ALTER TABLE tx add foreign key ([my best friend],[Is Pippo])references tx1(n1, [n 2])ON delete set null"); - st.execute("INSERT INTO tx1 values(1,\"ciao\")"); - st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) values(1,2,\"ciao\")"); - checkQuery("SELECT COUNT(*) FROM tx", 1); - st.execute("DELETE FROM tx1"); - checkQuery("SELECT COUNT(*) FROM tx", 1); - checkQuery("SELECT * FROM tx", 1, null, 2.0, null, "what's this?"); - st.execute("CREATE UNIQUE INDEX IDX111 ON tx ([my best friend])"); - - boolean b = false; - try { - st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) values(1,2,\"ciao\")"); - } catch (UcanaccessSQLException e) { - b = true; - System.err.println(e.getMessage()); - } - assertTrue(b); - st.close(); - } - private void executeErr(String _ddl, String _expectedMessage) { try (Statement st = ucanaccess.createStatement()) { - st.execute(_ddl); - } catch (SQLException _ex) { - assertTrue(_ex.getMessage().endsWith(_expectedMessage)); - return; + st.execute("ALTER TABLE tx ADD CONSTRAINT pk PRIMARY KEY ([i d]) "); + st.execute("ALTER TABLE tx ADD COLUMN [my best friend] LONG "); + st.execute("ALTER TABLE tx ADD COLUMN [my worst friend] SINGLE "); + st.execute("ALTER TABLE tx ADD COLUMN [Is Pippo] TEXT(100) "); + st.execute("ALTER TABLE tx ADD COLUMN [Is not Pippo] TEXT DEFAULT \"what's this?\""); + + st.execute("CREATE TABLE tx1 (n1 long, [n 2] text)"); + st.execute("ALTER TABLE tx1 add primary key (n1, [n 2])"); + st.execute("ALTER TABLE tx ADD FOREIGN KEY ([my best friend], [Is Pippo]) REFERENCES tx1(n1, [n 2]) ON DELETE CASCADE"); + st.execute("INSERT INTO tx1 values(1,\"ciao\")"); + st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) values(1, 2, \"ciao\")"); + checkQuery("SELECT COUNT(*) FROM tx", 1); + st.execute("DELETE FROM tx1"); + checkQuery("SELECT COUNT(*) FROM tx"); + checkQuery("SELECT COUNT(*) FROM tx", 0); + st.execute("DROP TABLE tx "); + st.execute("DROP TABLE tx1 "); + + st.execute("CREATE TABLE tx (id COUNTER PRIMARY KEY, [my best friend] LONG, [my worst friend] SINGLE, [Is Pippo] TEXT(100), [Is not Pippo] TEXT default \"what's this?\")"); + st.execute("CREATE TABLE tx1 (n1 LONG, [n 2] TEXT)"); + st.execute("ALTER TABLE tx1 ADD PRIMARY KEY (n1, [n 2])"); + st.execute("ALTER TABLE tx ADD FOREIGN KEY ([my best friend], [Is Pippo]) REFERENCES tx1(n1, [n 2]) ON DELETE SET NULL"); + st.execute("INSERT INTO tx1 VALUES(1, \"ciao\")"); + st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) VALUES(1, 2, \"ciao\")"); + checkQuery("SELECT COUNT(*) FROM tx", 1); + st.execute("DELETE FROM tx1"); + checkQuery("SELECT COUNT(*) FROM tx", 1); + checkQuery("SELECT * FROM tx", 1, null, 2.0, null, "what's this?"); + st.execute("CREATE UNIQUE INDEX IDX111 ON tx ([my best friend])"); + + assertThatThrownBy(() -> st.execute("INSERT INTO tx ([my best friend], [my worst friend], [Is Pippo]) values(1, 2, \"ciao\")")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("integrity constraint violation: foreign key no parent"); } - fail("Should have encountered error: " + _expectedMessage); } @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class, mode = Mode.INCLUDE, names = {"V2007"}) void testSqlErrors(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute( - "CREATE TABLE tx2 (id counter , [my best friend]long , [my worst friend] single,[Is Pippo] TEXT(100) ,[Is not Pippo]TEXT default \"what's this?\" )"); - st.execute("INSERT INTO tx2 ([my best friend], [my worst friend], [Is Pippo]) values(1,2,\"ciao\")"); - executeErr("ALTER TABLE tx2 add constraint primary key ([i d]) ", "unexpected token: PRIMARY"); - executeErr("ALTER TABLE tx2 add column [my best friend] ", "unexpected end of statement"); - executeErr( - "ALTER TABLE tx2 add constraint foreign key ([my best friend],[Is Pippo])references tx1(n1, [n 2])ON delete cascade", - "type not found or user lacks privilege: FOREIGN"); - executeErr("DROP TABLE tx2 cascade", "Feature not supported yet."); - executeErr("ALTER TABLE tx2 add constraint primary key (id)", "unexpected token: PRIMARY"); - executeErr("ALTER TABLE tx2 ALTER COLUMN [my best friend] SET DEFAULT 33", "Feature not supported yet."); - executeErr("ALTER TABLE tx2 drop COLUMN [my best friend]", "Feature not supported yet."); - executeErr("ALTER TABLE tx2 add COLUMN [1 my best friend]lonG not null", - "x2 already contains one or more records(1 records)"); - st.close(); + + try (Statement st = ucanaccess.createStatement()) { + st.execute("CREATE TABLE tx2 (id COUNTER, [my best friend] LONG, [my worst friend] SINGLE, [Is Pippo] TEXT(100), [Is not Pippo] TEXT DEFAULT \"what's this?\")"); + st.execute("INSERT INTO tx2 ([my best friend], [my worst friend], [Is Pippo]) VALUES(1, 2, \"ciao\")"); + + Map.of( + "ALTER TABLE tx2 ADD CONSTRAINT PRIMARY KEY ([i d]) ", "unexpected token: PRIMARY", + "ALTER TABLE tx2 ADD COLUMN [my best friend] ", "unexpected end of statement", + "ALTER TABLE tx2 ADD CONSTRAINT FOREIGN KEY ([my best friend], [Is Pippo]) REFERENCES tx1(n1, [n 2]) ON DELETE CASCADE", "type not found or user lacks privilege: FOREIGN", + "DROP TABLE tx2 CASCADE", "Feature not supported.", + "ALTER TABLE tx2 ADD CONSTRAINT PRIMARY KEY (id)", "unexpected token: PRIMARY", + "ALTER TABLE tx2 ALTER COLUMN [my best friend] SET DEFAULT 33", "Feature not supported.", + "ALTER TABLE tx2 DROP COLUMN [my best friend]", "Feature not supported.", + "ALTER TABLE tx2 ADD COLUMN [1 my best friend] LONG NOT NULL", "x2 already contains one or more records(1 records)").entrySet().stream().forEach( + e -> { + String ddl = e.getKey(); + String expectedMessage = e.getValue(); + assertThatThrownBy(() -> st.execute(ddl)) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageEndingWith(expectedMessage); + }); + } } } diff --git a/src/test/java/net/ucanaccess/test/integration/BlobOleLazyLoadingTest.java b/src/test/java/net/ucanaccess/test/integration/BlobOleLazyLoadingTest.java index b0f87f03..1cfc2424 100644 --- a/src/test/java/net/ucanaccess/test/integration/BlobOleLazyLoadingTest.java +++ b/src/test/java/net/ucanaccess/test/integration/BlobOleLazyLoadingTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -30,7 +32,7 @@ void testBlobOLE(AccessVersion _accessVersion) throws SQLException, IOException final long binaryFileSize = 32718; byte[] initialBlobBytes = getBlobBytes(); getLogger().info("BLOB size in backing database before retrieval: {} bytes", initialBlobBytes.length); - assertTrue(initialBlobBytes.length < binaryFileSize); + assertThat((long) initialBlobBytes.length).isLessThan(binaryFileSize); Statement st = ucanaccess.createStatement(); ResultSet rs = st.executeQuery("SELECT Ole FROM OleTable ORDER BY ID"); File fl = createTempFileName("Copied", ".jpeg"); diff --git a/src/test/java/net/ucanaccess/test/integration/BlobOleTest.java b/src/test/java/net/ucanaccess/test/integration/BlobOleTest.java index ef2f3483..fd5dd449 100644 --- a/src/test/java/net/ucanaccess/test/integration/BlobOleTest.java +++ b/src/test/java/net/ucanaccess/test/integration/BlobOleTest.java @@ -125,8 +125,8 @@ void testTwoColumnPk(AccessVersion _accessVersion) throws SQLException { // fix for ticket #23 should prevent this test from throwing an error try (Statement st = ucanaccess.createStatement()) { - assertDoesNotThrow(() -> st.execute("CREATE TABLE t_two_col_pk (pk_col1 LONG, pk_col2 LONG, blob_col OLE, " - + "CONSTRAINT pk_t_two_col_pk PRIMARY KEY (pk_col1, pk_col2))")); + st.execute("CREATE TABLE t_two_col_pk (pk_col1 LONG, pk_col2 LONG, blob_col OLE, " + + "CONSTRAINT pk_t_two_col_pk PRIMARY KEY (pk_col1, pk_col2))"); } try (PreparedStatement ps = ucanaccess.prepareStatement("INSERT INTO t_two_col_pk VALUES (?, ?, ?)")) { diff --git a/src/test/java/net/ucanaccess/test/integration/BooleanTest.java b/src/test/java/net/ucanaccess/test/integration/BooleanTest.java index bc4a8b4d..4cc1fc80 100644 --- a/src/test/java/net/ucanaccess/test/integration/BooleanTest.java +++ b/src/test/java/net/ucanaccess/test/integration/BooleanTest.java @@ -25,7 +25,7 @@ protected String getAccessPath() { protected void init(AccessVersion _accessVersion) throws SQLException { super.init(_accessVersion); executeStatements( - "CREATE TABLE tblMain (ID int NOT NULL PRIMARY KEY, company TEXT NOT NULL, Closed YESNO)", + "CREATE TABLE tblMain (id INT NOT NULL PRIMARY KEY, company TEXT NOT NULL, closed YESNO)", "INSERT INTO tblMain (id, company) VALUES(1, 'pippo')", "UPDATE tblMain SET closed=yes", "INSERT INTO t (pk) VALUES('pippo')"); diff --git a/src/test/java/net/ucanaccess/test/integration/CalculatedFieldTest.java b/src/test/java/net/ucanaccess/test/integration/CalculatedFieldTest.java index 0a40cc02..978acdc7 100644 --- a/src/test/java/net/ucanaccess/test/integration/CalculatedFieldTest.java +++ b/src/test/java/net/ucanaccess/test/integration/CalculatedFieldTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import net.ucanaccess.jdbc.UcanaccessSQLException; import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; @@ -45,8 +47,9 @@ void testFunctionBuiltInCall(AccessVersion _accessVersion) throws Exception { {"", "", "", "", ""}}); // inserting NULL into a calculated column containing a STRING formula is not permitted - UcanaccessSQLException ex = assertThrows(UcanaccessSQLException.class, () -> st.execute("INSERT INTO clcdFlds (input) VALUES (null)")); - assertContains(ex.getMessage(), "Value[NULL] 'null' cannot be converted to STRING"); + assertThatThrownBy(() -> st.execute("INSERT INTO clcdFlds (input) VALUES (null)")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("Value[NULL] 'null' cannot be converted to STRING"); } } diff --git a/src/test/java/net/ucanaccess/test/integration/CloseOnCompletionTest.java b/src/test/java/net/ucanaccess/test/integration/CloseOnCompletionTest.java index d591eff8..73acca24 100644 --- a/src/test/java/net/ucanaccess/test/integration/CloseOnCompletionTest.java +++ b/src/test/java/net/ucanaccess/test/integration/CloseOnCompletionTest.java @@ -6,17 +6,18 @@ import org.junit.jupiter.params.provider.EnumSource; import java.sql.PreparedStatement; +import java.sql.SQLException; class CloseOnCompletionTest extends UcanaccessBaseTest { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testCloseOnCompletion(AccessVersion _accessVersion) throws Exception { + void testCloseOnCompletion(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); try (PreparedStatement st = ucanaccess.prepareStatement("CREATE TABLE pluto1 (id varchar(23))")) { st.closeOnCompletion(); - assertDoesNotThrow(() -> st.execute()); + st.execute(); } } diff --git a/src/test/java/net/ucanaccess/test/integration/CounterTest.java b/src/test/java/net/ucanaccess/test/integration/CounterTest.java index 5b30df3e..da7b993f 100644 --- a/src/test/java/net/ucanaccess/test/integration/CounterTest.java +++ b/src/test/java/net/ucanaccess/test/integration/CounterTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import java.io.IOException; import java.sql.SQLException; import java.sql.Statement; @@ -26,7 +25,7 @@ void afterEachTest() throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testCreateTypes(AccessVersion _accessVersion) throws SQLException, IOException { + void testCreateTypes(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); try (Statement st = ucanaccess.createStatement()) { st.execute("DISABLE AUTOINCREMENT ON " + tableName); diff --git a/src/test/java/net/ucanaccess/test/integration/CreateTableTest.java b/src/test/java/net/ucanaccess/test/integration/CreateTableTest.java index e5215980..6adb2960 100644 --- a/src/test/java/net/ucanaccess/test/integration/CreateTableTest.java +++ b/src/test/java/net/ucanaccess/test/integration/CreateTableTest.java @@ -1,7 +1,9 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import com.healthmarketscience.jackcess.Database; -import com.healthmarketscience.jackcess.Index; import com.healthmarketscience.jackcess.Index.Column; import com.healthmarketscience.jackcess.PropertyMap; import com.healthmarketscience.jackcess.Table; @@ -17,8 +19,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; class CreateTableTest extends UcanaccessBaseTest { @@ -27,40 +27,40 @@ protected String getAccessPath() { return TEST_DB_DIR + "badDb.accdb"; } - private void createAsSelect() throws SQLException, IOException { - Statement st = ucanaccess.createStatement(); - st.executeUpdate("CREATE TABLE AAA_BIS as (SELECT baaaa,a,c FROM AAA) WITH DATA"); - Object[][] ver = {{"33A", 3, "G"}, {"33B", 111, "G"}}; - checkQuery("SELECT * FROM AAA_bis ORDER BY baaaa", ver); - st.executeUpdate("CREATE TABLE AAA_quadris as (SELECT AAA.baaaa,AAA_BIS.baaaa as xxx FROM AAA,AAA_BIS) WITH DATA"); - dumpQueryResult("SELECT * FROM AAA_quadris ORDER BY baaaa"); - st.close(); + private void createAsSelect() throws SQLException { + try (Statement st = ucanaccess.createStatement()) { + st.executeUpdate("CREATE TABLE AAA_BIS as (SELECT baaaa,a,c FROM AAA) WITH DATA"); + Object[][] ver = {{"33A", 3, "G"}, {"33B", 111, "G"}}; + checkQuery("SELECT * FROM AAA_bis ORDER BY baaaa", ver); + st.executeUpdate("CREATE TABLE AAA_quadris as (SELECT AAA.baaaa,AAA_BIS.baaaa as xxx FROM AAA,AAA_BIS) WITH DATA"); + dumpQueryResult("SELECT * FROM AAA_quadris ORDER BY baaaa"); + } } - private void createAsSelect2() throws SQLException, IOException { + private void createAsSelect2() throws SQLException { try (Statement st = ucanaccess.createStatement()) { - st.executeUpdate("CREATE TABLE AAA_TRIS as (SELECT baaaa,a,c FROM AAA) WITH no DATA "); + st.executeUpdate("CREATE TABLE AAA_TRIS as (SELECT baaaa,a,c FROM AAA) WITH NO DATA"); st.execute("INSERT INTO AAA_TRIS SELECT * FROM AAA_bis"); Object[][] ver = {{"33A", 3, "G"}, {"33B", 111, "G"}}; checkQuery("SELECT * FROM AAA_tris ORDER BY baaaa", ver); } } - private void createPs() { - try (PreparedStatement ps = ucanaccess.prepareStatement(" CREATE \nTABLE BBB ( baaaa \nvarchar(2) PRIMARY KEY)")) { - ps.execute(" CREATE TABLE BBB ( baaaa text PRIMARY KEY,b text)"); - throw new RuntimeException("To block DDL with PreparedStatement"); - } catch (SQLException ex) { - getLogger().info("ok"); + private void createPs() throws SQLException { + try (Statement st = ucanaccess.createStatement()) { + st.executeUpdate("CREATE \nTABLE BBB (baaaa \nVARCHAR(2) PRIMARY KEY)"); + assertThatThrownBy(() -> st.execute("CREATE TABLE BBB (baaaa TEXT PRIMARY KEY, b TEXT)")) + .isInstanceOf(SQLException.class) + .hasMessageContaining("object name already exists"); } } - private void createSimple() throws SQLException, IOException { + private void createSimple() throws SQLException { Statement st = ucanaccess.createStatement(); - st.execute("INSERT INTO AAA(baaaa,c) VALUES ('33A','G' )"); - st.execute("INSERT INTO AAA(baaaa,a,c) VALUES ('33B',111,'G' )"); + st.execute("INSERT INTO AAA(baaaa, c) VALUES ('33A', 'G')"); + st.execute("INSERT INTO AAA(baaaa, a, c) VALUES ('33B', 111, 'G')"); Object[][] ver = {{"33A", 3, "G"}, {"33B", 111, "G"}}; - checkQuery("SELECT baaaa,a,c FROM AAA ORDER BY baaaa", ver); + checkQuery("SELECT baaaa, a, c FROM AAA ORDER BY baaaa", ver); st.close(); } @@ -105,7 +105,7 @@ void setDPK() throws SQLException, IOException { st = ucanaccess.createStatement(); st.execute("alter table dtrx ADD CONSTRAINT pk_dtrx PRIMARY KEY (c,number)"); st.close(); - } catch (Exception e) { + } catch (Exception _ex) { ucanaccess.rollback(); } st = ucanaccess.createStatement(); @@ -201,24 +201,16 @@ void testNaming(AccessVersion _accessVersion) throws SQLException { void testCreateWithFK(AccessVersion _accessVersion) throws SQLException, IOException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute(" CREATE TABLE Parent( x autoincrement PRIMARY KEY, y text(222))"); - st.execute( - " CREATE TABLE Babe( k LONG , y LONG, PRIMARY KEY(k,y), FOREIGN KEY (y) REFERENCES Parent (x) )"); - Database db = ucanaccess.getDbIO(); - Table tb = db.getTable("Babe"); - Table tbr = db.getTable("Parent"); - Index idx = tb.getForeignKeyIndex(tbr); - List ar = new ArrayList<>(); - for (Column cl : idx.getColumns()) { - ar.add(cl.getName()); + try (Statement st = ucanaccess.createStatement()) { + st.execute("CREATE TABLE Parent(x AUTOINCREMENT PRIMARY KEY, y text(222))"); + st.execute("CREATE TABLE Babe(k LONG, y LONG, PRIMARY KEY(k, y), FOREIGN KEY (y) REFERENCES Parent (x))"); + Database db = ucanaccess.getDbIO(); + Table tb = db.getTable("Babe"); + Table tbr = db.getTable("Parent"); + assertThat(tb.getForeignKeyIndex(tbr).getColumns().stream().map(Column::getName)).contains("y"); + st.execute("CREATE TABLE [1 Parent]( [x 0] long , y long, PRIMARY KEY([x 0],y))"); + st.execute("CREATE TABLE [1 Babe]( k LONG , y LONG, [0 z] LONG, PRIMARY KEY(k,y), FOREIGN KEY (y,[0 z] ) REFERENCES [1 Parent] ( [x 0] , y) )"); } - assertTrue(ar.contains("y")); - st.execute(" CREATE TABLE [1 Parent]( [x 0] long , y long, PRIMARY KEY([x 0],y))"); - st.execute( - " CREATE TABLE [1 Babe]( k LONG , y LONG, [0 z] LONG, PRIMARY KEY(k,y), FOREIGN KEY (y,[0 z] ) REFERENCES [1 Parent] ( [x 0] , y) )"); - - st.close(); } @ParameterizedTest(name = "[{index}] {0}") @@ -252,34 +244,27 @@ void testPsHyphen(AccessVersion _accessVersion) throws SQLException { void testCreateHyperlink(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - Statement st = ucanaccess.createStatement(); - st.execute("CREATE TABLE urlTest (id LONG PRIMARY KEY, website HYPERLINK)"); - st.execute("INSERT INTO urlTest (id, website) VALUES (1, '#http://whatever#')"); - st.execute("INSERT INTO urlTest (id, website) VALUES (2, 'example.com#http://example.com#')"); - st.execute("INSERT INTO urlTest (id, website) VALUES (3, 'the works#http://burger#with_bacon#and_cheese')"); - st.execute("INSERT INTO urlTest (id, website) VALUES (4, 'http://bad_link_no_hash_characters')"); - ResultSet rs = ucanaccess.getMetaData().getColumns(null, null, "urlTest", "website"); - rs.next(); - assertEquals("HYPERLINK", rs.getString("ORIGINAL_TYPE")); - rs = st.executeQuery("SELECT website FROM urlTest ORDER BY id"); - rs.next(); - assertEquals("http://whatever", rs.getURL(1).toString()); - rs.next(); - assertEquals("http://example.com", rs.getURL(1).toString()); - rs.next(); - assertEquals("http://burger#with_bacon", rs.getURL(1).toString()); - rs.next(); - try { - rs.getURL(1); - fail("UcanaccessSQLException should have been thrown"); - } catch (UcanaccessSQLException use) { - if (!use.getMessage().endsWith("Invalid or unsupported URL format")) { - throw use; - } + try (Statement st = ucanaccess.createStatement()) { + st.execute("CREATE TABLE urlTest (id LONG PRIMARY KEY, website HYPERLINK)"); + st.execute("INSERT INTO urlTest (id, website) VALUES (1, '#http://whatever#')"); + st.execute("INSERT INTO urlTest (id, website) VALUES (2, 'example.com#http://example.com#')"); + st.execute("INSERT INTO urlTest (id, website) VALUES (3, 'the works#http://burger#with_bacon#and_cheese')"); + st.execute("INSERT INTO urlTest (id, website) VALUES (4, 'http://bad_link_no_hash_characters')"); + ResultSet rs1 = ucanaccess.getMetaData().getColumns(null, null, "urlTest", "website"); + rs1.next(); + assertEquals("HYPERLINK", rs1.getString("ORIGINAL_TYPE")); + ResultSet rs2 = st.executeQuery("SELECT website FROM urlTest ORDER BY id"); + rs2.next(); + assertEquals("http://whatever", rs2.getURL(1).toString()); + rs2.next(); + assertEquals("http://example.com", rs2.getURL(1).toString()); + rs2.next(); + assertEquals("http://burger#with_bacon", rs2.getURL(1).toString()); + rs2.next(); + assertThatThrownBy(() -> rs2.getURL(1)) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageEndingWith("Invalid or unsupported URL format"); } - rs.close(); - st.close(); - } @ParameterizedTest(name = "[{index}] {0}") diff --git a/src/test/java/net/ucanaccess/test/integration/CrudTest.java b/src/test/java/net/ucanaccess/test/integration/CrudTest.java index 69fbcea3..950c04b3 100644 --- a/src/test/java/net/ucanaccess/test/integration/CrudTest.java +++ b/src/test/java/net/ucanaccess/test/integration/CrudTest.java @@ -21,7 +21,7 @@ protected void init(AccessVersion _accessVersion) throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testCrud(AccessVersion _accessVersion) throws SQLException, IOException { + void testCrud(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); try (Statement st = ucanaccess.createStatement()) { @@ -40,7 +40,7 @@ void testCrud(AccessVersion _accessVersion) throws SQLException, IOException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testCrudPS(AccessVersion _accessVersion) throws SQLException, IOException { + void testCrudPS(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); int id1 = 1234; @@ -98,7 +98,7 @@ void testCrudPSBatch(AccessVersion _accessVersion) throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testUpdatableRS(AccessVersion _accessVersion) throws SQLException, IOException { + void testUpdatableRS(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); try (Statement st = ucanaccess.createStatement()) { int id = 6666554; @@ -139,7 +139,7 @@ void testDeleteRS(AccessVersion _accessVersion) throws SQLException, IOException @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testInsertRS(AccessVersion _accessVersion) throws SQLException, IOException { + void testInsertRS(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); ucanaccess.setAutoCommit(false); try (Statement st = ucanaccess.createStatement()) { @@ -193,7 +193,7 @@ void testInsertRSNoAllSet(AccessVersion _accessVersion) throws SQLException, IOE @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testPartialInsertRS(AccessVersion _accessVersion) throws SQLException, IOException { + void testPartialInsertRS(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); ucanaccess.setAutoCommit(false); try (Statement st = ucanaccess.createStatement()) { diff --git a/src/test/java/net/ucanaccess/test/integration/DataSourceTest.java b/src/test/java/net/ucanaccess/test/integration/DataSourceTest.java index 6d37fa7b..ba995970 100644 --- a/src/test/java/net/ucanaccess/test/integration/DataSourceTest.java +++ b/src/test/java/net/ucanaccess/test/integration/DataSourceTest.java @@ -1,11 +1,13 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import net.ucanaccess.jdbc.UcanaccessDataSource; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.api.Test; import java.io.File; -import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; @@ -14,7 +16,8 @@ class DataSourceTest extends UcanaccessBaseTest { @Test void setNewDatabaseVersionBad() { UcanaccessDataSource uds = new UcanaccessDataSource(); - assertThrows(IllegalArgumentException.class, () -> uds.setNewDatabaseVersion("V200?")); + assertThatThrownBy(() -> uds.setNewDatabaseVersion("V200?")) + .isInstanceOf(IllegalArgumentException.class); } @Test @@ -28,7 +31,8 @@ void setNewDatabaseVersionGood() { @Test void setLobScaleBad() { UcanaccessDataSource uds = new UcanaccessDataSource(); - assertThrows(IllegalArgumentException.class, () -> uds.setLobScale(3)); + assertThatThrownBy(() -> uds.setLobScale(3)) + .isInstanceOf(IllegalArgumentException.class); } @Test @@ -40,9 +44,9 @@ void setLobScaleGood() { } @Test - void createNewDatabase() throws SQLException, IOException { + void createNewDatabase() throws SQLException { File fileMdb = createTempFileName("ucaDataSourceTest", ".mdb"); - assertFalse(fileMdb.exists()); + assertThat(fileMdb).doesNotExist(); UcanaccessDataSource uds = new UcanaccessDataSource(); uds.setAccessPath(fileMdb.getAbsolutePath()); @@ -50,7 +54,7 @@ void createNewDatabase() throws SQLException, IOException { uds.setImmediatelyReleaseResources(true); // so we can delete it immediately after close Connection conn = uds.getConnection(); - assertTrue(fileMdb.exists()); + assertThat(fileMdb).exists(); getLogger().info("DataSource connection successfully created file {}", uds.getAccessPath()); conn.close(); @@ -59,7 +63,7 @@ void createNewDatabase() throws SQLException, IOException { irrEffective = irrEffective != null && irrEffective; if (irrEffective) { assertTrue(fileMdb.delete()); - assertFalse(fileMdb.exists()); + assertThat(fileMdb).doesNotExist(); } else { getLogger().info("(Test database remains on disk)"); } diff --git a/src/test/java/net/ucanaccess/test/integration/DisableAutoIncrementTest.java b/src/test/java/net/ucanaccess/test/integration/DisableAutoIncrementTest.java index ea3f4055..8b0b48e6 100644 --- a/src/test/java/net/ucanaccess/test/integration/DisableAutoIncrementTest.java +++ b/src/test/java/net/ucanaccess/test/integration/DisableAutoIncrementTest.java @@ -1,5 +1,8 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import net.ucanaccess.jdbc.UcanaccessSQLException; import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -34,20 +37,16 @@ void testGuid(AccessVersion _accessVersion) throws SQLException, IOException { @MethodSource("net.ucanaccess.test.util.AccessVersion#getDefaultAccessVersion()") void testDisable(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - boolean exc = false; - Statement st = ucanaccess.createStatement(); - st.execute("INSERT INTO CT (descr) VALUES ('CIAO')"); - st.execute("DISABLE AUTOINCREMENT ON CT "); - try { + try (Statement st = ucanaccess.createStatement()) { st.execute("INSERT INTO CT (descr) VALUES ('CIAO')"); - } catch (Exception e) { - exc = true; + st.execute("DISABLE AUTOINCREMENT ON CT "); + assertThatThrownBy(() -> st.execute("INSERT INTO CT (descr) VALUES ('CIAO')")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("integrity constraint violation: NOT NULL check constraint"); + st.execute("ENABLE AUTOINCREMENT ON CT "); + st.execute("INSERT INTO CT (descr) VALUES ('CIAO')"); + st.execute("DISABLE AUTOINCREMENT ON[C T]"); } - assertTrue(exc); - st.execute("enable AUTOINCREMENT ON CT "); - st.execute("INSERT INTO CT (descr) VALUES ('CIAO')"); - st.execute("DISABLE AUTOINCREMENT ON[C T]"); - st.close(); } } diff --git a/src/test/java/net/ucanaccess/test/integration/DropTableTest.java b/src/test/java/net/ucanaccess/test/integration/DropTableTest.java index 9d876abe..2f911a23 100644 --- a/src/test/java/net/ucanaccess/test/integration/DropTableTest.java +++ b/src/test/java/net/ucanaccess/test/integration/DropTableTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import java.io.IOException; import java.sql.SQLException; import java.sql.Statement; @@ -45,7 +44,7 @@ void testDrop(AccessVersion _accessVersion) throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testDropBlank(AccessVersion _accessVersion) throws SQLException, IOException { + void testDropBlank(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); // ucanaccess.setAutoCommit(false); diff --git a/src/test/java/net/ucanaccess/test/integration/ExceptionCodeTest.java b/src/test/java/net/ucanaccess/test/integration/ExceptionCodeTest.java index ac6e1045..e9a12313 100644 --- a/src/test/java/net/ucanaccess/test/integration/ExceptionCodeTest.java +++ b/src/test/java/net/ucanaccess/test/integration/ExceptionCodeTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import net.ucanaccess.jdbc.IUcanaccessErrorCodes; import net.ucanaccess.jdbc.UcanaccessDriver; import net.ucanaccess.jdbc.UcanaccessSQLException; @@ -30,15 +32,16 @@ protected String getAccessPath() { @EnumSource(value = AccessVersion.class) void testVUKException(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); + try (Statement st = ucanaccess.createStatement()) { st.execute("INSERT INTO T(pk,b) VALUES('pippo', true)"); - st.execute("INSERT INTO T(pk,b) VALUES('pippo', true)"); - - } catch (SQLException _ex) { - assertEquals(-ErrorCode.X_23505, _ex.getErrorCode()); - assertEquals("23505", _ex.getSQLState()); + assertThatThrownBy(() -> st.execute("INSERT INTO T(pk,b) VALUES('pippo', true)")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("integrity constraint violation: unique constraint or index violation") + .hasFieldOrPropertyWithValue("ErrorCode", -ErrorCode.X_23505) + .hasFieldOrPropertyWithValue("SQLState", "23505"); } } @@ -53,9 +56,11 @@ void testGenException(AccessVersion _accessVersion) { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) void testGException(AccessVersion _accessVersion) { - SQLException ex = assertThrows(SQLException.class, () -> DriverManager.getConnection(UcanaccessDriver.URL_PREFIX + "ciao ciao")); - assertEquals(IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR, ex.getErrorCode()); - assertEquals(IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR + "", ex.getSQLState()); + assertThatThrownBy(() -> DriverManager.getConnection(UcanaccessDriver.URL_PREFIX + "kuso_yaro")) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("given file does not exist") + .hasFieldOrPropertyWithValue("ErrorCode", IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR) + .hasFieldOrPropertyWithValue("SQLState", String.valueOf(IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR)); } } diff --git a/src/test/java/net/ucanaccess/test/integration/ExternalResourcesTest.java b/src/test/java/net/ucanaccess/test/integration/ExternalResourcesTest.java index c7b47f72..da68b987 100644 --- a/src/test/java/net/ucanaccess/test/integration/ExternalResourcesTest.java +++ b/src/test/java/net/ucanaccess/test/integration/ExternalResourcesTest.java @@ -13,10 +13,10 @@ class ExternalResourcesTest extends UcanaccessBaseTest { @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.util.AccessVersion#getDefaultAccessVersion()") - void testLinks(AccessVersion _accessVersion) throws SQLException { + void testLinks(AccessVersion _accessVersion) throws SQLException, ClassNotFoundException { init(_accessVersion); - assertDoesNotThrow(() -> Class.forName("net.ucanaccess.jdbc.UcanaccessDriver")); + Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); File main = copyResourceToTempFile(TEST_DB_DIR + "main.mdb"); File linkee1 = copyResourceToTempFile(TEST_DB_DIR + "linkee1.mdb"); diff --git a/src/test/java/net/ucanaccess/test/integration/FolderTest.java b/src/test/java/net/ucanaccess/test/integration/FolderTest.java index 0608e434..1809beef 100644 --- a/src/test/java/net/ucanaccess/test/integration/FolderTest.java +++ b/src/test/java/net/ucanaccess/test/integration/FolderTest.java @@ -7,41 +7,34 @@ import org.junit.jupiter.params.provider.MethodSource; import java.io.File; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.SQLWarning; class FolderTest extends UcanaccessBaseTest { @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.util.AccessVersion#getDefaultAccessVersion()") - void testFolderContent(AccessVersion _accessVersion) throws SQLException { + void testFolderContent(AccessVersion _accessVersion) throws SQLException, ClassNotFoundException { init(_accessVersion); String folderPath = System.getProperty("accessFolder"); if (folderPath == null) { return; } - Statement st = null; - assertDoesNotThrow(() -> Class.forName("net.ucanaccess.jdbc.UcanaccessDriver")); + Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); File folder = new File(folderPath); for (File fl : folder.listFiles()) { - try { - String url = UcanaccessDriver.URL_PREFIX + fl.getAbsolutePath(); - Connection conn = DriverManager.getConnection(url); - SQLWarning sqlw = conn.getWarnings(); - getLogger().info("open {}", fl.getAbsolutePath()); - while (sqlw != null) { - getLogger().info(sqlw.getMessage()); - sqlw = sqlw.getNextWarning(); - } - - } catch (Exception _ex) { - getLogger().warn("error {}: ", fl.getAbsolutePath(), _ex); - } finally { - if (st != null) { - st.close(); - } + String url = UcanaccessDriver.URL_PREFIX + fl.getAbsolutePath(); + Connection conn = DriverManager.getConnection(url); + SQLWarning sqlw = conn.getWarnings(); + getLogger().info("open {}", fl.getAbsolutePath()); + while (sqlw != null) { + getLogger().info(sqlw.getMessage()); + sqlw = sqlw.getNextWarning(); } } } diff --git a/src/test/java/net/ucanaccess/test/integration/InsertBigTest.java b/src/test/java/net/ucanaccess/test/integration/InsertBigTest.java index 2a790f0a..a6654124 100644 --- a/src/test/java/net/ucanaccess/test/integration/InsertBigTest.java +++ b/src/test/java/net/ucanaccess/test/integration/InsertBigTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -8,6 +10,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.stream.Collectors; +import java.util.stream.IntStream; class InsertBigTest extends UcanaccessBaseTest { @@ -21,22 +25,17 @@ protected void init(AccessVersion _accessVersion) throws SQLException { @EnumSource(value = AccessVersion.class) void testBig(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); - Statement st = null; - st = ucanaccess.createStatement(); - int id = 6666554; - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 100000; i++) { - sb.append(String.format("%05d", i)); - sb.append("\r\n"); + + try (Statement st = ucanaccess.createStatement()) { + String s = IntStream.range(0, 100000).mapToObj(i -> String.format("%05d", i)).collect(Collectors.joining("\r\n")); + int id = 6666554; + assertThat(s).hasSizeGreaterThanOrEqualTo(65536); + st.execute("INSERT INTO Tbig (id, descr) VALUES(" + id + ", '" + s + "')"); + ResultSet rs = st.executeQuery("SELECT descr FROM Tbig WHERE id=" + id); + rs.next(); + String retrieved = rs.getString(1); + assertEquals(s, retrieved); } - String s = sb.toString(); - assertTrue(s.length() >= 65536); - st.execute("INSERT INTO Tbig (id,descr) VALUES( " + id + ",'" + s + "')"); - ResultSet rs = st.executeQuery("SELECT descr FROM Tbig WHERE id=" + id); - rs.next(); - String retrieved = rs.getString(1); - assertEquals(s, retrieved); - st.close(); } } diff --git a/src/test/java/net/ucanaccess/test/integration/MetaDataParameterizedTest.java b/src/test/java/net/ucanaccess/test/integration/MetaDataParameterizedTest.java index a3a57093..76d572ff 100644 --- a/src/test/java/net/ucanaccess/test/integration/MetaDataParameterizedTest.java +++ b/src/test/java/net/ucanaccess/test/integration/MetaDataParameterizedTest.java @@ -1,5 +1,6 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -20,15 +21,12 @@ void testCreateBadMetadata(AccessVersion _accessVersion) throws Exception { init(_accessVersion); Connection conn = ucanaccess; Statement st = conn.createStatement(); - st.execute( - "create table [健康] ([q3¹²³¼½¾ß€ Ð×ÝÞðýþäüöß] guiD PRIMARY KEY, [Sometime I wonder who I am ] text )"); + st.execute("CREATE TABLE [健康] ([q3¹²³¼½¾ß€ Ð×ÝÞðýþäüöß] guiD PRIMARY KEY, [Sometime I wonder who I am ] text )"); st.execute("INSERT INTO [健康] ([Sometime I wonder who I am ] ) values ('I''m a crazy man')"); - st.execute("update [健康] set [Sometime I wonder who I am ]='d'"); + st.execute("UPDATE [健康] set [Sometime I wonder who I am ]='d'"); checkQuery("SELECT * FROM 健康 "); - getLogger().info("crazy names in create table..."); dumpQueryResult("SELECT * FROM [健康]"); - st.execute( - "create table [123456 nn%&/健康] ([q3¹²³¼½¾ß€ Ð×ÝÞðýþäüöß] aUtoIncrement PRIMARY KEY, [Sometime I wonder who I am ] text, " + st.execute("CREATE TABLE [123456 nn%&/健康] ([q3¹²³¼½¾ß€ Ð×ÝÞðýþäüöß] aUtoIncrement PRIMARY KEY, [Sometime I wonder who I am ] text, " + "[Πλήθος Αντιγράφων] CURRENCY,[ជំរាបសួរ] CURRENCY,[ЗДОРОВЫЙ] CURRENCY,[健康] CURRENCY,[健康な] CURRENCY,[किआओ ] CURRENCY default 12.88, [11q3 ¹²³¼½¾ß€] text(2), unique ([किआओ ] ,[健康な]) )"); st.execute( "INSERT INTO [123456 nn%&/健康] ([Sometime I wonder who I am ],[Πλήθος Αντιγράφων],[健康],[健康な],[किआओ ] ) VALUES('I''m a wonderful forty',10.56,10.33,13,14)"); @@ -46,14 +44,14 @@ void testCreateBadMetadata(AccessVersion _accessVersion) throws Exception { try { st.execute( "INSERT INTO [123456 nn%&/健康] ([Sometime I wonder who I am ],[Πλήθος Αντιγράφων],[健康],[किआओ ] ,健康な) VALUES('I''m a wonderful forty',11,11,14,13)"); - } catch (Exception e) { + } catch (Exception _ex) { getLogger().info("ok, unique constraint gotten"); } st.execute( "INSERT INTO [123456 nn%&/健康] ([Sometime I wonder who I am ],[Πλήθος Αντιγράφων],[健康],[किआओ ] ,[健康な]) VALUES('I''m a wonderful forty',11,11,14.01,13)"); try { st.execute("update [123456 nn%&/健康] set [健康な]=13, [किआओ ]=14"); - } catch (Exception e) { + } catch (Exception _ex) { getLogger().info("ok, unique constraint gotten"); } @@ -79,10 +77,10 @@ void testBadMetadata(AccessVersion _accessVersion) throws Exception { dumpQueryResult("SELECT * FROM NOROMAN"); Connection conn = ucanaccess; Statement st = conn.createStatement(); - ResultSetMetaData rsmd = st.executeQuery("SELECT * FROM NOROMAN").getMetaData(); - assertTrue(rsmd.isAutoIncrement(1)); - assertTrue(rsmd.isCurrency(6)); - assertFalse(rsmd.isCurrency(7)); + assertThat(st.executeQuery("SELECT * FROM NOROMAN").getMetaData()) + .satisfies(x -> x.isAutoIncrement(1)) + .satisfies(x -> x.isCurrency(6)) + .satisfies(x -> x.isCurrency(7)); DatabaseMetaData dbmd = ucanaccess.getMetaData(); ResultSet rs = dbmd.getTables(null, null, "NOROMAn", null); diff --git a/src/test/java/net/ucanaccess/test/integration/MetaDataTest.java b/src/test/java/net/ucanaccess/test/integration/MetaDataTest.java index be595e96..f2466238 100644 --- a/src/test/java/net/ucanaccess/test/integration/MetaDataTest.java +++ b/src/test/java/net/ucanaccess/test/integration/MetaDataTest.java @@ -28,7 +28,7 @@ void afterEachTest() throws SQLException { dropTable("t_metadata"); } - void createSimple(String a, Object[][] ver) throws SQLException, IOException { + void createSimple(String a, Object[][] ver) throws SQLException { try (Statement st = ucanaccess.createStatement()) { st.execute("INSERT INTO t_metadata VALUES ('33A', 11, '" + a + "' )"); st.execute("INSERT INTO t_metadata VALUES ('33B', 111, '" + a + "' )"); diff --git a/src/test/java/net/ucanaccess/test/integration/NotNullDdlTest.java b/src/test/java/net/ucanaccess/test/integration/NotNullDdlTest.java index 7b13800f..53d5339e 100644 --- a/src/test/java/net/ucanaccess/test/integration/NotNullDdlTest.java +++ b/src/test/java/net/ucanaccess/test/integration/NotNullDdlTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -8,8 +10,12 @@ import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; -import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; import java.sql.Statement; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; class NotNullDdlTest extends UcanaccessBaseTest { @@ -20,46 +26,43 @@ void confirmNotNullColumnUsingJet(AccessVersion _accessVersion) throws Exception // future-proofing in case default file version changes assertEquals(getFileFormat().name(), "V2003"); - if (System.getProperty("os.name").startsWith("Windows")) { - String mdbPath = ucanaccess.getDbIO().getFile().getAbsolutePath(); - - Statement st = ucanaccess.createStatement(); - st.execute("CREATE TABLE table1 (id LONG PRIMARY KEY, txt_required TEXT(50) NOT NULL)"); - ucanaccess.close(); - - File vbsFile = createTempFileName("NotNullDdlTest", ".vbs"); - vbsFile.deleteOnExit(); - PrintWriter pw = new PrintWriter(vbsFile); - pw.println("Set conn = CreateObject(\"ADODB.Connection\")"); - pw.println("conn.Open \"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + mdbPath + "\""); - pw.println("conn.Execute \"INSERT INTO table1 (id) VALUES (1)\""); - pw.println("conn.Close"); - pw.close(); - - String cscriptPath = System.getenv("SystemRoot") - + (System.getProperty("sun.arch.data.model").equals("64") ? "\\SYSWOW64" : "\\SYSTEM32") - + "\\CSCRIPT.EXE"; - - String command = "\"" + cscriptPath + "\" \"" + vbsFile.getAbsolutePath() + "\""; - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - BufferedReader rdr = new BufferedReader(new InputStreamReader(p.getErrorStream())); - StringBuilder sb = new StringBuilder(); // to capture error line(s) - int numErrorLines = 0; - String line = rdr.readLine(); - while (line != null) { - numErrorLines++; - getLogger().info(line); - sb.append(String.format("%s%n", line)); - line = rdr.readLine(); - } - if (numErrorLines == 0) { - fail("The VBScript should have thrown an error, but it did not."); - } else { - if (!sb.toString().contains("table1.txt_required")) { - fail("The VBScript threw an unexpected error."); - } + String mdbPath = ucanaccess.getDbIO().getFile().getAbsolutePath(); + + Statement st = ucanaccess.createStatement(); + st.execute("CREATE TABLE table1 (id LONG PRIMARY KEY, txt_required TEXT(50) NOT NULL)"); + ucanaccess.close(); + + File vbsFile = createTempFileName(getClass().getSimpleName(), ".vbs"); + vbsFile.deleteOnExit(); + Files.write(vbsFile.toPath(), List.of( + "Set conn = CreateObject(\"ADODB.Connection\")", + "conn.Open \"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + mdbPath + "\"", + "conn.Execute \"INSERT INTO table1 (id) VALUES (1)\"", + "conn.Close"), StandardOpenOption.WRITE, StandardOpenOption.CREATE); + + if (!System.getProperty("os.name").startsWith("Windows")) { + return; + } + + String cscriptPath = System.getenv("SystemRoot") + + (System.getProperty("sun.arch.data.model").equals("64") ? "\\SYSWOW64" : "\\SYSTEM32") + + "\\CSCRIPT.EXE"; + + String command = "\"" + cscriptPath + "\" \"" + vbsFile.getAbsolutePath() + "\""; + Process proc = Runtime.getRuntime().exec(command); + proc.waitFor(10, TimeUnit.SECONDS); + + assertThat(proc.exitValue()).isEqualTo(0); + + try (BufferedReader output = new BufferedReader(new InputStreamReader(proc.getErrorStream()))) { + List errLines = output.lines().collect(Collectors.toList()); + + if (errLines.isEmpty()) { + fail("The VBScript should have thrown an error, but it did not"); } + assertThat(errLines).contains("table1.txt_required").withFailMessage("The VBScript threw an unexpected error"); } + + } } diff --git a/src/test/java/net/ucanaccess/test/integration/PasswordTest.java b/src/test/java/net/ucanaccess/test/integration/PasswordTest.java index 7166f80e..48d63185 100644 --- a/src/test/java/net/ucanaccess/test/integration/PasswordTest.java +++ b/src/test/java/net/ucanaccess/test/integration/PasswordTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; import org.junit.jupiter.params.ParameterizedTest; @@ -19,7 +21,7 @@ void testPassword(AccessVersion _accessVersion) throws Exception { try { ucanaccessConnection = getUcanaccessConnection(dbFile.getAbsolutePath()); } catch (Exception _ex) { - assertContains(_ex.getMessage(), "Password authentication failed"); + assertThat(_ex.getMessage()).contains("Password authentication failed"); } assertNull(ucanaccessConnection); diff --git a/src/test/java/net/ucanaccess/test/integration/PhysicalRollbackTest.java b/src/test/java/net/ucanaccess/test/integration/PhysicalRollbackTest.java index 78c6dfef..ae6b7f0b 100644 --- a/src/test/java/net/ucanaccess/test/integration/PhysicalRollbackTest.java +++ b/src/test/java/net/ucanaccess/test/integration/PhysicalRollbackTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.integration; +import static org.assertj.core.api.Assertions.assertThat; + import net.ucanaccess.jdbc.UcanaccessConnection; import net.ucanaccess.test.util.AccessVersion; import net.ucanaccess.test.util.UcanaccessBaseTest; @@ -54,7 +56,7 @@ void testCommit(AccessVersion _accessVersion) throws Exception { ex = _ex; } assertNotNull(ex); - assertContains(ex.getMessage(), getClass().getSimpleName()); + assertThat(ex.getMessage()).contains(getClass().getSimpleName()); ucanaccess = getUcanaccessConnection(); dumpQueryResult("SELECT * FROM T4"); diff --git a/src/test/java/net/ucanaccess/test/integration/RegexTest.java b/src/test/java/net/ucanaccess/test/integration/RegexTest.java index 57dfef5b..acaea5d6 100644 --- a/src/test/java/net/ucanaccess/test/integration/RegexTest.java +++ b/src/test/java/net/ucanaccess/test/integration/RegexTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import java.io.IOException; import java.sql.SQLException; import java.sql.Statement; @@ -25,7 +24,7 @@ void afterEachTest() throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.util.AccessVersion#getDefaultAccessVersion()") - void testRegex(AccessVersion _accessVersion) throws SQLException, IOException { + void testRegex(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); String s = "C".repeat(5000); diff --git a/src/test/java/net/ucanaccess/test/integration/TransactionTest.java b/src/test/java/net/ucanaccess/test/integration/TransactionTest.java index db63f743..4f456d6d 100644 --- a/src/test/java/net/ucanaccess/test/integration/TransactionTest.java +++ b/src/test/java/net/ucanaccess/test/integration/TransactionTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import java.io.IOException; import java.sql.SQLException; import java.sql.Savepoint; import java.sql.Statement; @@ -26,7 +25,7 @@ void afterEachTest() throws SQLException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testCommit(AccessVersion _accessVersion) throws SQLException, IOException { + void testCommit(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); ucanaccess.setAutoCommit(false); Statement st = ucanaccess.createStatement(); @@ -40,7 +39,7 @@ void testCommit(AccessVersion _accessVersion) throws SQLException, IOException { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testSavepoint(AccessVersion _accessVersion) throws SQLException, IOException { + void testSavepoint(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); int count = getCount("SELECT COUNT(*) FROM T4"); ucanaccess.setAutoCommit(false); @@ -58,7 +57,7 @@ void testSavepoint(AccessVersion _accessVersion) throws SQLException, IOExceptio @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) - void testSavepoint2(AccessVersion _accessVersion) throws SQLException, IOException { + void testSavepoint2(AccessVersion _accessVersion) throws SQLException { init(_accessVersion); int count = getCount("SELECT COUNT(*) FROM T4"); ucanaccess.setAutoCommit(false); diff --git a/src/test/java/net/ucanaccess/test/integration/UnproperExecuteQueryTest.java b/src/test/java/net/ucanaccess/test/integration/UnproperExecuteQueryTest.java index 16c8f4c4..5cb04652 100644 --- a/src/test/java/net/ucanaccess/test/integration/UnproperExecuteQueryTest.java +++ b/src/test/java/net/ucanaccess/test/integration/UnproperExecuteQueryTest.java @@ -32,8 +32,7 @@ private void execute(String _sql) throws SQLException { assertThatThrownBy(() -> st.executeQuery(_sql)) .isInstanceOf(UcanaccessSQLException.class) .hasMessageMatching("UCAExc:::[0-9\\.]+ General error"); - - assertDoesNotThrow(() -> st.execute(_sql)); + st.execute(_sql); } } } diff --git a/src/test/java/net/ucanaccess/test/util/AbstractBaseTest.java b/src/test/java/net/ucanaccess/test/util/AbstractBaseTest.java index 422bd2f5..33478687 100644 --- a/src/test/java/net/ucanaccess/test/util/AbstractBaseTest.java +++ b/src/test/java/net/ucanaccess/test/util/AbstractBaseTest.java @@ -15,7 +15,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.List; import java.util.Optional; /** @@ -134,28 +133,4 @@ protected static void copyFile(InputStream _in, Path _target) { } } - public static void assertEmpty(String _string) { - assertTrue(_string == null || _string.isEmpty(), "String not empty"); - } - - public static void assertNotEmpty(String _string) { - assertFalse(_string == null || _string.isEmpty(), "String is empty"); - } - - public static void assertContains(String _string, String _contains) { - if (_contains != null) { - assertTrue(!(_string == null || _string.length() == 0) && _string.contains(_contains), "String does not contain [" + _contains + "]: " + _string); - } - } - - public static void assertDoubleEquals(double _expected, double _actual) { - assertEquals(_expected, _actual, 0.000001d); - } - - public static void assertListEquals(List _actualList, String... _expected) { - String[] actual = new String[_actualList.size()]; - _actualList.toArray(actual); - assertArrayEquals(_expected, actual); - } - } diff --git a/src/test/java/net/ucanaccess/test/util/UcanaccessBaseTest.java b/src/test/java/net/ucanaccess/test/util/UcanaccessBaseTest.java index 542843d9..6437ff54 100644 --- a/src/test/java/net/ucanaccess/test/util/UcanaccessBaseTest.java +++ b/src/test/java/net/ucanaccess/test/util/UcanaccessBaseTest.java @@ -1,5 +1,7 @@ package net.ucanaccess.test.util; +import static org.assertj.core.api.Assertions.assertThat; + import com.healthmarketscience.jackcess.Database; import com.healthmarketscience.jackcess.Database.FileFormat; import com.healthmarketscience.jackcess.DatabaseBuilder; @@ -7,6 +9,7 @@ import net.ucanaccess.console.Main; import net.ucanaccess.jdbc.UcanaccessConnection; import net.ucanaccess.jdbc.UcanaccessDriver; +import net.ucanaccess.jdbc.UcanaccessRuntimeException; import org.junit.jupiter.api.AfterEach; import java.io.*; @@ -55,7 +58,7 @@ protected void init(AccessVersion _accessVersion) throws SQLException { try { Class.forName(UcanaccessDriver.class.getName()); } catch (ClassNotFoundException _ex) { - throw new RuntimeException(_ex); + throw new UcanaccessRuntimeException(_ex); } ucanaccess = getUcanaccessConnection(); } @@ -96,7 +99,7 @@ public void checkQuery(String _query) throws SQLException, IOException { } } - public void checkQuery(String _query, Object... _expected) throws SQLException, IOException { + public void checkQuery(String _query, Object... _expected) throws SQLException { checkQuery(_query, new Object[][] {_expected}); } @@ -109,7 +112,7 @@ private void diff(ResultSet _resultSet, Object[][] _expectedResults, String _exp int j = 0; while (_resultSet.next()) { for (int i = 0; i < mycolmax; ++i) { - assertTrue(j < _expectedResults.length, "Matrix with different length was expected: " + _expectedResults.length + " not" + j); + assertThat(j).isLessThan(_expectedResults.length).withFailMessage("Matrix with different length was expected: " + _expectedResults.length + " not " + j); Object actualObj = _resultSet.getObject(i + 1); Object expectedObj = _expectedResults[j][i]; if (expectedObj == null) { @@ -142,7 +145,7 @@ private void diff(ResultSet _resultSet, Object[][] _expectedResults, String _exp assertEquals(_expectedResults.length, j, "Matrix with different length was expected"); } - public void diffResultSets(ResultSet _resultSet, ResultSet _verifyResultSet, String _query) throws SQLException, IOException { + public void diffResultSets(ResultSet _resultSet, ResultSet _verifyResultSet, String _query) throws SQLException { ResultSetMetaData msMetaData = _resultSet.getMetaData(); int mycolmax = msMetaData.getColumnCount(); ResultSetMetaData verifyRsMetaData = _verifyResultSet.getMetaData(); @@ -257,8 +260,8 @@ protected final String getAccessTempPath() { */ private static final class TempFileNameString { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"); - private static final AtomicInteger COUNTER = new AtomicInteger(1); - private final String name; + private static final AtomicInteger COUNTER = new AtomicInteger(1); + private final String name; private TempFileNameString() { name = LocalDateTime.now().format(FORMATTER) + '_' + String.format("%03d", COUNTER.getAndIncrement());