Fix tests

This commit is contained in:
Sylvia van Os
2019-12-16 16:09:56 +01:00
parent a05f9f6ec9
commit b4a532d183
10 changed files with 38 additions and 19 deletions

View File

@@ -4,6 +4,7 @@ import android.database.Cursor;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.json.JSONException;
import java.io.IOException;
import java.io.OutputStreamWriter;
@@ -25,7 +26,8 @@ public class CsvDatabaseExporter implements DatabaseExporter
DBHelper.LoyaltyCardDbIds.CARD_ID,
DBHelper.LoyaltyCardDbIds.HEADER_COLOR,
DBHelper.LoyaltyCardDbIds.HEADER_TEXT_COLOR,
DBHelper.LoyaltyCardDbIds.BARCODE_TYPE);
DBHelper.LoyaltyCardDbIds.BARCODE_TYPE,
DBHelper.LoyaltyCardDbIds.EXTRAS);
Cursor cursor = db.getLoyaltyCardCursor();
@@ -33,13 +35,23 @@ public class CsvDatabaseExporter implements DatabaseExporter
{
LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor);
String extras;
try {
extras = card.extras.toJSON().toString();
}
catch (JSONException ex)
{
throw new IOException(ex);
}
printer.printRecord(card.id,
card.store,
card.note,
card.cardId,
card.headerColor,
card.headerTextColor,
card.barcodeType);
card.barcodeType,
extras);
if(Thread.currentThread().isInterrupted())
{

View File

@@ -149,11 +149,11 @@ public class CsvDatabaseImporter implements DatabaseImporter
headerTextColor = extractInt(DBHelper.LoyaltyCardDbIds.HEADER_TEXT_COLOR, record, true);
}
ExtrasHelper extras;
ExtrasHelper extras = new ExtrasHelper();
try
{
extras = new ExtrasHelper().fromJSON(new JSONObject(extractString(DBHelper.LoyaltyCardDbIds.EXTRAS, record, "{}")));
extras.fromJSON(new JSONObject(extractString(DBHelper.LoyaltyCardDbIds.EXTRAS, record, "{}")));
helper.insertLoyaltyCard(database, id, store, note, cardId, barcodeType, headerColor, headerTextColor, extras);
}
catch (JSONException ex)

View File

@@ -1,6 +1,7 @@
package protect.card_locker;
import android.support.annotation.NonNull;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

View File

@@ -46,12 +46,7 @@ public class ImportURIHelper {
String barcodeType = uri.getQueryParameter(BARCODE_TYPE);
Integer headerColor = Integer.parseInt(uri.getQueryParameter(HEADER_COLOR));
Integer headerTextColor = Integer.parseInt(uri.getQueryParameter(HEADER_TEXT_COLOR));
// Extras was added in a later version, so don't crash if it doesn't exist
ExtrasHelper extras = new ExtrasHelper();
if(uri.getQueryParameter(EXTRAS) != null)
{
extras = new ExtrasHelper().fromJSON(new JSONObject(uri.getQueryParameter(EXTRAS)));
}
ExtrasHelper extras = new ExtrasHelper().fromJSON(new JSONObject(uri.getQueryParameter(EXTRAS)));
return new LoyaltyCard(-1, store, note, cardId, barcodeType, headerColor, headerTextColor, extras);
} catch (NullPointerException | NumberFormatException | JSONException ex) {
throw new InvalidObjectException("Not a valid import URI");
@@ -59,7 +54,7 @@ public class ImportURIHelper {
}
// Protected for usage in tests
protected Uri toUri(LoyaltyCard loyaltyCard) {
protected Uri toUri(LoyaltyCard loyaltyCard) throws JSONException {
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.scheme("https");
uriBuilder.authority(host);
@@ -70,7 +65,7 @@ public class ImportURIHelper {
uriBuilder.appendQueryParameter(BARCODE_TYPE, loyaltyCard.barcodeType);
uriBuilder.appendQueryParameter(HEADER_COLOR, loyaltyCard.headerColor.toString());
uriBuilder.appendQueryParameter(HEADER_TEXT_COLOR, loyaltyCard.headerTextColor.toString());
uriBuilder.appendQueryParameter(EXTRAS, loyaltyCard.extras.toString());
uriBuilder.appendQueryParameter(EXTRAS, loyaltyCard.extras.toJSON().toString());
return uriBuilder.build();
}
@@ -85,7 +80,14 @@ public class ImportURIHelper {
context.startActivity(shareIntent);
}
public void startShareIntent(LoyaltyCard loyaltyCard) {
startShareIntent(toUri(loyaltyCard));
public boolean startShareIntent(LoyaltyCard loyaltyCard) {
try
{
startShareIntent(toUri(loyaltyCard));
return true;
}
catch (JSONException ex) {}
return false;
}
}

View File

@@ -70,7 +70,7 @@ public class LoyaltyCard
{
try
{
extras = new ExtrasHelper().fromJSON(new JSONObject(cursor.getString(extrasColumn)));
extras = extras.fromJSON(new JSONObject(cursor.getString(extrasColumn)));
}
catch (JSONException ex)
{

View File

@@ -207,7 +207,10 @@ public class MainActivity extends AppCompatActivity
else if(item.getItemId() == R.id.action_share)
{
final ImportURIHelper importURIHelper = new ImportURIHelper(this);
importURIHelper.startShareIntent(card);
if(importURIHelper.startShareIntent(card))
{
Toast.makeText(this, R.string.failedSharingCard, Toast.LENGTH_LONG).show();
}
return true;
}
}

View File

@@ -45,6 +45,7 @@
<string name="noCardIdError">No Card ID entered</string>
<string name="noCardExistsError">Could not lookup loyalty card</string>
<string name="failedParsingImportUriError">Could not parse the import Uri</string>
<string name="failedSharingCard">Could not share card</string>
<string name="failedShowingExtras">Could not show extra information: data not correctly formatted</string>
<string name="failedSavingCard">Could not save card</string>

View File

@@ -105,7 +105,7 @@ public class ImportExportTest
assertEquals(BARCODE_TYPE, card.barcodeType);
assertEquals(Integer.valueOf(index), card.headerColor);
assertEquals(Integer.valueOf(index*2), card.headerTextColor);
assertEquals(EXTRAS.toJSON().toString(), card.extras.toJSON().toString());
assertEquals("{\"en\":{\"key\":\"value\"}}", card.extras.toJSON().toString());
index++;
}

View File

@@ -56,7 +56,7 @@ public class ImportURITest {
assertEquals(card.headerTextColor, parsedCard.headerTextColor);
assertEquals(card.note, parsedCard.note);
assertEquals(card.store, parsedCard.store);
assertEquals(card.extras.toString(), parsedCard.extras.toString());
assertEquals(card.extras.toJSON().toString(), parsedCard.extras.toJSON().toString());
}
@Test

View File

@@ -655,7 +655,7 @@ public class LoyaltyCardViewActivityTest
@Test
public void importCard()
{
Uri importUri = Uri.parse("https://brarcher.github.io/loyalty-card-locker/share?store=Example%20Store&note=&cardid=123456&barcodetype=AZTEC&headercolor=-416706&headertextcolor=-1");
Uri importUri = Uri.parse("https://brarcher.github.io/loyalty-card-locker/share?store=Example%20Store&note=&cardid=123456&barcodetype=AZTEC&headercolor=-416706&headertextcolor=-1&extras={}");
Intent intent = new Intent();
intent.setData(importUri);