Skip to content

Commit

Permalink
test[DbWrapper]: add testing for SQLException
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Apr 21, 2026
1 parent c4955de commit f0430cd
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,12 +26,14 @@ public class DbWrapperDonationsTest {
private Object[] cutoffDonation;
private Object[] freakyDonation;
private Object[] repeatingDonation;
private Object[] tooBigDonation;
private List<Object[]> donations;
private List<Object[]> donations2;
private List<Object[]> donations3;
private List<Object[]> repeatedDonations;
private List<Object[]> wrongFormatDonations;
private List<Object[]> wrongDatatypeDonations;
private List<Object[]> tooBigDonations;
private List<Object[]> nullList;

private static final int PRECISION = 5;
Expand All @@ -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<Object[]>();
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";
Expand Down Expand Up @@ -84,6 +87,13 @@ void init() {
this.wrongDatatypeDonations = new ArrayList<Object[]>();
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<Object[]>();
this.tooBigDonations.add(tooBigDonation);

Object[] nullRow = new Object[] {null, null, null, null, null, null};
this.nullList = new ArrayList<Object[]>();
this.nullList.add(nullRow);
Expand Down Expand Up @@ -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);
});
Expand All @@ -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,
() -> {
Expand Down Expand Up @@ -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<Object[]>()));
assertDoesNotThrow(() -> assertEquals(0, this.db.exportDonations(new ArrayList<Object[]>())));
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()
);
}
}

0 comments on commit f0430cd

Please sign in to comment.