Skip to content

Commit f1f38d1

Browse files
committed
better handling of the Parcelable implementation
1 parent f7d8f70 commit f1f38d1

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

app/src/main/java/protect/card_locker/LoyaltyCard.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ protected LoyaltyCard(Parcel in) {
6868
balanceType = (Currency) in.readValue(Currency.class.getClassLoader());
6969
cardId = in.readString();
7070
barcodeId = in.readString();
71-
String tmpBarcodeType = in.readString();
72-
barcodeType = !tmpBarcodeType.isEmpty() ? CatimaBarcode.fromName(tmpBarcodeType) : null;
71+
String barcodeTypeName = in.readString();
72+
barcodeType = barcodeTypeName != null && !barcodeTypeName.isEmpty() ? CatimaBarcode.fromName(barcodeTypeName) : null;
7373
int tmpHeaderColor = in.readInt();
7474
headerColor = tmpHeaderColor != -1 ? tmpHeaderColor : null;
7575
starStatus = in.readInt();
@@ -97,6 +97,18 @@ public void writeToParcel(Parcel parcel, int i) {
9797
parcel.writeInt(archiveStatus);
9898
}
9999

100+
public static final Creator<LoyaltyCard> CREATOR = new Creator<LoyaltyCard>() {
101+
@Override
102+
public LoyaltyCard createFromParcel(Parcel in) {
103+
return new LoyaltyCard(in);
104+
}
105+
106+
@Override
107+
public LoyaltyCard[] newArray(int size) {
108+
return new LoyaltyCard[size];
109+
}
110+
};
111+
}
100112
public static LoyaltyCard toLoyaltyCard(Cursor cursor) {
101113
int id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID));
102114
String store = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STORE));

app/src/test/java/protect/card_locker/LoyaltyCardTest.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package protect.card_locker;
2+
13
import android.os.Parcel;
24
import android.os.Parcelable;
35

@@ -9,32 +11,20 @@
911
import java.util.Currency;
1012
import java.util.Date;
1113

12-
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.assertFalse;
14-
import static org.junit.Assert.assertNotNull;
15-
import static org.junit.Assert.assertNull;
16-
import static org.junit.Assert.assertTrue;
14+
import com.google.zxing.BarcodeFormat;
15+
16+
import static org.junit.Assert.*;
1717

1818
@RunWith(MockitoJUnitRunner.class)
1919
public class LoyaltyCardTest {
2020

21-
/**
22-
* Test the parcelable implementation of the LoyaltyCard class.
23-
*
24-
* This test creates a LoyaltyCard object, writes it to a Parcel, and then
25-
* recreates the object from the Parcel. It asserts that the original and
26-
* recreated objects have the same values for all their fields.
27-
*
28-
* @throws AssertionError if any of the assertions fail
29-
*/
3021
@Test
3122
public void testParcelable() {
32-
3323
Date validFrom = new Date();
3424
Date expiry = new Date();
3525
BigDecimal balance = new BigDecimal("100.00");
3626
Currency currency = Currency.getInstance("USD");
37-
LoyaltyCard card = new LoyaltyCard(1, "Store A", "Note A", validFrom, expiry, balance, currency, "12345", "67890", CatimaBarcode.QR_CODE, 0xFF0000, 1, System.currentTimeMillis(), 10, 0);
27+
LoyaltyCard card = new LoyaltyCard(1, "Store A", "Note A", validFrom, expiry, balance, currency, "12345", "67890", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 0xFF0000, 1, System.currentTimeMillis(), 10, 0);
3828

3929
Parcel parcel = Parcel.obtain();
4030
card.writeToParcel(parcel, card.describeContents());
@@ -52,7 +42,7 @@ public void testParcelable() {
5242
assertEquals(card.balanceType, createdFromParcel.balanceType);
5343
assertEquals(card.cardId, createdFromParcel.cardId);
5444
assertEquals(card.barcodeId, createdFromParcel.barcodeId);
55-
assertEquals(card.barcodeType, createdFromParcel.barcodeType);
45+
assertEquals(card.barcodeType.name(), createdFromParcel.barcodeType.name());
5646
assertEquals(card.headerColor, createdFromParcel.headerColor);
5747
assertEquals(card.starStatus, createdFromParcel.starStatus);
5848
assertEquals(card.lastUsed, createdFromParcel.lastUsed);
@@ -67,7 +57,7 @@ public void testIsDuplicate_sameObject() {
6757
Date now = new Date();
6858
BigDecimal balance = new BigDecimal("50.00");
6959
Currency currency = Currency.getInstance("EUR");
70-
LoyaltyCard card1 = new LoyaltyCard(1, "Store B", "Note B", now, now, balance, currency, "22222", "33333", CatimaBarcode.PDF_417, 0x00FF00, 1, System.currentTimeMillis(), 5, 1);
60+
LoyaltyCard card1 = new LoyaltyCard(1, "Store B", "Note B", now, now, balance, currency, "22222", "33333", CatimaBarcode.fromBarcode(BarcodeFormat.PDF_417), 0x00FF00, 1, System.currentTimeMillis(), 5, 1);
7161

7262
assertTrue(LoyaltyCard.isDuplicate(card1, card1));
7363
}
@@ -78,8 +68,8 @@ public void testIsDuplicate_differentObjects() {
7868
BigDecimal balance1 = new BigDecimal("50.00");
7969
BigDecimal balance2 = new BigDecimal("75.00");
8070
Currency currency = Currency.getInstance("EUR");
81-
LoyaltyCard card1 = new LoyaltyCard(2, "Store C", "Note C", now, now, balance1, currency, "44444", "55555", CatimaBarcode.DATA_MATRIX, 0x0000FF, 0, System.currentTimeMillis(), 15, 1);
82-
LoyaltyCard card2 = new LoyaltyCard(2, "Store C", "Note C", now, now, balance2, currency, "44444", "55555", CatimaBarcode.DATA_MATRIX, 0x0000FF, 0, System.currentTimeMillis(), 15, 1);
71+
LoyaltyCard card1 = new LoyaltyCard(2, "Store C", "Note C", now, now, balance1, currency, "44444", "55555", CatimaBarcode.fromBarcode(BarcodeFormat.DATA_MATRIX), 0x0000FF, 0, System.currentTimeMillis(), 15, 1);
72+
LoyaltyCard card2 = new LoyaltyCard(2, "Store C", "Note C", now, now, balance2, currency, "44444", "55555", CatimaBarcode.fromBarcode(BarcodeFormat.DATA_MATRIX), 0x0000FF, 0, System.currentTimeMillis(), 15, 1);
8373

8474
assertFalse(LoyaltyCard.isDuplicate(card1, card2));
8575
}
@@ -89,7 +79,7 @@ public void testToString() {
8979
Date now = new Date();
9080
BigDecimal balance = new BigDecimal("100.00");
9181
Currency currency = Currency.getInstance("USD");
92-
LoyaltyCard card = new LoyaltyCard(3, "Store D", "Note D", now, now, balance, currency, "66666", "77777", CatimaBarcode.AZTEC, null, 2, System.currentTimeMillis(), 20, 2);
82+
LoyaltyCard card = new LoyaltyCard(3, "Store D", "Note D", now, now, balance, currency, "66666", "77777", CatimaBarcode.fromBarcode(BarcodeFormat.AZTEC), null, 2, System.currentTimeMillis(), 20, 2);
9383

9484
String expected = String.format(
9585
"LoyaltyCard{%n id=%s,%n store=%s,%n note=%s,%n validFrom=%s,%n expiry=%s,%n"

0 commit comments

Comments
 (0)