From f0430cdec860f44e89ed2feca8273fc3115aa7ae Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Tue, 21 Apr 2026 18:23:18 +0200 Subject: [PATCH] test[DbWrapper]: add testing for SQLException --- .../model/wrapper/DbWrapperDonationsTest.java | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java b/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java index 1db7a90..f9cc8ff 100644 --- a/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java +++ b/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java @@ -7,6 +7,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; @@ -25,12 +26,14 @@ public class DbWrapperDonationsTest { private Object[] cutoffDonation; private Object[] freakyDonation; private Object[] repeatingDonation; + private Object[] tooBigDonation; private List donations; private List donations2; private List donations3; private List repeatedDonations; private List wrongFormatDonations; private List wrongDatatypeDonations; + private List tooBigDonations; private List nullList; private static final int PRECISION = 5; @@ -40,10 +43,10 @@ public class DbWrapperDonationsTest { @BeforeEach void init() { this.db = new DbWrapper(true); - String[] firstNames = new String[] { "John", "Jane", "Cutoff", "Freaky", "Repeating" }; - String[] lastNames = new String[] { "Doe", "Doe", "Joh", "Bill", "JoeJoe" }; + String[] firstNames = new String[] { "John", "Jane", "Cutoff", "Freaky", "Repeating", "Big" }; + String[] lastNames = new String[] { "Doe", "Doe", "Joh", "Bill", "JoeJoe", "Willy" }; this.users = new ArrayList(); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < firstNames.length; i++) { Object[] row = new Object[6]; row[0] = i + 1; row[1] = "Customer"; @@ -84,6 +87,13 @@ void init() { this.wrongDatatypeDonations = new ArrayList(); this.wrongDatatypeDonations.add(freakyDonation); + this.tooBigDonation = new Object[] { + 6, 6, 999999, new BigDecimal("9999999999999999999999999999999"), + new Timestamp(new Date().getTime()), "Azerbaijani technologies" + }; + this.tooBigDonations = new ArrayList(); + this.tooBigDonations.add(tooBigDonation); + Object[] nullRow = new Object[] {null, null, null, null, null, null}; this.nullList = new ArrayList(); this.nullList.add(nullRow); @@ -150,7 +160,7 @@ public void wronglyDatatypedDonationsThrowsExpectedException() { @Test public void addingSameDonationTwiceThrowsExpectedException() { assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); - assertEquals(1, this.db.exportDonations(this.donations)); + assertDoesNotThrow(() -> assertEquals(1, this.db.exportDonations(this.donations))); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { this.db.exportDonations(this.donations); }); @@ -163,7 +173,7 @@ public void addingSameDonationTwiceThrowsExpectedException() { public void addingSameDonationTwiceThrowsExpectedException2() { assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); assertTrue(this.db.importDonations((int) this.users.get(1)[0]).size() == 0); - assertEquals(2, this.db.exportDonations(this.donations2)); + assertDoesNotThrow(() -> assertEquals(2, this.db.exportDonations(this.donations2))); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> { @@ -212,8 +222,20 @@ public void addingDonationListWithNullInRowThrowsExpectedException() { @Test public void dataIsEmptyAfterExportingAndImportingEmptyList() { assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); - assertEquals(0, this.db.exportDonations(new ArrayList())); + assertDoesNotThrow(() -> assertEquals(0, this.db.exportDonations(new ArrayList()))); assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); assertTrue(this.db.disconnect()); } + + @Test + public void exportTooBigNumberThrowsSQLException() { + SQLException exception = assertThrows(SQLException.class, () -> { + this.db.exportDonations(this.tooBigDonations); + }); + assertEquals( + "An unexpected SQL exception has occurred. " + + "This might be caused by inserting an item that is too large.", + exception.getMessage() + ); + } }