Skip to content

Commit

Permalink
Merge pull request #343 from TheLastProject/fix/sharing_npe
Browse files Browse the repository at this point in the history
Fix NPE when sharing cards without header values
  • Loading branch information
brarcher authored Jan 10, 2020
2 parents 2b50e50 + aad98ba commit 003aba5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
26 changes: 22 additions & 4 deletions app/src/main/java/protect/card_locker/ImportURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ public LoyaltyCard parse(Uri uri) throws InvalidObjectException {
}

try {
// These values are allowed to be null
Integer headerColor = null;
Integer headerTextColor = null;

String store = uri.getQueryParameter(STORE);
String note = uri.getQueryParameter(NOTE);
String cardId = uri.getQueryParameter(CARD_ID);
String barcodeType = uri.getQueryParameter(BARCODE_TYPE);
Integer headerColor = Integer.parseInt(uri.getQueryParameter(HEADER_COLOR));
Integer headerTextColor = Integer.parseInt(uri.getQueryParameter(HEADER_TEXT_COLOR));
String unparsedHeaderColor = uri.getQueryParameter(HEADER_COLOR);
if(unparsedHeaderColor != null)
{
headerColor = Integer.parseInt(unparsedHeaderColor);
}
String unparsedHeaderTextColor = uri.getQueryParameter(HEADER_TEXT_COLOR);
if(unparsedHeaderTextColor != null)
{
headerTextColor = Integer.parseInt(unparsedHeaderTextColor);
}
return new LoyaltyCard(-1, store, note, cardId, barcodeType, headerColor, headerTextColor);
} catch (NullPointerException | NumberFormatException ex) {
throw new InvalidObjectException("Not a valid import URI");
Expand All @@ -57,8 +69,14 @@ protected Uri toUri(LoyaltyCard loyaltyCard) {
uriBuilder.appendQueryParameter(NOTE, loyaltyCard.note);
uriBuilder.appendQueryParameter(CARD_ID, loyaltyCard.cardId);
uriBuilder.appendQueryParameter(BARCODE_TYPE, loyaltyCard.barcodeType);
uriBuilder.appendQueryParameter(HEADER_COLOR, loyaltyCard.headerColor.toString());
uriBuilder.appendQueryParameter(HEADER_TEXT_COLOR, loyaltyCard.headerTextColor.toString());
if(loyaltyCard.headerColor != null)
{
uriBuilder.appendQueryParameter(HEADER_COLOR, loyaltyCard.headerColor.toString());
}
if(loyaltyCard.headerTextColor != null)
{
uriBuilder.appendQueryParameter(HEADER_TEXT_COLOR, loyaltyCard.headerTextColor.toString());
}

return uriBuilder.build();
}
Expand Down
25 changes: 25 additions & 0 deletions app/src/test/java/protect/card_locker/ImportURITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.robolectric.annotation.Config;
import java.io.InvalidObjectException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static protect.card_locker.DBHelper.LoyaltyCardDbIds;

Expand Down Expand Up @@ -53,6 +54,30 @@ public void ensureNoDataLoss() throws InvalidObjectException
assertEquals(card.store, parsedCard.store);
}

@Test
public void ensureNoCrashOnMissingHeaderFields() throws InvalidObjectException
{
// Generate card
db.insertLoyaltyCard("store", "note", BarcodeFormat.UPC_A.toString(), LoyaltyCardDbIds.BARCODE_TYPE, null, null);

// Get card
LoyaltyCard card = db.getLoyaltyCard(1);

// Card to URI
Uri cardUri = importURIHelper.toUri(card);

// Parse URI
LoyaltyCard parsedCard = importURIHelper.parse(cardUri);

// Compare everything
assertEquals(card.barcodeType, parsedCard.barcodeType);
assertEquals(card.cardId, parsedCard.cardId);
assertEquals(card.note, parsedCard.note);
assertEquals(card.store, parsedCard.store);
assertNull(parsedCard.headerColor);
assertNull(parsedCard.headerTextColor);
}

@Test
public void failToParseInvalidUri()
{
Expand Down

0 comments on commit 003aba5

Please sign in to comment.