diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 53ec2ea5ca..aa9609f3df 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -23,6 +23,16 @@ public class DBHelper extends SQLiteOpenHelper { public static final int ORIGINAL_DATABASE_VERSION = 1; public static final int DATABASE_VERSION = 16; + public static class DBException extends Exception { + public DBException(String message) { + super(message); + } + + public DBException(String message, Exception rootCause) { + super(message, rootCause); + } + } + public static class LoyaltyCardDbGroups { public static final String TABLE = "groups"; public static final String ID = "_id"; @@ -323,6 +333,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } + public static void clearDatabase(final SQLiteDatabase db) { + db.execSQL("DELETE FROM " + LoyaltyCardDbGroups.TABLE); + db.execSQL("DELETE FROM " + LoyaltyCardDbIds.TABLE); + db.execSQL("DELETE FROM " + LoyaltyCardDbIdsGroups.TABLE); + } + private static ContentValues generateFTSContentValues(final int id, final String store, final String note) { // FTS on Android is severely limited and can only search for word starting with a certain string // So for each word, we grab every single substring @@ -368,7 +384,7 @@ public static long insertLoyaltyCard( final SQLiteDatabase database, final String store, final String note, final Date validFrom, final Date expiry, final BigDecimal balance, final Currency balanceType, final String cardId, final String barcodeId, final CatimaBarcode barcodeType, final Integer headerColor, - final int starStatus, final Long lastUsed, final int archiveStatus) { + final int starStatus, final Long lastUsed, final int archiveStatus) throws DBException { database.beginTransaction(); // Card @@ -388,6 +404,8 @@ public static long insertLoyaltyCard( contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus); long id = database.insert(LoyaltyCardDbIds.TABLE, null, contentValues); + if (id == -1) throw new DBException("Failed to insert card"); + // FTS insertFTS(database, (int) id, store, note); @@ -402,7 +420,7 @@ public static long insertLoyaltyCard( final Date validFrom, final Date expiry, final BigDecimal balance, final Currency balanceType, final String cardId, final String barcodeId, final CatimaBarcode barcodeType, final Integer headerColor, final int starStatus, - final Long lastUsed, final int archiveStatus) { + final Long lastUsed, final int archiveStatus) throws DBException { database.beginTransaction(); // Card @@ -421,7 +439,9 @@ public static long insertLoyaltyCard( contentValues.put(LoyaltyCardDbIds.STAR_STATUS, starStatus); contentValues.put(LoyaltyCardDbIds.LAST_USED, lastUsed != null ? lastUsed : Utils.getUnixTime()); contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus); - database.insert(LoyaltyCardDbIds.TABLE, null, contentValues); + long rowid = database.insert(LoyaltyCardDbIds.TABLE, null, contentValues); + + if (rowid == -1) throw new DBException("Failed to insert card with ID " + id); // FTS insertFTS(database, id, store, note); @@ -432,12 +452,12 @@ public static long insertLoyaltyCard( return id; } - public static boolean updateLoyaltyCard( + public static void updateLoyaltyCard( SQLiteDatabase database, final int id, final String store, final String note, final Date validFrom, final Date expiry, final BigDecimal balance, final Currency balanceType, final String cardId, final String barcodeId, final CatimaBarcode barcodeType, final Integer headerColor, final int starStatus, - final Long lastUsed, final int archiveStatus) { + final Long lastUsed, final int archiveStatus) throws DBException { database.beginTransaction(); // Card @@ -465,37 +485,37 @@ public static boolean updateLoyaltyCard( database.setTransactionSuccessful(); database.endTransaction(); - return (rowsUpdated == 1); + if (rowsUpdated != 1) throw new DBException("Failed to update card with ID " + id + ": " + rowsUpdated + " rows updated"); } - public static boolean updateLoyaltyCardArchiveStatus(SQLiteDatabase database, final int id, final int archiveStatus) { + public static void updateLoyaltyCardArchiveStatus(SQLiteDatabase database, final int id, final int archiveStatus) throws DBException { ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbIds.ARCHIVE_STATUS, archiveStatus); int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues, whereAttrs(LoyaltyCardDbIds.ID), withArgs(id)); - return (rowsUpdated == 1); + if (rowsUpdated != 1) throw new DBException("Failed to (un)archive card with ID " + id + ": " + rowsUpdated + " rows updated"); } - public static boolean updateLoyaltyCardStarStatus(SQLiteDatabase database, final int id, final int starStatus) { + public static void updateLoyaltyCardStarStatus(SQLiteDatabase database, final int id, final int starStatus) throws DBException { ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbIds.STAR_STATUS, starStatus); int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues, whereAttrs(LoyaltyCardDbIds.ID), withArgs(id)); - return (rowsUpdated == 1); + if (rowsUpdated != 1) throw new DBException("Failed to (un)star card with ID " + id + ": " + rowsUpdated + " rows updated"); } - public static boolean updateLoyaltyCardLastUsed(SQLiteDatabase database, final int id) { + public static void updateLoyaltyCardLastUsed(SQLiteDatabase database, final int id) throws DBException { ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbIds.LAST_USED, System.currentTimeMillis() / 1000); int rowsUpdated = database.update(LoyaltyCardDbIds.TABLE, contentValues, whereAttrs(LoyaltyCardDbIds.ID), withArgs(id)); - return (rowsUpdated == 1); + if (rowsUpdated != 1) throw new DBException("Failed to update last used for card with ID " + id + ": " + rowsUpdated + " rows updated"); } - public static boolean updateLoyaltyCardZoomLevel(SQLiteDatabase database, int loyaltyCardId, int zoomLevel) { + public static void updateLoyaltyCardZoomLevel(SQLiteDatabase database, int loyaltyCardId, int zoomLevel) throws DBException { ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbIds.ZOOM_LEVEL, zoomLevel); Log.d("updateLoyaltyCardZLevel", "Card Id = " + loyaltyCardId + " Zoom level= " + zoomLevel); @@ -503,7 +523,7 @@ public static boolean updateLoyaltyCardZoomLevel(SQLiteDatabase database, int lo whereAttrs(LoyaltyCardDbIds.ID), withArgs(loyaltyCardId)); Log.d("updateLoyaltyCardZLevel", "Rows changed = " + rowsUpdated); - return (rowsUpdated == 1); + if (rowsUpdated != 1) throw new DBException("Failed to update zoom level for card with ID " + loyaltyCardId + ": " + rowsUpdated + " rows updated"); } public static boolean updateLoyaltyCardBalance(SQLiteDatabase database, final int id, final BigDecimal newBalance) { @@ -569,7 +589,7 @@ public static void setLoyaltyCardGroups(SQLiteDatabase database, final int id, L } } - public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context, final int id) { + public static void deleteLoyaltyCard(SQLiteDatabase database, Context context, final int id) throws DBException { // Delete card int rowsDeleted = database.delete(LoyaltyCardDbIds.TABLE, whereAttrs(LoyaltyCardDbIds.ID), @@ -594,7 +614,7 @@ public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context } } - return (rowsDeleted == 1); + if (rowsDeleted != 1) throw new DBException("Failed to delete card with ID " + id + ": " + rowsDeleted + " rows deleted"); } public static int getArchivedCardsCount(SQLiteDatabase database) { @@ -792,19 +812,20 @@ public static List getGroupCardIds(SQLiteDatabase database, final Strin return cardIds; } - public static long insertGroup(SQLiteDatabase database, final String name) { - if (name.isEmpty()) return -1; + public static void insertGroup(SQLiteDatabase database, final String name) throws DBException { + if (name.isEmpty()) throw new DBException("Failed to insert group with empty name"); ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbGroups.ID, name); contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database)); - return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); - } + long rowid = database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); - public static boolean updateGroup(SQLiteDatabase database, final String groupName, final String newName) { - if (newName.isEmpty()) return false; + if (rowid == -1) throw new DBException("Failed to insert group with name " + name); + } - boolean success = false; + public static void updateGroup(SQLiteDatabase database, final String groupName, final String newName) throws DBException { + if (groupName.isEmpty()) throw new DBException("Failed to update group: empty old name"); + if (newName.isEmpty()) throw new DBException("Failed to update group: empty new name"); ContentValues groupContentValues = new ContentValues(); groupContentValues.put(LoyaltyCardDbGroups.ID, newName); @@ -826,19 +847,17 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam if (groupsChanged == 1) { database.setTransactionSuccessful(); - success = true; + return; } } catch (SQLiteException ignored) { } finally { database.endTransaction(); } - return success; + throw new DBException("Failed to update group"); } - public static boolean deleteGroup(SQLiteDatabase database, final String groupName) { - boolean success = false; - + public static void deleteGroup(SQLiteDatabase database, final String groupName) throws DBException { database.beginTransaction(); try { // Delete group @@ -853,16 +872,15 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam if (groupsDeleted == 1) { database.setTransactionSuccessful(); - success = true; + // Reorder after delete to ensure no bad order IDs + reorderGroups(database, getGroups(database)); + return; } } finally { database.endTransaction(); } - // Reorder after delete to ensure no bad order IDs - reorderGroups(database, getGroups(database)); - - return success; + throw new DBException("Failed to delete group"); } public static int getGroupCardCount(SQLiteDatabase database, final String groupName) { diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index aa6f75f776..e46d96fa55 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -1437,9 +1437,21 @@ private void doSave() { // This makes the DBHelper set it to the current date // So that new and edited card are always on top when sorting by recently used if (updateLoyaltyCard) { - DBHelper.updateLoyaltyCard(mDatabase, loyaltyCardId, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, tempLoyaltyCard.starStatus, null, tempLoyaltyCard.archiveStatus); + try { + DBHelper.updateLoyaltyCard(mDatabase, loyaltyCardId, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, tempLoyaltyCard.starStatus, null, tempLoyaltyCard.archiveStatus); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not update loyalty card " + loyaltyCardId); + Toast.makeText(this, "Could not update loyalty card", Toast.LENGTH_LONG).show(); // FIXME + return; + } } else { - loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, 0, null, 0); + try { + loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, tempLoyaltyCard.store, tempLoyaltyCard.note, tempLoyaltyCard.validFrom, tempLoyaltyCard.expiry, tempLoyaltyCard.balance, tempLoyaltyCard.balanceType, tempLoyaltyCard.cardId, tempLoyaltyCard.barcodeId, tempLoyaltyCard.barcodeType, tempLoyaltyCard.headerColor, 0, null, 0); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not insert loyalty card"); + Toast.makeText(this, "Could not insert loyalty card", Toast.LENGTH_LONG).show(); // FIXME + return; + } } try { diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 8d93879ccc..95f2aea34b 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -263,7 +263,12 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Log.d(TAG, "Scaling to " + scale); loyaltyCard.zoomLevel = progress; - DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel); + + try { + DBHelper.updateLoyaltyCardZoomLevel(database, loyaltyCardId, loyaltyCard.zoomLevel); + } catch (DBHelper.DBException e) { + Log.d(TAG, "Could not update zoom level for card " + loyaltyCardId); + } setScalerGuideline(loyaltyCard.zoomLevel); @@ -669,7 +674,11 @@ public void onResume() { setFullscreen(isFullscreen); - DBHelper.updateLoyaltyCardLastUsed(database, loyaltyCard.id); + try { + DBHelper.updateLoyaltyCardLastUsed(database, loyaltyCard.id); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not update last used for card " + loyaltyCardId); + } invalidateOptionsMenu(); } @@ -770,7 +779,12 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } else if (id == R.id.action_star_unstar) { - DBHelper.updateLoyaltyCardStarStatus(database, loyaltyCardId, loyaltyCard.starStatus == 0 ? 1 : 0); + try { + DBHelper.updateLoyaltyCardStarStatus(database, loyaltyCardId, loyaltyCard.starStatus == 0 ? 1 : 0); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not (un)star card " + loyaltyCardId); + Toast.makeText(LoyaltyCardViewActivity.this, "Failed to (un)star card", Toast.LENGTH_LONG).show(); // FIXME + } // Re-init loyaltyCard with new data from DB onResume(); @@ -778,7 +792,12 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } else if (id == R.id.action_archive) { - DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 1); + try { + DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 1); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not archive card " + loyaltyCardId); + Toast.makeText(LoyaltyCardViewActivity.this, "Failed to archive card", Toast.LENGTH_LONG).show(); // FIXME + } Toast.makeText(LoyaltyCardViewActivity.this, R.string.archived, Toast.LENGTH_LONG).show(); // Re-init loyaltyCard with new data from DB @@ -787,7 +806,12 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } else if (id == R.id.action_unarchive) { - DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 0); + try { + DBHelper.updateLoyaltyCardArchiveStatus(database, loyaltyCardId, 0); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not unarchive card " + loyaltyCardId); + Toast.makeText(LoyaltyCardViewActivity.this, "Failed to unarchive card", Toast.LENGTH_LONG).show(); // FIXME + } Toast.makeText(LoyaltyCardViewActivity.this, R.string.unarchived, Toast.LENGTH_LONG).show(); // Re-init loyaltyCard with new data from DB @@ -802,7 +826,12 @@ public boolean onOptionsItemSelected(MenuItem item) { builder.setPositiveButton(R.string.confirm, (dialog, which) -> { Log.e(TAG, "Deleting card: " + loyaltyCardId); - DBHelper.deleteLoyaltyCard(database, LoyaltyCardViewActivity.this, loyaltyCardId); + try { + DBHelper.deleteLoyaltyCard(database, LoyaltyCardViewActivity.this, loyaltyCardId); + } catch (DBHelper.DBException e) { + Log.e(TAG, "Could not delete card " + loyaltyCardId); + Toast.makeText(LoyaltyCardViewActivity.this, "Failed to delete card", Toast.LENGTH_LONG).show(); // FIXME + } ShortcutHelper.removeShortcut(LoyaltyCardViewActivity.this, loyaltyCardId); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 905925aae9..2902b20606 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -164,7 +164,12 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) { for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) { Log.d(TAG, "Deleting card: " + loyaltyCard.id); - DBHelper.deleteLoyaltyCard(mDatabase, MainActivity.this, loyaltyCard.id); + try { + DBHelper.deleteLoyaltyCard(mDatabase, MainActivity.this, loyaltyCard.id); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not delete card " + loyaltyCard.id); + Toast.makeText(MainActivity.this, "Failed to delete card", Toast.LENGTH_LONG).show(); // FIXME + } ShortcutHelper.removeShortcut(MainActivity.this, loyaltyCard.id); } @@ -184,7 +189,12 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) { } else if (inputItem.getItemId() == R.id.action_archive) { for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) { Log.d(TAG, "Archiving card: " + loyaltyCard.id); - DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 1); + try { + DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 1); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not archive card " + loyaltyCard.id); + Toast.makeText(MainActivity.this, "Failed to archive card", Toast.LENGTH_LONG).show(); // FIXME + } updateLoyaltyCardList(false); inputMode.finish(); invalidateOptionsMenu(); @@ -193,7 +203,12 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) { } else if (inputItem.getItemId() == R.id.action_unarchive) { for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) { Log.d(TAG, "Unarchiving card: " + loyaltyCard.id); - DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 0); + try { + DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 0); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not unarchive card " + loyaltyCard.id); + Toast.makeText(MainActivity.this, "Failed to unarchive card", Toast.LENGTH_LONG).show(); // FIXME + } updateLoyaltyCardList(false); inputMode.finish(); invalidateOptionsMenu(); @@ -202,7 +217,12 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) { } else if (inputItem.getItemId() == R.id.action_star) { for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) { Log.d(TAG, "Starring card: " + loyaltyCard.id); - DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 1); + try { + DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 1); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not star card " + loyaltyCard.id); + Toast.makeText(MainActivity.this, "Failed to star card", Toast.LENGTH_LONG).show(); // FIXME + } updateLoyaltyCardList(false); inputMode.finish(); } @@ -210,7 +230,12 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) { } else if (inputItem.getItemId() == R.id.action_unstar) { for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) { Log.d(TAG, "Unstarring card: " + loyaltyCard.id); - DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 0); + try { + DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 0); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not unstar card " + loyaltyCard.id); + Toast.makeText(MainActivity.this, "Failed to unstar card", Toast.LENGTH_LONG).show(); // FIXME + } updateLoyaltyCardList(false); inputMode.finish(); } diff --git a/app/src/main/java/protect/card_locker/ManageGroupActivity.java b/app/src/main/java/protect/card_locker/ManageGroupActivity.java index a161bf922a..c8314aa5dd 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupActivity.java @@ -34,6 +34,8 @@ public class ManageGroupActivity extends CatimaAppCompatActivity implements Mana private SQLiteDatabase mDatabase; private ManageGroupCursorAdapter mAdapter; + private static final String TAG = "Catima"; + private final String SAVE_INSTANCE_ADAPTER_STATE = "adapterState"; private final String SAVE_INSTANCE_CURRENT_GROUP_NAME = "currentGroupName"; @@ -127,7 +129,13 @@ public void afterTextChanged(Editable s) { mAdapter.commitToDatabase(); if (!currentGroupName.equals(mGroup._id)) { - DBHelper.updateGroup(mDatabase, mGroup._id, currentGroupName); + try { + DBHelper.updateGroup(mDatabase, mGroup._id, currentGroupName); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not update group"); + Toast.makeText(getApplicationContext(), "Failed to update group", Toast.LENGTH_LONG).show(); // FIXME + return; + } } Toast.makeText(getApplicationContext(), R.string.group_updated, Toast.LENGTH_SHORT).show(); finish(); diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 7b6674f95d..4c19dbc3c3 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -6,6 +6,7 @@ import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.InputType; +import android.util.Log; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -166,7 +167,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { return; } - DBHelper.insertGroup(mDatabase, sanitizeAddGroupNameField(input.getText())); + String groupName = sanitizeAddGroupNameField(input.getText()); + try { + DBHelper.insertGroup(mDatabase, groupName); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not insert group " + groupName); + Toast.makeText(getApplicationContext(), "Failed to insert group", Toast.LENGTH_LONG).show(); // FIXME + return; + } updateGroupList(); }); builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.cancel()); @@ -239,7 +247,13 @@ public void onDeleteButtonClicked(View view) { builder.setMessage(groupName); builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - DBHelper.deleteGroup(mDatabase, groupName); + try { + DBHelper.deleteGroup(mDatabase, groupName); + } catch (DBHelper.DBException e) { + Log.w(TAG, "Could not delete group " + groupName); + Toast.makeText(getApplicationContext(), "Failed to delete group", Toast.LENGTH_LONG).show(); // FIXME + return; + } updateGroupList(); // Delete may change ordering, so invalidate invalidateHomescreenActiveTab(); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 52d521781c..f18eb2a676 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -39,7 +39,7 @@ * A header is expected for the each table showing the names of the columns. */ public class CatimaImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException { + public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, DBHelper.DBException { InputStream bufferedInputStream = new BufferedInputStream(input); bufferedInputStream.mark(100); @@ -71,7 +71,7 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp input.close(); } - public void importCSV(Context context, SQLiteDatabase database, InputStream input) throws IOException, FormatException, InterruptedException { + public void importCSV(Context context, SQLiteDatabase database, InputStream input) throws IOException, FormatException, InterruptedException, DBHelper.DBException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); int version = parseVersion(bufferedReader); @@ -87,7 +87,7 @@ public void importCSV(Context context, SQLiteDatabase database, InputStream inpu } } - public void parseV1(SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { + public void parseV1(SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException, DBHelper.DBException { final CSVParser parser = new CSVParser(input, CSVFormat.RFC4180.builder().setHeader().build()); try { @@ -105,7 +105,7 @@ public void parseV1(SQLiteDatabase database, BufferedReader input) throws IOExce } } - public void parseV2(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { + public void parseV2(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException, DBHelper.DBException { int part = 0; StringBuilder stringPart = new StringBuilder(); @@ -168,7 +168,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp } } - public void parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException, DBHelper.DBException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -193,7 +193,7 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } } - public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException, DBHelper.DBException { // Parse cards final CSVParser cardParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -277,7 +277,7 @@ private int parseVersion(BufferedReader reader) throws IOException { * session. */ private void importLoyaltyCard(SQLiteDatabase database, CSVRecord record) - throws FormatException { + throws FormatException, DBHelper.DBException { int id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIds.ID, record); String store = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIds.STORE, record, ""); @@ -381,7 +381,7 @@ private void importLoyaltyCard(SQLiteDatabase database, CSVRecord record) * Import a single group into the database using the given * session. */ - private void importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { + private void importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException, DBHelper.DBException { String id = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); if (id == null) { diff --git a/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java b/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java index b1e411a0f1..952a05c630 100644 --- a/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/FidmeImporter.java @@ -31,7 +31,7 @@ * A header is expected for the each table showing the names of the columns. */ public class FidmeImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException, DBHelper.DBException { // We actually retrieve a .zip file ZipInputStream zipInputStream = new ZipInputStream(input, password); @@ -77,7 +77,7 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp * session. */ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVRecord record) - throws FormatException { + throws FormatException, DBHelper.DBException { // A loyalty card export from Fidme contains the following fields: // Retailer (store name) // Program (program name) diff --git a/app/src/main/java/protect/card_locker/importexport/Importer.java b/app/src/main/java/protect/card_locker/importexport/Importer.java index 41f73df262..dca11ac761 100644 --- a/app/src/main/java/protect/card_locker/importexport/Importer.java +++ b/app/src/main/java/protect/card_locker/importexport/Importer.java @@ -9,6 +9,7 @@ import java.io.InputStream; import java.text.ParseException; +import protect.card_locker.DBHelper; import protect.card_locker.FormatException; /** @@ -23,5 +24,5 @@ public interface Importer { * @throws IOException * @throws FormatException */ - void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException; + void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException, DBHelper.DBException; } diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java index a5306dd336..2914756e64 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java @@ -8,6 +8,8 @@ import java.io.InputStream; +import protect.card_locker.DBHelper; + public class MultiFormatImporter { private static final String TAG = "Catima"; @@ -17,6 +19,8 @@ public class MultiFormatImporter { *

* The input stream is not closed, and doing so is the * responsibility of the caller. + *

+ * NB: this deletes all existing data! * * @return ImportExportResult.Success if the database was successfully imported, * or another result otherwise. If no Success, no data was written to @@ -43,6 +47,7 @@ public static ImportExportResult importData(Context context, SQLiteDatabase data String error = null; if (importer != null) { database.beginTransaction(); + DBHelper.clearDatabase(database); try { importer.importData(context, database, input, password); database.setTransactionSuccessful(); diff --git a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java index 1aa90e3e03..8abb71b290 100644 --- a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java @@ -42,7 +42,7 @@ public class StocardImporter implements Importer { private static final String TAG = "Catima"; - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException, DBHelper.DBException { HashMap> loyaltyCardHashMap = new HashMap<>(); HashMap> providers = new HashMap<>(); diff --git a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java index b0d1033664..9336b9f96a 100644 --- a/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/VoucherVaultImporter.java @@ -36,7 +36,7 @@ * A header is expected for the each table showing the names of the columns. */ public class VoucherVaultImporter implements Importer { - public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException { + public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException, DBHelper.DBException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index fb756ffecd..b207cd1c05 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -8,6 +8,7 @@ import com.google.zxing.BarcodeFormat; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,9 +43,12 @@ public void setUp() { @Test public void addRemoveOneGiftCard() { assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(mDatabase, 1); @@ -62,21 +66,30 @@ public void addRemoveOneGiftCard() { assertEquals(0, loyaltyCard.starStatus); assertEquals(0, loyaltyCard.archiveStatus); - result = DBHelper.deleteLoyaltyCard(mDatabase, mActivity, 1); - assertTrue(result); + try { + DBHelper.deleteLoyaltyCard(mDatabase, mActivity, 1); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); assertNull(DBHelper.getLoyaltyCard(mDatabase, 1)); } @Test public void updateGiftCard() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); - result = DBHelper.updateLoyaltyCard(mDatabase, 1, "store1", "note1", null, null, new BigDecimal("10.00"), Currency.getInstance("EUR"), "cardId1", null, CatimaBarcode.fromBarcode(BarcodeFormat.AZTEC), DEFAULT_HEADER_COLOR, 0, null, 0); - assertTrue(result); + try { + DBHelper.updateLoyaltyCard(mDatabase, 1, "store1", "note1", null, null, new BigDecimal("10.00"), Currency.getInstance("EUR"), "cardId1", null, CatimaBarcode.fromBarcode(BarcodeFormat.AZTEC), DEFAULT_HEADER_COLOR, 0, null, 0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(mDatabase, 1); @@ -97,13 +110,19 @@ public void updateGiftCard() { @Test public void updateGiftCardOnlyStar() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); - result = DBHelper.updateLoyaltyCardStarStatus(mDatabase, 1, 1); - assertTrue(result); + try { + DBHelper.updateLoyaltyCardStarStatus(mDatabase, 1, 1); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(mDatabase, 1); @@ -126,17 +145,23 @@ public void updateGiftCardOnlyStar() { public void updateMissingGiftCard() { assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - boolean result = DBHelper.updateLoyaltyCard(mDatabase, 1, "store1", "note1", null, null, new BigDecimal("0"), null, "cardId1", - null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); - assertEquals(false, result); + try { + DBHelper.updateLoyaltyCard(mDatabase, 1, "store1", "note1", null, null, new BigDecimal("0"), null, "cardId1", + null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); + Assert.fail(); // FIXME + } catch (DBHelper.DBException e) { + } assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); } @Test public void emptyGiftCardValues() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(mDatabase, 1); @@ -162,10 +187,12 @@ public void giftCardsViaCursor() { // Add the gift cards in reverse order, to ensure // that they are sorted for (int index = CARDS_TO_ADD - 1; index >= 0; index--) { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, + try { + DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } assertEquals(CARDS_TO_ADD, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -208,14 +235,20 @@ public void giftCardsViaCursor() { // that they are sorted for (int index = CARDS_TO_ADD - 1; index >= 0; index--) { if (index == CARDS_TO_ADD - 1) { - id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 1, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } else { - id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } - boolean result = (id != -1); - assertTrue(result); } assertEquals(CARDS_TO_ADD, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -292,17 +325,22 @@ private int insertCardVersion1(SQLiteDatabase database, @Test public void addRemoveOneGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - long id = DBHelper.insertGroup(mDatabase, "group one"); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertGroup(mDatabase, "group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); Group group = DBHelper.getGroup(mDatabase, "group one"); assertNotNull(group); assertEquals("group one", group._id); - result = DBHelper.deleteGroup(mDatabase, "group one"); - assertTrue(result); + try { + DBHelper.deleteGroup(mDatabase, "group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(0, DBHelper.getGroupCount(mDatabase)); assertNull(DBHelper.getGroup(mDatabase, "group one")); } @@ -311,15 +349,20 @@ public void addRemoveOneGroup() { public void updateGroup() { // Create card assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); // Create group - long groupId = DBHelper.insertGroup(mDatabase, "group one"); - result = (groupId != -1); - assertTrue(result); + try { + DBHelper.insertGroup(mDatabase, "group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Add card to group @@ -335,8 +378,11 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one")); // Rename group - result = DBHelper.updateGroup(mDatabase, "group one", "group one renamed"); - assertTrue(result); + try { + DBHelper.updateGroup(mDatabase, "group one", "group one renamed"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Group one no longer exists @@ -360,25 +406,32 @@ public void updateGroup() { public void updateMissingGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - boolean result = DBHelper.updateGroup(mDatabase, "group one", "new name"); - assertEquals(false, result); + try { + DBHelper.updateGroup(mDatabase, "group one", "new name"); + Assert.fail(); // FIXME + } catch (DBHelper.DBException e) { + } assertEquals(0, DBHelper.getGroupCount(mDatabase)); } @Test public void emptyGroupValues() { - long id = DBHelper.insertGroup(mDatabase, ""); - boolean result = (id != -1); - assertFalse(result); + try { + DBHelper.insertGroup(mDatabase, ""); + Assert.fail(); // FIXME + } catch (DBHelper.DBException e) { + } assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); } @Test public void duplicateGroupName() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - long id = DBHelper.insertGroup(mDatabase, "group one"); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertGroup(mDatabase, "group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); Group group = DBHelper.getGroup(mDatabase, "group one"); @@ -386,27 +439,36 @@ public void duplicateGroupName() { assertEquals("group one", group._id); // Should fail on duplicate - long id2 = DBHelper.insertGroup(mDatabase, "group one"); - boolean result2 = (id2 != -1); - assertFalse(result2); + try { + DBHelper.insertGroup(mDatabase, "group one"); + Assert.fail(); // FIXME + } catch (DBHelper.DBException e) { + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); } @Test public void updateGroupDuplicate() { - long id = DBHelper.insertGroup(mDatabase, "group one"); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertGroup(mDatabase, "group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getGroupCount(mDatabase)); - long id2 = DBHelper.insertGroup(mDatabase, "group two"); - boolean result2 = (id2 != -1); - assertTrue(result2); + try { + DBHelper.insertGroup(mDatabase, "group two"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Should fail when trying to rename group two to one - boolean result3 = DBHelper.updateGroup(mDatabase, "group two", "group one"); - assertFalse(result3); + try { + DBHelper.updateGroup(mDatabase, "group two", "group one"); + Assert.fail(); // FIXME + } catch (DBHelper.DBException e) { + } assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Rename failed so both should still be the same @@ -423,20 +485,27 @@ public void updateGroupDuplicate() { public void cardAddAndRemoveGroups() { // Create card assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); // Create two groups to only one card assertEquals(0, DBHelper.getGroupCount(mDatabase)); - long gid = DBHelper.insertGroup(mDatabase, "one"); - boolean gresult = (gid != -1); - assertTrue(gresult); + try { + DBHelper.insertGroup(mDatabase, "one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } - long gid2 = DBHelper.insertGroup(mDatabase, "two"); - boolean gresult2 = (gid2 != -1); - assertTrue(gresult2); + try { + DBHelper.insertGroup(mDatabase, "two"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(2, DBHelper.getGroupCount(mDatabase)); @@ -515,12 +584,15 @@ public void databaseUpgradeFromVersion1() { @Test public void updateGiftCardOnlyBalance() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); - result = DBHelper.updateLoyaltyCardBalance(mDatabase, 1, new BigDecimal(60)); + boolean result = DBHelper.updateLoyaltyCardBalance(mDatabase, 1, new BigDecimal(60)); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 3bd6adfe7d..7e124a0496 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -11,6 +11,7 @@ import com.google.zxing.BarcodeFormat; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -78,9 +79,11 @@ private void addLoyaltyCards(int cardsToAdd) { for (int index = cardsToAdd; index > 0; index--) { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } assertEquals(cardsToAdd, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -92,26 +95,33 @@ private void addLoyaltyCardsFiveStarred() { for (int index = cardsToAdd; index > 4; index--) { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 1, null,0); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 1, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } for (int index = cardsToAdd - 5; index > 0; index--) { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); //if index is even - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } assertEquals(cardsToAdd, DBHelper.getLoyaltyCardCount(mDatabase)); } @Test public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "No Expiry", "", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); - boolean result = (id != -1); - assertTrue(result); + long id = -1; + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "No Expiry", "", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, (int) id); assertEquals("No Expiry", card.store); @@ -126,9 +136,11 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { assertEquals(Integer.valueOf(0), card.headerColor); assertEquals(0, card.starStatus); - id = DBHelper.insertLoyaltyCard(mDatabase, "Past", "", null, new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); - result = (id != -1); - assertTrue(result); + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "Past", "", null, new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } card = DBHelper.getLoyaltyCard(mDatabase, (int) id); assertEquals("Past", card.store); @@ -143,9 +155,11 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { assertEquals(Integer.valueOf(0), card.headerColor); assertEquals(0, card.starStatus); - id = DBHelper.insertLoyaltyCard(mDatabase, "Today", "", null, new Date(), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); - result = (id != -1); - assertTrue(result); + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "Today", "", null, new Date(), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } card = DBHelper.getLoyaltyCard(mDatabase, (int) id); assertEquals("Today", card.store); @@ -163,9 +177,11 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { // This will break after 19 January 2038 // If someone is still maintaining this code base by then: I love you - id = DBHelper.insertLoyaltyCard(mDatabase, "Future", "", null, new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); - result = (id != -1); - assertTrue(result); + try { + id = DBHelper.insertLoyaltyCard(mDatabase, "Future", "", null, new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } card = DBHelper.getLoyaltyCard(mDatabase, (int) id); assertEquals("Future", card.store); @@ -187,9 +203,11 @@ private void addGroups(int groupsToAdd) { // Add in reverse order to test sorting for (int index = groupsToAdd; index > 0; index--) { String groupName = String.format("group, \"%4d", index); - long id = DBHelper.insertGroup(mDatabase, groupName); - boolean result = (id != -1); - assertTrue(result); + try { + DBHelper.insertGroup(mDatabase, groupName); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } assertEquals(groupsToAdd, DBHelper.getGroupCount(mDatabase)); @@ -797,9 +815,18 @@ public void exportImportV2Zip() throws FileNotFoundException { HashMap loyaltyCardIconImages = new HashMap<>(); // Create card 1 - int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null,0); + int loyaltyCardId = -1; + try { + loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); - DBHelper.insertGroup(mDatabase, "One"); + try { + DBHelper.insertGroup(mDatabase, "One"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } List groups = Arrays.asList(DBHelper.getGroup(mDatabase, "One")); DBHelper.setLoyaltyCardGroups(mDatabase, loyaltyCardId, groups); loyaltyCardGroups.put(loyaltyCardId, groups); @@ -811,7 +838,11 @@ public void exportImportV2Zip() throws FileNotFoundException { loyaltyCardIconImages.put(loyaltyCardId, bitmap1); // Create card 2 - loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 2", "", null, null, new BigDecimal(0), null, "123456", null, null, 2, 1, null,0); + try { + loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 2", "", null, null, new BigDecimal(0), null, "123456", null, null, 2, 1, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); // Export everything diff --git a/app/src/test/java/protect/card_locker/ImportURITest.java b/app/src/test/java/protect/card_locker/ImportURITest.java index 7d16b1fa5c..80f7402fb2 100644 --- a/app/src/test/java/protect/card_locker/ImportURITest.java +++ b/app/src/test/java/protect/card_locker/ImportURITest.java @@ -7,6 +7,7 @@ import com.google.zxing.BarcodeFormat; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,7 +41,11 @@ public void ensureNoDataLoss() throws InvalidObjectException, UnsupportedEncodin // Generate card Date date = new Date(); - DBHelper.insertLoyaltyCard(mDatabase, "store", "This note contains evil symbols like & and = that will break the parser if not escaped right $#!%()*+;:á", date, date, new BigDecimal("100"), null, BarcodeFormat.UPC_E.toString(), BarcodeFormat.UPC_A.toString(), CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), Color.BLACK, 1, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "This note contains evil symbols like & and = that will break the parser if not escaped right $#!%()*+;:á", date, date, new BigDecimal("100"), null, BarcodeFormat.UPC_E.toString(), BarcodeFormat.UPC_A.toString(), CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), Color.BLACK, 1, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } // Get card LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); @@ -70,7 +75,11 @@ public void ensureNoDataLoss() throws InvalidObjectException, UnsupportedEncodin @Test public void ensureNoCrashOnMissingHeaderFields() throws InvalidObjectException, UnsupportedEncodingException { // Generate card - DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("EUR"), BarcodeFormat.UPC_A.toString(), null, CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), null, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("EUR"), BarcodeFormat.UPC_A.toString(), null, CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), null, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } // Get card LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java index f005bd3082..203770cdf3 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java @@ -10,6 +10,7 @@ import com.google.zxing.BarcodeFormat; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -99,7 +100,11 @@ private void checkView(final View view, final String store, final String note, f @Test public void TestCursorAdapterEmptyNote() { - DBHelper.insertLoyaltyCard(mDatabase, "store", "", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null, 0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); @@ -114,7 +119,11 @@ public void TestCursorAdapterEmptyNote() { @Test public void TestCursorAdapterWithNote() { - DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); @@ -129,10 +138,14 @@ public void TestCursorAdapterWithNote() { @Test public void TestCursorAdapterStarring() { - assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeA", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,1)); - assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeB", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,1)); - assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeC", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0)); - assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeD", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0)); + try { + assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeA", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,1)); + assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeB", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,1)); + assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeC", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0)); + assertNotEquals(-1, DBHelper.insertLoyaltyCard(mDatabase, "storeD", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0)); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(4, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -180,7 +193,11 @@ public void TestCursorAdapterStarring() { @Test public void TestCursorAdapter0Points() { - DBHelper.insertLoyaltyCard(mDatabase, "store", "", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); @@ -195,7 +212,11 @@ public void TestCursorAdapter0Points() { @Test public void TestCursorAdapter0EUR() { - DBHelper.insertLoyaltyCard(mDatabase,"store", "", null, null, new BigDecimal("0"), Currency.getInstance("EUR"), "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase,"store", "", null, null, new BigDecimal("0"), Currency.getInstance("EUR"), "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); @@ -210,7 +231,11 @@ public void TestCursorAdapter0EUR() { @Test public void TestCursorAdapter100Points() { - DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); @@ -225,7 +250,11 @@ public void TestCursorAdapter100Points() { @Test public void TestCursorAdapter10USD() { - DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } LoyaltyCard card = DBHelper.getLoyaltyCard(mDatabase, 1); Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java index 62c0c3cc1e..6d1164fd21 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java @@ -37,6 +37,7 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.client.android.Intents; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -371,7 +372,11 @@ public void noDataLossOnResumeOrRotate() { SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); if (!newCard) { - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } } activityController.start(); @@ -610,7 +615,11 @@ public void startWithLoyaltyCardEditModeCheckDisplay() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -628,7 +637,11 @@ public void startWithLoyaltyCardViewModeCheckDisplay() { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null, 0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -646,7 +659,11 @@ public void startWithLoyaltyCardWithBarcodeUpdateBarcode() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -670,7 +687,11 @@ public void startWithLoyaltyCardWithReceiptUpdateReceiptCancel() throws IOExcept final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -708,7 +729,11 @@ public void startWithLoyaltyCardNoExpirySetExpiry() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -740,7 +765,11 @@ public void startWithLoyaltyCardExpirySetNoExpiry() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, new Date(), new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, new Date(), new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -764,7 +793,11 @@ public void startWithLoyaltyCardNoBalanceSetBalance() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -814,7 +847,11 @@ public void startWithLoyaltyCardBalanceSetNoBalance() throws IOException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("10.00"), Currency.getInstance("USD"), EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -854,7 +891,11 @@ public void startWithLoyaltyCardSameAsCardIDUpdateBarcodeID() { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -885,7 +926,11 @@ public void startWithLoyaltyCardSameAsCardIDUpdateCardID() { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, null, EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -918,7 +963,11 @@ public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDUpdate() { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -954,7 +1003,11 @@ public void startWithLoyaltyCardDifferentFromCardIDUpdateCardIDDoNotUpdate() { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, EAN_BARCODE_DATA, "123456", EAN_BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -989,7 +1042,11 @@ public void checkMenu() throws IOException { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1034,7 +1091,12 @@ public void startWithoutParametersViewBack() { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1053,7 +1115,12 @@ public void startWithoutColors() { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1072,7 +1139,12 @@ public void startLoyaltyCardWithoutColorsSave() throws IOException, ParseExcepti Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, null, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1090,7 +1162,12 @@ public void startLoyaltyCardWithExplicitNoBarcodeSave() throws IOException, Pars Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1109,7 +1186,11 @@ public void removeBarcodeFromLoyaltyCard() throws IOException, ParseException { final Context context = activity.getApplicationContext(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1138,7 +1219,13 @@ public void checkPushStarIcon() { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } + activityController.start(); activityController.visible(); activityController.resume(); @@ -1172,7 +1259,12 @@ public void checkBarcodeFullscreenWorkflow() { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); @@ -1266,7 +1358,12 @@ public void checkNoBarcodeFullscreenWorkflow() { Activity activity = (Activity) activityController.get(); SQLiteDatabase database = TestHelpers.getEmptyDb(activity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, null, Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.start(); activityController.visible(); diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index c76386c83e..e48cabfa52 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -13,6 +13,7 @@ import com.google.android.material.tabs.TabLayout; import com.google.zxing.BarcodeFormat; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; @@ -95,7 +96,12 @@ public void addOneLoyaltyCard() { assertEquals(0, list.getAdapter().getItemCount()); SQLiteDatabase database = TestHelpers.getEmptyDb(mainActivity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(View.VISIBLE, helpSection.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); @@ -130,10 +136,15 @@ public void addFourLoyaltyCardsTwoStarred() // Main screen showing starred card assertEquals(0, list.getAdapter().getItemCount()); SQLiteDatabase database = TestHelpers.getEmptyDb(mainActivity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "storeB", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); - DBHelper.insertLoyaltyCard(database, "storeA", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); - DBHelper.insertLoyaltyCard(database, "storeD", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0); - DBHelper.insertLoyaltyCard(database, "storeC", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0); + + try { + DBHelper.insertLoyaltyCard(database, "storeB", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + DBHelper.insertLoyaltyCard(database, "storeA", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + DBHelper.insertLoyaltyCard(database, "storeD", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0); + DBHelper.insertLoyaltyCard(database, "storeC", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 1, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } assertEquals(View.VISIBLE, helpSection.getVisibility()); assertEquals(View.GONE, noMatchingCardsText.getVisibility()); @@ -177,7 +188,11 @@ public void testGroups() { assertEquals(0, groupTabs.getTabCount()); // Having at least one group should create two tabs: One all and one for each group - DBHelper.insertGroup(database, "One"); + try { + DBHelper.insertGroup(database, "One"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.pause(); activityController.resume(); assertEquals(2, groupTabs.getTabCount()); @@ -185,7 +200,11 @@ public void testGroups() { assertEquals("One", groupTabs.getTabAt(1).getText().toString()); // Adding another group should have it added to the end - DBHelper.insertGroup(database, "Alphabetical two"); + try { + DBHelper.insertGroup(database, "Alphabetical two"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.pause(); activityController.resume(); assertEquals(3, groupTabs.getTabCount()); @@ -194,7 +213,11 @@ public void testGroups() { assertEquals("Alphabetical two", groupTabs.getTabAt(2).getText().toString()); // Removing a group should also change the list - DBHelper.deleteGroup(database, "Alphabetical two"); + try { + DBHelper.deleteGroup(database, "Alphabetical two"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.pause(); activityController.resume(); assertEquals(2, groupTabs.getTabCount()); @@ -202,7 +225,11 @@ public void testGroups() { assertEquals("One", groupTabs.getTabAt(1).getText().toString()); // Removing the last group should make the tabs disappear - DBHelper.deleteGroup(database, "One"); + try { + DBHelper.deleteGroup(database, "One"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } activityController.pause(); activityController.resume(); assertEquals(0, groupTabs.getTabCount()); @@ -224,10 +251,19 @@ public void testFiltering() { TabLayout groupTabs = mainActivity.findViewById(R.id.groups); SQLiteDatabase database = TestHelpers.getEmptyDb(mainActivity).getWritableDatabase(); - DBHelper.insertLoyaltyCard(database, "The First Store", "Initial note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); - DBHelper.insertLoyaltyCard(database, "The Second Store", "Secondary note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); - DBHelper.insertGroup(database, "Group one"); + try { + DBHelper.insertLoyaltyCard(database, "The First Store", "Initial note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + DBHelper.insertLoyaltyCard(database, "The Second Store", "Secondary note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), Color.BLACK, 0, null,0); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } + + try { + DBHelper.insertGroup(database, "Group one"); + } catch (DBHelper.DBException e) { + Assert.fail(); // FIXME + } List groups = new ArrayList<>(); groups.add(DBHelper.getGroup(database, "Group one")); DBHelper.setLoyaltyCardGroups(database, 1, groups);